org.el 653 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515
  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.32trans
  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.32trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let* ((version org-version)
  96. (git-version)
  97. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  98. (if (and (file-exists-p (expand-file-name ".git" dir))
  99. (executable-find "git"))
  100. (let ((pwd (substring (pwd) 10)))
  101. (cd dir)
  102. (if (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  103. (save-excursion
  104. (set-buffer "*Shell Command Output*")
  105. (goto-char (point-min))
  106. (re-search-forward "[^\n]+")
  107. (setq git-version (match-string 0))
  108. (subst-char-in-string ?- ?. git-version t)
  109. (shell-command "git diff-index --name-only HEAD --")
  110. (unless (eql 1 (point-max))
  111. (setq git-version (concat git-version ".dirty")))
  112. (setq version (concat version " (" git-version ")")))
  113. (cd pwd))))
  114. (setq version (format "Org-mode version %s" version))
  115. (if here (insert version))
  116. (message version)
  117. version))
  118. ;;; Compatibility constants
  119. ;;; The custom variables
  120. (defgroup org nil
  121. "Outline-based notes management and organizer."
  122. :tag "Org"
  123. :group 'outlines
  124. :group 'hypermedia
  125. :group 'calendar)
  126. (defcustom org-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 annotation-helper: Call Remember directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-annotation-helper)
  188. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  189. (const :tag "C browser-url: Store link, directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-browser-url)
  190. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  191. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  192. (const :tag "C collector: Collect properties into tables" org-collector)
  193. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  194. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  195. (const :tag "C eval: Include command output as text" org-eval)
  196. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  197. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  198. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  199. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  200. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  201. (const :tag "C invoice Help manage client invoices in Org-mode" org-invoice)
  202. (const :tag "C jira Add a jira:ticket protocol to Org-mode" org-jira)
  203. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  204. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  205. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  206. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  207. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  208. (const :tag "C R: Computation using the R language" org-R)
  209. (const :tag "C registry: A registry for Org-mode links" org-registry)
  210. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  211. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  212. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  213. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  214. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  215. (const :tag "C track: Keep up with Org-mode development" org-track)
  216. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  217. (defcustom org-support-shift-select nil
  218. "Non-nil means, make shift-cursor commands select text when possible.
  219. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  220. selecting a region, or enlarge thusly regions started in this way.
  221. In Org-mode, in special contexts, these same keys are used for other
  222. purposes, important enough to compete with shift selection. Org tries
  223. to balance these needs by supporting `shift-select-mode' outside these
  224. special contexts, under control of this variable.
  225. The default of this variable is nil, to avoid confusing behavior. Shifted
  226. cursor keys will then execute Org commands in the following contexts:
  227. - on a headline, changing TODO state (left/right) and priority (up/down)
  228. - on a time stamp, changing the time
  229. - in a plain list item, changing the bullet type
  230. - in a property definition line, switching between allowed values
  231. - in the BEGIN line of a clock table (changing the time block).
  232. Outside these contexts, the commands will throw an error.
  233. When this variable is t and the cursor is not in a special context,
  234. Org-mode will support shift-selection for making and enlarging regions.
  235. To make this more effective, the bullet cycling will no longer happen
  236. anywhere in an item line, but only if the cursor is exactly on the bullet.
  237. If you set this variable to the symbol `always', then the keys
  238. will not be special in headlines, property lines, and item lines, to make
  239. shift selection work there as well. If this is what you want, you can
  240. use the following alternative commands: `C-c C-t' and `C-c ,' to
  241. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  242. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  243. edited by hand or in column view.
  244. However, when the cursor is on a timestamp, shift-cursor commands
  245. will still edit the time stamp - this is just too good to give up.
  246. XEmacs user should have this variable set to nil, because shift-select-mode
  247. is Emacs 23 only."
  248. :group 'org
  249. :type '(choice
  250. (const :tag "Never" nil)
  251. (const :tag "When outside special context" t)
  252. (const :tag "Everywhere except timestamps" always)))
  253. (defgroup org-startup nil
  254. "Options concerning startup of Org-mode."
  255. :tag "Org Startup"
  256. :group 'org)
  257. (defcustom org-startup-folded t
  258. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  259. This can also be configured on a per-file basis by adding one of
  260. the following lines anywhere in the buffer:
  261. #+STARTUP: fold (or `overview', this is equivalent)
  262. #+STARTUP: nofold (or `showall', this is equivalent)
  263. #+STARTUP: content
  264. #+STARTUP: showeverything"
  265. :group 'org-startup
  266. :type '(choice
  267. (const :tag "nofold: show all" nil)
  268. (const :tag "fold: overview" t)
  269. (const :tag "content: all headlines" content)
  270. (const :tag "show everything, even drawers" showeverything)))
  271. (defcustom org-startup-truncated t
  272. "Non-nil means, entering Org-mode will set `truncate-lines'.
  273. This is useful since some lines containing links can be very long and
  274. uninteresting. Also tables look terrible when wrapped."
  275. :group 'org-startup
  276. :type 'boolean)
  277. (defcustom org-startup-indented nil
  278. "Non-nil means, turn on `org-indent-mode' on startup.
  279. This can also be configured on a per-file basis by adding one of
  280. the following lines anywhere in the buffer:
  281. #+STARTUP: indent
  282. #+STARTUP: noindent"
  283. :group 'org-structure
  284. :type '(choice
  285. (const :tag "Not" nil)
  286. (const :tag "Globally (slow on startup in large files)" t)))
  287. (defcustom org-startup-align-all-tables nil
  288. "Non-nil means, align all tables when visiting a file.
  289. This is useful when the column width in tables is forced with <N> cookies
  290. in table fields. Such tables will look correct only after the first re-align.
  291. This can also be configured on a per-file basis by adding one of
  292. the following lines anywhere in the buffer:
  293. #+STARTUP: align
  294. #+STARTUP: noalign"
  295. :group 'org-startup
  296. :type 'boolean)
  297. (defcustom org-insert-mode-line-in-empty-file nil
  298. "Non-nil means insert the first line setting Org-mode in empty files.
  299. When the function `org-mode' is called interactively in an empty file, this
  300. normally means that the file name does not automatically trigger Org-mode.
  301. To ensure that the file will always be in Org-mode in the future, a
  302. line enforcing Org-mode will be inserted into the buffer, if this option
  303. has been set."
  304. :group 'org-startup
  305. :type 'boolean)
  306. (defcustom org-replace-disputed-keys nil
  307. "Non-nil means use alternative key bindings for some keys.
  308. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  309. These keys are also used by other packages like shift-selection-mode'
  310. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  311. If you want to use Org-mode together with one of these other modes,
  312. or more generally if you would like to move some Org-mode commands to
  313. other keys, set this variable and configure the keys with the variable
  314. `org-disputed-keys'.
  315. This option is only relevant at load-time of Org-mode, and must be set
  316. *before* org.el is loaded. Changing it requires a restart of Emacs to
  317. become effective."
  318. :group 'org-startup
  319. :type 'boolean)
  320. (defcustom org-use-extra-keys nil
  321. "Non-nil means use extra key sequence definitions for certain
  322. commands. This happens automatically if you run XEmacs or if
  323. window-system is nil. This variable lets you do the same
  324. manually. You must set it before loading org.
  325. Example: on Carbon Emacs 22 running graphically, with an external
  326. keyboard on a Powerbook, the default way of setting M-left might
  327. not work for either Alt or ESC. Setting this variable will make
  328. it work for ESC."
  329. :group 'org-startup
  330. :type 'boolean)
  331. (if (fboundp 'defvaralias)
  332. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  333. (defcustom org-disputed-keys
  334. '(([(shift up)] . [(meta p)])
  335. ([(shift down)] . [(meta n)])
  336. ([(shift left)] . [(meta -)])
  337. ([(shift right)] . [(meta +)])
  338. ([(control shift right)] . [(meta shift +)])
  339. ([(control shift left)] . [(meta shift -)]))
  340. "Keys for which Org-mode and other modes compete.
  341. This is an alist, cars are the default keys, second element specifies
  342. the alternative to use when `org-replace-disputed-keys' is t.
  343. Keys can be specified in any syntax supported by `define-key'.
  344. The value of this option takes effect only at Org-mode's startup,
  345. therefore you'll have to restart Emacs to apply it after changing."
  346. :group 'org-startup
  347. :type 'alist)
  348. (defun org-key (key)
  349. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  350. Or return the original if not disputed."
  351. (if org-replace-disputed-keys
  352. (let* ((nkey (key-description key))
  353. (x (org-find-if (lambda (x)
  354. (equal (key-description (car x)) nkey))
  355. org-disputed-keys)))
  356. (if x (cdr x) key))
  357. key))
  358. (defun org-find-if (predicate seq)
  359. (catch 'exit
  360. (while seq
  361. (if (funcall predicate (car seq))
  362. (throw 'exit (car seq))
  363. (pop seq)))))
  364. (defun org-defkey (keymap key def)
  365. "Define a key, possibly translated, as returned by `org-key'."
  366. (define-key keymap (org-key key) def))
  367. (defcustom org-ellipsis nil
  368. "The ellipsis to use in the Org-mode outline.
  369. When nil, just use the standard three dots. When a string, use that instead,
  370. When a face, use the standard 3 dots, but with the specified face.
  371. The change affects only Org-mode (which will then use its own display table).
  372. Changing this requires executing `M-x org-mode' in a buffer to become
  373. effective."
  374. :group 'org-startup
  375. :type '(choice (const :tag "Default" nil)
  376. (face :tag "Face" :value org-warning)
  377. (string :tag "String" :value "...#")))
  378. (defvar org-display-table nil
  379. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  380. (defgroup org-keywords nil
  381. "Keywords in Org-mode."
  382. :tag "Org Keywords"
  383. :group 'org)
  384. (defcustom org-deadline-string "DEADLINE:"
  385. "String to mark deadline entries.
  386. A deadline is this string, followed by a time stamp. Should be a word,
  387. terminated by a colon. You can insert a schedule keyword and
  388. a timestamp with \\[org-deadline].
  389. Changes become only effective after restarting Emacs."
  390. :group 'org-keywords
  391. :type 'string)
  392. (defcustom org-scheduled-string "SCHEDULED:"
  393. "String to mark scheduled TODO entries.
  394. A schedule is this string, followed by a time stamp. Should be a word,
  395. terminated by a colon. You can insert a schedule keyword and
  396. a timestamp with \\[org-schedule].
  397. Changes become only effective after restarting Emacs."
  398. :group 'org-keywords
  399. :type 'string)
  400. (defcustom org-closed-string "CLOSED:"
  401. "String used as the prefix for timestamps logging closing a TODO entry."
  402. :group 'org-keywords
  403. :type 'string)
  404. (defcustom org-clock-string "CLOCK:"
  405. "String used as prefix for timestamps clocking work hours on an item."
  406. :group 'org-keywords
  407. :type 'string)
  408. (defcustom org-comment-string "COMMENT"
  409. "Entries starting with this keyword will never be exported.
  410. An entry can be toggled between COMMENT and normal with
  411. \\[org-toggle-comment].
  412. Changes become only effective after restarting Emacs."
  413. :group 'org-keywords
  414. :type 'string)
  415. (defcustom org-quote-string "QUOTE"
  416. "Entries starting with this keyword will be exported in fixed-width font.
  417. Quoting applies only to the text in the entry following the headline, and does
  418. not extend beyond the next headline, even if that is lower level.
  419. An entry can be toggled between QUOTE and normal with
  420. \\[org-toggle-fixed-width-section]."
  421. :group 'org-keywords
  422. :type 'string)
  423. (defconst org-repeat-re
  424. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  425. "Regular expression for specifying repeated events.
  426. After a match, group 1 contains the repeat expression.")
  427. (defgroup org-structure nil
  428. "Options concerning the general structure of Org-mode files."
  429. :tag "Org Structure"
  430. :group 'org)
  431. (defgroup org-reveal-location nil
  432. "Options about how to make context of a location visible."
  433. :tag "Org Reveal Location"
  434. :group 'org-structure)
  435. (defconst org-context-choice
  436. '(choice
  437. (const :tag "Always" t)
  438. (const :tag "Never" nil)
  439. (repeat :greedy t :tag "Individual contexts"
  440. (cons
  441. (choice :tag "Context"
  442. (const agenda)
  443. (const org-goto)
  444. (const occur-tree)
  445. (const tags-tree)
  446. (const link-search)
  447. (const mark-goto)
  448. (const bookmark-jump)
  449. (const isearch)
  450. (const default))
  451. (boolean))))
  452. "Contexts for the reveal options.")
  453. (defcustom org-show-hierarchy-above '((default . t))
  454. "Non-nil means, show full hierarchy when revealing a location.
  455. Org-mode often shows locations in an org-mode file which might have
  456. been invisible before. When this is set, the hierarchy of headings
  457. above the exposed location is shown.
  458. Turning this off for example for sparse trees makes them very compact.
  459. Instead of t, this can also be an alist specifying this option for different
  460. contexts. Valid contexts are
  461. agenda when exposing an entry from the agenda
  462. org-goto when using the command `org-goto' on key C-c C-j
  463. occur-tree when using the command `org-occur' on key C-c /
  464. tags-tree when constructing a sparse tree based on tags matches
  465. link-search when exposing search matches associated with a link
  466. mark-goto when exposing the jump goal of a mark
  467. bookmark-jump when exposing a bookmark location
  468. isearch when exiting from an incremental search
  469. default default for all contexts not set explicitly"
  470. :group 'org-reveal-location
  471. :type org-context-choice)
  472. (defcustom org-show-following-heading '((default . nil))
  473. "Non-nil means, show following heading when revealing a location.
  474. Org-mode often shows locations in an org-mode file which might have
  475. been invisible before. When this is set, the heading following the
  476. match is shown.
  477. Turning this off for example for sparse trees makes them very compact,
  478. but makes it harder to edit the location of the match. In such a case,
  479. use the command \\[org-reveal] to show more context.
  480. Instead of t, this can also be an alist specifying this option for different
  481. contexts. See `org-show-hierarchy-above' for valid contexts."
  482. :group 'org-reveal-location
  483. :type org-context-choice)
  484. (defcustom org-show-siblings '((default . nil) (isearch t))
  485. "Non-nil means, show all sibling heading when revealing a location.
  486. Org-mode often shows locations in an org-mode file which might have
  487. been invisible before. When this is set, the sibling of the current entry
  488. heading are all made visible. If `org-show-hierarchy-above' is t,
  489. the same happens on each level of the hierarchy above the current entry.
  490. By default this is on for the isearch context, off for all other contexts.
  491. Turning this off for example for sparse trees makes them very compact,
  492. but makes it harder to edit the location of the match. In such a case,
  493. use the command \\[org-reveal] to show more context.
  494. Instead of t, this can also be an alist specifying this option for different
  495. contexts. See `org-show-hierarchy-above' for valid contexts."
  496. :group 'org-reveal-location
  497. :type org-context-choice)
  498. (defcustom org-show-entry-below '((default . nil))
  499. "Non-nil means, show the entry below a headline when revealing a location.
  500. Org-mode often shows locations in an org-mode file which might have
  501. been invisible before. When this is set, the text below the headline that is
  502. exposed is also shown.
  503. By default this is off for all contexts.
  504. Instead of t, this can also be an alist specifying this option for different
  505. contexts. See `org-show-hierarchy-above' for valid contexts."
  506. :group 'org-reveal-location
  507. :type org-context-choice)
  508. (defcustom org-indirect-buffer-display 'other-window
  509. "How should indirect tree buffers be displayed?
  510. This applies to indirect buffers created with the commands
  511. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  512. Valid values are:
  513. current-window Display in the current window
  514. other-window Just display in another window.
  515. dedicated-frame Create one new frame, and re-use it each time.
  516. new-frame Make a new frame each time. Note that in this case
  517. previously-made indirect buffers are kept, and you need to
  518. kill these buffers yourself."
  519. :group 'org-structure
  520. :group 'org-agenda-windows
  521. :type '(choice
  522. (const :tag "In current window" current-window)
  523. (const :tag "In current frame, other window" other-window)
  524. (const :tag "Each time a new frame" new-frame)
  525. (const :tag "One dedicated frame" dedicated-frame)))
  526. (defcustom org-use-speed-commands nil
  527. "Non-nil means, activate single letter commands at beginning of a headline."
  528. :group 'org-structure
  529. :type 'boolean)
  530. (defcustom org-speed-commands-user nil
  531. "Alist of additional speed commands.
  532. This list will be checked before `org-speed-commands-default'
  533. when the variable `org-use-speed-commands' is non-nil
  534. and when the cursor is at the beginning of a headline.
  535. The car if each entry is a string with a single letter, which must
  536. be assigned to `self-insert-command' in the global map.
  537. The cdr is either a command to be called interactively, a function
  538. to be called, or a form to be evaluated."
  539. :group 'org-structure
  540. :type '(repeat
  541. (cons
  542. (string "Command letter")
  543. (choice
  544. (function)
  545. (sexp)))))
  546. (defgroup org-cycle nil
  547. "Options concerning visibility cycling in Org-mode."
  548. :tag "Org Cycle"
  549. :group 'org-structure)
  550. (defcustom org-cycle-skip-children-state-if-no-children t
  551. "Non-nil means, skip CHILDREN state in entries that don't have any."
  552. :group 'org-cycle
  553. :type 'boolean)
  554. (defcustom org-cycle-max-level nil
  555. "Maximum level which should still be subject to visibility cycling.
  556. Levels higher than this will, for cycling, be treated as text, not a headline.
  557. When `org-odd-levels-only' is set, a value of N in this variable actually
  558. means 2N-1 stars as the limiting headline.
  559. When nil, cycle all levels.
  560. Note that the limiting level of cycling is also influenced by
  561. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  562. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  563. than its value."
  564. :group 'org-cycle
  565. :type '(choice
  566. (const :tag "No limit" nil)
  567. (integer :tag "Maximum level")))
  568. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  569. "Names of drawers. Drawers are not opened by cycling on the headline above.
  570. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  571. this:
  572. :DRAWERNAME:
  573. .....
  574. :END:
  575. The drawer \"PROPERTIES\" is special for capturing properties through
  576. the property API.
  577. Drawers can be defined on the per-file basis with a line like:
  578. #+DRAWERS: HIDDEN STATE PROPERTIES"
  579. :group 'org-structure
  580. :group 'org-cycle
  581. :type '(repeat (string :tag "Drawer Name")))
  582. (defcustom org-hide-block-startup nil
  583. "Non-nil means, , entering Org-mode will fold all blocks.
  584. This can also be set in on a per-file basis with
  585. #+STARTUP: hideblocks
  586. #+STARTUP: showblocks"
  587. :group 'org-startup
  588. :group 'org-cycle
  589. :type 'boolean)
  590. (defcustom org-cycle-global-at-bob nil
  591. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  592. This makes it possible to do global cycling without having to use S-TAB or
  593. C-u TAB. For this special case to work, the first line of the buffer
  594. must not be a headline - it may be empty or some other text. When used in
  595. this way, `org-cycle-hook' is disables temporarily, to make sure the
  596. cursor stays at the beginning of the buffer.
  597. When this option is nil, don't do anything special at the beginning
  598. of the buffer."
  599. :group 'org-cycle
  600. :type 'boolean)
  601. (defcustom org-cycle-emulate-tab t
  602. "Where should `org-cycle' emulate TAB.
  603. nil Never
  604. white Only in completely white lines
  605. whitestart Only at the beginning of lines, before the first non-white char
  606. t Everywhere except in headlines
  607. exc-hl-bol Everywhere except at the start of a headline
  608. If TAB is used in a place where it does not emulate TAB, the current subtree
  609. visibility is cycled."
  610. :group 'org-cycle
  611. :type '(choice (const :tag "Never" nil)
  612. (const :tag "Only in completely white lines" white)
  613. (const :tag "Before first char in a line" whitestart)
  614. (const :tag "Everywhere except in headlines" t)
  615. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  616. ))
  617. (defcustom org-cycle-separator-lines 2
  618. "Number of empty lines needed to keep an empty line between collapsed trees.
  619. If you leave an empty line between the end of a subtree and the following
  620. headline, this empty line is hidden when the subtree is folded.
  621. Org-mode will leave (exactly) one empty line visible if the number of
  622. empty lines is equal or larger to the number given in this variable.
  623. So the default 2 means, at least 2 empty lines after the end of a subtree
  624. are needed to produce free space between a collapsed subtree and the
  625. following headline.
  626. If the number is negative, and the number of empty lines is at least -N,
  627. all empty lines are shown.
  628. Special case: when 0, never leave empty lines in collapsed view."
  629. :group 'org-cycle
  630. :type 'integer)
  631. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  632. (defcustom org-pre-cycle-hook nil
  633. "Hook that is run before visibility cycling is happening.
  634. The function(s) in this hook must accept a single argument which indicates
  635. the new state that will be set right after running this hook. The
  636. argument is a symbol. Before a global state change, it can have the values
  637. `overview', `content', or `all'. Before a local state change, it can have
  638. the values `folded', `children', or `subtree'."
  639. :group 'org-cycle
  640. :type 'hook)
  641. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  642. org-cycle-hide-drawers
  643. org-cycle-show-empty-lines
  644. org-optimize-window-after-visibility-change)
  645. "Hook that is run after `org-cycle' has changed the buffer visibility.
  646. The function(s) in this hook must accept a single argument which indicates
  647. the new state that was set by the most recent `org-cycle' command. The
  648. argument is a symbol. After a global state change, it can have the values
  649. `overview', `content', or `all'. After a local state change, it can have
  650. the values `folded', `children', or `subtree'."
  651. :group 'org-cycle
  652. :type 'hook)
  653. (defgroup org-edit-structure nil
  654. "Options concerning structure editing in Org-mode."
  655. :tag "Org Edit Structure"
  656. :group 'org-structure)
  657. (defcustom org-odd-levels-only nil
  658. "Non-nil means, skip even levels and only use odd levels for the outline.
  659. This has the effect that two stars are being added/taken away in
  660. promotion/demotion commands. It also influences how levels are
  661. handled by the exporters.
  662. Changing it requires restart of `font-lock-mode' to become effective
  663. for fontification also in regions already fontified.
  664. You may also set this on a per-file basis by adding one of the following
  665. lines to the buffer:
  666. #+STARTUP: odd
  667. #+STARTUP: oddeven"
  668. :group 'org-edit-structure
  669. :group 'org-font-lock
  670. :type 'boolean)
  671. (defcustom org-adapt-indentation t
  672. "Non-nil means, adapt indentation to outline node level.
  673. When this variable is set, Org assumes that you write outlines by
  674. indenting text in each node to align with the headline (after the stars).
  675. The following issues are influenced by this variable:
  676. - When this is set and the *entire* text in an entry is indented, the
  677. indentation is increased by one space in a demotion command, and
  678. decreased by one in a promotion command. If any line in the entry
  679. body starts with text at column 0, indentation is not changed at all.
  680. - Property drawers and planning information is inserted indented when
  681. this variable s set. When nil, they will not be indented.
  682. - TAB indents a line relative to context. The lines below a headline
  683. will be indented when this variable is set.
  684. Note that this is all about true indentation, by adding and removing
  685. space characters. See also `org-indent.el' which does level-dependent
  686. indentation in a virtual way, i.e. at display time in Emacs."
  687. :group 'org-edit-structure
  688. :type 'boolean)
  689. (defcustom org-special-ctrl-a/e nil
  690. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  691. When t, `C-a' will bring back the cursor to the beginning of the
  692. headline text, i.e. after the stars and after a possible TODO keyword.
  693. In an item, this will be the position after the bullet.
  694. When the cursor is already at that position, another `C-a' will bring
  695. it to the beginning of the line.
  696. `C-e' will jump to the end of the headline, ignoring the presence of tags
  697. in the headline. A second `C-e' will then jump to the true end of the
  698. line, after any tags. This also means that, when this variable is
  699. non-nil, `C-e' also will never jump beyond the end of the heading of a
  700. folded section, i.e. not after the ellipses.
  701. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  702. going to the true line boundary first. Only a directly following, identical
  703. keypress will bring the cursor to the special positions.
  704. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  705. set separately."
  706. :group 'org-edit-structure
  707. :type '(choice
  708. (const :tag "off" nil)
  709. (const :tag "on: after stars/bullet and before tags first" t)
  710. (const :tag "reversed: true line boundary first" reversed)
  711. (cons :tag "Set C-a and C-e separately"
  712. (choice :tag "Special C-a"
  713. (const :tag "off" nil)
  714. (const :tag "on: after stars/bullet first" t)
  715. (const :tag "reversed: before stars/bullet first" reversed))
  716. (choice :tag "Special C-e"
  717. (const :tag "off" nil)
  718. (const :tag "on: before tags first" t)
  719. (const :tag "reversed: after tags first" reversed)))))
  720. (if (fboundp 'defvaralias)
  721. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  722. (defcustom org-special-ctrl-k nil
  723. "Non-nil means `C-k' will behave specially in headlines.
  724. When nil, `C-k' will call the default `kill-line' command.
  725. When t, the following will happen while the cursor is in the headline:
  726. - When the cursor is at the beginning of a headline, kill the entire
  727. line and possible the folded subtree below the line.
  728. - When in the middle of the headline text, kill the headline up to the tags.
  729. - When after the headline text, kill the tags."
  730. :group 'org-edit-structure
  731. :type 'boolean)
  732. (defcustom org-yank-folded-subtrees t
  733. "Non-nil means, when yanking subtrees, fold them.
  734. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  735. it starts with a heading and all other headings in it are either children
  736. or siblings, then fold all the subtrees. However, do this only if no
  737. text after the yank would be swallowed into a folded tree by this action."
  738. :group 'org-edit-structure
  739. :type 'boolean)
  740. (defcustom org-yank-adjusted-subtrees nil
  741. "Non-nil means, when yanking subtrees, adjust the level.
  742. With this setting, `org-paste-subtree' is used to insert the subtree, see
  743. this function for details."
  744. :group 'org-edit-structure
  745. :type 'boolean)
  746. (defcustom org-M-RET-may-split-line '((default . t))
  747. "Non-nil means, M-RET will split the line at the cursor position.
  748. When nil, it will go to the end of the line before making a
  749. new line.
  750. You may also set this option in a different way for different
  751. contexts. Valid contexts are:
  752. headline when creating a new headline
  753. item when creating a new item
  754. table in a table field
  755. default the value to be used for all contexts not explicitly
  756. customized"
  757. :group 'org-structure
  758. :group 'org-table
  759. :type '(choice
  760. (const :tag "Always" t)
  761. (const :tag "Never" nil)
  762. (repeat :greedy t :tag "Individual contexts"
  763. (cons
  764. (choice :tag "Context"
  765. (const headline)
  766. (const item)
  767. (const table)
  768. (const default))
  769. (boolean)))))
  770. (defcustom org-insert-heading-respect-content nil
  771. "Non-nil means, insert new headings after the current subtree.
  772. When nil, the new heading is created directly after the current line.
  773. The commands \\[org-insert-heading-respect-content] and
  774. \\[org-insert-todo-heading-respect-content] turn this variable on
  775. for the duration of the command."
  776. :group 'org-structure
  777. :type 'boolean)
  778. (defcustom org-blank-before-new-entry '((heading . auto)
  779. (plain-list-item . auto))
  780. "Should `org-insert-heading' leave a blank line before new heading/item?
  781. The value is an alist, with `heading' and `plain-list-item' as car,
  782. and a boolean flag as cdr. For plain lists, if the variable
  783. `org-empty-line-terminates-plain-lists' is set, the setting here
  784. is ignored and no empty line is inserted, to keep the list in tact."
  785. :group 'org-edit-structure
  786. :type '(list
  787. (cons (const heading)
  788. (choice (const :tag "Never" nil)
  789. (const :tag "Always" t)
  790. (const :tag "Auto" auto)))
  791. (cons (const plain-list-item)
  792. (choice (const :tag "Never" nil)
  793. (const :tag "Always" t)
  794. (const :tag "Auto" auto)))))
  795. (defcustom org-insert-heading-hook nil
  796. "Hook being run after inserting a new heading."
  797. :group 'org-edit-structure
  798. :type 'hook)
  799. (defcustom org-enable-fixed-width-editor t
  800. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  801. This currently only means, they are never auto-wrapped.
  802. When nil, such lines will be treated like ordinary lines.
  803. See also the QUOTE keyword."
  804. :group 'org-edit-structure
  805. :type 'boolean)
  806. (defcustom org-goto-auto-isearch t
  807. "Non-nil means, typing characters in org-goto starts incremental search."
  808. :group 'org-edit-structure
  809. :type 'boolean)
  810. (defgroup org-sparse-trees nil
  811. "Options concerning sparse trees in Org-mode."
  812. :tag "Org Sparse Trees"
  813. :group 'org-structure)
  814. (defcustom org-highlight-sparse-tree-matches t
  815. "Non-nil means, highlight all matches that define a sparse tree.
  816. The highlights will automatically disappear the next time the buffer is
  817. changed by an edit command."
  818. :group 'org-sparse-trees
  819. :type 'boolean)
  820. (defcustom org-remove-highlights-with-change t
  821. "Non-nil means, any change to the buffer will remove temporary highlights.
  822. Such highlights are created by `org-occur' and `org-clock-display'.
  823. When nil, `C-c C-c needs to be used to get rid of the highlights.
  824. The highlights created by `org-preview-latex-fragment' always need
  825. `C-c C-c' to be removed."
  826. :group 'org-sparse-trees
  827. :group 'org-time
  828. :type 'boolean)
  829. (defcustom org-occur-hook '(org-first-headline-recenter)
  830. "Hook that is run after `org-occur' has constructed a sparse tree.
  831. This can be used to recenter the window to show as much of the structure
  832. as possible."
  833. :group 'org-sparse-trees
  834. :type 'hook)
  835. (defgroup org-imenu-and-speedbar nil
  836. "Options concerning imenu and speedbar in Org-mode."
  837. :tag "Org Imenu and Speedbar"
  838. :group 'org-structure)
  839. (defcustom org-imenu-depth 2
  840. "The maximum level for Imenu access to Org-mode headlines.
  841. This also applied for speedbar access."
  842. :group 'org-imenu-and-speedbar
  843. :type 'integer)
  844. (defgroup org-table nil
  845. "Options concerning tables in Org-mode."
  846. :tag "Org Table"
  847. :group 'org)
  848. (defcustom org-enable-table-editor 'optimized
  849. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  850. When nil, such lines will be treated like ordinary lines.
  851. When equal to the symbol `optimized', the table editor will be optimized to
  852. do the following:
  853. - Automatic overwrite mode in front of whitespace in table fields.
  854. This makes the structure of the table stay in tact as long as the edited
  855. field does not exceed the column width.
  856. - Minimize the number of realigns. Normally, the table is aligned each time
  857. TAB or RET are pressed to move to another field. With optimization this
  858. happens only if changes to a field might have changed the column width.
  859. Optimization requires replacing the functions `self-insert-command',
  860. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  861. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  862. very good at guessing when a re-align will be necessary, but you can always
  863. force one with \\[org-ctrl-c-ctrl-c].
  864. If you would like to use the optimized version in Org-mode, but the
  865. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  866. This variable can be used to turn on and off the table editor during a session,
  867. but in order to toggle optimization, a restart is required.
  868. See also the variable `org-table-auto-blank-field'."
  869. :group 'org-table
  870. :type '(choice
  871. (const :tag "off" nil)
  872. (const :tag "on" t)
  873. (const :tag "on, optimized" optimized)))
  874. (defcustom org-self-insert-cluster-for-undo t
  875. "Non-nil means cluster self-insert commands for undo when possible.
  876. If this is set, then, like in the Emacs command loop, 20 consequtive
  877. characters will be undone together.
  878. This is configurable, because there is some impact on typing performance."
  879. :group 'org-table
  880. :type 'boolean)
  881. (defcustom org-table-tab-recognizes-table.el t
  882. "Non-nil means, TAB will automatically notice a table.el table.
  883. When it sees such a table, it moves point into it and - if necessary -
  884. calls `table-recognize-table'."
  885. :group 'org-table-editing
  886. :type 'boolean)
  887. (defgroup org-link nil
  888. "Options concerning links in Org-mode."
  889. :tag "Org Link"
  890. :group 'org)
  891. (defvar org-link-abbrev-alist-local nil
  892. "Buffer-local version of `org-link-abbrev-alist', which see.
  893. The value of this is taken from the #+LINK lines.")
  894. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  895. (defcustom org-link-abbrev-alist nil
  896. "Alist of link abbreviations.
  897. The car of each element is a string, to be replaced at the start of a link.
  898. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  899. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  900. [[linkkey:tag][description]]
  901. The 'linkkey' must be a word word, starting with a letter, followed
  902. by letters, numbers, '-' or '_'.
  903. If REPLACE is a string, the tag will simply be appended to create the link.
  904. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  905. the placeholder \"%h\" will cause a url-encoded version of the tag to
  906. be inserted at that point (see the function `url-hexify-string').
  907. REPLACE may also be a function that will be called with the tag as the
  908. only argument to create the link, which should be returned as a string.
  909. See the manual for examples."
  910. :group 'org-link
  911. :type '(repeat
  912. (cons
  913. (string :tag "Protocol")
  914. (choice
  915. (string :tag "Format")
  916. (function)))))
  917. (defcustom org-descriptive-links t
  918. "Non-nil means, hide link part and only show description of bracket links.
  919. Bracket links are like [[link][description]]. This variable sets the initial
  920. state in new org-mode buffers. The setting can then be toggled on a
  921. per-buffer basis from the Org->Hyperlinks menu."
  922. :group 'org-link
  923. :type 'boolean)
  924. (defcustom org-link-file-path-type 'adaptive
  925. "How the path name in file links should be stored.
  926. Valid values are:
  927. relative Relative to the current directory, i.e. the directory of the file
  928. into which the link is being inserted.
  929. absolute Absolute path, if possible with ~ for home directory.
  930. noabbrev Absolute path, no abbreviation of home directory.
  931. adaptive Use relative path for files in the current directory and sub-
  932. directories of it. For other files, use an absolute path."
  933. :group 'org-link
  934. :type '(choice
  935. (const relative)
  936. (const absolute)
  937. (const noabbrev)
  938. (const adaptive)))
  939. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  940. "Types of links that should be activated in Org-mode files.
  941. This is a list of symbols, each leading to the activation of a certain link
  942. type. In principle, it does not hurt to turn on most link types - there may
  943. be a small gain when turning off unused link types. The types are:
  944. bracket The recommended [[link][description]] or [[link]] links with hiding.
  945. angular Links in angular brackets that may contain whitespace like
  946. <bbdb:Carsten Dominik>.
  947. plain Plain links in normal text, no whitespace, like http://google.com.
  948. radio Text that is matched by a radio target, see manual for details.
  949. tag Tag settings in a headline (link to tag search).
  950. date Time stamps (link to calendar).
  951. footnote Footnote labels.
  952. Changing this variable requires a restart of Emacs to become effective."
  953. :group 'org-link
  954. :type '(set :greedy t
  955. (const :tag "Double bracket links (new style)" bracket)
  956. (const :tag "Angular bracket links (old style)" angular)
  957. (const :tag "Plain text links" plain)
  958. (const :tag "Radio target matches" radio)
  959. (const :tag "Tags" tag)
  960. (const :tag "Timestamps" date)
  961. (const :tag "Footnotes" footnote)))
  962. (defcustom org-make-link-description-function nil
  963. "Function to use to generate link descriptions from links. If
  964. nil the link location will be used. This function must take two
  965. parameters; the first is the link and the second the description
  966. org-insert-link has generated, and should return the description
  967. to use."
  968. :group 'org-link
  969. :type 'function)
  970. (defgroup org-link-store nil
  971. "Options concerning storing links in Org-mode."
  972. :tag "Org Store Link"
  973. :group 'org-link)
  974. (defcustom org-email-link-description-format "Email %c: %.30s"
  975. "Format of the description part of a link to an email or usenet message.
  976. The following %-escapes will be replaced by corresponding information:
  977. %F full \"From\" field
  978. %f name, taken from \"From\" field, address if no name
  979. %T full \"To\" field
  980. %t first name in \"To\" field, address if no name
  981. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  982. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  983. %s subject
  984. %m message-id.
  985. You may use normal field width specification between the % and the letter.
  986. This is for example useful to limit the length of the subject.
  987. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  988. :group 'org-link-store
  989. :type 'string)
  990. (defcustom org-from-is-user-regexp
  991. (let (r1 r2)
  992. (when (and user-mail-address (not (string= user-mail-address "")))
  993. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  994. (when (and user-full-name (not (string= user-full-name "")))
  995. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  996. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  997. "Regexp matched against the \"From:\" header of an email or usenet message.
  998. It should match if the message is from the user him/herself."
  999. :group 'org-link-store
  1000. :type 'regexp)
  1001. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1002. "Non-nil means, storing a link to an Org file will use entry IDs.
  1003. Note that before this variable is even considered, org-id must be loaded,
  1004. so please customize `org-modules' and turn it on.
  1005. The variable can have the following values:
  1006. t Create an ID if needed to make a link to the current entry.
  1007. create-if-interactive
  1008. If `org-store-link' is called directly (interactively, as a user
  1009. command), do create an ID to support the link. But when doing the
  1010. job for remember, only use the ID if it already exists. The
  1011. purpose of this setting is to avoid proliferation of unwanted
  1012. IDs, just because you happen to be in an Org file when you
  1013. call `org-remember' that automatically and preemptively
  1014. creates a link. If you do want to get an ID link in a remember
  1015. template to an entry not having an ID, create it first by
  1016. explicitly creating a link to it, using `C-c C-l' first.
  1017. create-if-interactive-and-no-custom-id
  1018. Like create-if-interactive, but do not create an ID if there is
  1019. a CUSTOM_ID property defined in the entry. This is the default.
  1020. use-existing
  1021. Use existing ID, do not create one.
  1022. nil Never use an ID to make a link, instead link using a text search for
  1023. the headline text."
  1024. :group 'org-link-store
  1025. :type '(choice
  1026. (const :tag "Create ID to make link" t)
  1027. (const :tag "Create if storing link interactively"
  1028. create-if-interactive)
  1029. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1030. create-if-interactive-and-no-custom-id)
  1031. (const :tag "Only use existing" use-existing)
  1032. (const :tag "Do not use ID to create link" nil)))
  1033. (defcustom org-context-in-file-links t
  1034. "Non-nil means, file links from `org-store-link' contain context.
  1035. A search string will be added to the file name with :: as separator and
  1036. used to find the context when the link is activated by the command
  1037. `org-open-at-point'.
  1038. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1039. negates this setting for the duration of the command."
  1040. :group 'org-link-store
  1041. :type 'boolean)
  1042. (defcustom org-keep-stored-link-after-insertion nil
  1043. "Non-nil means, keep link in list for entire session.
  1044. The command `org-store-link' adds a link pointing to the current
  1045. location to an internal list. These links accumulate during a session.
  1046. The command `org-insert-link' can be used to insert links into any
  1047. Org-mode file (offering completion for all stored links). When this
  1048. option is nil, every link which has been inserted once using \\[org-insert-link]
  1049. will be removed from the list, to make completing the unused links
  1050. more efficient."
  1051. :group 'org-link-store
  1052. :type 'boolean)
  1053. (defgroup org-link-follow nil
  1054. "Options concerning following links in Org-mode."
  1055. :tag "Org Follow Link"
  1056. :group 'org-link)
  1057. (defcustom org-link-translation-function nil
  1058. "Function to translate links with different syntax to Org syntax.
  1059. This can be used to translate links created for example by the Planner
  1060. or emacs-wiki packages to Org syntax.
  1061. The function must accept two parameters, a TYPE containing the link
  1062. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1063. which is everything after the link protocol. It should return a cons
  1064. with possibly modified values of type and path.
  1065. Org contains a function for this, so if you set this variable to
  1066. `org-translate-link-from-planner', you should be able follow many
  1067. links created by planner."
  1068. :group 'org-link-follow
  1069. :type 'function)
  1070. (defcustom org-follow-link-hook nil
  1071. "Hook that is run after a link has been followed."
  1072. :group 'org-link-follow
  1073. :type 'hook)
  1074. (defcustom org-tab-follows-link nil
  1075. "Non-nil means, on links TAB will follow the link.
  1076. Needs to be set before org.el is loaded.
  1077. This really should not be used, it does not make sense, and the
  1078. implementation is bad."
  1079. :group 'org-link-follow
  1080. :type 'boolean)
  1081. (defcustom org-return-follows-link nil
  1082. "Non-nil means, on links RET will follow the link.
  1083. Needs to be set before org.el is loaded."
  1084. :group 'org-link-follow
  1085. :type 'boolean)
  1086. (defcustom org-mouse-1-follows-link
  1087. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1088. "Non-nil means, mouse-1 on a link will follow the link.
  1089. A longer mouse click will still set point. Does not work on XEmacs.
  1090. Needs to be set before org.el is loaded."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defcustom org-mark-ring-length 4
  1094. "Number of different positions to be recorded in the ring
  1095. Changing this requires a restart of Emacs to work correctly."
  1096. :group 'org-link-follow
  1097. :type 'integer)
  1098. (defcustom org-link-frame-setup
  1099. '((vm . vm-visit-folder-other-frame)
  1100. (gnus . gnus-other-frame)
  1101. (file . find-file-other-window))
  1102. "Setup the frame configuration for following links.
  1103. When following a link with Emacs, it may often be useful to display
  1104. this link in another window or frame. This variable can be used to
  1105. set this up for the different types of links.
  1106. For VM, use any of
  1107. `vm-visit-folder'
  1108. `vm-visit-folder-other-frame'
  1109. For Gnus, use any of
  1110. `gnus'
  1111. `gnus-other-frame'
  1112. `org-gnus-no-new-news'
  1113. For FILE, use any of
  1114. `find-file'
  1115. `find-file-other-window'
  1116. `find-file-other-frame'
  1117. For the calendar, use the variable `calendar-setup'.
  1118. For BBDB, it is currently only possible to display the matches in
  1119. another window."
  1120. :group 'org-link-follow
  1121. :type '(list
  1122. (cons (const vm)
  1123. (choice
  1124. (const vm-visit-folder)
  1125. (const vm-visit-folder-other-window)
  1126. (const vm-visit-folder-other-frame)))
  1127. (cons (const gnus)
  1128. (choice
  1129. (const gnus)
  1130. (const gnus-other-frame)
  1131. (const org-gnus-no-new-news)))
  1132. (cons (const file)
  1133. (choice
  1134. (const find-file)
  1135. (const find-file-other-window)
  1136. (const find-file-other-frame)))))
  1137. (defcustom org-display-internal-link-with-indirect-buffer nil
  1138. "Non-nil means, use indirect buffer to display infile links.
  1139. Activating internal links (from one location in a file to another location
  1140. in the same file) normally just jumps to the location. When the link is
  1141. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1142. another window. When this option is set, the other window actually displays
  1143. an indirect buffer clone of the current buffer, to avoid any visibility
  1144. changes to the current buffer."
  1145. :group 'org-link-follow
  1146. :type 'boolean)
  1147. (defcustom org-open-non-existing-files nil
  1148. "Non-nil means, `org-open-file' will open non-existing files.
  1149. When nil, an error will be generated.
  1150. This variable applies only to external applications because they
  1151. might choke on non-existing files. If the link is to a file that
  1152. will be openend in Emacs, the variable is ignored."
  1153. :group 'org-link-follow
  1154. :type 'boolean)
  1155. (defcustom org-open-directory-means-index-dot-org nil
  1156. "Non-nil means, a link to a directory really means to index.org.
  1157. When nil, following a directory link will run dired or open a finder/explorer
  1158. window on that directory."
  1159. :group 'org-link-follow
  1160. :type 'boolean)
  1161. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1162. "Function and arguments to call for following mailto links.
  1163. This is a list with the first element being a lisp function, and the
  1164. remaining elements being arguments to the function. In string arguments,
  1165. %a will be replaced by the address, and %s will be replaced by the subject
  1166. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1167. :group 'org-link-follow
  1168. :type '(choice
  1169. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1170. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1171. (const :tag "message-mail" (message-mail "%a" "%s"))
  1172. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1173. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1174. "Non-nil means, ask for confirmation before executing shell links.
  1175. Shell links can be dangerous: just think about a link
  1176. [[shell:rm -rf ~/*][Google Search]]
  1177. This link would show up in your Org-mode document as \"Google Search\",
  1178. but really it would remove your entire home directory.
  1179. Therefore we advise against setting this variable to nil.
  1180. Just change it to `y-or-n-p' if you want to confirm with a
  1181. single keystroke rather than having to type \"yes\"."
  1182. :group 'org-link-follow
  1183. :type '(choice
  1184. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1185. (const :tag "with y-or-n (faster)" y-or-n-p)
  1186. (const :tag "no confirmation (dangerous)" nil)))
  1187. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1188. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1189. Elisp links can be dangerous: just think about a link
  1190. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1191. This link would show up in your Org-mode document as \"Google Search\",
  1192. but really it would remove your entire home directory.
  1193. Therefore we advise against setting this variable to nil.
  1194. Just change it to `y-or-n-p' if you want to confirm with a
  1195. single keystroke rather than having to type \"yes\"."
  1196. :group 'org-link-follow
  1197. :type '(choice
  1198. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1199. (const :tag "with y-or-n (faster)" y-or-n-p)
  1200. (const :tag "no confirmation (dangerous)" nil)))
  1201. (defconst org-file-apps-defaults-gnu
  1202. '((remote . emacs)
  1203. (system . mailcap)
  1204. (t . mailcap))
  1205. "Default file applications on a UNIX or GNU/Linux system.
  1206. See `org-file-apps'.")
  1207. (defconst org-file-apps-defaults-macosx
  1208. '((remote . emacs)
  1209. (t . "open %s")
  1210. (system . "open %s")
  1211. ("ps.gz" . "gv %s")
  1212. ("eps.gz" . "gv %s")
  1213. ("dvi" . "xdvi %s")
  1214. ("fig" . "xfig %s"))
  1215. "Default file applications on a MacOS X system.
  1216. The system \"open\" is known as a default, but we use X11 applications
  1217. for some files for which the OS does not have a good default.
  1218. See `org-file-apps'.")
  1219. (defconst org-file-apps-defaults-windowsnt
  1220. (list
  1221. '(remote . emacs)
  1222. (cons t
  1223. (list (if (featurep 'xemacs)
  1224. 'mswindows-shell-execute
  1225. 'w32-shell-execute)
  1226. "open" 'file))
  1227. (cons 'system
  1228. (list (if (featurep 'xemacs)
  1229. 'mswindows-shell-execute
  1230. 'w32-shell-execute)
  1231. "open" 'file)))
  1232. "Default file applications on a Windows NT system.
  1233. The system \"open\" is used for most files.
  1234. See `org-file-apps'.")
  1235. (defcustom org-file-apps
  1236. '(
  1237. (auto-mode . emacs)
  1238. ("\\.mm\\'" . default)
  1239. ("\\.x?html?\\'" . default)
  1240. ("\\.pdf\\'" . default)
  1241. )
  1242. "External applications for opening `file:path' items in a document.
  1243. Org-mode uses system defaults for different file types, but
  1244. you can use this variable to set the application for a given file
  1245. extension. The entries in this list are cons cells where the car identifies
  1246. files and the cdr the corresponding command. Possible values for the
  1247. file identifier are
  1248. \"regex\" Regular expression matched against the file name. For backward
  1249. compatibility, this can also be a string with only alphanumeric
  1250. characters, which is then interpreted as an extension.
  1251. `directory' Matches a directory
  1252. `remote' Matches a remote file, accessible through tramp or efs.
  1253. Remote files most likely should be visited through Emacs
  1254. because external applications cannot handle such paths.
  1255. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1256. so all files Emacs knows how to handle. Using this with
  1257. command `emacs' will open most files in Emacs. Beware that this
  1258. will also open html files inside Emacs, unless you add
  1259. (\"html\" . default) to the list as well.
  1260. t Default for files not matched by any of the other options.
  1261. `system' The system command to open files, like `open' on Windows
  1262. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1263. that will be selected if you call `C-c C-o' with a double
  1264. `C-u C-u' prefix.
  1265. Possible values for the command are:
  1266. `emacs' The file will be visited by the current Emacs process.
  1267. `default' Use the default application for this file type, which is the
  1268. association for t in the list, most likely in the system-specific
  1269. part.
  1270. This can be used to overrule an unwanted setting in the
  1271. system-specific variable.
  1272. `system' Use the system command for opening files, like \"open\".
  1273. This command is specified by the entry whose car is `system'.
  1274. Most likely, the system-specific version of this variable
  1275. does define this command, but you can overrule/replace it
  1276. here.
  1277. string A command to be executed by a shell; %s will be replaced
  1278. by the path to the file.
  1279. sexp A Lisp form which will be evaluated. The file path will
  1280. be available in the Lisp variable `file'.
  1281. For more examples, see the system specific constants
  1282. `org-file-apps-defaults-macosx'
  1283. `org-file-apps-defaults-windowsnt'
  1284. `org-file-apps-defaults-gnu'."
  1285. :group 'org-link-follow
  1286. :type '(repeat
  1287. (cons (choice :value ""
  1288. (string :tag "Extension")
  1289. (const :tag "System command to open files" system)
  1290. (const :tag "Default for unrecognized files" t)
  1291. (const :tag "Remote file" remote)
  1292. (const :tag "Links to a directory" directory)
  1293. (const :tag "Any files that have Emacs modes"
  1294. auto-mode))
  1295. (choice :value ""
  1296. (const :tag "Visit with Emacs" emacs)
  1297. (const :tag "Use default" default)
  1298. (const :tag "Use the system command" system)
  1299. (string :tag "Command")
  1300. (sexp :tag "Lisp form")))))
  1301. (defgroup org-refile nil
  1302. "Options concerning refiling entries in Org-mode."
  1303. :tag "Org Refile"
  1304. :group 'org)
  1305. (defcustom org-directory "~/org"
  1306. "Directory with org files.
  1307. This is just a default location to look for Org files. There is no need
  1308. at all to put your files into this directory. It is only used in the
  1309. following situations:
  1310. 1. When a remember template specifies a target file that is not an
  1311. absolute path. The path will then be interpreted relative to
  1312. `org-directory'
  1313. 2. When a remember note is filed away in an interactive way (when exiting the
  1314. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1315. with `org-directory' as the default path."
  1316. :group 'org-refile
  1317. :group 'org-remember
  1318. :type 'directory)
  1319. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1320. "Default target for storing notes.
  1321. Used by the hooks for remember.el. This can be a string, or nil to mean
  1322. the value of `remember-data-file'.
  1323. You can set this on a per-template basis with the variable
  1324. `org-remember-templates'."
  1325. :group 'org-refile
  1326. :group 'org-remember
  1327. :type '(choice
  1328. (const :tag "Default from remember-data-file" nil)
  1329. file))
  1330. (defcustom org-goto-interface 'outline
  1331. "The default interface to be used for `org-goto'.
  1332. Allowed values are:
  1333. outline The interface shows an outline of the relevant file
  1334. and the correct heading is found by moving through
  1335. the outline or by searching with incremental search.
  1336. outline-path-completion Headlines in the current buffer are offered via
  1337. completion. This is the interface also used by
  1338. the refile command."
  1339. :group 'org-refile
  1340. :type '(choice
  1341. (const :tag "Outline" outline)
  1342. (const :tag "Outline-path-completion" outline-path-completion)))
  1343. (defcustom org-goto-max-level 5
  1344. "Maximum level to be considered when running org-goto with refile interface."
  1345. :group 'org-refile
  1346. :type 'integer)
  1347. (defcustom org-reverse-note-order nil
  1348. "Non-nil means, store new notes at the beginning of a file or entry.
  1349. When nil, new notes will be filed to the end of a file or entry.
  1350. This can also be a list with cons cells of regular expressions that
  1351. are matched against file names, and values."
  1352. :group 'org-remember
  1353. :group 'org-refile
  1354. :type '(choice
  1355. (const :tag "Reverse always" t)
  1356. (const :tag "Reverse never" nil)
  1357. (repeat :tag "By file name regexp"
  1358. (cons regexp boolean))))
  1359. (defcustom org-refile-targets nil
  1360. "Targets for refiling entries with \\[org-refile].
  1361. This is list of cons cells. Each cell contains:
  1362. - a specification of the files to be considered, either a list of files,
  1363. or a symbol whose function or variable value will be used to retrieve
  1364. a file name or a list of file names. If you use `org-agenda-files' for
  1365. that, all agenda files will be scanned for targets. Nil means, consider
  1366. headings in the current buffer.
  1367. - A specification of how to find candidate refile targets. This may be
  1368. any of:
  1369. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1370. This tag has to be present in all target headlines, inheritance will
  1371. not be considered.
  1372. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1373. todo keyword.
  1374. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1375. headlines that are refiling targets.
  1376. - a cons cell (:level . N). Any headline of level N is considered a target.
  1377. Note that, when `org-odd-levels-only' is set, level corresponds to
  1378. order in hierarchy, not to the number of stars.
  1379. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1380. Note that, when `org-odd-levels-only' is set, level corresponds to
  1381. order in hierarchy, not to the number of stars.
  1382. You can set the variable `org-refile-target-verify-function' to a function
  1383. to verify each headline found by the simple critery above.
  1384. When this variable is nil, all top-level headlines in the current buffer
  1385. are used, equivalent to the value `((nil . (:level . 1))'."
  1386. :group 'org-refile
  1387. :type '(repeat
  1388. (cons
  1389. (choice :value org-agenda-files
  1390. (const :tag "All agenda files" org-agenda-files)
  1391. (const :tag "Current buffer" nil)
  1392. (function) (variable) (file))
  1393. (choice :tag "Identify target headline by"
  1394. (cons :tag "Specific tag" (const :value :tag) (string))
  1395. (cons :tag "TODO keyword" (const :value :todo) (string))
  1396. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1397. (cons :tag "Level number" (const :value :level) (integer))
  1398. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1399. (defcustom org-refile-target-verify-function nil
  1400. "Function to verify if the headline at point should be a refile target.
  1401. The function will be called without arguments, with point at the
  1402. beginning of the headline. It should return t and leave point
  1403. where it is if the headline is a valid target for refiling.
  1404. If the target should not be selected, the function must return nil.
  1405. In addition to this, it may move point to a place from where the search
  1406. should be continued. For example, the function may decide that the entire
  1407. subtree of the current entry should be excluded and move point to the end
  1408. of the subtree."
  1409. :group 'org-refile
  1410. :type 'function)
  1411. (defcustom org-refile-use-outline-path nil
  1412. "Non-nil means, provide refile targets as paths.
  1413. So a level 3 headline will be available as level1/level2/level3.
  1414. When the value is `file', also include the file name (without directory)
  1415. into the path. In this case, you can also stop the completion after
  1416. the file name, to get entries inserted as top level in the file.
  1417. When `full-file-path', include the full file path."
  1418. :group 'org-refile
  1419. :type '(choice
  1420. (const :tag "Not" nil)
  1421. (const :tag "Yes" t)
  1422. (const :tag "Start with file name" file)
  1423. (const :tag "Start with full file path" full-file-path)))
  1424. (defcustom org-outline-path-complete-in-steps t
  1425. "Non-nil means, complete the outline path in hierarchical steps.
  1426. When Org-mode uses the refile interface to select an outline path
  1427. \(see variable `org-refile-use-outline-path'), the completion of
  1428. the path can be done is a single go, or if can be done in steps down
  1429. the headline hierarchy. Going in steps is probably the best if you
  1430. do not use a special completion package like `ido' or `icicles'.
  1431. However, when using these packages, going in one step can be very
  1432. fast, while still showing the whole path to the entry."
  1433. :group 'org-refile
  1434. :type 'boolean)
  1435. (defcustom org-refile-allow-creating-parent-nodes nil
  1436. "Non-nil means, allow to create new nodes as refile targets.
  1437. New nodes are then created by adding \"/new node name\" to the completion
  1438. of an existing node. When the value of this variable is `confirm',
  1439. new node creation must be confirmed by the user (recommended)
  1440. When nil, the completion must match an existing entry.
  1441. Note that, if the new heading is not seen by the criteria
  1442. listed in `org-refile-targets', multiple instances of the same
  1443. heading would be created by trying again to file under the new
  1444. heading."
  1445. :group 'org-refile
  1446. :type '(choice
  1447. (const :tag "Never" nil)
  1448. (const :tag "Always" t)
  1449. (const :tag "Prompt for confirmation" confirm)))
  1450. (defgroup org-todo nil
  1451. "Options concerning TODO items in Org-mode."
  1452. :tag "Org TODO"
  1453. :group 'org)
  1454. (defgroup org-progress nil
  1455. "Options concerning Progress logging in Org-mode."
  1456. :tag "Org Progress"
  1457. :group 'org-time)
  1458. (defvar org-todo-interpretation-widgets
  1459. '(
  1460. (:tag "Sequence (cycling hits every state)" sequence)
  1461. (:tag "Type (cycling directly to DONE)" type))
  1462. "The available interpretation symbols for customizing
  1463. `org-todo-keywords'.
  1464. Interested libraries should add to this list.")
  1465. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1466. "List of TODO entry keyword sequences and their interpretation.
  1467. \\<org-mode-map>This is a list of sequences.
  1468. Each sequence starts with a symbol, either `sequence' or `type',
  1469. indicating if the keywords should be interpreted as a sequence of
  1470. action steps, or as different types of TODO items. The first
  1471. keywords are states requiring action - these states will select a headline
  1472. for inclusion into the global TODO list Org-mode produces. If one of
  1473. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1474. signify that no further action is necessary. If \"|\" is not found,
  1475. the last keyword is treated as the only DONE state of the sequence.
  1476. The command \\[org-todo] cycles an entry through these states, and one
  1477. additional state where no keyword is present. For details about this
  1478. cycling, see the manual.
  1479. TODO keywords and interpretation can also be set on a per-file basis with
  1480. the special #+SEQ_TODO and #+TYP_TODO lines.
  1481. Each keyword can optionally specify a character for fast state selection
  1482. \(in combination with the variable `org-use-fast-todo-selection')
  1483. and specifiers for state change logging, using the same syntax
  1484. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1485. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1486. indicates to record a time stamp each time this state is selected.
  1487. Each keyword may also specify if a timestamp or a note should be
  1488. recorded when entering or leaving the state, by adding additional
  1489. characters in the parenthesis after the keyword. This looks like this:
  1490. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1491. record only the time of the state change. With X and Y being either
  1492. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1493. Y when leaving the state if and only if the *target* state does not
  1494. define X. You may omit any of the fast-selection key or X or /Y,
  1495. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1496. For backward compatibility, this variable may also be just a list
  1497. of keywords - in this case the interpretation (sequence or type) will be
  1498. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1499. :group 'org-todo
  1500. :group 'org-keywords
  1501. :type '(choice
  1502. (repeat :tag "Old syntax, just keywords"
  1503. (string :tag "Keyword"))
  1504. (repeat :tag "New syntax"
  1505. (cons
  1506. (choice
  1507. :tag "Interpretation"
  1508. ;;Quick and dirty way to see
  1509. ;;`org-todo-interpretations'. This takes the
  1510. ;;place of item arguments
  1511. :convert-widget
  1512. (lambda (widget)
  1513. (widget-put widget
  1514. :args (mapcar
  1515. #'(lambda (x)
  1516. (widget-convert
  1517. (cons 'const x)))
  1518. org-todo-interpretation-widgets))
  1519. widget))
  1520. (repeat
  1521. (string :tag "Keyword"))))))
  1522. (defvar org-todo-keywords-1 nil
  1523. "All TODO and DONE keywords active in a buffer.")
  1524. (make-variable-buffer-local 'org-todo-keywords-1)
  1525. (defvar org-todo-keywords-for-agenda nil)
  1526. (defvar org-done-keywords-for-agenda nil)
  1527. (defvar org-drawers-for-agenda nil)
  1528. (defvar org-todo-keyword-alist-for-agenda nil)
  1529. (defvar org-tag-alist-for-agenda nil)
  1530. (defvar org-agenda-contributing-files nil)
  1531. (defvar org-not-done-keywords nil)
  1532. (make-variable-buffer-local 'org-not-done-keywords)
  1533. (defvar org-done-keywords nil)
  1534. (make-variable-buffer-local 'org-done-keywords)
  1535. (defvar org-todo-heads nil)
  1536. (make-variable-buffer-local 'org-todo-heads)
  1537. (defvar org-todo-sets nil)
  1538. (make-variable-buffer-local 'org-todo-sets)
  1539. (defvar org-todo-log-states nil)
  1540. (make-variable-buffer-local 'org-todo-log-states)
  1541. (defvar org-todo-kwd-alist nil)
  1542. (make-variable-buffer-local 'org-todo-kwd-alist)
  1543. (defvar org-todo-key-alist nil)
  1544. (make-variable-buffer-local 'org-todo-key-alist)
  1545. (defvar org-todo-key-trigger nil)
  1546. (make-variable-buffer-local 'org-todo-key-trigger)
  1547. (defcustom org-todo-interpretation 'sequence
  1548. "Controls how TODO keywords are interpreted.
  1549. This variable is in principle obsolete and is only used for
  1550. backward compatibility, if the interpretation of todo keywords is
  1551. not given already in `org-todo-keywords'. See that variable for
  1552. more information."
  1553. :group 'org-todo
  1554. :group 'org-keywords
  1555. :type '(choice (const sequence)
  1556. (const type)))
  1557. (defcustom org-use-fast-todo-selection t
  1558. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1559. This variable describes if and under what circumstances the cycling
  1560. mechanism for TODO keywords will be replaced by a single-key, direct
  1561. selection scheme.
  1562. When nil, fast selection is never used.
  1563. When the symbol `prefix', it will be used when `org-todo' is called with
  1564. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1565. in an agenda buffer.
  1566. When t, fast selection is used by default. In this case, the prefix
  1567. argument forces cycling instead.
  1568. In all cases, the special interface is only used if access keys have actually
  1569. been assigned by the user, i.e. if keywords in the configuration are followed
  1570. by a letter in parenthesis, like TODO(t)."
  1571. :group 'org-todo
  1572. :type '(choice
  1573. (const :tag "Never" nil)
  1574. (const :tag "By default" t)
  1575. (const :tag "Only with C-u C-c C-t" prefix)))
  1576. (defcustom org-provide-todo-statistics t
  1577. "Non-nil means, update todo statistics after insert and toggle.
  1578. ALL-HEADLINES means update todo statistics by including headlines
  1579. with no TODO keyword as well, counting them as not done.
  1580. A list of TODO keywords means the same, but skip keywords that are
  1581. not in this list.
  1582. When this is set, todo statistics is updated in the parent of the
  1583. current entry each time a todo state is changed."
  1584. :group 'org-todo
  1585. :type '(choice
  1586. (const :tag "Yes, only for TODO entries" t)
  1587. (const :tag "Yes, including all entries" 'all-headlines)
  1588. (repeat :tag "Yes, for TODOs in this list"
  1589. (string :tag "TODO keyword"))
  1590. (other :tag "No TODO statistics" nil)))
  1591. (defcustom org-hierarchical-todo-statistics t
  1592. "Non-nil means, TODO statistics covers just direct children.
  1593. When nil, all entries in the subtree are considered.
  1594. This has only an effect if `org-provide-todo-statistics' is set.
  1595. To set this to nil for only a single subtree, use a COOKIE_DATA
  1596. property and include the word \"recursive\" into the value."
  1597. :group 'org-todo
  1598. :type 'boolean)
  1599. (defcustom org-after-todo-state-change-hook nil
  1600. "Hook which is run after the state of a TODO item was changed.
  1601. The new state (a string with a TODO keyword, or nil) is available in the
  1602. Lisp variable `state'."
  1603. :group 'org-todo
  1604. :type 'hook)
  1605. (defvar org-blocker-hook nil
  1606. "Hook for functions that are allowed to block a state change.
  1607. Each function gets as its single argument a property list, see
  1608. `org-trigger-hook' for more information about this list.
  1609. If any of the functions in this hook returns nil, the state change
  1610. is blocked.")
  1611. (defvar org-trigger-hook nil
  1612. "Hook for functions that are triggered by a state change.
  1613. Each function gets as its single argument a property list with at least
  1614. the following elements:
  1615. (:type type-of-change :position pos-at-entry-start
  1616. :from old-state :to new-state)
  1617. Depending on the type, more properties may be present.
  1618. This mechanism is currently implemented for:
  1619. TODO state changes
  1620. ------------------
  1621. :type todo-state-change
  1622. :from previous state (keyword as a string), or nil, or a symbol
  1623. 'todo' or 'done', to indicate the general type of state.
  1624. :to new state, like in :from")
  1625. (defcustom org-enforce-todo-dependencies nil
  1626. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1627. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1628. be blocked if any prior sibling is not yet done.
  1629. Finally, if the parent is blocked because of ordered siblings of its own,
  1630. the child will also be blocked.
  1631. This variable needs to be set before org.el is loaded, and you need to
  1632. restart Emacs after a change to make the change effective. The only way
  1633. to change is while Emacs is running is through the customize interface."
  1634. :set (lambda (var val)
  1635. (set var val)
  1636. (if val
  1637. (add-hook 'org-blocker-hook
  1638. 'org-block-todo-from-children-or-siblings-or-parent)
  1639. (remove-hook 'org-blocker-hook
  1640. 'org-block-todo-from-children-or-siblings-or-parent)))
  1641. :group 'org-todo
  1642. :type 'boolean)
  1643. (defcustom org-enforce-todo-checkbox-dependencies nil
  1644. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1645. When this is nil, checkboxes have no influence on switching TODO states.
  1646. When non-nil, you first need to check off all check boxes before the TODO
  1647. entry can be switched to DONE.
  1648. This variable needs to be set before org.el is loaded, and you need to
  1649. restart Emacs after a change to make the change effective. The only way
  1650. to change is while Emacs is running is through the customize interface."
  1651. :set (lambda (var val)
  1652. (set var val)
  1653. (if val
  1654. (add-hook 'org-blocker-hook
  1655. 'org-block-todo-from-checkboxes)
  1656. (remove-hook 'org-blocker-hook
  1657. 'org-block-todo-from-checkboxes)))
  1658. :group 'org-todo
  1659. :type 'boolean)
  1660. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1661. "Non-nil means, inserting a TODO heading is treated as state change.
  1662. So when the command \\[org-insert-todo-heading] is used, state change
  1663. logging will apply if appropriate. When nil, the new TODO item will
  1664. be inserted directly, and no logging will take place."
  1665. :group 'org-todo
  1666. :type 'boolean)
  1667. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1668. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1669. This is the default behavior. However, setting this to nil allows a
  1670. convenient way to select a TODO state and bypass any logging associated
  1671. with that."
  1672. :group 'org-todo
  1673. :type 'boolean)
  1674. (defcustom org-todo-state-tags-triggers nil
  1675. "Tag changes that should be triggered by TODO state changes.
  1676. This is a list. Each entry is
  1677. (state-change (tag . flag) .......)
  1678. State-change can be a string with a state, and empty string to indicate the
  1679. state that has no TODO keyword, or it can be one of the symbols `todo'
  1680. or `done', meaning any not-done or done state, respectively."
  1681. :group 'org-todo
  1682. :group 'org-tags
  1683. :type '(repeat
  1684. (cons (choice :tag "When changing to"
  1685. (const :tag "Not-done state" todo)
  1686. (const :tag "Done state" done)
  1687. (string :tag "State"))
  1688. (repeat
  1689. (cons :tag "Tag action"
  1690. (string :tag "Tag")
  1691. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1692. (defcustom org-log-done nil
  1693. "Information to record when a task moves to the DONE state.
  1694. Possible values are:
  1695. nil Don't add anything, just change the keyword
  1696. time Add a time stamp to the task
  1697. note Prompt a closing note and add it with template `org-log-note-headings'
  1698. This option can also be set with on a per-file-basis with
  1699. #+STARTUP: nologdone
  1700. #+STARTUP: logdone
  1701. #+STARTUP: lognotedone
  1702. You can have local logging settings for a subtree by setting the LOGGING
  1703. property to one or more of these keywords."
  1704. :group 'org-todo
  1705. :group 'org-progress
  1706. :type '(choice
  1707. (const :tag "No logging" nil)
  1708. (const :tag "Record CLOSED timestamp" time)
  1709. (const :tag "Record CLOSED timestamp with closing note." note)))
  1710. ;; Normalize old uses of org-log-done.
  1711. (cond
  1712. ((eq org-log-done t) (setq org-log-done 'time))
  1713. ((and (listp org-log-done) (memq 'done org-log-done))
  1714. (setq org-log-done 'note)))
  1715. (defcustom org-log-note-clock-out nil
  1716. "Non-nil means, record a note when clocking out of an item.
  1717. This can also be configured on a per-file basis by adding one of
  1718. the following lines anywhere in the buffer:
  1719. #+STARTUP: lognoteclock-out
  1720. #+STARTUP: nolognoteclock-out"
  1721. :group 'org-todo
  1722. :group 'org-progress
  1723. :type 'boolean)
  1724. (defcustom org-log-done-with-time t
  1725. "Non-nil means, the CLOSED time stamp will contain date and time.
  1726. When nil, only the date will be recorded."
  1727. :group 'org-progress
  1728. :type 'boolean)
  1729. (defcustom org-log-note-headings
  1730. '((done . "CLOSING NOTE %t")
  1731. (state . "State %-12s from %-12S %t")
  1732. (note . "Note taken on %t")
  1733. (clock-out . ""))
  1734. "Headings for notes added to entries.
  1735. The value is an alist, with the car being a symbol indicating the note
  1736. context, and the cdr is the heading to be used. The heading may also be the
  1737. empty string.
  1738. %t in the heading will be replaced by a time stamp.
  1739. %s will be replaced by the new TODO state, in double quotes.
  1740. %S will be replaced by the old TODO state, in double quotes.
  1741. %u will be replaced by the user name.
  1742. %U will be replaced by the full user name."
  1743. :group 'org-todo
  1744. :group 'org-progress
  1745. :type '(list :greedy t
  1746. (cons (const :tag "Heading when closing an item" done) string)
  1747. (cons (const :tag
  1748. "Heading when changing todo state (todo sequence only)"
  1749. state) string)
  1750. (cons (const :tag "Heading when just taking a note" note) string)
  1751. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1752. (unless (assq 'note org-log-note-headings)
  1753. (push '(note . "%t") org-log-note-headings))
  1754. (defcustom org-log-into-drawer nil
  1755. "Non-nil means, insert state change notes and time stamps into a drawer.
  1756. When nil, state changes notes will be inserted after the headline and
  1757. any scheduling and clock lines, but not inside a drawer.
  1758. The value of this variable should be the name of the drawer to use.
  1759. LOGBOOK is proposed at the default drawer for this purpose, you can
  1760. also set this to a string to define the drawer of your choice.
  1761. A value of t is also allowed, representing \"LOGBOOK\".
  1762. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1763. will be ignored.
  1764. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1765. a subtree."
  1766. :group 'org-todo
  1767. :group 'org-progress
  1768. :type '(choice
  1769. (const :tag "Not into a drawer" nil)
  1770. (const :tag "LOGBOOK" t)
  1771. (string :tag "Other")))
  1772. (if (fboundp 'defvaralias)
  1773. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1774. (defun org-log-into-drawer ()
  1775. "Return the value of `org-log-into-drawer', but let properties overrule.
  1776. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1777. used instead of the default value."
  1778. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1779. (cond
  1780. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1781. ((equal p "t") "LOGBOOK")
  1782. (t p))))
  1783. (defcustom org-log-state-notes-insert-after-drawers nil
  1784. "Non-nil means, insert state change notes after any drawers in entry.
  1785. Only the drawers that *immediately* follow the headline and the
  1786. deadline/scheduled line are skipped.
  1787. When nil, insert notes right after the heading and perhaps the line
  1788. with deadline/scheduling if present.
  1789. This variable will have no effect if `org-log-into-drawer' is
  1790. set."
  1791. :group 'org-todo
  1792. :group 'org-progress
  1793. :type 'boolean)
  1794. (defcustom org-log-states-order-reversed t
  1795. "Non-nil means, the latest state change note will be directly after heading.
  1796. When nil, the notes will be orderer according to time."
  1797. :group 'org-todo
  1798. :group 'org-progress
  1799. :type 'boolean)
  1800. (defcustom org-log-repeat 'time
  1801. "Non-nil means, record moving through the DONE state when triggering repeat.
  1802. An auto-repeating task is immediately switched back to TODO when
  1803. marked DONE. If you are not logging state changes (by adding \"@\"
  1804. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1805. record a closing note, there will be no record of the task moving
  1806. through DONE. This variable forces taking a note anyway.
  1807. nil Don't force a record
  1808. time Record a time stamp
  1809. note Record a note
  1810. This option can also be set with on a per-file-basis with
  1811. #+STARTUP: logrepeat
  1812. #+STARTUP: lognoterepeat
  1813. #+STARTUP: nologrepeat
  1814. You can have local logging settings for a subtree by setting the LOGGING
  1815. property to one or more of these keywords."
  1816. :group 'org-todo
  1817. :group 'org-progress
  1818. :type '(choice
  1819. (const :tag "Don't force a record" nil)
  1820. (const :tag "Force recording the DONE state" time)
  1821. (const :tag "Force recording a note with the DONE state" note)))
  1822. (defgroup org-priorities nil
  1823. "Priorities in Org-mode."
  1824. :tag "Org Priorities"
  1825. :group 'org-todo)
  1826. (defcustom org-enable-priority-commands t
  1827. "Non-nil means, priority commands are active.
  1828. When nil, these commands will be disabled, so that you never accidentally
  1829. set a priority."
  1830. :group 'org-priorities
  1831. :type 'boolean)
  1832. (defcustom org-highest-priority ?A
  1833. "The highest priority of TODO items. A character like ?A, ?B etc.
  1834. Must have a smaller ASCII number than `org-lowest-priority'."
  1835. :group 'org-priorities
  1836. :type 'character)
  1837. (defcustom org-lowest-priority ?C
  1838. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1839. Must have a larger ASCII number than `org-highest-priority'."
  1840. :group 'org-priorities
  1841. :type 'character)
  1842. (defcustom org-default-priority ?B
  1843. "The default priority of TODO items.
  1844. This is the priority an item get if no explicit priority is given."
  1845. :group 'org-priorities
  1846. :type 'character)
  1847. (defcustom org-priority-start-cycle-with-default t
  1848. "Non-nil means, start with default priority when starting to cycle.
  1849. When this is nil, the first step in the cycle will be (depending on the
  1850. command used) one higher or lower that the default priority."
  1851. :group 'org-priorities
  1852. :type 'boolean)
  1853. (defgroup org-time nil
  1854. "Options concerning time stamps and deadlines in Org-mode."
  1855. :tag "Org Time"
  1856. :group 'org)
  1857. (defcustom org-insert-labeled-timestamps-at-point nil
  1858. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1859. When nil, these labeled time stamps are forces into the second line of an
  1860. entry, just after the headline. When scheduling from the global TODO list,
  1861. the time stamp will always be forced into the second line."
  1862. :group 'org-time
  1863. :type 'boolean)
  1864. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1865. "Formats for `format-time-string' which are used for time stamps.
  1866. It is not recommended to change this constant.")
  1867. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1868. "Number of minutes to round time stamps to.
  1869. These are two values, the first applies when first creating a time stamp.
  1870. The second applies when changing it with the commands `S-up' and `S-down'.
  1871. When changing the time stamp, this means that it will change in steps
  1872. of N minutes, as given by the second value.
  1873. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1874. numbers should be factors of 60, so for example 5, 10, 15.
  1875. When this is larger than 1, you can still force an exact time-stamp by using
  1876. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1877. and by using a prefix arg to `S-up/down' to specify the exact number
  1878. of minutes to shift."
  1879. :group 'org-time
  1880. :get '(lambda (var) ; Make sure all entries have 5 elements
  1881. (if (integerp (default-value var))
  1882. (list (default-value var) 5)
  1883. (default-value var)))
  1884. :type '(list
  1885. (integer :tag "when inserting times")
  1886. (integer :tag "when modifying times")))
  1887. ;; Normalize old customizations of this variable.
  1888. (when (integerp org-time-stamp-rounding-minutes)
  1889. (setq org-time-stamp-rounding-minutes
  1890. (list org-time-stamp-rounding-minutes
  1891. org-time-stamp-rounding-minutes)))
  1892. (defcustom org-display-custom-times nil
  1893. "Non-nil means, overlay custom formats over all time stamps.
  1894. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1895. To turn this on on a per-file basis, insert anywhere in the file:
  1896. #+STARTUP: customtime"
  1897. :group 'org-time
  1898. :set 'set-default
  1899. :type 'sexp)
  1900. (make-variable-buffer-local 'org-display-custom-times)
  1901. (defcustom org-time-stamp-custom-formats
  1902. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1903. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1904. These are overlayed over the default ISO format if the variable
  1905. `org-display-custom-times' is set. Time like %H:%M should be at the
  1906. end of the second format. The custom formats are also honored by export
  1907. commands, if custom time display is turned on at the time of export."
  1908. :group 'org-time
  1909. :type 'sexp)
  1910. (defun org-time-stamp-format (&optional long inactive)
  1911. "Get the right format for a time string."
  1912. (let ((f (if long (cdr org-time-stamp-formats)
  1913. (car org-time-stamp-formats))))
  1914. (if inactive
  1915. (concat "[" (substring f 1 -1) "]")
  1916. f)))
  1917. (defcustom org-time-clocksum-format "%d:%02d"
  1918. "The format string used when creating CLOCKSUM lines, or when
  1919. org-mode generates a time duration."
  1920. :group 'org-time
  1921. :type 'string)
  1922. (defcustom org-time-clocksum-use-fractional nil
  1923. "If non-nil, \\[org-clock-display] uses fractional times.
  1924. org-mode generates a time duration."
  1925. :group 'org-time
  1926. :type 'boolean)
  1927. (defcustom org-time-clocksum-fractional-format "%.2f"
  1928. "The format string used when creating CLOCKSUM lines, or when
  1929. org-mode generates a time duration."
  1930. :group 'org-time
  1931. :type 'string)
  1932. (defcustom org-deadline-warning-days 14
  1933. "No. of days before expiration during which a deadline becomes active.
  1934. This variable governs the display in sparse trees and in the agenda.
  1935. When 0 or negative, it means use this number (the absolute value of it)
  1936. even if a deadline has a different individual lead time specified.
  1937. Custom commands can set this variable in the options section."
  1938. :group 'org-time
  1939. :group 'org-agenda-daily/weekly
  1940. :type 'integer)
  1941. (defcustom org-read-date-prefer-future t
  1942. "Non-nil means, assume future for incomplete date input from user.
  1943. This affects the following situations:
  1944. 1. The user gives a month but not a year.
  1945. For example, if it is april and you enter \"feb 2\", this will be read
  1946. as feb 2, *next* year. \"May 5\", however, will be this year.
  1947. 2. The user gives a day, but no month.
  1948. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1949. read this as the third of *next* month. However, if you enter \"17\",
  1950. it will be considered as *this* month.
  1951. If you set this variable to the symbol `time', then also the following
  1952. will work:
  1953. 3. If the user gives a time, but no day. If the time is before now,
  1954. to will be interpreted as tomorrow.
  1955. Currently none of this works for ISO week specifications.
  1956. When this option is nil, the current day, month and year will always be
  1957. used as defaults."
  1958. :group 'org-time
  1959. :type '(choice
  1960. (const :tag "Never" nil)
  1961. (const :tag "Check month and day" t)
  1962. (const :tag "Check month, day, and time" time)))
  1963. (defcustom org-read-date-display-live t
  1964. "Non-nil means, display current interpretation of date prompt live.
  1965. This display will be in an overlay, in the minibuffer."
  1966. :group 'org-time
  1967. :type 'boolean)
  1968. (defcustom org-read-date-popup-calendar t
  1969. "Non-nil means, pop up a calendar when prompting for a date.
  1970. In the calendar, the date can be selected with mouse-1. However, the
  1971. minibuffer will also be active, and you can simply enter the date as well.
  1972. When nil, only the minibuffer will be available."
  1973. :group 'org-time
  1974. :type 'boolean)
  1975. (if (fboundp 'defvaralias)
  1976. (defvaralias 'org-popup-calendar-for-date-prompt
  1977. 'org-read-date-popup-calendar))
  1978. (defcustom org-read-date-minibuffer-setup-hook nil
  1979. "Hook to be used to set up keys for the date/time interface.
  1980. Add key definitions to `minibuffer-local-map', which will be a temporary
  1981. copy."
  1982. :group 'org-time
  1983. :type 'hook)
  1984. (defcustom org-extend-today-until 0
  1985. "The hour when your day really ends. Must be an integer.
  1986. This has influence for the following applications:
  1987. - When switching the agenda to \"today\". It it is still earlier than
  1988. the time given here, the day recognized as TODAY is actually yesterday.
  1989. - When a date is read from the user and it is still before the time given
  1990. here, the current date and time will be assumed to be yesterday, 23:59.
  1991. Also, timestamps inserted in remember templates follow this rule.
  1992. IMPORTANT: This is a feature whose implementation is and likely will
  1993. remain incomplete. Really, it is only here because past midnight seems to
  1994. be the favorite working time of John Wiegley :-)"
  1995. :group 'org-time
  1996. :type 'integer)
  1997. (defcustom org-edit-timestamp-down-means-later nil
  1998. "Non-nil means, S-down will increase the time in a time stamp.
  1999. When nil, S-up will increase."
  2000. :group 'org-time
  2001. :type 'boolean)
  2002. (defcustom org-calendar-follow-timestamp-change t
  2003. "Non-nil means, make the calendar window follow timestamp changes.
  2004. When a timestamp is modified and the calendar window is visible, it will be
  2005. moved to the new date."
  2006. :group 'org-time
  2007. :type 'boolean)
  2008. (defgroup org-tags nil
  2009. "Options concerning tags in Org-mode."
  2010. :tag "Org Tags"
  2011. :group 'org)
  2012. (defcustom org-tag-alist nil
  2013. "List of tags allowed in Org-mode files.
  2014. When this list is nil, Org-mode will base TAG input on what is already in the
  2015. buffer.
  2016. The value of this variable is an alist, the car of each entry must be a
  2017. keyword as a string, the cdr may be a character that is used to select
  2018. that tag through the fast-tag-selection interface.
  2019. See the manual for details."
  2020. :group 'org-tags
  2021. :type '(repeat
  2022. (choice
  2023. (cons (string :tag "Tag name")
  2024. (character :tag "Access char"))
  2025. (list :tag "Start radio group"
  2026. (const :startgroup)
  2027. (option (string :tag "Group description")))
  2028. (list :tag "End radio group"
  2029. (const :endgroup)
  2030. (option (string :tag "Group description")))
  2031. (const :tag "New line" (:newline)))))
  2032. (defcustom org-tag-persistent-alist nil
  2033. "List of tags that will always appear in all Org-mode files.
  2034. This is in addition to any in buffer settings or customizations
  2035. of `org-tag-alist'.
  2036. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2037. The value of this variable is an alist, the car of each entry must be a
  2038. keyword as a string, the cdr may be a character that is used to select
  2039. that tag through the fast-tag-selection interface.
  2040. See the manual for details.
  2041. To disable these tags on a per-file basis, insert anywhere in the file:
  2042. #+STARTUP: noptag"
  2043. :group 'org-tags
  2044. :type '(repeat
  2045. (choice
  2046. (cons (string :tag "Tag name")
  2047. (character :tag "Access char"))
  2048. (const :tag "Start radio group" (:startgroup))
  2049. (const :tag "End radio group" (:endgroup))
  2050. (const :tag "New line" (:newline)))))
  2051. (defvar org-file-tags nil
  2052. "List of tags that can be inherited by all entries in the file.
  2053. The tags will be inherited if the variable `org-use-tag-inheritance'
  2054. says they should be.
  2055. This variable is populated from #+FILETAGS lines.")
  2056. (defcustom org-use-fast-tag-selection 'auto
  2057. "Non-nil means, use fast tag selection scheme.
  2058. This is a special interface to select and deselect tags with single keys.
  2059. When nil, fast selection is never used.
  2060. When the symbol `auto', fast selection is used if and only if selection
  2061. characters for tags have been configured, either through the variable
  2062. `org-tag-alist' or through a #+TAGS line in the buffer.
  2063. When t, fast selection is always used and selection keys are assigned
  2064. automatically if necessary."
  2065. :group 'org-tags
  2066. :type '(choice
  2067. (const :tag "Always" t)
  2068. (const :tag "Never" nil)
  2069. (const :tag "When selection characters are configured" 'auto)))
  2070. (defcustom org-fast-tag-selection-single-key nil
  2071. "Non-nil means, fast tag selection exits after first change.
  2072. When nil, you have to press RET to exit it.
  2073. During fast tag selection, you can toggle this flag with `C-c'.
  2074. This variable can also have the value `expert'. In this case, the window
  2075. displaying the tags menu is not even shown, until you press C-c again."
  2076. :group 'org-tags
  2077. :type '(choice
  2078. (const :tag "No" nil)
  2079. (const :tag "Yes" t)
  2080. (const :tag "Expert" expert)))
  2081. (defvar org-fast-tag-selection-include-todo nil
  2082. "Non-nil means, fast tags selection interface will also offer TODO states.
  2083. This is an undocumented feature, you should not rely on it.")
  2084. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2085. "The column to which tags should be indented in a headline.
  2086. If this number is positive, it specifies the column. If it is negative,
  2087. it means that the tags should be flushright to that column. For example,
  2088. -80 works well for a normal 80 character screen."
  2089. :group 'org-tags
  2090. :type 'integer)
  2091. (defcustom org-auto-align-tags t
  2092. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2093. These operations change the length of a headline and therefore shift
  2094. the tags around. With this options turned on, after each such operation
  2095. the tags are again aligned to `org-tags-column'."
  2096. :group 'org-tags
  2097. :type 'boolean)
  2098. (defcustom org-use-tag-inheritance t
  2099. "Non-nil means, tags in levels apply also for sublevels.
  2100. When nil, only the tags directly given in a specific line apply there.
  2101. This may also be a list of tags that should be inherited, or a regexp that
  2102. matches tags that should be inherited. Additional control is possible
  2103. with the variable `org-tags-exclude-from-inheritance' which gives an
  2104. explicit list of tags to be excluded from inheritance., even if the value of
  2105. `org-use-tag-inheritance' would select it for inheritance.
  2106. If this option is t, a match early-on in a tree can lead to a large
  2107. number of matches in the subtree when constructing the agenda or creating
  2108. a sparse tree. If you only want to see the first match in a tree during
  2109. a search, check out the variable `org-tags-match-list-sublevels'."
  2110. :group 'org-tags
  2111. :type '(choice
  2112. (const :tag "Not" nil)
  2113. (const :tag "Always" t)
  2114. (repeat :tag "Specific tags" (string :tag "Tag"))
  2115. (regexp :tag "Tags matched by regexp")))
  2116. (defcustom org-tags-exclude-from-inheritance nil
  2117. "List of tags that should never be inherited.
  2118. This is a way to exclude a few tags from inheritance. For way to do
  2119. the opposite, to actively allow inheritance for selected tags,
  2120. see the variable `org-use-tag-inheritance'."
  2121. :group 'org-tags
  2122. :type '(repeat (string :tag "Tag")))
  2123. (defun org-tag-inherit-p (tag)
  2124. "Check if TAG is one that should be inherited."
  2125. (cond
  2126. ((member tag org-tags-exclude-from-inheritance) nil)
  2127. ((eq org-use-tag-inheritance t) t)
  2128. ((not org-use-tag-inheritance) nil)
  2129. ((stringp org-use-tag-inheritance)
  2130. (string-match org-use-tag-inheritance tag))
  2131. ((listp org-use-tag-inheritance)
  2132. (member tag org-use-tag-inheritance))
  2133. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2134. (defcustom org-tags-match-list-sublevels t
  2135. "Non-nil means list also sublevels of headlines matching a search.
  2136. This variable applies to tags/property searches, and also to stuck
  2137. projects because this search is based on a tags match as well.
  2138. When set to the symbol `indented', sublevels are indented with
  2139. leading dots.
  2140. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2141. the sublevels of a headline matching a tag search often also match
  2142. the same search. Listing all of them can create very long lists.
  2143. Setting this variable to nil causes subtrees of a match to be skipped.
  2144. This variable is semi-obsolete and probably should always be true. It
  2145. is better to limit inheritance to certain tags using the variables
  2146. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2147. :group 'org-tags
  2148. :type '(choice
  2149. (const :tag "No, don't list them" nil)
  2150. (const :tag "Yes, do list them" t)
  2151. (const :tag "List them, indented with leading dots" indented)))
  2152. (defcustom org-tags-sort-function nil
  2153. "When set, tags are sorted using this function as a comparator"
  2154. :group 'org-tags
  2155. :type '(choice
  2156. (const :tag "No sorting" nil)
  2157. (const :tag "Alphabetical" string<)
  2158. (const :tag "Reverse alphabetical" string>)
  2159. (function :tag "Custom function" nil)))
  2160. (defvar org-tags-history nil
  2161. "History of minibuffer reads for tags.")
  2162. (defvar org-last-tags-completion-table nil
  2163. "The last used completion table for tags.")
  2164. (defvar org-after-tags-change-hook nil
  2165. "Hook that is run after the tags in a line have changed.")
  2166. (defgroup org-properties nil
  2167. "Options concerning properties in Org-mode."
  2168. :tag "Org Properties"
  2169. :group 'org)
  2170. (defcustom org-property-format "%-10s %s"
  2171. "How property key/value pairs should be formatted by `indent-line'.
  2172. When `indent-line' hits a property definition, it will format the line
  2173. according to this format, mainly to make sure that the values are
  2174. lined-up with respect to each other."
  2175. :group 'org-properties
  2176. :type 'string)
  2177. (defcustom org-use-property-inheritance nil
  2178. "Non-nil means, properties apply also for sublevels.
  2179. This setting is chiefly used during property searches. Turning it on can
  2180. cause significant overhead when doing a search, which is why it is not
  2181. on by default.
  2182. When nil, only the properties directly given in the current entry count.
  2183. When t, every property is inherited. The value may also be a list of
  2184. properties that should have inheritance, or a regular expression matching
  2185. properties that should be inherited.
  2186. However, note that some special properties use inheritance under special
  2187. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2188. and the properties ending in \"_ALL\" when they are used as descriptor
  2189. for valid values of a property.
  2190. Note for programmers:
  2191. When querying an entry with `org-entry-get', you can control if inheritance
  2192. should be used. By default, `org-entry-get' looks only at the local
  2193. properties. You can request inheritance by setting the inherit argument
  2194. to t (to force inheritance) or to `selective' (to respect the setting
  2195. in this variable)."
  2196. :group 'org-properties
  2197. :type '(choice
  2198. (const :tag "Not" nil)
  2199. (const :tag "Always" t)
  2200. (repeat :tag "Specific properties" (string :tag "Property"))
  2201. (regexp :tag "Properties matched by regexp")))
  2202. (defun org-property-inherit-p (property)
  2203. "Check if PROPERTY is one that should be inherited."
  2204. (cond
  2205. ((eq org-use-property-inheritance t) t)
  2206. ((not org-use-property-inheritance) nil)
  2207. ((stringp org-use-property-inheritance)
  2208. (string-match org-use-property-inheritance property))
  2209. ((listp org-use-property-inheritance)
  2210. (member property org-use-property-inheritance))
  2211. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2212. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2213. "The default column format, if no other format has been defined.
  2214. This variable can be set on the per-file basis by inserting a line
  2215. #+COLUMNS: %25ITEM ....."
  2216. :group 'org-properties
  2217. :type 'string)
  2218. (defcustom org-columns-ellipses ".."
  2219. "The ellipses to be used when a field in column view is truncated.
  2220. When this is the empty string, as many characters as possible are shown,
  2221. but then there will be no visual indication that the field has been truncated.
  2222. When this is a string of length N, the last N characters of a truncated
  2223. field are replaced by this string. If the column is narrower than the
  2224. ellipses string, only part of the ellipses string will be shown."
  2225. :group 'org-properties
  2226. :type 'string)
  2227. (defcustom org-columns-modify-value-for-display-function nil
  2228. "Function that modifies values for display in column view.
  2229. For example, it can be used to cut out a certain part from a time stamp.
  2230. The function must take 2 arguments:
  2231. column-title The title of the column (*not* the property name)
  2232. value The value that should be modified.
  2233. The function should return the value that should be displayed,
  2234. or nil if the normal value should be used."
  2235. :group 'org-properties
  2236. :type 'function)
  2237. (defcustom org-effort-property "Effort"
  2238. "The property that is being used to keep track of effort estimates.
  2239. Effort estimates given in this property need to have the format H:MM."
  2240. :group 'org-properties
  2241. :group 'org-progress
  2242. :type '(string :tag "Property"))
  2243. (defconst org-global-properties-fixed
  2244. '(("VISIBILITY_ALL" . "folded children content all")
  2245. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2246. "List of property/value pairs that can be inherited by any entry.
  2247. These are fixed values, for the preset properties. The user variable
  2248. that can be used to add to this list is `org-global-properties'.
  2249. The entries in this list are cons cells where the car is a property
  2250. name and cdr is a string with the value. If the value represents
  2251. multiple items like an \"_ALL\" property, separate the items by
  2252. spaces.")
  2253. (defcustom org-global-properties nil
  2254. "List of property/value pairs that can be inherited by any entry.
  2255. This list will be combined with the constant `org-global-properties-fixed'.
  2256. The entries in this list are cons cells where the car is a property
  2257. name and cdr is a string with the value.
  2258. You can set buffer-local values for the same purpose in the variable
  2259. `org-file-properties' this by adding lines like
  2260. #+PROPERTY: NAME VALUE"
  2261. :group 'org-properties
  2262. :type '(repeat
  2263. (cons (string :tag "Property")
  2264. (string :tag "Value"))))
  2265. (defvar org-file-properties nil
  2266. "List of property/value pairs that can be inherited by any entry.
  2267. Valid for the current buffer.
  2268. This variable is populated from #+PROPERTY lines.")
  2269. (make-variable-buffer-local 'org-file-properties)
  2270. (defgroup org-agenda nil
  2271. "Options concerning agenda views in Org-mode."
  2272. :tag "Org Agenda"
  2273. :group 'org)
  2274. (defvar org-category nil
  2275. "Variable used by org files to set a category for agenda display.
  2276. Such files should use a file variable to set it, for example
  2277. # -*- mode: org; org-category: \"ELisp\"
  2278. or contain a special line
  2279. #+CATEGORY: ELisp
  2280. If the file does not specify a category, then file's base name
  2281. is used instead.")
  2282. (make-variable-buffer-local 'org-category)
  2283. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2284. (defcustom org-agenda-files nil
  2285. "The files to be used for agenda display.
  2286. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2287. \\[org-remove-file]. You can also use customize to edit the list.
  2288. If an entry is a directory, all files in that directory that are matched by
  2289. `org-agenda-file-regexp' will be part of the file list.
  2290. If the value of the variable is not a list but a single file name, then
  2291. the list of agenda files is actually stored and maintained in that file, one
  2292. agenda file per line."
  2293. :group 'org-agenda
  2294. :type '(choice
  2295. (repeat :tag "List of files and directories" file)
  2296. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2297. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2298. "Regular expression to match files for `org-agenda-files'.
  2299. If any element in the list in that variable contains a directory instead
  2300. of a normal file, all files in that directory that are matched by this
  2301. regular expression will be included."
  2302. :group 'org-agenda
  2303. :type 'regexp)
  2304. (defcustom org-agenda-text-search-extra-files nil
  2305. "List of extra files to be searched by text search commands.
  2306. These files will be search in addition to the agenda files by the
  2307. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2308. Note that these files will only be searched for text search commands,
  2309. not for the other agenda views like todo lists, tag searches or the weekly
  2310. agenda. This variable is intended to list notes and possibly archive files
  2311. that should also be searched by these two commands.
  2312. In fact, if the first element in the list is the symbol `agenda-archives',
  2313. than all archive files of all agenda files will be added to the search
  2314. scope."
  2315. :group 'org-agenda
  2316. :type '(set :greedy t
  2317. (const :tag "Agenda Archives" agenda-archives)
  2318. (repeat :inline t (file))))
  2319. (if (fboundp 'defvaralias)
  2320. (defvaralias 'org-agenda-multi-occur-extra-files
  2321. 'org-agenda-text-search-extra-files))
  2322. (defcustom org-agenda-skip-unavailable-files nil
  2323. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2324. A nil value means to remove them, after a query, from the list."
  2325. :group 'org-agenda
  2326. :type 'boolean)
  2327. (defcustom org-calendar-to-agenda-key [?c]
  2328. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2329. The command `org-calendar-goto-agenda' will be bound to this key. The
  2330. default is the character `c' because then `c' can be used to switch back and
  2331. forth between agenda and calendar."
  2332. :group 'org-agenda
  2333. :type 'sexp)
  2334. (defcustom org-calendar-agenda-action-key [?k]
  2335. "The key to be installed in `calendar-mode-map' for agenda-action.
  2336. The command `org-agenda-action' will be bound to this key. The
  2337. default is the character `k' because we use the same key in the agenda."
  2338. :group 'org-agenda
  2339. :type 'sexp)
  2340. (eval-after-load "calendar"
  2341. '(progn
  2342. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2343. 'org-calendar-goto-agenda)
  2344. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2345. 'org-agenda-action)))
  2346. (defgroup org-latex nil
  2347. "Options for embedding LaTeX code into Org-mode."
  2348. :tag "Org LaTeX"
  2349. :group 'org)
  2350. (defcustom org-format-latex-options
  2351. '(:foreground default :background default :scale 1.0
  2352. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2353. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2354. "Options for creating images from LaTeX fragments.
  2355. This is a property list with the following properties:
  2356. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2357. `default' means use the foreground of the default face.
  2358. :background the background color, or \"Transparent\".
  2359. `default' means use the background of the default face.
  2360. :scale a scaling factor for the size of the images.
  2361. :html-foreground, :html-background, :html-scale
  2362. the same numbers for HTML export.
  2363. :matchers a list indicating which matchers should be used to
  2364. find LaTeX fragments. Valid members of this list are:
  2365. \"begin\" find environments
  2366. \"$1\" find single characters surrounded by $.$
  2367. \"$\" find math expressions surrounded by $...$
  2368. \"$$\" find math expressions surrounded by $$....$$
  2369. \"\\(\" find math expressions surrounded by \\(...\\)
  2370. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2371. :group 'org-latex
  2372. :type 'plist)
  2373. (defcustom org-format-latex-header "\\documentclass{article}
  2374. \\usepackage{amssymb}
  2375. \\usepackage[usenames]{color}
  2376. \\usepackage{amsmath}
  2377. \\usepackage{latexsym}
  2378. \\usepackage[mathscr]{eucal}
  2379. \\pagestyle{empty} % do not remove
  2380. % The settings below are copied from fullpage.sty
  2381. \\setlength{\\textwidth}{\\paperwidth}
  2382. \\addtolength{\\textwidth}{-3cm}
  2383. \\setlength{\\oddsidemargin}{1.5cm}
  2384. \\addtolength{\\oddsidemargin}{-2.54cm}
  2385. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2386. \\setlength{\\textheight}{\\paperheight}
  2387. \\addtolength{\\textheight}{-\\headheight}
  2388. \\addtolength{\\textheight}{-\\headsep}
  2389. \\addtolength{\\textheight}{-\\footskip}
  2390. \\addtolength{\\textheight}{-3cm}
  2391. \\setlength{\\topmargin}{1.5cm}
  2392. \\addtolength{\\topmargin}{-2.54cm}"
  2393. "The document header used for processing LaTeX fragments.
  2394. It is imperative that this header make sure that no page number
  2395. appears on the page."
  2396. :group 'org-latex
  2397. :type 'string)
  2398. (defgroup org-font-lock nil
  2399. "Font-lock settings for highlighting in Org-mode."
  2400. :tag "Org Font Lock"
  2401. :group 'org)
  2402. (defcustom org-level-color-stars-only nil
  2403. "Non-nil means fontify only the stars in each headline.
  2404. When nil, the entire headline is fontified.
  2405. Changing it requires restart of `font-lock-mode' to become effective
  2406. also in regions already fontified."
  2407. :group 'org-font-lock
  2408. :type 'boolean)
  2409. (defcustom org-hide-leading-stars nil
  2410. "Non-nil means, hide the first N-1 stars in a headline.
  2411. This works by using the face `org-hide' for these stars. This
  2412. face is white for a light background, and black for a dark
  2413. background. You may have to customize the face `org-hide' to
  2414. make this work.
  2415. Changing it requires restart of `font-lock-mode' to become effective
  2416. also in regions already fontified.
  2417. You may also set this on a per-file basis by adding one of the following
  2418. lines to the buffer:
  2419. #+STARTUP: hidestars
  2420. #+STARTUP: showstars"
  2421. :group 'org-font-lock
  2422. :type 'boolean)
  2423. (defcustom org-fontify-done-headline nil
  2424. "Non-nil means, change the face of a headline if it is marked DONE.
  2425. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2426. When this is non-nil, the headline after the keyword is set to the
  2427. `org-headline-done' as an additional indication."
  2428. :group 'org-font-lock
  2429. :type 'boolean)
  2430. (defcustom org-fontify-emphasized-text t
  2431. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2432. Changing this variable requires a restart of Emacs to take effect."
  2433. :group 'org-font-lock
  2434. :type 'boolean)
  2435. (defcustom org-fontify-whole-heading-line nil
  2436. "Non-nil means fontify the whole line for headings.
  2437. This is useful when setting a background color for the
  2438. org-leve-* faces."
  2439. :group 'org-font-lock
  2440. :type 'boolean)
  2441. (defcustom org-highlight-latex-fragments-and-specials nil
  2442. "Non-nil means, fontify what is treated specially by the exporters."
  2443. :group 'org-font-lock
  2444. :type 'boolean)
  2445. (defcustom org-hide-emphasis-markers nil
  2446. "Non-nil mean font-lock should hide the emphasis marker characters."
  2447. :group 'org-font-lock
  2448. :type 'boolean)
  2449. (defvar org-emph-re nil
  2450. "Regular expression for matching emphasis.")
  2451. (defvar org-verbatim-re nil
  2452. "Regular expression for matching verbatim text.")
  2453. (defvar org-emphasis-regexp-components) ; defined just below
  2454. (defvar org-emphasis-alist) ; defined just below
  2455. (defun org-set-emph-re (var val)
  2456. "Set variable and compute the emphasis regular expression."
  2457. (set var val)
  2458. (when (and (boundp 'org-emphasis-alist)
  2459. (boundp 'org-emphasis-regexp-components)
  2460. org-emphasis-alist org-emphasis-regexp-components)
  2461. (let* ((e org-emphasis-regexp-components)
  2462. (pre (car e))
  2463. (post (nth 1 e))
  2464. (border (nth 2 e))
  2465. (body (nth 3 e))
  2466. (nl (nth 4 e))
  2467. (body1 (concat body "*?"))
  2468. (markers (mapconcat 'car org-emphasis-alist ""))
  2469. (vmarkers (mapconcat
  2470. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2471. org-emphasis-alist "")))
  2472. ;; make sure special characters appear at the right position in the class
  2473. (if (string-match "\\^" markers)
  2474. (setq markers (concat (replace-match "" t t markers) "^")))
  2475. (if (string-match "-" markers)
  2476. (setq markers (concat (replace-match "" t t markers) "-")))
  2477. (if (string-match "\\^" vmarkers)
  2478. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2479. (if (string-match "-" vmarkers)
  2480. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2481. (if (> nl 0)
  2482. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2483. (int-to-string nl) "\\}")))
  2484. ;; Make the regexp
  2485. (setq org-emph-re
  2486. (concat "\\([" pre "]\\|^\\)"
  2487. "\\("
  2488. "\\([" markers "]\\)"
  2489. "\\("
  2490. "[^" border "]\\|"
  2491. "[^" border "]"
  2492. body1
  2493. "[^" border "]"
  2494. "\\)"
  2495. "\\3\\)"
  2496. "\\([" post "]\\|$\\)"))
  2497. (setq org-verbatim-re
  2498. (concat "\\([" pre "]\\|^\\)"
  2499. "\\("
  2500. "\\([" vmarkers "]\\)"
  2501. "\\("
  2502. "[^" border "]\\|"
  2503. "[^" border "]"
  2504. body1
  2505. "[^" border "]"
  2506. "\\)"
  2507. "\\3\\)"
  2508. "\\([" post "]\\|$\\)")))))
  2509. (defcustom org-emphasis-regexp-components
  2510. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2511. "Components used to build the regular expression for emphasis.
  2512. This is a list with 6 entries. Terminology: In an emphasis string
  2513. like \" *strong word* \", we call the initial space PREMATCH, the final
  2514. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2515. and \"trong wor\" is the body. The different components in this variable
  2516. specify what is allowed/forbidden in each part:
  2517. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2518. post Chars allowed as postmatch. End of line will be allowed too.
  2519. border The chars *forbidden* as border characters.
  2520. body-regexp A regexp like \".\" to match a body character. Don't use
  2521. non-shy groups here, and don't allow newline here.
  2522. newline The maximum number of newlines allowed in an emphasis exp.
  2523. Use customize to modify this, or restart Emacs after changing it."
  2524. :group 'org-font-lock
  2525. :set 'org-set-emph-re
  2526. :type '(list
  2527. (sexp :tag "Allowed chars in pre ")
  2528. (sexp :tag "Allowed chars in post ")
  2529. (sexp :tag "Forbidden chars in border ")
  2530. (sexp :tag "Regexp for body ")
  2531. (integer :tag "number of newlines allowed")
  2532. (option (boolean :tag "Please ignore this button"))))
  2533. (defcustom org-emphasis-alist
  2534. `(("*" bold "<b>" "</b>")
  2535. ("/" italic "<i>" "</i>")
  2536. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2537. ("=" org-code "<code>" "</code>" verbatim)
  2538. ("~" org-verbatim "<code>" "</code>" verbatim)
  2539. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2540. "<del>" "</del>")
  2541. )
  2542. "Special syntax for emphasized text.
  2543. Text starting and ending with a special character will be emphasized, for
  2544. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2545. characters, the face to be used by font-lock for highlighting in Org-mode
  2546. Emacs buffers, and the HTML tags to be used for this.
  2547. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2548. Use customize to modify this, or restart Emacs after changing it."
  2549. :group 'org-font-lock
  2550. :set 'org-set-emph-re
  2551. :type '(repeat
  2552. (list
  2553. (string :tag "Marker character")
  2554. (choice
  2555. (face :tag "Font-lock-face")
  2556. (plist :tag "Face property list"))
  2557. (string :tag "HTML start tag")
  2558. (string :tag "HTML end tag")
  2559. (option (const verbatim)))))
  2560. (defvar org-protecting-blocks
  2561. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2562. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2563. This is needed for font-lock setup.")
  2564. ;;; Miscellaneous options
  2565. (defgroup org-completion nil
  2566. "Completion in Org-mode."
  2567. :tag "Org Completion"
  2568. :group 'org)
  2569. (defcustom org-completion-use-ido nil
  2570. "Non-nil means, use ido completion wherever possible.
  2571. Note that `ido-mode' must be active for this variable to be relevant.
  2572. If you decide to turn this variable on, you might well want to turn off
  2573. `org-outline-path-complete-in-steps'.
  2574. See also `org-completion-use-iswitchb'."
  2575. :group 'org-completion
  2576. :type 'boolean)
  2577. (defcustom org-completion-use-iswitchb nil
  2578. "Non-nil means, use iswitchb completion wherever possible.
  2579. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2580. If you decide to turn this variable on, you might well want to turn off
  2581. `org-outline-path-complete-in-steps'.
  2582. Note that thi variable has only an effect if `org-completion-use-ido' is nil."
  2583. :group 'org-completion
  2584. :type 'boolean)
  2585. (defcustom org-completion-fallback-command 'hippie-expand
  2586. "The expansion command called by \\[org-complete] in normal context.
  2587. Normal means, no org-mode-specific context."
  2588. :group 'org-completion
  2589. :type 'function)
  2590. ;;; Functions and variables from ther packages
  2591. ;; Declared here to avoid compiler warnings
  2592. ;; XEmacs only
  2593. (defvar outline-mode-menu-heading)
  2594. (defvar outline-mode-menu-show)
  2595. (defvar outline-mode-menu-hide)
  2596. (defvar zmacs-regions) ; XEmacs regions
  2597. ;; Emacs only
  2598. (defvar mark-active)
  2599. ;; Various packages
  2600. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2601. (declare-function calendar-forward-day "cal-move" (arg))
  2602. (declare-function calendar-goto-date "cal-move" (date))
  2603. (declare-function calendar-goto-today "cal-move" ())
  2604. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2605. (defvar calc-embedded-close-formula)
  2606. (defvar calc-embedded-open-formula)
  2607. (declare-function cdlatex-tab "ext:cdlatex" ())
  2608. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2609. (defvar font-lock-unfontify-region-function)
  2610. (declare-function iswitchb-read-buffer "iswitchb"
  2611. (prompt &optional default require-match start matches-set))
  2612. (defvar iswitchb-temp-buflist)
  2613. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2614. (defvar org-agenda-tags-todo-honor-ignore-options)
  2615. (declare-function org-agenda-skip "org-agenda" ())
  2616. (declare-function org-format-agenda-item "org-agenda"
  2617. (extra txt &optional category tags dotime noprefix remove-re))
  2618. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2619. (declare-function org-agenda-change-all-lines "org-agenda"
  2620. (newhead hdmarker &optional fixface just-this))
  2621. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2622. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2623. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2624. (beg end))
  2625. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2626. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2627. "org-agenda" (&optional end))
  2628. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2629. (declare-function org-indent-mode "org-indent" (&optional arg))
  2630. (declare-function parse-time-string "parse-time" (string))
  2631. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2632. (defvar remember-data-file)
  2633. (defvar texmathp-why)
  2634. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2635. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2636. (defvar w3m-current-url)
  2637. (defvar w3m-current-title)
  2638. (defvar org-latex-regexps)
  2639. ;;; Autoload and prepare some org modules
  2640. ;; Some table stuff that needs to be defined here, because it is used
  2641. ;; by the functions setting up org-mode or checking for table context.
  2642. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2643. "Detects an org-type or table-type table.")
  2644. (defconst org-table-line-regexp "^[ \t]*|"
  2645. "Detects an org-type table line.")
  2646. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2647. "Detects an org-type table line.")
  2648. (defconst org-table-hline-regexp "^[ \t]*|-"
  2649. "Detects an org-type table hline.")
  2650. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2651. "Detects a table-type table hline.")
  2652. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2653. "Searching from within a table (any type) this finds the first line
  2654. outside the table.")
  2655. ;; Autoload the functions in org-table.el that are needed by functions here.
  2656. (eval-and-compile
  2657. (org-autoload "org-table"
  2658. '(org-table-align org-table-begin org-table-blank-field
  2659. org-table-convert org-table-convert-region org-table-copy-down
  2660. org-table-copy-region org-table-create
  2661. org-table-create-or-convert-from-region
  2662. org-table-create-with-table.el org-table-current-dline
  2663. org-table-cut-region org-table-delete-column org-table-edit-field
  2664. org-table-edit-formulas org-table-end org-table-eval-formula
  2665. org-table-export org-table-field-info
  2666. org-table-get-stored-formulas org-table-goto-column
  2667. org-table-hline-and-move org-table-import org-table-insert-column
  2668. org-table-insert-hline org-table-insert-row org-table-iterate
  2669. org-table-justify-field-maybe org-table-kill-row
  2670. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2671. org-table-move-column org-table-move-column-left
  2672. org-table-move-column-right org-table-move-row
  2673. org-table-move-row-down org-table-move-row-up
  2674. org-table-next-field org-table-next-row org-table-paste-rectangle
  2675. org-table-previous-field org-table-recalculate
  2676. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2677. org-table-toggle-coordinate-overlays
  2678. org-table-toggle-formula-debugger org-table-wrap-region
  2679. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2680. (defun org-at-table-p (&optional table-type)
  2681. "Return t if the cursor is inside an org-type table.
  2682. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2683. (if org-enable-table-editor
  2684. (save-excursion
  2685. (beginning-of-line 1)
  2686. (looking-at (if table-type org-table-any-line-regexp
  2687. org-table-line-regexp)))
  2688. nil))
  2689. (defsubst org-table-p () (org-at-table-p))
  2690. (defun org-at-table.el-p ()
  2691. "Return t if and only if we are at a table.el table."
  2692. (and (org-at-table-p 'any)
  2693. (save-excursion
  2694. (goto-char (org-table-begin 'any))
  2695. (looking-at org-table1-hline-regexp))))
  2696. (defun org-table-recognize-table.el ()
  2697. "If there is a table.el table nearby, recognize it and move into it."
  2698. (if org-table-tab-recognizes-table.el
  2699. (if (org-at-table.el-p)
  2700. (progn
  2701. (beginning-of-line 1)
  2702. (if (looking-at org-table-dataline-regexp)
  2703. nil
  2704. (if (looking-at org-table1-hline-regexp)
  2705. (progn
  2706. (beginning-of-line 2)
  2707. (if (looking-at org-table-any-border-regexp)
  2708. (beginning-of-line -1)))))
  2709. (if (re-search-forward "|" (org-table-end t) t)
  2710. (progn
  2711. (require 'table)
  2712. (if (table--at-cell-p (point))
  2713. t
  2714. (message "recognizing table.el table...")
  2715. (table-recognize-table)
  2716. (message "recognizing table.el table...done")))
  2717. (error "This should not happen..."))
  2718. t)
  2719. nil)
  2720. nil))
  2721. (defun org-at-table-hline-p ()
  2722. "Return t if the cursor is inside a hline in a table."
  2723. (if org-enable-table-editor
  2724. (save-excursion
  2725. (beginning-of-line 1)
  2726. (looking-at org-table-hline-regexp))
  2727. nil))
  2728. (defvar org-table-clean-did-remove-column nil)
  2729. (defun org-table-map-tables (function)
  2730. "Apply FUNCTION to the start of all tables in the buffer."
  2731. (save-excursion
  2732. (save-restriction
  2733. (widen)
  2734. (goto-char (point-min))
  2735. (while (re-search-forward org-table-any-line-regexp nil t)
  2736. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2737. (beginning-of-line 1)
  2738. (when (looking-at org-table-line-regexp)
  2739. (save-excursion (funcall function))
  2740. (or (looking-at org-table-line-regexp)
  2741. (forward-char 1)))
  2742. (re-search-forward org-table-any-border-regexp nil 1))))
  2743. (message "Mapping tables: done"))
  2744. ;; Declare and autoload functions from org-exp.el & Co
  2745. (declare-function org-default-export-plist "org-exp")
  2746. (declare-function org-infile-export-plist "org-exp")
  2747. (declare-function org-get-current-options "org-exp")
  2748. (eval-and-compile
  2749. (org-autoload "org-exp"
  2750. '(org-export org-export-visible
  2751. org-insert-export-options-template
  2752. org-table-clean-before-export))
  2753. (org-autoload "org-ascii"
  2754. '(org-export-as-ascii org-export-ascii-preprocess
  2755. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2756. org-export-region-as-ascii))
  2757. (org-autoload "org-html"
  2758. '(org-export-as-html-and-open
  2759. org-export-as-html-batch org-export-as-html-to-buffer
  2760. org-replace-region-by-html org-export-region-as-html
  2761. org-export-as-html))
  2762. (org-autoload "org-icalendar"
  2763. '(org-export-icalendar-this-file
  2764. org-export-icalendar-all-agenda-files
  2765. org-export-icalendar-combine-agenda-files))
  2766. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2767. ;; Declare and autoload functions from org-agenda.el
  2768. (eval-and-compile
  2769. (org-autoload "org-agenda"
  2770. '(org-agenda org-agenda-list org-search-view
  2771. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2772. org-diary org-agenda-to-appt
  2773. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2774. ;; Autoload org-remember
  2775. (eval-and-compile
  2776. (org-autoload "org-remember"
  2777. '(org-remember-insinuate org-remember-annotation
  2778. org-remember-apply-template org-remember org-remember-handler)))
  2779. ;; Autoload org-clock.el
  2780. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2781. (beg end))
  2782. (declare-function org-clock-update-mode-line "org-clock" ())
  2783. (declare-function org-resolve-clocks "org-clock"
  2784. (&optional also-non-dangling-p prompt last-valid))
  2785. (defvar org-clock-start-time)
  2786. (defvar org-clock-marker (make-marker)
  2787. "Marker recording the last clock-in.")
  2788. (defvar org-clock-hd-marker (make-marker)
  2789. "Marker recording the last clock-in, but the headline position.")
  2790. (defun org-clock-is-active ()
  2791. "Return non-nil if clock is currently running.
  2792. The return value is actually the clock marker."
  2793. (marker-buffer org-clock-marker))
  2794. (eval-and-compile
  2795. (org-autoload
  2796. "org-clock"
  2797. '(org-clock-in org-clock-out org-clock-cancel
  2798. org-clock-goto org-clock-sum org-clock-display
  2799. org-clock-remove-overlays org-clock-report
  2800. org-clocktable-shift org-dblock-write:clocktable
  2801. org-get-clocktable org-resolve-clocks)))
  2802. (defun org-clock-update-time-maybe ()
  2803. "If this is a CLOCK line, update it and return t.
  2804. Otherwise, return nil."
  2805. (interactive)
  2806. (save-excursion
  2807. (beginning-of-line 1)
  2808. (skip-chars-forward " \t")
  2809. (when (looking-at org-clock-string)
  2810. (let ((re (concat "[ \t]*" org-clock-string
  2811. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2812. "\\([ \t]*=>.*\\)?\\)?"))
  2813. ts te h m s neg)
  2814. (cond
  2815. ((not (looking-at re))
  2816. nil)
  2817. ((not (match-end 2))
  2818. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2819. (> org-clock-marker (point))
  2820. (<= org-clock-marker (point-at-eol)))
  2821. ;; The clock is running here
  2822. (setq org-clock-start-time
  2823. (apply 'encode-time
  2824. (org-parse-time-string (match-string 1))))
  2825. (org-clock-update-mode-line)))
  2826. (t
  2827. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2828. (end-of-line 1)
  2829. (setq ts (match-string 1)
  2830. te (match-string 3))
  2831. (setq s (- (org-float-time
  2832. (apply 'encode-time (org-parse-time-string te)))
  2833. (org-float-time
  2834. (apply 'encode-time (org-parse-time-string ts))))
  2835. neg (< s 0)
  2836. s (abs s)
  2837. h (floor (/ s 3600))
  2838. s (- s (* 3600 h))
  2839. m (floor (/ s 60))
  2840. s (- s (* 60 s)))
  2841. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2842. t))))))
  2843. (defun org-check-running-clock ()
  2844. "Check if the current buffer contains the running clock.
  2845. If yes, offer to stop it and to save the buffer with the changes."
  2846. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2847. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2848. (buffer-name))))
  2849. (org-clock-out)
  2850. (when (y-or-n-p "Save changed buffer?")
  2851. (save-buffer))))
  2852. (defun org-clocktable-try-shift (dir n)
  2853. "Check if this line starts a clock table, if yes, shift the time block."
  2854. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2855. (org-clocktable-shift dir n)))
  2856. ;; Autoload org-timer.el
  2857. (eval-and-compile
  2858. (org-autoload
  2859. "org-timer"
  2860. '(org-timer-start org-timer org-timer-item
  2861. org-timer-change-times-in-region
  2862. org-timer-set-timer
  2863. org-timer-reset-timers
  2864. org-timer-show-remaining-time)))
  2865. ;; Autoload org-feed.el
  2866. (eval-and-compile
  2867. (org-autoload
  2868. "org-feed"
  2869. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2870. ;; Autoload org-indent.el
  2871. (eval-and-compile
  2872. (org-autoload
  2873. "org-indent"
  2874. '(org-indent-mode)))
  2875. ;; Autoload org-mobile.el
  2876. (eval-and-compile
  2877. (org-autoload
  2878. "org-mobile"
  2879. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  2880. ;; Autoload archiving code
  2881. ;; The stuff that is needed for cycling and tags has to be defined here.
  2882. (defgroup org-archive nil
  2883. "Options concerning archiving in Org-mode."
  2884. :tag "Org Archive"
  2885. :group 'org-structure)
  2886. (defcustom org-archive-location "%s_archive::"
  2887. "The location where subtrees should be archived.
  2888. The value of this variable is a string, consisting of two parts,
  2889. separated by a double-colon. The first part is a filename and
  2890. the second part is a headline.
  2891. When the filename is omitted, archiving happens in the same file.
  2892. %s in the filename will be replaced by the current file
  2893. name (without the directory part). Archiving to a different file
  2894. is useful to keep archived entries from contributing to the
  2895. Org-mode Agenda.
  2896. The archived entries will be filed as subtrees of the specified
  2897. headline. When the headline is omitted, the subtrees are simply
  2898. filed away at the end of the file, as top-level entries. Also in
  2899. the heading you can use %s to represent the file name, this can be
  2900. useful when using the same archive for a number of different files.
  2901. Here are a few examples:
  2902. \"%s_archive::\"
  2903. If the current file is Projects.org, archive in file
  2904. Projects.org_archive, as top-level trees. This is the default.
  2905. \"::* Archived Tasks\"
  2906. Archive in the current file, under the top-level headline
  2907. \"* Archived Tasks\".
  2908. \"~/org/archive.org::\"
  2909. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2910. \"~/org/archive.org::From %s\"
  2911. Archive in file ~/org/archive.org (absolute path), und headlines
  2912. \"From FILENAME\" where file name is the current file name.
  2913. \"basement::** Finished Tasks\"
  2914. Archive in file ./basement (relative path), as level 3 trees
  2915. below the level 2 heading \"** Finished Tasks\".
  2916. You may set this option on a per-file basis by adding to the buffer a
  2917. line like
  2918. #+ARCHIVE: basement::** Finished Tasks
  2919. You may also define it locally for a subtree by setting an ARCHIVE property
  2920. in the entry. If such a property is found in an entry, or anywhere up
  2921. the hierarchy, it will be used."
  2922. :group 'org-archive
  2923. :type 'string)
  2924. (defcustom org-archive-tag "ARCHIVE"
  2925. "The tag that marks a subtree as archived.
  2926. An archived subtree does not open during visibility cycling, and does
  2927. not contribute to the agenda listings.
  2928. After changing this, font-lock must be restarted in the relevant buffers to
  2929. get the proper fontification."
  2930. :group 'org-archive
  2931. :group 'org-keywords
  2932. :type 'string)
  2933. (defcustom org-agenda-skip-archived-trees t
  2934. "Non-nil means, the agenda will skip any items located in archived trees.
  2935. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2936. variable is no longer recommended, you should leave it at the value t.
  2937. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2938. :group 'org-archive
  2939. :group 'org-agenda-skip
  2940. :type 'boolean)
  2941. (defcustom org-columns-skip-arrchived-trees t
  2942. "Non-nil means, irgnore archived trees when creating column view."
  2943. :group 'org-archive
  2944. :group 'org-properties
  2945. :type 'boolean)
  2946. (defcustom org-cycle-open-archived-trees nil
  2947. "Non-nil means, `org-cycle' will open archived trees.
  2948. An archived tree is a tree marked with the tag ARCHIVE.
  2949. When nil, archived trees will stay folded. You can still open them with
  2950. normal outline commands like `show-all', but not with the cycling commands."
  2951. :group 'org-archive
  2952. :group 'org-cycle
  2953. :type 'boolean)
  2954. (defcustom org-sparse-tree-open-archived-trees nil
  2955. "Non-nil means sparse tree construction shows matches in archived trees.
  2956. When nil, matches in these trees are highlighted, but the trees are kept in
  2957. collapsed state."
  2958. :group 'org-archive
  2959. :group 'org-sparse-trees
  2960. :type 'boolean)
  2961. (defun org-cycle-hide-archived-subtrees (state)
  2962. "Re-hide all archived subtrees after a visibility state change."
  2963. (when (and (not org-cycle-open-archived-trees)
  2964. (not (memq state '(overview folded))))
  2965. (save-excursion
  2966. (let* ((globalp (memq state '(contents all)))
  2967. (beg (if globalp (point-min) (point)))
  2968. (end (if globalp (point-max) (org-end-of-subtree t))))
  2969. (org-hide-archived-subtrees beg end)
  2970. (goto-char beg)
  2971. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2972. (message "%s" (substitute-command-keys
  2973. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2974. (defun org-force-cycle-archived ()
  2975. "Cycle subtree even if it is archived."
  2976. (interactive)
  2977. (setq this-command 'org-cycle)
  2978. (let ((org-cycle-open-archived-trees t))
  2979. (call-interactively 'org-cycle)))
  2980. (defun org-hide-archived-subtrees (beg end)
  2981. "Re-hide all archived subtrees after a visibility state change."
  2982. (save-excursion
  2983. (let* ((re (concat ":" org-archive-tag ":")))
  2984. (goto-char beg)
  2985. (while (re-search-forward re end t)
  2986. (and (org-on-heading-p) (hide-subtree))
  2987. (org-end-of-subtree t)))))
  2988. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2989. (eval-and-compile
  2990. (org-autoload "org-archive"
  2991. '(org-add-archive-files org-archive-subtree
  2992. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2993. ;; Autoload Column View Code
  2994. (declare-function org-columns-number-to-string "org-colview")
  2995. (declare-function org-columns-get-format-and-top-level "org-colview")
  2996. (declare-function org-columns-compute "org-colview")
  2997. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2998. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2999. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3000. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3001. ;; Autoload ID code
  3002. (declare-function org-id-store-link "org-id")
  3003. (declare-function org-id-locations-load "org-id")
  3004. (declare-function org-id-locations-save "org-id")
  3005. (defvar org-id-track-globally)
  3006. (org-autoload "org-id"
  3007. '(org-id-get-create org-id-new org-id-copy org-id-get
  3008. org-id-get-with-outline-path-completion
  3009. org-id-get-with-outline-drilling
  3010. org-id-goto org-id-find org-id-store-link))
  3011. ;; Autoload Plotting Code
  3012. (org-autoload "org-plot"
  3013. '(org-plot/gnuplot))
  3014. ;;; Variables for pre-computed regular expressions, all buffer local
  3015. (defvar org-drawer-regexp nil
  3016. "Matches first line of a hidden block.")
  3017. (make-variable-buffer-local 'org-drawer-regexp)
  3018. (defvar org-todo-regexp nil
  3019. "Matches any of the TODO state keywords.")
  3020. (make-variable-buffer-local 'org-todo-regexp)
  3021. (defvar org-not-done-regexp nil
  3022. "Matches any of the TODO state keywords except the last one.")
  3023. (make-variable-buffer-local 'org-not-done-regexp)
  3024. (defvar org-not-done-heading-regexp nil
  3025. "Matches a TODO headline that is not done.")
  3026. (make-variable-buffer-local 'org-not-done-regexp)
  3027. (defvar org-todo-line-regexp nil
  3028. "Matches a headline and puts TODO state into group 2 if present.")
  3029. (make-variable-buffer-local 'org-todo-line-regexp)
  3030. (defvar org-complex-heading-regexp nil
  3031. "Matches a headline and puts everything into groups:
  3032. group 1: the stars
  3033. group 2: The todo keyword, maybe
  3034. group 3: Priority cookie
  3035. group 4: True headline
  3036. group 5: Tags")
  3037. (make-variable-buffer-local 'org-complex-heading-regexp)
  3038. (defvar org-complex-heading-regexp-format nil)
  3039. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3040. (defvar org-todo-line-tags-regexp nil
  3041. "Matches a headline and puts TODO state into group 2 if present.
  3042. Also put tags into group 4 if tags are present.")
  3043. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3044. (defvar org-nl-done-regexp nil
  3045. "Matches newline followed by a headline with the DONE keyword.")
  3046. (make-variable-buffer-local 'org-nl-done-regexp)
  3047. (defvar org-looking-at-done-regexp nil
  3048. "Matches the DONE keyword a point.")
  3049. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3050. (defvar org-ds-keyword-length 12
  3051. "Maximum length of the Deadline and SCHEDULED keywords.")
  3052. (make-variable-buffer-local 'org-ds-keyword-length)
  3053. (defvar org-deadline-regexp nil
  3054. "Matches the DEADLINE keyword.")
  3055. (make-variable-buffer-local 'org-deadline-regexp)
  3056. (defvar org-deadline-time-regexp nil
  3057. "Matches the DEADLINE keyword together with a time stamp.")
  3058. (make-variable-buffer-local 'org-deadline-time-regexp)
  3059. (defvar org-deadline-line-regexp nil
  3060. "Matches the DEADLINE keyword and the rest of the line.")
  3061. (make-variable-buffer-local 'org-deadline-line-regexp)
  3062. (defvar org-scheduled-regexp nil
  3063. "Matches the SCHEDULED keyword.")
  3064. (make-variable-buffer-local 'org-scheduled-regexp)
  3065. (defvar org-scheduled-time-regexp nil
  3066. "Matches the SCHEDULED keyword together with a time stamp.")
  3067. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3068. (defvar org-closed-time-regexp nil
  3069. "Matches the CLOSED keyword together with a time stamp.")
  3070. (make-variable-buffer-local 'org-closed-time-regexp)
  3071. (defvar org-keyword-time-regexp nil
  3072. "Matches any of the 4 keywords, together with the time stamp.")
  3073. (make-variable-buffer-local 'org-keyword-time-regexp)
  3074. (defvar org-keyword-time-not-clock-regexp nil
  3075. "Matches any of the 3 keywords, together with the time stamp.")
  3076. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3077. (defvar org-maybe-keyword-time-regexp nil
  3078. "Matches a timestamp, possibly preceeded by a keyword.")
  3079. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3080. (defvar org-planning-or-clock-line-re nil
  3081. "Matches a line with planning or clock info.")
  3082. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3083. (defconst org-plain-time-of-day-regexp
  3084. (concat
  3085. "\\(\\<[012]?[0-9]"
  3086. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3087. "\\(--?"
  3088. "\\(\\<[012]?[0-9]"
  3089. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3090. "\\)?")
  3091. "Regular expression to match a plain time or time range.
  3092. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3093. groups carry important information:
  3094. 0 the full match
  3095. 1 the first time, range or not
  3096. 8 the second time, if it is a range.")
  3097. (defconst org-plain-time-extension-regexp
  3098. (concat
  3099. "\\(\\<[012]?[0-9]"
  3100. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3101. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3102. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3103. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3104. groups carry important information:
  3105. 0 the full match
  3106. 7 hours of duration
  3107. 9 minutes of duration")
  3108. (defconst org-stamp-time-of-day-regexp
  3109. (concat
  3110. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3111. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3112. "\\(--?"
  3113. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3114. "Regular expression to match a timestamp time or time range.
  3115. After a match, the following groups carry important information:
  3116. 0 the full match
  3117. 1 date plus weekday, for backreferencing to make sure both times on same day
  3118. 2 the first time, range or not
  3119. 4 the second time, if it is a range.")
  3120. (defconst org-startup-options
  3121. '(("fold" org-startup-folded t)
  3122. ("overview" org-startup-folded t)
  3123. ("nofold" org-startup-folded nil)
  3124. ("showall" org-startup-folded nil)
  3125. ("showeverything" org-startup-folded showeverything)
  3126. ("content" org-startup-folded content)
  3127. ("indent" org-startup-indented t)
  3128. ("noindent" org-startup-indented nil)
  3129. ("hidestars" org-hide-leading-stars t)
  3130. ("showstars" org-hide-leading-stars nil)
  3131. ("odd" org-odd-levels-only t)
  3132. ("oddeven" org-odd-levels-only nil)
  3133. ("align" org-startup-align-all-tables t)
  3134. ("noalign" org-startup-align-all-tables nil)
  3135. ("customtime" org-display-custom-times t)
  3136. ("logdone" org-log-done time)
  3137. ("lognotedone" org-log-done note)
  3138. ("nologdone" org-log-done nil)
  3139. ("lognoteclock-out" org-log-note-clock-out t)
  3140. ("nolognoteclock-out" org-log-note-clock-out nil)
  3141. ("logrepeat" org-log-repeat state)
  3142. ("lognoterepeat" org-log-repeat note)
  3143. ("nologrepeat" org-log-repeat nil)
  3144. ("fninline" org-footnote-define-inline t)
  3145. ("nofninline" org-footnote-define-inline nil)
  3146. ("fnlocal" org-footnote-section nil)
  3147. ("fnauto" org-footnote-auto-label t)
  3148. ("fnprompt" org-footnote-auto-label nil)
  3149. ("fnconfirm" org-footnote-auto-label confirm)
  3150. ("fnplain" org-footnote-auto-label plain)
  3151. ("fnadjust" org-footnote-auto-adjust t)
  3152. ("nofnadjust" org-footnote-auto-adjust nil)
  3153. ("constcgs" constants-unit-system cgs)
  3154. ("constSI" constants-unit-system SI)
  3155. ("noptag" org-tag-persistent-alist nil)
  3156. ("hideblocks" org-hide-block-startup t)
  3157. ("nohideblocks" org-hide-block-startup nil))
  3158. "Variable associated with STARTUP options for org-mode.
  3159. Each element is a list of three items: The startup options as written
  3160. in the #+STARTUP line, the corresponding variable, and the value to
  3161. set this variable to if the option is found. An optional forth element PUSH
  3162. means to push this value onto the list in the variable.")
  3163. (defun org-set-regexps-and-options ()
  3164. "Precompute regular expressions for current buffer."
  3165. (when (org-mode-p)
  3166. (org-set-local 'org-todo-kwd-alist nil)
  3167. (org-set-local 'org-todo-key-alist nil)
  3168. (org-set-local 'org-todo-key-trigger nil)
  3169. (org-set-local 'org-todo-keywords-1 nil)
  3170. (org-set-local 'org-done-keywords nil)
  3171. (org-set-local 'org-todo-heads nil)
  3172. (org-set-local 'org-todo-sets nil)
  3173. (org-set-local 'org-todo-log-states nil)
  3174. (org-set-local 'org-file-properties nil)
  3175. (org-set-local 'org-file-tags nil)
  3176. (let ((re (org-make-options-regexp
  3177. '("CATEGORY" "TODO" "COLUMNS"
  3178. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3179. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3180. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3181. (splitre "[ \t]+")
  3182. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3183. tail sep kws1 prio props ftags drawers
  3184. ext-setup-or-nil setup-contents (start 0))
  3185. (save-excursion
  3186. (save-restriction
  3187. (widen)
  3188. (goto-char (point-min))
  3189. (while (or (and ext-setup-or-nil
  3190. (string-match re ext-setup-or-nil start)
  3191. (setq start (match-end 0)))
  3192. (and (setq ext-setup-or-nil nil start 0)
  3193. (re-search-forward re nil t)))
  3194. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3195. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3196. (cond
  3197. ((equal key "CATEGORY")
  3198. (if (string-match "[ \t]+$" value)
  3199. (setq value (replace-match "" t t value)))
  3200. (setq cat value))
  3201. ((member key '("SEQ_TODO" "TODO"))
  3202. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3203. ((equal key "TYP_TODO")
  3204. (push (cons 'type (org-split-string value splitre)) kwds))
  3205. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3206. ;; general TODO-like setup
  3207. (push (cons (intern (downcase (match-string 1 key)))
  3208. (org-split-string value splitre)) kwds))
  3209. ((equal key "TAGS")
  3210. (setq tags (append tags (if tags '("\\n") nil)
  3211. (org-split-string value splitre))))
  3212. ((equal key "COLUMNS")
  3213. (org-set-local 'org-columns-default-format value))
  3214. ((equal key "LINK")
  3215. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3216. (push (cons (match-string 1 value)
  3217. (org-trim (match-string 2 value)))
  3218. links)))
  3219. ((equal key "PRIORITIES")
  3220. (setq prio (org-split-string value " +")))
  3221. ((equal key "PROPERTY")
  3222. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3223. (push (cons (match-string 1 value) (match-string 2 value))
  3224. props)))
  3225. ((equal key "FILETAGS")
  3226. (when (string-match "\\S-" value)
  3227. (setq ftags
  3228. (append
  3229. ftags
  3230. (apply 'append
  3231. (mapcar (lambda (x) (org-split-string x ":"))
  3232. (org-split-string value)))))))
  3233. ((equal key "DRAWERS")
  3234. (setq drawers (org-split-string value splitre)))
  3235. ((equal key "CONSTANTS")
  3236. (setq const (append const (org-split-string value splitre))))
  3237. ((equal key "STARTUP")
  3238. (let ((opts (org-split-string value splitre))
  3239. l var val)
  3240. (while (setq l (pop opts))
  3241. (when (setq l (assoc l org-startup-options))
  3242. (setq var (nth 1 l) val (nth 2 l))
  3243. (if (not (nth 3 l))
  3244. (set (make-local-variable var) val)
  3245. (if (not (listp (symbol-value var)))
  3246. (set (make-local-variable var) nil))
  3247. (set (make-local-variable var) (symbol-value var))
  3248. (add-to-list var val))))))
  3249. ((equal key "ARCHIVE")
  3250. (string-match " *$" value)
  3251. (setq arch (replace-match "" t t value))
  3252. (remove-text-properties 0 (length arch)
  3253. '(face t fontified t) arch))
  3254. ((equal key "SETUPFILE")
  3255. (setq setup-contents (org-file-contents
  3256. (expand-file-name
  3257. (org-remove-double-quotes value))
  3258. 'noerror))
  3259. (if (not ext-setup-or-nil)
  3260. (setq ext-setup-or-nil setup-contents start 0)
  3261. (setq ext-setup-or-nil
  3262. (concat (substring ext-setup-or-nil 0 start)
  3263. "\n" setup-contents "\n"
  3264. (substring ext-setup-or-nil start)))))
  3265. ))))
  3266. (when cat
  3267. (org-set-local 'org-category (intern cat))
  3268. (push (cons "CATEGORY" cat) props))
  3269. (when prio
  3270. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3271. (setq prio (mapcar 'string-to-char prio))
  3272. (org-set-local 'org-highest-priority (nth 0 prio))
  3273. (org-set-local 'org-lowest-priority (nth 1 prio))
  3274. (org-set-local 'org-default-priority (nth 2 prio)))
  3275. (and props (org-set-local 'org-file-properties (nreverse props)))
  3276. (and ftags (org-set-local 'org-file-tags
  3277. (mapcar 'org-add-prop-inherited ftags)))
  3278. (and drawers (org-set-local 'org-drawers drawers))
  3279. (and arch (org-set-local 'org-archive-location arch))
  3280. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3281. ;; Process the TODO keywords
  3282. (unless kwds
  3283. ;; Use the global values as if they had been given locally.
  3284. (setq kwds (default-value 'org-todo-keywords))
  3285. (if (stringp (car kwds))
  3286. (setq kwds (list (cons org-todo-interpretation
  3287. (default-value 'org-todo-keywords)))))
  3288. (setq kwds (reverse kwds)))
  3289. (setq kwds (nreverse kwds))
  3290. (let (inter kws kw)
  3291. (while (setq kws (pop kwds))
  3292. (let ((kws (or
  3293. (run-hook-with-args-until-success
  3294. 'org-todo-setup-filter-hook kws)
  3295. kws)))
  3296. (setq inter (pop kws) sep (member "|" kws)
  3297. kws0 (delete "|" (copy-sequence kws))
  3298. kwsa nil
  3299. kws1 (mapcar
  3300. (lambda (x)
  3301. ;; 1 2
  3302. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3303. (progn
  3304. (setq kw (match-string 1 x)
  3305. key (and (match-end 2) (match-string 2 x))
  3306. log (org-extract-log-state-settings x))
  3307. (push (cons kw (and key (string-to-char key))) kwsa)
  3308. (and log (push log org-todo-log-states))
  3309. kw)
  3310. (error "Invalid TODO keyword %s" x)))
  3311. kws0)
  3312. kwsa (if kwsa (append '((:startgroup))
  3313. (nreverse kwsa)
  3314. '((:endgroup))))
  3315. hw (car kws1)
  3316. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3317. tail (list inter hw (car dws) (org-last dws))))
  3318. (add-to-list 'org-todo-heads hw 'append)
  3319. (push kws1 org-todo-sets)
  3320. (setq org-done-keywords (append org-done-keywords dws nil))
  3321. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3322. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3323. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3324. (setq org-todo-sets (nreverse org-todo-sets)
  3325. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3326. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3327. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3328. ;; Process the constants
  3329. (when const
  3330. (let (e cst)
  3331. (while (setq e (pop const))
  3332. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3333. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3334. (setq org-table-formula-constants-local cst)))
  3335. ;; Process the tags.
  3336. (when tags
  3337. (let (e tgs)
  3338. (while (setq e (pop tags))
  3339. (cond
  3340. ((equal e "{") (push '(:startgroup) tgs))
  3341. ((equal e "}") (push '(:endgroup) tgs))
  3342. ((equal e "\\n") (push '(:newline) tgs))
  3343. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3344. (push (cons (match-string 1 e)
  3345. (string-to-char (match-string 2 e)))
  3346. tgs))
  3347. (t (push (list e) tgs))))
  3348. (org-set-local 'org-tag-alist nil)
  3349. (while (setq e (pop tgs))
  3350. (or (and (stringp (car e))
  3351. (assoc (car e) org-tag-alist))
  3352. (push e org-tag-alist)))))
  3353. ;; Compute the regular expressions and other local variables
  3354. (if (not org-done-keywords)
  3355. (setq org-done-keywords (and org-todo-keywords-1
  3356. (list (org-last org-todo-keywords-1)))))
  3357. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3358. (length org-scheduled-string)
  3359. (length org-clock-string)
  3360. (length org-closed-string)))
  3361. org-drawer-regexp
  3362. (concat "^[ \t]*:\\("
  3363. (mapconcat 'regexp-quote org-drawers "\\|")
  3364. "\\):[ \t]*$")
  3365. org-not-done-keywords
  3366. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3367. org-todo-regexp
  3368. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3369. "\\|") "\\)\\>")
  3370. org-not-done-regexp
  3371. (concat "\\<\\("
  3372. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3373. "\\)\\>")
  3374. org-not-done-heading-regexp
  3375. (concat "^\\(\\*+\\)[ \t]+\\("
  3376. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3377. "\\)\\>")
  3378. org-todo-line-regexp
  3379. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3380. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3381. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3382. org-complex-heading-regexp
  3383. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3384. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3385. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3386. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3387. org-complex-heading-regexp-format
  3388. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3389. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3390. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3391. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3392. org-nl-done-regexp
  3393. (concat "\n\\*+[ \t]+"
  3394. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3395. "\\)" "\\>")
  3396. org-todo-line-tags-regexp
  3397. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3398. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3399. (org-re
  3400. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3401. org-looking-at-done-regexp
  3402. (concat "^" "\\(?:"
  3403. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3404. "\\>")
  3405. org-deadline-regexp (concat "\\<" org-deadline-string)
  3406. org-deadline-time-regexp
  3407. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3408. org-deadline-line-regexp
  3409. (concat "\\<\\(" org-deadline-string "\\).*")
  3410. org-scheduled-regexp
  3411. (concat "\\<" org-scheduled-string)
  3412. org-scheduled-time-regexp
  3413. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3414. org-closed-time-regexp
  3415. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3416. org-keyword-time-regexp
  3417. (concat "\\<\\(" org-scheduled-string
  3418. "\\|" org-deadline-string
  3419. "\\|" org-closed-string
  3420. "\\|" org-clock-string "\\)"
  3421. " *[[<]\\([^]>]+\\)[]>]")
  3422. org-keyword-time-not-clock-regexp
  3423. (concat "\\<\\(" org-scheduled-string
  3424. "\\|" org-deadline-string
  3425. "\\|" org-closed-string
  3426. "\\)"
  3427. " *[[<]\\([^]>]+\\)[]>]")
  3428. org-maybe-keyword-time-regexp
  3429. (concat "\\(\\<\\(" org-scheduled-string
  3430. "\\|" org-deadline-string
  3431. "\\|" org-closed-string
  3432. "\\|" org-clock-string "\\)\\)?"
  3433. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3434. org-planning-or-clock-line-re
  3435. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3436. "\\|" org-deadline-string
  3437. "\\|" org-closed-string "\\|" org-clock-string
  3438. "\\)\\>\\)")
  3439. )
  3440. (org-compute-latex-and-specials-regexp)
  3441. (org-set-font-lock-defaults))))
  3442. (defun org-file-contents (file &optional noerror)
  3443. "Return the contents of FILE, as a string."
  3444. (if (or (not file)
  3445. (not (file-readable-p file)))
  3446. (if noerror
  3447. (progn
  3448. (message "Cannot read file %s" file)
  3449. (ding) (sit-for 2)
  3450. "")
  3451. (error "Cannot read file %s" file))
  3452. (with-temp-buffer
  3453. (insert-file-contents file)
  3454. (buffer-string))))
  3455. (defun org-extract-log-state-settings (x)
  3456. "Extract the log state setting from a TODO keyword string.
  3457. This will extract info from a string like \"WAIT(w@/!)\"."
  3458. (let (kw key log1 log2)
  3459. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3460. (setq kw (match-string 1 x)
  3461. key (and (match-end 2) (match-string 2 x))
  3462. log1 (and (match-end 3) (match-string 3 x))
  3463. log2 (and (match-end 4) (match-string 4 x)))
  3464. (and (or log1 log2)
  3465. (list kw
  3466. (and log1 (if (equal log1 "!") 'time 'note))
  3467. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3468. (defun org-remove-keyword-keys (list)
  3469. "Remove a pair of parenthesis at the end of each string in LIST."
  3470. (mapcar (lambda (x)
  3471. (if (string-match "(.*)$" x)
  3472. (substring x 0 (match-beginning 0))
  3473. x))
  3474. list))
  3475. ;; FIXME: this could be done much better, using second characters etc.
  3476. (defun org-assign-fast-keys (alist)
  3477. "Assign fast keys to a keyword-key alist.
  3478. Respect keys that are already there."
  3479. (let (new e k c c1 c2 (char ?a))
  3480. (while (setq e (pop alist))
  3481. (cond
  3482. ((equal e '(:startgroup)) (push e new))
  3483. ((equal e '(:endgroup)) (push e new))
  3484. ((equal e '(:newline)) (push e new))
  3485. (t
  3486. (setq k (car e) c2 nil)
  3487. (if (cdr e)
  3488. (setq c (cdr e))
  3489. ;; automatically assign a character.
  3490. (setq c1 (string-to-char
  3491. (downcase (substring
  3492. k (if (= (string-to-char k) ?@) 1 0)))))
  3493. (if (or (rassoc c1 new) (rassoc c1 alist))
  3494. (while (or (rassoc char new) (rassoc char alist))
  3495. (setq char (1+ char)))
  3496. (setq c2 c1))
  3497. (setq c (or c2 char)))
  3498. (push (cons k c) new))))
  3499. (nreverse new)))
  3500. ;;; Some variables used in various places
  3501. (defvar org-window-configuration nil
  3502. "Used in various places to store a window configuration.")
  3503. (defvar org-selected-window nil
  3504. "Used in various places to store a window configuration.")
  3505. (defvar org-finish-function nil
  3506. "Function to be called when `C-c C-c' is used.
  3507. This is for getting out of special buffers like remember.")
  3508. ;; FIXME: Occasionally check by commenting these, to make sure
  3509. ;; no other functions uses these, forgetting to let-bind them.
  3510. (defvar entry)
  3511. (defvar last-state)
  3512. (defvar date)
  3513. ;; Defined somewhere in this file, but used before definition.
  3514. (defvar org-html-entities)
  3515. (defvar org-struct-menu)
  3516. (defvar org-org-menu)
  3517. (defvar org-tbl-menu)
  3518. (defvar org-agenda-keymap)
  3519. ;;;; Define the Org-mode
  3520. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3521. (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."))
  3522. ;; We use a before-change function to check if a table might need
  3523. ;; an update.
  3524. (defvar org-table-may-need-update t
  3525. "Indicates that a table might need an update.
  3526. This variable is set by `org-before-change-function'.
  3527. `org-table-align' sets it back to nil.")
  3528. (defun org-before-change-function (beg end)
  3529. "Every change indicates that a table might need an update."
  3530. (setq org-table-may-need-update t))
  3531. (defvar org-mode-map)
  3532. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3533. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3534. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3535. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3536. (defvar org-table-buffer-is-an nil)
  3537. (defconst org-outline-regexp "\\*+ ")
  3538. ;;;###autoload
  3539. (define-derived-mode org-mode outline-mode "Org"
  3540. "Outline-based notes management and organizer, alias
  3541. \"Carsten's outline-mode for keeping track of everything.\"
  3542. Org-mode develops organizational tasks around a NOTES file which
  3543. contains information about projects as plain text. Org-mode is
  3544. implemented on top of outline-mode, which is ideal to keep the content
  3545. of large files well structured. It supports ToDo items, deadlines and
  3546. time stamps, which magically appear in the diary listing of the Emacs
  3547. calendar. Tables are easily created with a built-in table editor.
  3548. Plain text URL-like links connect to websites, emails (VM), Usenet
  3549. messages (Gnus), BBDB entries, and any files related to the project.
  3550. For printing and sharing of notes, an Org-mode file (or a part of it)
  3551. can be exported as a structured ASCII or HTML file.
  3552. The following commands are available:
  3553. \\{org-mode-map}"
  3554. ;; Get rid of Outline menus, they are not needed
  3555. ;; Need to do this here because define-derived-mode sets up
  3556. ;; the keymap so late. Still, it is a waste to call this each time
  3557. ;; we switch another buffer into org-mode.
  3558. (if (featurep 'xemacs)
  3559. (when (boundp 'outline-mode-menu-heading)
  3560. ;; Assume this is Greg's port, it used easymenu
  3561. (easy-menu-remove outline-mode-menu-heading)
  3562. (easy-menu-remove outline-mode-menu-show)
  3563. (easy-menu-remove outline-mode-menu-hide))
  3564. (define-key org-mode-map [menu-bar headings] 'undefined)
  3565. (define-key org-mode-map [menu-bar hide] 'undefined)
  3566. (define-key org-mode-map [menu-bar show] 'undefined))
  3567. (org-load-modules-maybe)
  3568. (easy-menu-add org-org-menu)
  3569. (easy-menu-add org-tbl-menu)
  3570. (org-install-agenda-files-menu)
  3571. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3572. (org-add-to-invisibility-spec '(org-cwidth))
  3573. (org-add-to-invisibility-spec '(org-hide-block . t))
  3574. (when (featurep 'xemacs)
  3575. (org-set-local 'line-move-ignore-invisible t))
  3576. (org-set-local 'outline-regexp org-outline-regexp)
  3577. (org-set-local 'outline-level 'org-outline-level)
  3578. (when (and org-ellipsis
  3579. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3580. (fboundp 'make-glyph-code))
  3581. (unless org-display-table
  3582. (setq org-display-table (make-display-table)))
  3583. (set-display-table-slot
  3584. org-display-table 4
  3585. (vconcat (mapcar
  3586. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3587. org-ellipsis)))
  3588. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3589. (setq buffer-display-table org-display-table))
  3590. (org-set-regexps-and-options)
  3591. (when (and org-tag-faces (not org-tags-special-faces-re))
  3592. ;; tag faces set outside customize.... force initialization.
  3593. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3594. ;; Calc embedded
  3595. (org-set-local 'calc-embedded-open-mode "# ")
  3596. (modify-syntax-entry ?# "<")
  3597. (modify-syntax-entry ?@ "w")
  3598. (if org-startup-truncated (setq truncate-lines t))
  3599. (org-set-local 'font-lock-unfontify-region-function
  3600. 'org-unfontify-region)
  3601. ;; Activate before-change-function
  3602. (org-set-local 'org-table-may-need-update t)
  3603. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3604. 'local)
  3605. ;; Check for running clock before killing a buffer
  3606. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3607. ;; Paragraphs and auto-filling
  3608. (org-set-autofill-regexps)
  3609. (setq indent-line-function 'org-indent-line-function)
  3610. (org-update-radio-target-regexp)
  3611. ;; Make sure dependence stuff works reliably, even for users who set it
  3612. ;; too late :-(
  3613. (if org-enforce-todo-dependencies
  3614. (add-hook 'org-blocker-hook
  3615. 'org-block-todo-from-children-or-siblings-or-parent)
  3616. (remove-hook 'org-blocker-hook
  3617. 'org-block-todo-from-children-or-siblings-or-parent))
  3618. (if org-enforce-todo-checkbox-dependencies
  3619. (add-hook 'org-blocker-hook
  3620. 'org-block-todo-from-checkboxes)
  3621. (remove-hook 'org-blocker-hook
  3622. 'org-block-todo-from-checkboxes))
  3623. ;; Comment characters
  3624. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3625. (org-set-local 'comment-padding " ")
  3626. ;; Align options lines
  3627. (org-set-local
  3628. 'align-mode-rules-list
  3629. '((org-in-buffer-settings
  3630. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3631. (modes . '(org-mode)))))
  3632. ;; Imenu
  3633. (org-set-local 'imenu-create-index-function
  3634. 'org-imenu-get-tree)
  3635. ;; Make isearch reveal context
  3636. (if (or (featurep 'xemacs)
  3637. (not (boundp 'outline-isearch-open-invisible-function)))
  3638. ;; Emacs 21 and XEmacs make use of the hook
  3639. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3640. ;; Emacs 22 deals with this through a special variable
  3641. (org-set-local 'outline-isearch-open-invisible-function
  3642. (lambda (&rest ignore) (org-show-context 'isearch))))
  3643. ;; If empty file that did not turn on org-mode automatically, make it to.
  3644. (if (and org-insert-mode-line-in-empty-file
  3645. (interactive-p)
  3646. (= (point-min) (point-max)))
  3647. (insert "# -*- mode: org -*-\n\n"))
  3648. (unless org-inhibit-startup
  3649. (when org-startup-align-all-tables
  3650. (let ((bmp (buffer-modified-p)))
  3651. (org-table-map-tables 'org-table-align)
  3652. (set-buffer-modified-p bmp)))
  3653. (when org-startup-indented
  3654. (require 'org-indent)
  3655. (org-indent-mode 1))
  3656. (org-set-startup-visibility)))
  3657. (when (fboundp 'abbrev-table-put)
  3658. (abbrev-table-put org-mode-abbrev-table
  3659. :parents (list text-mode-abbrev-table)))
  3660. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3661. (defun org-current-time ()
  3662. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3663. (if (> (car org-time-stamp-rounding-minutes) 1)
  3664. (let ((r (car org-time-stamp-rounding-minutes))
  3665. (time (decode-time)))
  3666. (apply 'encode-time
  3667. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3668. (nthcdr 2 time))))
  3669. (current-time)))
  3670. ;;;; Font-Lock stuff, including the activators
  3671. (defvar org-mouse-map (make-sparse-keymap))
  3672. (org-defkey org-mouse-map
  3673. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3674. (org-defkey org-mouse-map
  3675. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3676. (when org-mouse-1-follows-link
  3677. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3678. (when org-tab-follows-link
  3679. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3680. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3681. (require 'font-lock)
  3682. (defconst org-non-link-chars "]\t\n\r<>")
  3683. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3684. "shell" "elisp"))
  3685. (defvar org-link-types-re nil
  3686. "Matches a link that has a url-like prefix like \"http:\"")
  3687. (defvar org-link-re-with-space nil
  3688. "Matches a link with spaces, optional angular brackets around it.")
  3689. (defvar org-link-re-with-space2 nil
  3690. "Matches a link with spaces, optional angular brackets around it.")
  3691. (defvar org-link-re-with-space3 nil
  3692. "Matches a link with spaces, only for internal part in bracket links.")
  3693. (defvar org-angle-link-re nil
  3694. "Matches link with angular brackets, spaces are allowed.")
  3695. (defvar org-plain-link-re nil
  3696. "Matches plain link, without spaces.")
  3697. (defvar org-bracket-link-regexp nil
  3698. "Matches a link in double brackets.")
  3699. (defvar org-bracket-link-analytic-regexp nil
  3700. "Regular expression used to analyze links.
  3701. Here is what the match groups contain after a match:
  3702. 1: http:
  3703. 2: http
  3704. 3: path
  3705. 4: [desc]
  3706. 5: desc")
  3707. (defvar org-bracket-link-analytic-regexp++ nil
  3708. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3709. (defvar org-any-link-re nil
  3710. "Regular expression matching any link.")
  3711. (defun org-make-link-regexps ()
  3712. "Update the link regular expressions.
  3713. This should be called after the variable `org-link-types' has changed."
  3714. (setq org-link-types-re
  3715. (concat
  3716. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3717. org-link-re-with-space
  3718. (concat
  3719. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3720. "\\([^" org-non-link-chars " ]"
  3721. "[^" org-non-link-chars "]*"
  3722. "[^" org-non-link-chars " ]\\)>?")
  3723. org-link-re-with-space2
  3724. (concat
  3725. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3726. "\\([^" org-non-link-chars " ]"
  3727. "[^\t\n\r]*"
  3728. "[^" org-non-link-chars " ]\\)>?")
  3729. org-link-re-with-space3
  3730. (concat
  3731. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3732. "\\([^" org-non-link-chars " ]"
  3733. "[^\t\n\r]*\\)")
  3734. org-angle-link-re
  3735. (concat
  3736. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3737. "\\([^" org-non-link-chars " ]"
  3738. "[^" org-non-link-chars "]*"
  3739. "\\)>")
  3740. org-plain-link-re
  3741. (concat
  3742. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3743. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3744. org-bracket-link-regexp
  3745. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3746. org-bracket-link-analytic-regexp
  3747. (concat
  3748. "\\[\\["
  3749. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3750. "\\([^]]+\\)"
  3751. "\\]"
  3752. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3753. "\\]")
  3754. org-bracket-link-analytic-regexp++
  3755. (concat
  3756. "\\[\\["
  3757. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3758. "\\([^]]+\\)"
  3759. "\\]"
  3760. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3761. "\\]")
  3762. org-any-link-re
  3763. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3764. org-angle-link-re "\\)\\|\\("
  3765. org-plain-link-re "\\)")))
  3766. (org-make-link-regexps)
  3767. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3768. "Regular expression for fast time stamp matching.")
  3769. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3770. "Regular expression for fast time stamp matching.")
  3771. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3772. "Regular expression matching time strings for analysis.
  3773. This one does not require the space after the date, so it can be used
  3774. on a string that terminates immediately after the date.")
  3775. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3776. "Regular expression matching time strings for analysis.")
  3777. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3778. "Regular expression matching time stamps, with groups.")
  3779. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3780. "Regular expression matching time stamps (also [..]), with groups.")
  3781. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3782. "Regular expression matching a time stamp range.")
  3783. (defconst org-tr-regexp-both
  3784. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3785. "Regular expression matching a time stamp range.")
  3786. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3787. org-ts-regexp "\\)?")
  3788. "Regular expression matching a time stamp or time stamp range.")
  3789. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3790. org-ts-regexp-both "\\)?")
  3791. "Regular expression matching a time stamp or time stamp range.
  3792. The time stamps may be either active or inactive.")
  3793. (defvar org-emph-face nil)
  3794. (defun org-do-emphasis-faces (limit)
  3795. "Run through the buffer and add overlays to links."
  3796. (let (rtn a)
  3797. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3798. (if (not (= (char-after (match-beginning 3))
  3799. (char-after (match-beginning 4))))
  3800. (progn
  3801. (setq rtn t)
  3802. (setq a (assoc (match-string 3) org-emphasis-alist))
  3803. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3804. 'face
  3805. (nth 1 a))
  3806. (and (nth 4 a)
  3807. (org-remove-flyspell-overlays-in
  3808. (match-beginning 0) (match-end 0)))
  3809. (add-text-properties (match-beginning 2) (match-end 2)
  3810. '(font-lock-multiline t))
  3811. (when org-hide-emphasis-markers
  3812. (add-text-properties (match-end 4) (match-beginning 5)
  3813. '(invisible org-link))
  3814. (add-text-properties (match-beginning 3) (match-end 3)
  3815. '(invisible org-link)))))
  3816. (backward-char 1))
  3817. rtn))
  3818. (defun org-emphasize (&optional char)
  3819. "Insert or change an emphasis, i.e. a font like bold or italic.
  3820. If there is an active region, change that region to a new emphasis.
  3821. If there is no region, just insert the marker characters and position
  3822. the cursor between them.
  3823. CHAR should be either the marker character, or the first character of the
  3824. HTML tag associated with that emphasis. If CHAR is a space, the means
  3825. to remove the emphasis of the selected region.
  3826. If char is not given (for example in an interactive call) it
  3827. will be prompted for."
  3828. (interactive)
  3829. (let ((eal org-emphasis-alist) e det
  3830. (erc org-emphasis-regexp-components)
  3831. (prompt "")
  3832. (string "") beg end move tag c s)
  3833. (if (org-region-active-p)
  3834. (setq beg (region-beginning) end (region-end)
  3835. string (buffer-substring beg end))
  3836. (setq move t))
  3837. (while (setq e (pop eal))
  3838. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3839. c (aref tag 0))
  3840. (push (cons c (string-to-char (car e))) det)
  3841. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3842. (substring tag 1)))))
  3843. (setq det (nreverse det))
  3844. (unless char
  3845. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3846. (setq char (read-char-exclusive)))
  3847. (setq char (or (cdr (assoc char det)) char))
  3848. (if (equal char ?\ )
  3849. (setq s "" move nil)
  3850. (unless (assoc (char-to-string char) org-emphasis-alist)
  3851. (error "No such emphasis marker: \"%c\"" char))
  3852. (setq s (char-to-string char)))
  3853. (while (and (> (length string) 1)
  3854. (equal (substring string 0 1) (substring string -1))
  3855. (assoc (substring string 0 1) org-emphasis-alist))
  3856. (setq string (substring string 1 -1)))
  3857. (setq string (concat s string s))
  3858. (if beg (delete-region beg end))
  3859. (unless (or (bolp)
  3860. (string-match (concat "[" (nth 0 erc) "\n]")
  3861. (char-to-string (char-before (point)))))
  3862. (insert " "))
  3863. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3864. (char-to-string (char-after (point))))
  3865. (insert " ") (backward-char 1))
  3866. (insert string)
  3867. (and move (backward-char 1))))
  3868. (defconst org-nonsticky-props
  3869. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3870. (defsubst org-rear-nonsticky-at (pos)
  3871. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3872. (defun org-activate-plain-links (limit)
  3873. "Run through the buffer and add overlays to links."
  3874. (catch 'exit
  3875. (let (f)
  3876. (if (re-search-forward org-plain-link-re limit t)
  3877. (progn
  3878. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3879. (setq f (get-text-property (match-beginning 0) 'face))
  3880. (if (or (eq f 'org-tag)
  3881. (and (listp f) (memq 'org-tag f)))
  3882. nil
  3883. (add-text-properties (match-beginning 0) (match-end 0)
  3884. (list 'mouse-face 'highlight
  3885. 'keymap org-mouse-map))
  3886. (org-rear-nonsticky-at (match-end 0)))
  3887. t)))))
  3888. (defun org-activate-code (limit)
  3889. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3890. (progn
  3891. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3892. (remove-text-properties (match-beginning 0) (match-end 0)
  3893. '(display t invisible t intangible t))
  3894. t)))
  3895. (defun org-fontify-meta-lines-and-blocks (limit)
  3896. "Fontify #+ lines and blocks, in the correct ways."
  3897. (let ((case-fold-search t))
  3898. (if (re-search-forward
  3899. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3900. limit t)
  3901. (let ((beg (match-beginning 0))
  3902. (beg1 (line-beginning-position 2))
  3903. (dc1 (downcase (match-string 2)))
  3904. (dc3 (downcase (match-string 3)))
  3905. end end1 quoting)
  3906. (cond
  3907. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3908. ;; a single line of backend-specific content
  3909. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3910. (remove-text-properties (match-beginning 0) (match-end 0)
  3911. '(display t invisible t intangible t))
  3912. (add-text-properties (match-beginning 1) (match-end 3)
  3913. '(font-lock-fontified t face org-meta-line))
  3914. (add-text-properties (match-beginning 6) (match-end 6)
  3915. '(font-lock-fontified t face org-block))
  3916. t)
  3917. ((and (match-end 4) (equal dc3 "begin"))
  3918. ;; Truely a block
  3919. (setq quoting (member (downcase (match-string 5))
  3920. org-protecting-blocks))
  3921. (when (re-search-forward
  3922. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3923. nil t) ;; on purpose, we look further than LIMIT
  3924. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3925. (when quoting
  3926. (remove-text-properties beg end
  3927. '(display t invisible t intangible t)))
  3928. (add-text-properties
  3929. beg end
  3930. '(font-lock-fontified t font-lock-multiline t))
  3931. (add-text-properties beg beg1 '(face org-meta-line))
  3932. (add-text-properties end1 end '(face org-meta-line))
  3933. (when quoting
  3934. (add-text-properties beg1 end1 '(face org-block)))
  3935. t))
  3936. ((not (member (char-after beg) '(?\ ?\t)))
  3937. ;; just any other in-buffer setting, but not indented
  3938. (add-text-properties
  3939. beg (match-end 0)
  3940. '(font-lock-fontified t face org-meta-line))
  3941. t)
  3942. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  3943. "orgtbl:" "tblfm:" "tblname:"))
  3944. (and (match-end 4) (equal dc3 "attr")))
  3945. (add-text-properties
  3946. beg (match-end 0)
  3947. '(font-lock-fontified t face org-meta-line))
  3948. t)
  3949. ((member dc3 '(" " ""))
  3950. (add-text-properties
  3951. beg (match-end 0)
  3952. '(font-lock-fontified t face font-lock-comment-face)))
  3953. (t nil))))))
  3954. (defun org-activate-angle-links (limit)
  3955. "Run through the buffer and add overlays to links."
  3956. (if (re-search-forward org-angle-link-re limit t)
  3957. (progn
  3958. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3959. (add-text-properties (match-beginning 0) (match-end 0)
  3960. (list 'mouse-face 'highlight
  3961. 'keymap org-mouse-map))
  3962. (org-rear-nonsticky-at (match-end 0))
  3963. t)))
  3964. (defun org-activate-footnote-links (limit)
  3965. "Run through the buffer and add overlays to links."
  3966. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3967. limit t)
  3968. (progn
  3969. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3970. (add-text-properties (match-beginning 2) (match-end 2)
  3971. (list 'mouse-face 'highlight
  3972. 'keymap org-mouse-map
  3973. 'help-echo
  3974. (if (= (point-at-bol) (match-beginning 2))
  3975. "Footnote definition"
  3976. "Footnote reference")
  3977. ))
  3978. (org-rear-nonsticky-at (match-end 2))
  3979. t)))
  3980. (defun org-activate-bracket-links (limit)
  3981. "Run through the buffer and add overlays to bracketed links."
  3982. (if (re-search-forward org-bracket-link-regexp limit t)
  3983. (let* ((help (concat "LINK: "
  3984. (org-match-string-no-properties 1)))
  3985. ;; FIXME: above we should remove the escapes.
  3986. ;; but that requires another match, protecting match data,
  3987. ;; a lot of overhead for font-lock.
  3988. (ip (org-maybe-intangible
  3989. (list 'invisible 'org-link
  3990. 'keymap org-mouse-map 'mouse-face 'highlight
  3991. 'font-lock-multiline t 'help-echo help)))
  3992. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3993. 'font-lock-multiline t 'help-echo help)))
  3994. ;; We need to remove the invisible property here. Table narrowing
  3995. ;; may have made some of this invisible.
  3996. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3997. (remove-text-properties (match-beginning 0) (match-end 0)
  3998. '(invisible nil))
  3999. (if (match-end 3)
  4000. (progn
  4001. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4002. (org-rear-nonsticky-at (match-beginning 3))
  4003. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4004. (org-rear-nonsticky-at (match-end 3))
  4005. (add-text-properties (match-end 3) (match-end 0) ip)
  4006. (org-rear-nonsticky-at (match-end 0)))
  4007. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4008. (org-rear-nonsticky-at (match-beginning 1))
  4009. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4010. (org-rear-nonsticky-at (match-end 1))
  4011. (add-text-properties (match-end 1) (match-end 0) ip)
  4012. (org-rear-nonsticky-at (match-end 0)))
  4013. t)))
  4014. (defun org-activate-dates (limit)
  4015. "Run through the buffer and add overlays to dates."
  4016. (if (re-search-forward org-tsr-regexp-both limit t)
  4017. (progn
  4018. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4019. (add-text-properties (match-beginning 0) (match-end 0)
  4020. (list 'mouse-face 'highlight
  4021. 'keymap org-mouse-map))
  4022. (org-rear-nonsticky-at (match-end 0))
  4023. (when org-display-custom-times
  4024. (if (match-end 3)
  4025. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4026. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4027. t)))
  4028. (defvar org-target-link-regexp nil
  4029. "Regular expression matching radio targets in plain text.")
  4030. (make-variable-buffer-local 'org-target-link-regexp)
  4031. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4032. "Regular expression matching a link target.")
  4033. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4034. "Regular expression matching a radio target.")
  4035. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4036. "Regular expression matching any target.")
  4037. (defun org-activate-target-links (limit)
  4038. "Run through the buffer and add overlays to target matches."
  4039. (when org-target-link-regexp
  4040. (let ((case-fold-search t))
  4041. (if (re-search-forward org-target-link-regexp limit t)
  4042. (progn
  4043. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4044. (add-text-properties (match-beginning 0) (match-end 0)
  4045. (list 'mouse-face 'highlight
  4046. 'keymap org-mouse-map
  4047. 'help-echo "Radio target link"
  4048. 'org-linked-text t))
  4049. (org-rear-nonsticky-at (match-end 0))
  4050. t)))))
  4051. (defun org-update-radio-target-regexp ()
  4052. "Find all radio targets in this file and update the regular expression."
  4053. (interactive)
  4054. (when (memq 'radio org-activate-links)
  4055. (setq org-target-link-regexp
  4056. (org-make-target-link-regexp (org-all-targets 'radio)))
  4057. (org-restart-font-lock)))
  4058. (defun org-hide-wide-columns (limit)
  4059. (let (s e)
  4060. (setq s (text-property-any (point) (or limit (point-max))
  4061. 'org-cwidth t))
  4062. (when s
  4063. (setq e (next-single-property-change s 'org-cwidth))
  4064. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4065. (goto-char e)
  4066. t)))
  4067. (defvar org-latex-and-specials-regexp nil
  4068. "Regular expression for highlighting export special stuff.")
  4069. (defvar org-match-substring-regexp)
  4070. (defvar org-match-substring-with-braces-regexp)
  4071. ;; This should be with the exporter code, but we also use if for font-locking
  4072. (defconst org-export-html-special-string-regexps
  4073. '(("\\\\-" . "&shy;")
  4074. ("---\\([^-]\\)" . "&mdash;\\1")
  4075. ("--\\([^-]\\)" . "&ndash;\\1")
  4076. ("\\.\\.\\." . "&hellip;"))
  4077. "Regular expressions for special string conversion.")
  4078. (defun org-compute-latex-and-specials-regexp ()
  4079. "Compute regular expression for stuff treated specially by exporters."
  4080. (if (not org-highlight-latex-fragments-and-specials)
  4081. (org-set-local 'org-latex-and-specials-regexp nil)
  4082. (require 'org-exp)
  4083. (let*
  4084. ((matchers (plist-get org-format-latex-options :matchers))
  4085. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4086. org-latex-regexps)))
  4087. (options (org-combine-plists (org-default-export-plist)
  4088. (org-infile-export-plist)))
  4089. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4090. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4091. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4092. (org-export-html-expand (plist-get options :expand-quoted-html))
  4093. (org-export-with-special-strings (plist-get options :special-strings))
  4094. (re-sub
  4095. (cond
  4096. ((equal org-export-with-sub-superscripts '{})
  4097. (list org-match-substring-with-braces-regexp))
  4098. (org-export-with-sub-superscripts
  4099. (list org-match-substring-regexp))
  4100. (t nil)))
  4101. (re-latex
  4102. (if org-export-with-LaTeX-fragments
  4103. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4104. (re-macros
  4105. (if org-export-with-TeX-macros
  4106. (list (concat "\\\\"
  4107. (regexp-opt
  4108. (append (mapcar 'car org-html-entities)
  4109. (if (boundp 'org-latex-entities)
  4110. (mapcar (lambda (x)
  4111. (or (car-safe x) x))
  4112. org-latex-entities)
  4113. nil))
  4114. 'words))) ; FIXME
  4115. ))
  4116. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4117. (re-special (if org-export-with-special-strings
  4118. (mapcar (lambda (x) (car x))
  4119. org-export-html-special-string-regexps)))
  4120. (re-rest
  4121. (delq nil
  4122. (list
  4123. (if org-export-html-expand "@<[^>\n]+>")
  4124. ))))
  4125. (org-set-local
  4126. 'org-latex-and-specials-regexp
  4127. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4128. re-rest) "\\|")))))
  4129. (defun org-do-latex-and-special-faces (limit)
  4130. "Run through the buffer and add overlays to links."
  4131. (when org-latex-and-specials-regexp
  4132. (let (rtn d)
  4133. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4134. limit t))
  4135. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4136. 'face))
  4137. '(org-code org-verbatim underline)))
  4138. (progn
  4139. (setq rtn t
  4140. d (cond ((member (char-after (1+ (match-beginning 0)))
  4141. '(?_ ?^)) 1)
  4142. (t 0)))
  4143. (font-lock-prepend-text-property
  4144. (+ d (match-beginning 0)) (match-end 0)
  4145. 'face 'org-latex-and-export-specials)
  4146. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4147. '(font-lock-multiline t)))))
  4148. rtn)))
  4149. (defun org-restart-font-lock ()
  4150. "Restart font-lock-mode, to force refontification."
  4151. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4152. (font-lock-mode -1)
  4153. (font-lock-mode 1)))
  4154. (defun org-all-targets (&optional radio)
  4155. "Return a list of all targets in this file.
  4156. With optional argument RADIO, only find radio targets."
  4157. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4158. rtn)
  4159. (save-excursion
  4160. (goto-char (point-min))
  4161. (while (re-search-forward re nil t)
  4162. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4163. rtn)))
  4164. (defun org-make-target-link-regexp (targets)
  4165. "Make regular expression matching all strings in TARGETS.
  4166. The regular expression finds the targets also if there is a line break
  4167. between words."
  4168. (and targets
  4169. (concat
  4170. "\\<\\("
  4171. (mapconcat
  4172. (lambda (x)
  4173. (while (string-match " +" x)
  4174. (setq x (replace-match "\\s-+" t t x)))
  4175. x)
  4176. targets
  4177. "\\|")
  4178. "\\)\\>")))
  4179. (defun org-activate-tags (limit)
  4180. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4181. (progn
  4182. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4183. (add-text-properties (match-beginning 1) (match-end 1)
  4184. (list 'mouse-face 'highlight
  4185. 'keymap org-mouse-map))
  4186. (org-rear-nonsticky-at (match-end 1))
  4187. t)))
  4188. (defun org-outline-level ()
  4189. "Compute the outline level of the heading at point.
  4190. This function assumes that the cursor is at the beginning of a line matched
  4191. by outline-regexp. Otherwise it returns garbage.
  4192. If this is called at a normal headline, the level is the number of stars.
  4193. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4194. For plain list items, if they are matched by `outline-regexp', this returns
  4195. 1000 plus the line indentation."
  4196. (save-excursion
  4197. (looking-at outline-regexp)
  4198. (if (match-beginning 1)
  4199. (+ (org-get-string-indentation (match-string 1)) 1000)
  4200. (1- (- (match-end 0) (match-beginning 0))))))
  4201. (defvar org-font-lock-keywords nil)
  4202. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4203. "Regular expression matching a property line.")
  4204. (defvar org-font-lock-hook nil
  4205. "Functions to be called for special font lock stuff.")
  4206. (defun org-font-lock-hook (limit)
  4207. (run-hook-with-args 'org-font-lock-hook limit))
  4208. (defun org-set-font-lock-defaults ()
  4209. (let* ((em org-fontify-emphasized-text)
  4210. (lk org-activate-links)
  4211. (org-font-lock-extra-keywords
  4212. (list
  4213. ;; Call the hook
  4214. '(org-font-lock-hook)
  4215. ;; Headlines
  4216. `(,(if org-fontify-whole-heading-line
  4217. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4218. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4219. (1 (org-get-level-face 1))
  4220. (2 (org-get-level-face 2))
  4221. (3 (org-get-level-face 3)))
  4222. ;; Table lines
  4223. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4224. (1 'org-table t))
  4225. ;; Table internals
  4226. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4227. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4228. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4229. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4230. ;; Drawers
  4231. (list org-drawer-regexp '(0 'org-special-keyword t))
  4232. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4233. ;; Properties
  4234. (list org-property-re
  4235. '(1 'org-special-keyword t)
  4236. '(3 'org-property-value t))
  4237. ;; Links
  4238. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4239. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4240. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4241. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4242. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4243. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4244. (if (memq 'footnote lk) '(org-activate-footnote-links
  4245. (2 'org-footnote t)))
  4246. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4247. '(org-hide-wide-columns (0 nil append))
  4248. ;; TODO lines
  4249. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4250. '(1 (org-get-todo-face 1) t))
  4251. ;; DONE
  4252. (if org-fontify-done-headline
  4253. (list (concat "^[*]+ +\\<\\("
  4254. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4255. "\\)\\(.*\\)")
  4256. '(2 'org-headline-done t))
  4257. nil)
  4258. ;; Priorities
  4259. '(org-font-lock-add-priority-faces)
  4260. ;; Tags
  4261. '(org-font-lock-add-tag-faces)
  4262. ;; Special keywords
  4263. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4264. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4265. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4266. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4267. ;; Emphasis
  4268. (if em
  4269. (if (featurep 'xemacs)
  4270. '(org-do-emphasis-faces (0 nil append))
  4271. '(org-do-emphasis-faces)))
  4272. ;; Checkboxes
  4273. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4274. 2 'org-checkbox prepend)
  4275. (if org-provide-checkbox-statistics
  4276. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4277. (0 (org-get-checkbox-statistics-face) t)))
  4278. ;; Description list items
  4279. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4280. 2 'bold prepend)
  4281. ;; ARCHIVEd headings
  4282. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4283. '(1 'org-archived prepend))
  4284. ;; Specials
  4285. '(org-do-latex-and-special-faces)
  4286. ;; Code
  4287. '(org-activate-code (1 'org-code t))
  4288. ;; COMMENT
  4289. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4290. "\\|" org-quote-string "\\)\\>")
  4291. '(1 'org-special-keyword t))
  4292. '("^#.*" (0 'font-lock-comment-face t))
  4293. ;; Blocks and meta lines
  4294. '(org-fontify-meta-lines-and-blocks)
  4295. )))
  4296. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4297. ;; Now set the full font-lock-keywords
  4298. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4299. (org-set-local 'font-lock-defaults
  4300. '(org-font-lock-keywords t nil nil backward-paragraph))
  4301. (kill-local-variable 'font-lock-keywords) nil))
  4302. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4303. "Fontify string S like in Org-mode"
  4304. (with-temp-buffer
  4305. (insert s)
  4306. (let ((org-odd-levels-only odd-levels))
  4307. (org-mode)
  4308. (font-lock-fontify-buffer)
  4309. (buffer-string))))
  4310. (defvar org-m nil)
  4311. (defvar org-l nil)
  4312. (defvar org-f nil)
  4313. (defun org-get-level-face (n)
  4314. "Get the right face for match N in font-lock matching of headlines."
  4315. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4316. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4317. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4318. (cond
  4319. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4320. ((eq n 2) org-f)
  4321. (t (if org-level-color-stars-only nil org-f))))
  4322. (defun org-get-todo-face (kwd)
  4323. "Get the right face for a TODO keyword KWD.
  4324. If KWD is a number, get the corresponding match group."
  4325. (if (numberp kwd) (setq kwd (match-string kwd)))
  4326. (or (cdr (assoc kwd org-todo-keyword-faces))
  4327. (and (member kwd org-done-keywords) 'org-done)
  4328. 'org-todo))
  4329. (defun org-font-lock-add-tag-faces (limit)
  4330. "Add the special tag faces."
  4331. (when (and org-tag-faces org-tags-special-faces-re)
  4332. (while (re-search-forward org-tags-special-faces-re limit t)
  4333. (add-text-properties (match-beginning 1) (match-end 1)
  4334. (list 'face (org-get-tag-face 1)
  4335. 'font-lock-fontified t))
  4336. (backward-char 1))))
  4337. (defun org-font-lock-add-priority-faces (limit)
  4338. "Add the special priority faces."
  4339. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4340. (add-text-properties
  4341. (match-beginning 0) (match-end 0)
  4342. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4343. org-priority-faces))
  4344. 'org-special-keyword)
  4345. 'font-lock-fontified t))))
  4346. (defun org-get-tag-face (kwd)
  4347. "Get the right face for a TODO keyword KWD.
  4348. If KWD is a number, get the corresponding match group."
  4349. (if (numberp kwd) (setq kwd (match-string kwd)))
  4350. (or (cdr (assoc kwd org-tag-faces))
  4351. 'org-tag))
  4352. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4353. "Remove fontification and activation overlays from links."
  4354. (font-lock-default-unfontify-region beg end)
  4355. (let* ((buffer-undo-list t)
  4356. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4357. (inhibit-modification-hooks t)
  4358. deactivate-mark buffer-file-name buffer-file-truename)
  4359. (remove-text-properties beg end
  4360. '(mouse-face t keymap t org-linked-text t
  4361. invisible t intangible t
  4362. line-prefix t wrap-prefix t
  4363. org-no-flyspell t))))
  4364. ;;;; Visibility cycling, including org-goto and indirect buffer
  4365. ;;; Cycling
  4366. (defvar org-cycle-global-status nil)
  4367. (make-variable-buffer-local 'org-cycle-global-status)
  4368. (defvar org-cycle-subtree-status nil)
  4369. (make-variable-buffer-local 'org-cycle-subtree-status)
  4370. ;;;###autoload
  4371. (defvar org-inlinetask-min-level)
  4372. (defun org-cycle (&optional arg)
  4373. "TAB-action and visibility cycling for Org-mode.
  4374. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4375. is outine visibility cycling, but it also invokes other actions
  4376. in special contexts.
  4377. - When this function is called with a prefix argument, rotate the entire
  4378. buffer through 3 states (global cycling)
  4379. 1. OVERVIEW: Show only top-level headlines.
  4380. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4381. 3. SHOW ALL: Show everything.
  4382. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4383. determined by the variable `org-startup-folded', and by any VISIBILITY
  4384. properties in the buffer.
  4385. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4386. including any drawers.
  4387. - When inside a table, re-align the table and move to the next field.
  4388. - When point is at the beginning of a headline, rotate the subtree started
  4389. by this line through 3 different states (local cycling)
  4390. 1. FOLDED: Only the main headline is shown.
  4391. 2. CHILDREN: The main headline and the direct children are shown.
  4392. From this state, you can move to one of the children
  4393. and zoom in further.
  4394. 3. SUBTREE: Show the entire subtree, including body text.
  4395. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4396. - When there is a numeric prefix, go up to a heading with level ARG, do
  4397. a `show-subtree' and return to the previous cursor position. If ARG
  4398. is negative, go up that many levels.
  4399. - When point is not at the beginning of a headline, execute the global
  4400. binding for TAB, which is re-indenting the line. See the option
  4401. `org-cycle-emulate-tab' for details.
  4402. - Special case: if point is at the beginning of the buffer and there is
  4403. no headline in line 1, this function will act as if called with prefix arg.
  4404. But only if also the variable `org-cycle-global-at-bob' is t."
  4405. (interactive "P")
  4406. (org-load-modules-maybe)
  4407. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4408. (let* ((limit-level
  4409. (or org-cycle-max-level
  4410. (and (boundp 'org-inlinetask-min-level)
  4411. org-inlinetask-min-level
  4412. (1- org-inlinetask-min-level))))
  4413. (nstars (and limit-level
  4414. (if org-odd-levels-only
  4415. (and limit-level (1- (* limit-level 2)))
  4416. limit-level)))
  4417. (outline-regexp
  4418. (cond
  4419. ((not (org-mode-p)) outline-regexp)
  4420. ((or (eq org-cycle-include-plain-lists 'integrate)
  4421. (and org-cycle-include-plain-lists (org-at-item-p)))
  4422. (concat "\\(?:\\*"
  4423. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4424. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4425. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4426. (bob-special (and org-cycle-global-at-bob (bobp)
  4427. (not (looking-at outline-regexp))))
  4428. (org-cycle-hook
  4429. (if bob-special
  4430. (delq 'org-optimize-window-after-visibility-change
  4431. (copy-sequence org-cycle-hook))
  4432. org-cycle-hook))
  4433. (pos (point)))
  4434. (if (or bob-special (equal arg '(4)))
  4435. ;; special case: use global cycling
  4436. (setq arg t))
  4437. (cond
  4438. ((equal arg '(16))
  4439. (org-set-startup-visibility)
  4440. (message "Startup visibility, plus VISIBILITY properties"))
  4441. ((equal arg '(64))
  4442. (show-all)
  4443. (message "Entire buffer visible, including drawers"))
  4444. ((org-at-table-p 'any)
  4445. ;; Enter the table or move to the next field in the table
  4446. (or (org-table-recognize-table.el)
  4447. (progn
  4448. (if arg (org-table-edit-field t)
  4449. (org-table-justify-field-maybe)
  4450. (call-interactively 'org-table-next-field)))))
  4451. ((run-hook-with-args-until-success
  4452. 'org-tab-after-check-for-table-hook))
  4453. ((eq arg t) ;; Global cycling
  4454. (org-cycle-internal-global))
  4455. ((and org-drawers org-drawer-regexp
  4456. (save-excursion
  4457. (beginning-of-line 1)
  4458. (looking-at org-drawer-regexp)))
  4459. ;; Toggle block visibility
  4460. (org-flag-drawer
  4461. (not (get-char-property (match-end 0) 'invisible))))
  4462. ((integerp arg)
  4463. ;; Show-subtree, ARG levels up from here.
  4464. (save-excursion
  4465. (org-back-to-heading)
  4466. (outline-up-heading (if (< arg 0) (- arg)
  4467. (- (funcall outline-level) arg)))
  4468. (org-show-subtree)))
  4469. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4470. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4471. (org-cycle-internal-local))
  4472. ;; TAB emulation and template completion
  4473. (buffer-read-only (org-back-to-heading))
  4474. ((run-hook-with-args-until-success
  4475. 'org-tab-after-check-for-cycling-hook))
  4476. ((org-try-structure-completion))
  4477. ((org-try-cdlatex-tab))
  4478. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4479. (or (not (bolp))
  4480. (not (looking-at outline-regexp))))
  4481. (call-interactively (global-key-binding "\t")))
  4482. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4483. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4484. (or (and (eq org-cycle-emulate-tab 'white)
  4485. (= (match-end 0) (point-at-eol)))
  4486. (and (eq org-cycle-emulate-tab 'whitestart)
  4487. (>= (match-end 0) pos))))
  4488. t
  4489. (eq org-cycle-emulate-tab t))
  4490. (call-interactively (global-key-binding "\t")))
  4491. (t (save-excursion
  4492. (org-back-to-heading)
  4493. (org-cycle)))))))
  4494. (defun org-cycle-internal-global ()
  4495. "Do the global cycling action."
  4496. (cond
  4497. ((and (eq last-command this-command)
  4498. (eq org-cycle-global-status 'overview))
  4499. ;; We just created the overview - now do table of contents
  4500. ;; This can be slow in very large buffers, so indicate action
  4501. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4502. (message "CONTENTS...")
  4503. (org-content)
  4504. (message "CONTENTS...done")
  4505. (setq org-cycle-global-status 'contents)
  4506. (run-hook-with-args 'org-cycle-hook 'contents))
  4507. ((and (eq last-command this-command)
  4508. (eq org-cycle-global-status 'contents))
  4509. ;; We just showed the table of contents - now show everything
  4510. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4511. (show-all)
  4512. (message "SHOW ALL")
  4513. (setq org-cycle-global-status 'all)
  4514. (run-hook-with-args 'org-cycle-hook 'all))
  4515. (t
  4516. ;; Default action: go to overview
  4517. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4518. (org-overview)
  4519. (message "OVERVIEW")
  4520. (setq org-cycle-global-status 'overview)
  4521. (run-hook-with-args 'org-cycle-hook 'overview))))
  4522. (defun org-cycle-internal-local ()
  4523. "Do the local cycling action."
  4524. (org-back-to-heading)
  4525. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4526. ;; First, some boundaries
  4527. (save-excursion
  4528. (org-back-to-heading)
  4529. (setq level (funcall outline-level))
  4530. (save-excursion
  4531. (beginning-of-line 2)
  4532. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4533. ; XEmacs does not have `next-single-char-property-change'
  4534. ; I'm not sure about Emacs 21.
  4535. (while (and (not (eobp)) ;; this is like `next-line'
  4536. (get-char-property (1- (point)) 'invisible))
  4537. (beginning-of-line 2))
  4538. (while (and (not (eobp)) ;; this is like `next-line'
  4539. (get-char-property (1- (point)) 'invisible))
  4540. (goto-char (next-single-char-property-change (point) 'invisible))
  4541. ;;;??? (or (bolp) (beginning-of-line 2))))
  4542. (and (eolp) (beginning-of-line 2))))
  4543. (setq eol (point)))
  4544. (outline-end-of-heading) (setq eoh (point))
  4545. (save-excursion
  4546. (outline-next-heading)
  4547. (setq has-children (and (org-at-heading-p t)
  4548. (> (funcall outline-level) level))))
  4549. (org-end-of-subtree t)
  4550. (unless (eobp)
  4551. (skip-chars-forward " \t\n")
  4552. (beginning-of-line 1) ; in case this is an item
  4553. )
  4554. (setq eos (if (eobp) (point) (1- (point)))))
  4555. ;; Find out what to do next and set `this-command'
  4556. (cond
  4557. ((= eos eoh)
  4558. ;; Nothing is hidden behind this heading
  4559. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4560. (message "EMPTY ENTRY")
  4561. (setq org-cycle-subtree-status nil)
  4562. (save-excursion
  4563. (goto-char eos)
  4564. (outline-next-heading)
  4565. (if (org-invisible-p) (org-flag-heading nil))))
  4566. ((and (or (>= eol eos)
  4567. (not (string-match "\\S-" (buffer-substring eol eos))))
  4568. (or has-children
  4569. (not (setq children-skipped
  4570. org-cycle-skip-children-state-if-no-children))))
  4571. ;; Entire subtree is hidden in one line: children view
  4572. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4573. (org-show-entry)
  4574. (show-children)
  4575. (message "CHILDREN")
  4576. (save-excursion
  4577. (goto-char eos)
  4578. (outline-next-heading)
  4579. (if (org-invisible-p) (org-flag-heading nil)))
  4580. (setq org-cycle-subtree-status 'children)
  4581. (run-hook-with-args 'org-cycle-hook 'children))
  4582. ((or children-skipped
  4583. (and (eq last-command this-command)
  4584. (eq org-cycle-subtree-status 'children)))
  4585. ;; We just showed the children, or no children are there,
  4586. ;; now show everything.
  4587. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4588. (org-show-subtree)
  4589. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4590. (setq org-cycle-subtree-status 'subtree)
  4591. (run-hook-with-args 'org-cycle-hook 'subtree))
  4592. (t
  4593. ;; Default action: hide the subtree.
  4594. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4595. (hide-subtree)
  4596. (message "FOLDED")
  4597. (setq org-cycle-subtree-status 'folded)
  4598. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4599. ;;;###autoload
  4600. (defun org-global-cycle (&optional arg)
  4601. "Cycle the global visibility. For details see `org-cycle'.
  4602. With C-u prefix arg, switch to startup visibility.
  4603. With a numeric prefix, show all headlines up to that level."
  4604. (interactive "P")
  4605. (let ((org-cycle-include-plain-lists
  4606. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4607. (cond
  4608. ((integerp arg)
  4609. (show-all)
  4610. (hide-sublevels arg)
  4611. (setq org-cycle-global-status 'contents))
  4612. ((equal arg '(4))
  4613. (org-set-startup-visibility)
  4614. (message "Startup visibility, plus VISIBILITY properties."))
  4615. (t
  4616. (org-cycle '(4))))))
  4617. (defun org-set-startup-visibility ()
  4618. "Set the visibility required by startup options and properties."
  4619. (cond
  4620. ((eq org-startup-folded t)
  4621. (org-cycle '(4)))
  4622. ((eq org-startup-folded 'content)
  4623. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4624. (org-cycle '(4)) (org-cycle '(4)))))
  4625. (unless (eq org-startup-folded 'showeverything)
  4626. (if org-hide-block-startup (org-hide-block-all))
  4627. (org-set-visibility-according-to-property 'no-cleanup)
  4628. (org-cycle-hide-archived-subtrees 'all)
  4629. (org-cycle-hide-drawers 'all)
  4630. (org-cycle-show-empty-lines 'all)))
  4631. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4632. "Switch subtree visibilities according to :VISIBILITY: property."
  4633. (interactive)
  4634. (let (org-show-entry-below state)
  4635. (save-excursion
  4636. (goto-char (point-min))
  4637. (while (re-search-forward
  4638. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4639. nil t)
  4640. (setq state (match-string 1))
  4641. (save-excursion
  4642. (org-back-to-heading t)
  4643. (hide-subtree)
  4644. (org-reveal)
  4645. (cond
  4646. ((equal state '("fold" "folded"))
  4647. (hide-subtree))
  4648. ((equal state "children")
  4649. (org-show-hidden-entry)
  4650. (show-children))
  4651. ((equal state "content")
  4652. (save-excursion
  4653. (save-restriction
  4654. (org-narrow-to-subtree)
  4655. (org-content))))
  4656. ((member state '("all" "showall"))
  4657. (show-subtree)))))
  4658. (unless no-cleanup
  4659. (org-cycle-hide-archived-subtrees 'all)
  4660. (org-cycle-hide-drawers 'all)
  4661. (org-cycle-show-empty-lines 'all)))))
  4662. (defun org-overview ()
  4663. "Switch to overview mode, showing only top-level headlines.
  4664. Really, this shows all headlines with level equal or greater than the level
  4665. of the first headline in the buffer. This is important, because if the
  4666. first headline is not level one, then (hide-sublevels 1) gives confusing
  4667. results."
  4668. (interactive)
  4669. (let ((level (save-excursion
  4670. (goto-char (point-min))
  4671. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4672. (progn
  4673. (goto-char (match-beginning 0))
  4674. (funcall outline-level))))))
  4675. (and level (hide-sublevels level))))
  4676. (defun org-content (&optional arg)
  4677. "Show all headlines in the buffer, like a table of contents.
  4678. With numerical argument N, show content up to level N."
  4679. (interactive "P")
  4680. (save-excursion
  4681. ;; Visit all headings and show their offspring
  4682. (and (integerp arg) (org-overview))
  4683. (goto-char (point-max))
  4684. (catch 'exit
  4685. (while (and (progn (condition-case nil
  4686. (outline-previous-visible-heading 1)
  4687. (error (goto-char (point-min))))
  4688. t)
  4689. (looking-at outline-regexp))
  4690. (if (integerp arg)
  4691. (show-children (1- arg))
  4692. (show-branches))
  4693. (if (bobp) (throw 'exit nil))))))
  4694. (defun org-optimize-window-after-visibility-change (state)
  4695. "Adjust the window after a change in outline visibility.
  4696. This function is the default value of the hook `org-cycle-hook'."
  4697. (when (get-buffer-window (current-buffer))
  4698. (cond
  4699. ((eq state 'content) nil)
  4700. ((eq state 'all) nil)
  4701. ((eq state 'folded) nil)
  4702. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4703. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4704. ;; FIXME: no longer in use
  4705. (defun org-compact-display-after-subtree-move ()
  4706. "Show a compacter version of the tree of the entry's parent."
  4707. (save-excursion
  4708. (if (org-up-heading-safe)
  4709. (progn
  4710. (hide-subtree)
  4711. (show-entry)
  4712. (show-children)
  4713. (org-cycle-show-empty-lines 'children)
  4714. (org-cycle-hide-drawers 'children))
  4715. (org-overview))))
  4716. (defun org-remove-empty-overlays-at (pos)
  4717. "Remove outline overlays that do not contain non-white stuff."
  4718. (mapc
  4719. (lambda (o)
  4720. (and (eq 'outline (org-overlay-get o 'invisible))
  4721. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4722. (org-overlay-end o))))
  4723. (org-delete-overlay o)))
  4724. (org-overlays-at pos)))
  4725. (defun org-clean-visibility-after-subtree-move ()
  4726. "Fix visibility issues after moving a subtree."
  4727. ;; First, find a reasonable region to look at:
  4728. ;; Start two siblings above, end three below
  4729. (let* ((beg (save-excursion
  4730. (and (org-get-last-sibling)
  4731. (org-get-last-sibling))
  4732. (point)))
  4733. (end (save-excursion
  4734. (and (org-get-next-sibling)
  4735. (org-get-next-sibling)
  4736. (org-get-next-sibling))
  4737. (if (org-at-heading-p)
  4738. (point-at-eol)
  4739. (point))))
  4740. (level (looking-at "\\*+"))
  4741. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4742. (save-excursion
  4743. (save-restriction
  4744. (narrow-to-region beg end)
  4745. (when re
  4746. ;; Properly fold already folded siblings
  4747. (goto-char (point-min))
  4748. (while (re-search-forward re nil t)
  4749. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4750. (hide-entry))))
  4751. (org-cycle-show-empty-lines 'overview)
  4752. (org-cycle-hide-drawers 'overview)))))
  4753. (defun org-cycle-show-empty-lines (state)
  4754. "Show empty lines above all visible headlines.
  4755. The region to be covered depends on STATE when called through
  4756. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4757. entire buffer covered. Note that an empty line is only shown if there
  4758. are at least `org-cycle-separator-lines' empty lines before the headline."
  4759. (when (not (= org-cycle-separator-lines 0))
  4760. (save-excursion
  4761. (let* ((n (abs org-cycle-separator-lines))
  4762. (re (cond
  4763. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4764. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4765. (t (let ((ns (number-to-string (- n 2))))
  4766. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4767. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4768. beg end b e)
  4769. (cond
  4770. ((memq state '(overview contents t))
  4771. (setq beg (point-min) end (point-max)))
  4772. ((memq state '(children folded))
  4773. (setq beg (point) end (progn (org-end-of-subtree t t)
  4774. (beginning-of-line 2)
  4775. (point)))))
  4776. (when beg
  4777. (goto-char beg)
  4778. (while (re-search-forward re end t)
  4779. (unless (get-char-property (match-end 1) 'invisible)
  4780. (setq e (match-end 1))
  4781. (if (< org-cycle-separator-lines 0)
  4782. (setq b (save-excursion
  4783. (goto-char (match-beginning 0))
  4784. (org-back-over-empty-lines)
  4785. (if (save-excursion
  4786. (goto-char (max (point-min) (1- (point))))
  4787. (org-on-heading-p))
  4788. (1- (point))
  4789. (point))))
  4790. (setq b (match-beginning 1)))
  4791. (outline-flag-region b e nil)))))))
  4792. ;; Never hide empty lines at the end of the file.
  4793. (save-excursion
  4794. (goto-char (point-max))
  4795. (outline-previous-heading)
  4796. (outline-end-of-heading)
  4797. (if (and (looking-at "[ \t\n]+")
  4798. (= (match-end 0) (point-max)))
  4799. (outline-flag-region (point) (match-end 0) nil))))
  4800. (defun org-show-empty-lines-in-parent ()
  4801. "Move to the parent and re-show empty lines before visible headlines."
  4802. (save-excursion
  4803. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4804. (org-cycle-show-empty-lines context))))
  4805. (defun org-files-list ()
  4806. "Return `org-agenda-files' list, plus all open org-mode files.
  4807. This is useful for operations that need to scan all of a user's
  4808. open and agenda-wise Org files."
  4809. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  4810. (dolist (buf (buffer-list))
  4811. (with-current-buffer buf
  4812. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  4813. (let ((file (expand-file-name (buffer-file-name))))
  4814. (unless (member file files)
  4815. (push file files))))))
  4816. files))
  4817. (defsubst org-entry-beginning-position ()
  4818. "Return the beginning position of the current entry."
  4819. (save-excursion (outline-back-to-heading t) (point)))
  4820. (defsubst org-entry-end-position ()
  4821. "Return the end position of the current entry."
  4822. (save-excursion (outline-next-heading) (point)))
  4823. (defun org-cycle-hide-drawers (state)
  4824. "Re-hide all drawers after a visibility state change."
  4825. (when (and (org-mode-p)
  4826. (not (memq state '(overview folded contents))))
  4827. (save-excursion
  4828. (let* ((globalp (memq state '(contents all)))
  4829. (beg (if globalp (point-min) (point)))
  4830. (end (if globalp (point-max)
  4831. (if (eq state 'children)
  4832. (save-excursion (outline-next-heading) (point))
  4833. (org-end-of-subtree t)))))
  4834. (goto-char beg)
  4835. (while (re-search-forward org-drawer-regexp end t)
  4836. (org-flag-drawer t))))))
  4837. (defun org-flag-drawer (flag)
  4838. (save-excursion
  4839. (beginning-of-line 1)
  4840. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4841. (let ((b (match-end 0))
  4842. (outline-regexp org-outline-regexp))
  4843. (if (re-search-forward
  4844. "^[ \t]*:END:"
  4845. (save-excursion (outline-next-heading) (point)) t)
  4846. (outline-flag-region b (point-at-eol) flag)
  4847. (error ":END: line missing at position %s" b))))))
  4848. (defun org-subtree-end-visible-p ()
  4849. "Is the end of the current subtree visible?"
  4850. (pos-visible-in-window-p
  4851. (save-excursion (org-end-of-subtree t) (point))))
  4852. (defun org-first-headline-recenter (&optional N)
  4853. "Move cursor to the first headline and recenter the headline.
  4854. Optional argument N means, put the headline into the Nth line of the window."
  4855. (goto-char (point-min))
  4856. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4857. (beginning-of-line)
  4858. (recenter (prefix-numeric-value N))))
  4859. ;;; Folding of blocks
  4860. (defconst org-block-regexp
  4861. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4862. "Regular expression for hiding blocks.")
  4863. (defvar org-hide-block-overlays nil
  4864. "Overays hiding blocks.")
  4865. (make-variable-buffer-local 'org-hide-block-overlays)
  4866. (defun org-block-map (function &optional start end)
  4867. "Call func at the head of all source blocks in the current
  4868. buffer. Optional arguments START and END can be used to limit
  4869. the range."
  4870. (let ((start (or start (point-min)))
  4871. (end (or end (point-max))))
  4872. (save-excursion
  4873. (goto-char start)
  4874. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4875. (save-excursion
  4876. (save-match-data
  4877. (goto-char (match-beginning 0))
  4878. (funcall function)))))))
  4879. (defun org-hide-block-toggle-all ()
  4880. "Toggle the visibility of all blocks in the current buffer."
  4881. (org-block-map #'org-hide-block-toggle))
  4882. (defun org-hide-block-all ()
  4883. "Fold all blocks in the current buffer."
  4884. (interactive)
  4885. (org-show-block-all)
  4886. (org-block-map #'org-hide-block-toggle-maybe))
  4887. (defun org-show-block-all ()
  4888. "Unfold all blocks in the current buffer."
  4889. (mapc 'org-delete-overlay org-hide-block-overlays)
  4890. (setq org-hide-block-overlays nil))
  4891. (defun org-hide-block-toggle-maybe ()
  4892. "Toggle visibility of block at point."
  4893. (interactive)
  4894. (let ((case-fold-search t))
  4895. (if (save-excursion
  4896. (beginning-of-line 1)
  4897. (looking-at org-block-regexp))
  4898. (progn (org-hide-block-toggle)
  4899. t) ;; to signal that we took action
  4900. nil))) ;; to signal that we did not
  4901. (defun org-hide-block-toggle (&optional force)
  4902. "Toggle the visibility of the current block."
  4903. (interactive)
  4904. (save-excursion
  4905. (beginning-of-line)
  4906. (if (re-search-forward org-block-regexp nil t)
  4907. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4908. (end (match-end 0)) ;; end of entire body
  4909. ov)
  4910. (if (memq t (mapcar (lambda (overlay)
  4911. (eq (org-overlay-get overlay 'invisible)
  4912. 'org-hide-block))
  4913. (org-overlays-at start)))
  4914. (if (or (not force) (eq force 'off))
  4915. (mapc (lambda (ov)
  4916. (when (member ov org-hide-block-overlays)
  4917. (setq org-hide-block-overlays
  4918. (delq ov org-hide-block-overlays)))
  4919. (when (eq (org-overlay-get ov 'invisible)
  4920. 'org-hide-block)
  4921. (org-delete-overlay ov)))
  4922. (org-overlays-at start)))
  4923. (setq ov (org-make-overlay start end))
  4924. (org-overlay-put ov 'invisible 'org-hide-block)
  4925. ;; make the block accessible to isearch
  4926. (org-overlay-put
  4927. ov 'isearch-open-invisible
  4928. (lambda (ov)
  4929. (when (member ov org-hide-block-overlays)
  4930. (setq org-hide-block-overlays
  4931. (delq ov org-hide-block-overlays)))
  4932. (when (eq (org-overlay-get ov 'invisible)
  4933. 'org-hide-block)
  4934. (org-delete-overlay ov))))
  4935. (push ov org-hide-block-overlays)))
  4936. (error "Not looking at a source block"))))
  4937. ;; org-tab-after-check-for-cycling-hook
  4938. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4939. ;; Remove overlays when changing major mode
  4940. (add-hook 'org-mode-hook
  4941. (lambda () (org-add-hook 'change-major-mode-hook
  4942. 'org-show-block-all 'append 'local)))
  4943. ;;; Org-goto
  4944. (defvar org-goto-window-configuration nil)
  4945. (defvar org-goto-marker nil)
  4946. (defvar org-goto-map
  4947. (let ((map (make-sparse-keymap)))
  4948. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4949. (while (setq cmd (pop cmds))
  4950. (substitute-key-definition cmd cmd map global-map)))
  4951. (suppress-keymap map)
  4952. (org-defkey map "\C-m" 'org-goto-ret)
  4953. (org-defkey map [(return)] 'org-goto-ret)
  4954. (org-defkey map [(left)] 'org-goto-left)
  4955. (org-defkey map [(right)] 'org-goto-right)
  4956. (org-defkey map [(control ?g)] 'org-goto-quit)
  4957. (org-defkey map "\C-i" 'org-cycle)
  4958. (org-defkey map [(tab)] 'org-cycle)
  4959. (org-defkey map [(down)] 'outline-next-visible-heading)
  4960. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4961. (if org-goto-auto-isearch
  4962. (if (fboundp 'define-key-after)
  4963. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4964. nil)
  4965. (org-defkey map "q" 'org-goto-quit)
  4966. (org-defkey map "n" 'outline-next-visible-heading)
  4967. (org-defkey map "p" 'outline-previous-visible-heading)
  4968. (org-defkey map "f" 'outline-forward-same-level)
  4969. (org-defkey map "b" 'outline-backward-same-level)
  4970. (org-defkey map "u" 'outline-up-heading))
  4971. (org-defkey map "/" 'org-occur)
  4972. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4973. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4974. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4975. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4976. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4977. map))
  4978. (defconst org-goto-help
  4979. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4980. RET=jump to location [Q]uit and return to previous location
  4981. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4982. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4983. ;; FIXME: Docstring doe not mention both interfaces
  4984. (defun org-goto (&optional alternative-interface)
  4985. "Look up a different location in the current file, keeping current visibility.
  4986. When you want look-up or go to a different location in a document, the
  4987. fastest way is often to fold the entire buffer and then dive into the tree.
  4988. This method has the disadvantage, that the previous location will be folded,
  4989. which may not be what you want.
  4990. This command works around this by showing a copy of the current buffer
  4991. in an indirect buffer, in overview mode. You can dive into the tree in
  4992. that copy, use org-occur and incremental search to find a location.
  4993. When pressing RET or `Q', the command returns to the original buffer in
  4994. which the visibility is still unchanged. After RET is will also jump to
  4995. the location selected in the indirect buffer and expose the
  4996. the headline hierarchy above."
  4997. (interactive "P")
  4998. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4999. (org-refile-use-outline-path t)
  5000. (org-refile-target-verify-function nil)
  5001. (interface
  5002. (if (not alternative-interface)
  5003. org-goto-interface
  5004. (if (eq org-goto-interface 'outline)
  5005. 'outline-path-completion
  5006. 'outline)))
  5007. (org-goto-start-pos (point))
  5008. (selected-point
  5009. (if (eq interface 'outline)
  5010. (car (org-get-location (current-buffer) org-goto-help))
  5011. (nth 3 (org-refile-get-location "Goto: ")))))
  5012. (if selected-point
  5013. (progn
  5014. (org-mark-ring-push org-goto-start-pos)
  5015. (goto-char selected-point)
  5016. (if (or (org-invisible-p) (org-invisible-p2))
  5017. (org-show-context 'org-goto)))
  5018. (message "Quit"))))
  5019. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5020. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5021. (defvar org-goto-local-auto-isearch-map) ; defined below
  5022. (defun org-get-location (buf help)
  5023. "Let the user select a location in the Org-mode buffer BUF.
  5024. This function uses a recursive edit. It returns the selected position
  5025. or nil."
  5026. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5027. (isearch-hide-immediately nil)
  5028. (isearch-search-fun-function
  5029. (lambda () 'org-goto-local-search-headings))
  5030. (org-goto-selected-point org-goto-exit-command))
  5031. (save-excursion
  5032. (save-window-excursion
  5033. (delete-other-windows)
  5034. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5035. (switch-to-buffer
  5036. (condition-case nil
  5037. (make-indirect-buffer (current-buffer) "*org-goto*")
  5038. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5039. (with-output-to-temp-buffer "*Help*"
  5040. (princ help))
  5041. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5042. (setq buffer-read-only nil)
  5043. (let ((org-startup-truncated t)
  5044. (org-startup-folded nil)
  5045. (org-startup-align-all-tables nil))
  5046. (org-mode)
  5047. (org-overview))
  5048. (setq buffer-read-only t)
  5049. (if (and (boundp 'org-goto-start-pos)
  5050. (integer-or-marker-p org-goto-start-pos))
  5051. (let ((org-show-hierarchy-above t)
  5052. (org-show-siblings t)
  5053. (org-show-following-heading t))
  5054. (goto-char org-goto-start-pos)
  5055. (and (org-invisible-p) (org-show-context)))
  5056. (goto-char (point-min)))
  5057. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5058. (message "Select location and press RET")
  5059. (use-local-map org-goto-map)
  5060. (recursive-edit)
  5061. ))
  5062. (kill-buffer "*org-goto*")
  5063. (cons org-goto-selected-point org-goto-exit-command)))
  5064. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5065. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5066. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5067. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5068. (defun org-goto-local-search-headings (string bound noerror)
  5069. "Search and make sure that any matches are in headlines."
  5070. (catch 'return
  5071. (while (if isearch-forward
  5072. (search-forward string bound noerror)
  5073. (search-backward string bound noerror))
  5074. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5075. (and (member :headline context)
  5076. (not (member :tags context))))
  5077. (throw 'return (point))))))
  5078. (defun org-goto-local-auto-isearch ()
  5079. "Start isearch."
  5080. (interactive)
  5081. (goto-char (point-min))
  5082. (let ((keys (this-command-keys)))
  5083. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5084. (isearch-mode t)
  5085. (isearch-process-search-char (string-to-char keys)))))
  5086. (defun org-goto-ret (&optional arg)
  5087. "Finish `org-goto' by going to the new location."
  5088. (interactive "P")
  5089. (setq org-goto-selected-point (point)
  5090. org-goto-exit-command 'return)
  5091. (throw 'exit nil))
  5092. (defun org-goto-left ()
  5093. "Finish `org-goto' by going to the new location."
  5094. (interactive)
  5095. (if (org-on-heading-p)
  5096. (progn
  5097. (beginning-of-line 1)
  5098. (setq org-goto-selected-point (point)
  5099. org-goto-exit-command 'left)
  5100. (throw 'exit nil))
  5101. (error "Not on a heading")))
  5102. (defun org-goto-right ()
  5103. "Finish `org-goto' by going to the new location."
  5104. (interactive)
  5105. (if (org-on-heading-p)
  5106. (progn
  5107. (setq org-goto-selected-point (point)
  5108. org-goto-exit-command 'right)
  5109. (throw 'exit nil))
  5110. (error "Not on a heading")))
  5111. (defun org-goto-quit ()
  5112. "Finish `org-goto' without cursor motion."
  5113. (interactive)
  5114. (setq org-goto-selected-point nil)
  5115. (setq org-goto-exit-command 'quit)
  5116. (throw 'exit nil))
  5117. ;;; Indirect buffer display of subtrees
  5118. (defvar org-indirect-dedicated-frame nil
  5119. "This is the frame being used for indirect tree display.")
  5120. (defvar org-last-indirect-buffer nil)
  5121. (defun org-tree-to-indirect-buffer (&optional arg)
  5122. "Create indirect buffer and narrow it to current subtree.
  5123. With numerical prefix ARG, go up to this level and then take that tree.
  5124. If ARG is negative, go up that many levels.
  5125. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5126. indirect buffer previously made with this command, to avoid proliferation of
  5127. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5128. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5129. is kept so that you can work with several indirect buffers at the same time.
  5130. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5131. requests that a new frame be made for the new buffer, so that the dedicated
  5132. frame is not changed."
  5133. (interactive "P")
  5134. (let ((cbuf (current-buffer))
  5135. (cwin (selected-window))
  5136. (pos (point))
  5137. beg end level heading ibuf)
  5138. (save-excursion
  5139. (org-back-to-heading t)
  5140. (when (numberp arg)
  5141. (setq level (org-outline-level))
  5142. (if (< arg 0) (setq arg (+ level arg)))
  5143. (while (> (setq level (org-outline-level)) arg)
  5144. (outline-up-heading 1 t)))
  5145. (setq beg (point)
  5146. heading (org-get-heading))
  5147. (org-end-of-subtree t t) (setq end (point)))
  5148. (if (and (buffer-live-p org-last-indirect-buffer)
  5149. (not (eq org-indirect-buffer-display 'new-frame))
  5150. (not arg))
  5151. (kill-buffer org-last-indirect-buffer))
  5152. (setq ibuf (org-get-indirect-buffer cbuf)
  5153. org-last-indirect-buffer ibuf)
  5154. (cond
  5155. ((or (eq org-indirect-buffer-display 'new-frame)
  5156. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5157. (select-frame (make-frame))
  5158. (delete-other-windows)
  5159. (switch-to-buffer ibuf)
  5160. (org-set-frame-title heading))
  5161. ((eq org-indirect-buffer-display 'dedicated-frame)
  5162. (raise-frame
  5163. (select-frame (or (and org-indirect-dedicated-frame
  5164. (frame-live-p org-indirect-dedicated-frame)
  5165. org-indirect-dedicated-frame)
  5166. (setq org-indirect-dedicated-frame (make-frame)))))
  5167. (delete-other-windows)
  5168. (switch-to-buffer ibuf)
  5169. (org-set-frame-title (concat "Indirect: " heading)))
  5170. ((eq org-indirect-buffer-display 'current-window)
  5171. (switch-to-buffer ibuf))
  5172. ((eq org-indirect-buffer-display 'other-window)
  5173. (pop-to-buffer ibuf))
  5174. (t (error "Invalid value")))
  5175. (if (featurep 'xemacs)
  5176. (save-excursion (org-mode) (turn-on-font-lock)))
  5177. (narrow-to-region beg end)
  5178. (show-all)
  5179. (goto-char pos)
  5180. (and (window-live-p cwin) (select-window cwin))))
  5181. (defun org-get-indirect-buffer (&optional buffer)
  5182. (setq buffer (or buffer (current-buffer)))
  5183. (let ((n 1) (base (buffer-name buffer)) bname)
  5184. (while (buffer-live-p
  5185. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5186. (setq n (1+ n)))
  5187. (condition-case nil
  5188. (make-indirect-buffer buffer bname 'clone)
  5189. (error (make-indirect-buffer buffer bname)))))
  5190. (defun org-set-frame-title (title)
  5191. "Set the title of the current frame to the string TITLE."
  5192. ;; FIXME: how to name a single frame in XEmacs???
  5193. (unless (featurep 'xemacs)
  5194. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5195. ;;;; Structure editing
  5196. ;;; Inserting headlines
  5197. (defun org-previous-line-empty-p ()
  5198. (save-excursion
  5199. (and (not (bobp))
  5200. (or (beginning-of-line 0) t)
  5201. (save-match-data
  5202. (looking-at "[ \t]*$")))))
  5203. (defun org-insert-heading (&optional force-heading)
  5204. "Insert a new heading or item with same depth at point.
  5205. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5206. If point is at the beginning of a headline, insert a sibling before the
  5207. current headline. If point is not at the beginning, do not split the line,
  5208. but create the new headline after the current line."
  5209. (interactive "P")
  5210. (if (= (buffer-size) 0)
  5211. (insert "\n* ")
  5212. (when (or force-heading (not (org-insert-item)))
  5213. (let* ((empty-line-p nil)
  5214. (head (save-excursion
  5215. (condition-case nil
  5216. (progn
  5217. (org-back-to-heading)
  5218. (setq empty-line-p (org-previous-line-empty-p))
  5219. (match-string 0))
  5220. (error "*"))))
  5221. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5222. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5223. pos hide-previous previous-pos)
  5224. (cond
  5225. ((and (org-on-heading-p) (bolp)
  5226. (or (bobp)
  5227. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5228. ;; insert before the current line
  5229. (open-line (if blank 2 1)))
  5230. ((and (bolp)
  5231. (not org-insert-heading-respect-content)
  5232. (or (bobp)
  5233. (save-excursion
  5234. (backward-char 1) (not (org-invisible-p)))))
  5235. ;; insert right here
  5236. nil)
  5237. (t
  5238. ;; somewhere in the line
  5239. (save-excursion
  5240. (setq previous-pos (point-at-bol))
  5241. (end-of-line)
  5242. (setq hide-previous (org-invisible-p)))
  5243. (and org-insert-heading-respect-content (org-show-subtree))
  5244. (let ((split
  5245. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5246. (save-excursion
  5247. (let ((p (point)))
  5248. (goto-char (point-at-bol))
  5249. (and (looking-at org-complex-heading-regexp)
  5250. (> p (match-beginning 4)))))))
  5251. tags pos)
  5252. (cond
  5253. (org-insert-heading-respect-content
  5254. (org-end-of-subtree nil t)
  5255. (or (bolp) (newline))
  5256. (or (org-previous-line-empty-p)
  5257. (and blank (newline)))
  5258. (open-line 1))
  5259. ((org-on-heading-p)
  5260. (when hide-previous
  5261. (show-children)
  5262. (org-show-entry))
  5263. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5264. (setq tags (and (match-end 2) (match-string 2)))
  5265. (and (match-end 1)
  5266. (delete-region (match-beginning 1) (match-end 1)))
  5267. (setq pos (point-at-bol))
  5268. (or split (end-of-line 1))
  5269. (delete-horizontal-space)
  5270. (newline (if blank 2 1))
  5271. (when tags
  5272. (save-excursion
  5273. (goto-char pos)
  5274. (end-of-line 1)
  5275. (insert " " tags)
  5276. (org-set-tags nil 'align))))
  5277. (t
  5278. (or split (end-of-line 1))
  5279. (newline (if blank 2 1)))))))
  5280. (insert head) (just-one-space)
  5281. (setq pos (point))
  5282. (end-of-line 1)
  5283. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5284. (when (and org-insert-heading-respect-content hide-previous)
  5285. (save-excursion
  5286. (goto-char previous-pos)
  5287. (hide-subtree)))
  5288. (run-hooks 'org-insert-heading-hook)))))
  5289. (defun org-get-heading (&optional no-tags)
  5290. "Return the heading of the current entry, without the stars."
  5291. (save-excursion
  5292. (org-back-to-heading t)
  5293. (if (looking-at
  5294. (if no-tags
  5295. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5296. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5297. (match-string 1) "")))
  5298. (defun org-heading-components ()
  5299. "Return the components of the current heading.
  5300. This is a list with the following elements:
  5301. - the level as an integer
  5302. - the reduced level, different if `org-odd-levels-only' is set.
  5303. - the TODO keyword, or nil
  5304. - the priority character, like ?A, or nil if no priority is given
  5305. - the headline text itself, or the tags string if no headline text
  5306. - the tags string, or nil."
  5307. (save-excursion
  5308. (org-back-to-heading t)
  5309. (if (looking-at org-complex-heading-regexp)
  5310. (list (length (match-string 1))
  5311. (org-reduced-level (length (match-string 1)))
  5312. (org-match-string-no-properties 2)
  5313. (and (match-end 3) (aref (match-string 3) 2))
  5314. (org-match-string-no-properties 4)
  5315. (org-match-string-no-properties 5)))))
  5316. (defun org-get-entry ()
  5317. "Get the entry text, after heading, entire subtree."
  5318. (save-excursion
  5319. (org-back-to-heading t)
  5320. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5321. (defun org-insert-heading-after-current ()
  5322. "Insert a new heading with same level as current, after current subtree."
  5323. (interactive)
  5324. (org-back-to-heading)
  5325. (org-insert-heading)
  5326. (org-move-subtree-down)
  5327. (end-of-line 1))
  5328. (defun org-insert-heading-respect-content ()
  5329. (interactive)
  5330. (let ((org-insert-heading-respect-content t))
  5331. (org-insert-heading t)))
  5332. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5333. (interactive "P")
  5334. (let ((org-insert-heading-respect-content t))
  5335. (org-insert-todo-heading force-state t)))
  5336. (defun org-insert-todo-heading (arg &optional force-heading)
  5337. "Insert a new heading with the same level and TODO state as current heading.
  5338. If the heading has no TODO state, or if the state is DONE, use the first
  5339. state (TODO by default). Also with prefix arg, force first state."
  5340. (interactive "P")
  5341. (when (or force-heading (not (org-insert-item 'checkbox)))
  5342. (org-insert-heading force-heading)
  5343. (save-excursion
  5344. (org-back-to-heading)
  5345. (outline-previous-heading)
  5346. (looking-at org-todo-line-regexp))
  5347. (let*
  5348. ((new-mark-x
  5349. (if (or arg
  5350. (not (match-beginning 2))
  5351. (member (match-string 2) org-done-keywords))
  5352. (car org-todo-keywords-1)
  5353. (match-string 2)))
  5354. (new-mark
  5355. (or
  5356. (run-hook-with-args-until-success
  5357. 'org-todo-get-default-hook new-mark-x nil)
  5358. new-mark-x)))
  5359. (beginning-of-line 1)
  5360. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5361. (if org-treat-insert-todo-heading-as-state-change
  5362. (org-todo new-mark)
  5363. (insert new-mark " "))))
  5364. (when org-provide-todo-statistics
  5365. (org-update-parent-todo-statistics))))
  5366. (defun org-insert-subheading (arg)
  5367. "Insert a new subheading and demote it.
  5368. Works for outline headings and for plain lists alike."
  5369. (interactive "P")
  5370. (org-insert-heading arg)
  5371. (cond
  5372. ((org-on-heading-p) (org-do-demote))
  5373. ((org-at-item-p) (org-indent-item 1))))
  5374. (defun org-insert-todo-subheading (arg)
  5375. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5376. Works for outline headings and for plain lists alike."
  5377. (interactive "P")
  5378. (org-insert-todo-heading arg)
  5379. (cond
  5380. ((org-on-heading-p) (org-do-demote))
  5381. ((org-at-item-p) (org-indent-item 1))))
  5382. ;;; Promotion and Demotion
  5383. (defvar org-after-demote-entry-hook nil
  5384. "Hook run after an entry has been demoted.
  5385. The cursor will be at the beginning of the entry.
  5386. When a subtree is being demoted, the hook will be called for each node.")
  5387. (defvar org-after-promote-entry-hook nil
  5388. "Hook run after an entry has been promoted.
  5389. The cursor will be at the beginning of the entry.
  5390. When a subtree is being promoted, the hook will be called for each node.")
  5391. (defun org-promote-subtree ()
  5392. "Promote the entire subtree.
  5393. See also `org-promote'."
  5394. (interactive)
  5395. (save-excursion
  5396. (org-map-tree 'org-promote))
  5397. (org-fix-position-after-promote))
  5398. (defun org-demote-subtree ()
  5399. "Demote the entire subtree. See `org-demote'.
  5400. See also `org-promote'."
  5401. (interactive)
  5402. (save-excursion
  5403. (org-map-tree 'org-demote))
  5404. (org-fix-position-after-promote))
  5405. (defun org-do-promote ()
  5406. "Promote the current heading higher up the tree.
  5407. If the region is active in `transient-mark-mode', promote all headings
  5408. in the region."
  5409. (interactive)
  5410. (save-excursion
  5411. (if (org-region-active-p)
  5412. (org-map-region 'org-promote (region-beginning) (region-end))
  5413. (org-promote)))
  5414. (org-fix-position-after-promote))
  5415. (defun org-do-demote ()
  5416. "Demote the current heading lower down the tree.
  5417. If the region is active in `transient-mark-mode', demote all headings
  5418. in the region."
  5419. (interactive)
  5420. (save-excursion
  5421. (if (org-region-active-p)
  5422. (org-map-region 'org-demote (region-beginning) (region-end))
  5423. (org-demote)))
  5424. (org-fix-position-after-promote))
  5425. (defun org-fix-position-after-promote ()
  5426. "Make sure that after pro/demotion cursor position is right."
  5427. (let ((pos (point)))
  5428. (when (save-excursion
  5429. (beginning-of-line 1)
  5430. (looking-at org-todo-line-regexp)
  5431. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5432. (cond ((eobp) (insert " "))
  5433. ((eolp) (insert " "))
  5434. ((equal (char-after) ?\ ) (forward-char 1))))))
  5435. (defun org-reduced-level (l)
  5436. "Compute the effective level of a heading.
  5437. This takes into account the setting of `org-odd-levels-only'."
  5438. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5439. (defun org-get-valid-level (level &optional change)
  5440. "Rectify a level change under the influence of `org-odd-levels-only'
  5441. LEVEL is a current level, CHANGE is by how much the level should be
  5442. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5443. even level numbers will become the next higher odd number."
  5444. (if org-odd-levels-only
  5445. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5446. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5447. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5448. (max 1 (+ level (or change 0)))))
  5449. (if (boundp 'define-obsolete-function-alias)
  5450. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5451. (define-obsolete-function-alias 'org-get-legal-level
  5452. 'org-get-valid-level)
  5453. (define-obsolete-function-alias 'org-get-legal-level
  5454. 'org-get-valid-level "23.1")))
  5455. (defun org-promote ()
  5456. "Promote the current heading higher up the tree.
  5457. If the region is active in `transient-mark-mode', promote all headings
  5458. in the region."
  5459. (org-back-to-heading t)
  5460. (let* ((level (save-match-data (funcall outline-level)))
  5461. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5462. (diff (abs (- level (length up-head) -1))))
  5463. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5464. (replace-match up-head nil t)
  5465. ;; Fixup tag positioning
  5466. (and org-auto-align-tags (org-set-tags nil t))
  5467. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5468. (run-hooks 'org-after-promote-entry-hook)))
  5469. (defun org-demote ()
  5470. "Demote the current heading lower down the tree.
  5471. If the region is active in `transient-mark-mode', demote all headings
  5472. in the region."
  5473. (org-back-to-heading t)
  5474. (let* ((level (save-match-data (funcall outline-level)))
  5475. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5476. (diff (abs (- level (length down-head) -1))))
  5477. (replace-match down-head nil t)
  5478. ;; Fixup tag positioning
  5479. (and org-auto-align-tags (org-set-tags nil t))
  5480. (if org-adapt-indentation (org-fixup-indentation diff))
  5481. (run-hooks 'org-after-demote-entry-hook)))
  5482. (defun org-map-tree (fun)
  5483. "Call FUN for every heading underneath the current one."
  5484. (org-back-to-heading)
  5485. (let ((level (funcall outline-level)))
  5486. (save-excursion
  5487. (funcall fun)
  5488. (while (and (progn
  5489. (outline-next-heading)
  5490. (> (funcall outline-level) level))
  5491. (not (eobp)))
  5492. (funcall fun)))))
  5493. (defun org-map-region (fun beg end)
  5494. "Call FUN for every heading between BEG and END."
  5495. (let ((org-ignore-region t))
  5496. (save-excursion
  5497. (setq end (copy-marker end))
  5498. (goto-char beg)
  5499. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5500. (< (point) end))
  5501. (funcall fun))
  5502. (while (and (progn
  5503. (outline-next-heading)
  5504. (< (point) end))
  5505. (not (eobp)))
  5506. (funcall fun)))))
  5507. (defun org-fixup-indentation (diff)
  5508. "Change the indentation in the current entry by DIFF
  5509. However, if any line in the current entry has no indentation, or if it
  5510. would end up with no indentation after the change, nothing at all is done."
  5511. (save-excursion
  5512. (let ((end (save-excursion (outline-next-heading)
  5513. (point-marker)))
  5514. (prohibit (if (> diff 0)
  5515. "^\\S-"
  5516. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5517. col)
  5518. (unless (save-excursion (end-of-line 1)
  5519. (re-search-forward prohibit end t))
  5520. (while (and (< (point) end)
  5521. (re-search-forward "^[ \t]+" end t))
  5522. (goto-char (match-end 0))
  5523. (setq col (current-column))
  5524. (if (< diff 0) (replace-match ""))
  5525. (org-indent-to-column (+ diff col))))
  5526. (move-marker end nil))))
  5527. (defun org-convert-to-odd-levels ()
  5528. "Convert an org-mode file with all levels allowed to one with odd levels.
  5529. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5530. level 5 etc."
  5531. (interactive)
  5532. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5533. (let ((outline-regexp org-outline-regexp)
  5534. (outline-level 'org-outline-level)
  5535. (org-odd-levels-only nil) n)
  5536. (save-excursion
  5537. (goto-char (point-min))
  5538. (while (re-search-forward "^\\*\\*+ " nil t)
  5539. (setq n (- (length (match-string 0)) 2))
  5540. (while (>= (setq n (1- n)) 0)
  5541. (org-demote))
  5542. (end-of-line 1))))))
  5543. (defun org-convert-to-oddeven-levels ()
  5544. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5545. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5546. section with an even level, conversion would destroy the structure of the file. An error
  5547. is signaled in this case."
  5548. (interactive)
  5549. (goto-char (point-min))
  5550. ;; First check if there are no even levels
  5551. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5552. (org-show-context t)
  5553. (error "Not all levels are odd in this file. Conversion not possible"))
  5554. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5555. (let ((outline-regexp org-outline-regexp)
  5556. (outline-level 'org-outline-level)
  5557. (org-odd-levels-only nil) n)
  5558. (save-excursion
  5559. (goto-char (point-min))
  5560. (while (re-search-forward "^\\*\\*+ " nil t)
  5561. (setq n (/ (1- (length (match-string 0))) 2))
  5562. (while (>= (setq n (1- n)) 0)
  5563. (org-promote))
  5564. (end-of-line 1))))))
  5565. (defun org-tr-level (n)
  5566. "Make N odd if required."
  5567. (if org-odd-levels-only (1+ (/ n 2)) n))
  5568. ;;; Vertical tree motion, cutting and pasting of subtrees
  5569. (defun org-move-subtree-up (&optional arg)
  5570. "Move the current subtree up past ARG headlines of the same level."
  5571. (interactive "p")
  5572. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5573. (defun org-move-subtree-down (&optional arg)
  5574. "Move the current subtree down past ARG headlines of the same level."
  5575. (interactive "p")
  5576. (setq arg (prefix-numeric-value arg))
  5577. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5578. 'org-get-last-sibling))
  5579. (ins-point (make-marker))
  5580. (cnt (abs arg))
  5581. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5582. ;; Select the tree
  5583. (org-back-to-heading)
  5584. (setq beg0 (point))
  5585. (save-excursion
  5586. (setq ne-beg (org-back-over-empty-lines))
  5587. (setq beg (point)))
  5588. (save-match-data
  5589. (save-excursion (outline-end-of-heading)
  5590. (setq folded (org-invisible-p)))
  5591. (outline-end-of-subtree))
  5592. (outline-next-heading)
  5593. (setq ne-end (org-back-over-empty-lines))
  5594. (setq end (point))
  5595. (goto-char beg0)
  5596. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5597. ;; include less whitespace
  5598. (save-excursion
  5599. (goto-char beg)
  5600. (forward-line (- ne-beg ne-end))
  5601. (setq beg (point))))
  5602. ;; Find insertion point, with error handling
  5603. (while (> cnt 0)
  5604. (or (and (funcall movfunc) (looking-at outline-regexp))
  5605. (progn (goto-char beg0)
  5606. (error "Cannot move past superior level or buffer limit")))
  5607. (setq cnt (1- cnt)))
  5608. (if (> arg 0)
  5609. ;; Moving forward - still need to move over subtree
  5610. (progn (org-end-of-subtree t t)
  5611. (save-excursion
  5612. (org-back-over-empty-lines)
  5613. (or (bolp) (newline)))))
  5614. (setq ne-ins (org-back-over-empty-lines))
  5615. (move-marker ins-point (point))
  5616. (setq txt (buffer-substring beg end))
  5617. (org-save-markers-in-region beg end)
  5618. (delete-region beg end)
  5619. (org-remove-empty-overlays-at beg)
  5620. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5621. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5622. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5623. (let ((bbb (point)))
  5624. (insert-before-markers txt)
  5625. (org-reinstall-markers-in-region bbb)
  5626. (move-marker ins-point bbb))
  5627. (or (bolp) (insert "\n"))
  5628. (setq ins-end (point))
  5629. (goto-char ins-point)
  5630. (org-skip-whitespace)
  5631. (when (and (< arg 0)
  5632. (org-first-sibling-p)
  5633. (> ne-ins ne-beg))
  5634. ;; Move whitespace back to beginning
  5635. (save-excursion
  5636. (goto-char ins-end)
  5637. (let ((kill-whole-line t))
  5638. (kill-line (- ne-ins ne-beg)) (point)))
  5639. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5640. (move-marker ins-point nil)
  5641. (if folded
  5642. (hide-subtree)
  5643. (org-show-entry)
  5644. (show-children)
  5645. (org-cycle-hide-drawers 'children))
  5646. (org-clean-visibility-after-subtree-move)))
  5647. (defvar org-subtree-clip ""
  5648. "Clipboard for cut and paste of subtrees.
  5649. This is actually only a copy of the kill, because we use the normal kill
  5650. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5651. (defvar org-subtree-clip-folded nil
  5652. "Was the last copied subtree folded?
  5653. This is used to fold the tree back after pasting.")
  5654. (defun org-cut-subtree (&optional n)
  5655. "Cut the current subtree into the clipboard.
  5656. With prefix arg N, cut this many sequential subtrees.
  5657. This is a short-hand for marking the subtree and then cutting it."
  5658. (interactive "p")
  5659. (org-copy-subtree n 'cut))
  5660. (defun org-copy-subtree (&optional n cut force-store-markers)
  5661. "Cut the current subtree into the clipboard.
  5662. With prefix arg N, cut this many sequential subtrees.
  5663. This is a short-hand for marking the subtree and then copying it.
  5664. If CUT is non-nil, actually cut the subtree.
  5665. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5666. of some markers in the region, even if CUT is non-nil. This is
  5667. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5668. (interactive "p")
  5669. (let (beg end folded (beg0 (point)))
  5670. (if (interactive-p)
  5671. (org-back-to-heading nil) ; take what looks like a subtree
  5672. (org-back-to-heading t)) ; take what is really there
  5673. (org-back-over-empty-lines)
  5674. (setq beg (point))
  5675. (skip-chars-forward " \t\r\n")
  5676. (save-match-data
  5677. (save-excursion (outline-end-of-heading)
  5678. (setq folded (org-invisible-p)))
  5679. (condition-case nil
  5680. (org-forward-same-level (1- n) t)
  5681. (error nil))
  5682. (org-end-of-subtree t t))
  5683. (org-back-over-empty-lines)
  5684. (setq end (point))
  5685. (goto-char beg0)
  5686. (when (> end beg)
  5687. (setq org-subtree-clip-folded folded)
  5688. (when (or cut force-store-markers)
  5689. (org-save-markers-in-region beg end))
  5690. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5691. (setq org-subtree-clip (current-kill 0))
  5692. (message "%s: Subtree(s) with %d characters"
  5693. (if cut "Cut" "Copied")
  5694. (length org-subtree-clip)))))
  5695. (defun org-paste-subtree (&optional level tree for-yank)
  5696. "Paste the clipboard as a subtree, with modification of headline level.
  5697. The entire subtree is promoted or demoted in order to match a new headline
  5698. level.
  5699. If the cursor is at the beginning of a headline, the same level as
  5700. that headline is used to paste the tree
  5701. If not, the new level is derived from the *visible* headings
  5702. before and after the insertion point, and taken to be the inferior headline
  5703. level of the two. So if the previous visible heading is level 3 and the
  5704. next is level 4 (or vice versa), level 4 will be used for insertion.
  5705. This makes sure that the subtree remains an independent subtree and does
  5706. not swallow low level entries.
  5707. You can also force a different level, either by using a numeric prefix
  5708. argument, or by inserting the heading marker by hand. For example, if the
  5709. cursor is after \"*****\", then the tree will be shifted to level 5.
  5710. If optional TREE is given, use this text instead of the kill ring.
  5711. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5712. move back over whitespace before inserting, and move point to the end of
  5713. the inserted text when done."
  5714. (interactive "P")
  5715. (setq tree (or tree (and kill-ring (current-kill 0))))
  5716. (unless (org-kill-is-subtree-p tree)
  5717. (error "%s"
  5718. (substitute-command-keys
  5719. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5720. (let* ((visp (not (org-invisible-p)))
  5721. (txt tree)
  5722. (^re (concat "^\\(" outline-regexp "\\)"))
  5723. (re (concat "\\(" outline-regexp "\\)"))
  5724. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5725. (old-level (if (string-match ^re txt)
  5726. (- (match-end 0) (match-beginning 0) 1)
  5727. -1))
  5728. (force-level (cond (level (prefix-numeric-value level))
  5729. ((and (looking-at "[ \t]*$")
  5730. (string-match
  5731. ^re_ (buffer-substring
  5732. (point-at-bol) (point))))
  5733. (- (match-end 1) (match-beginning 1)))
  5734. ((and (bolp)
  5735. (looking-at org-outline-regexp))
  5736. (- (match-end 0) (point) 1))
  5737. (t nil)))
  5738. (previous-level (save-excursion
  5739. (condition-case nil
  5740. (progn
  5741. (outline-previous-visible-heading 1)
  5742. (if (looking-at re)
  5743. (- (match-end 0) (match-beginning 0) 1)
  5744. 1))
  5745. (error 1))))
  5746. (next-level (save-excursion
  5747. (condition-case nil
  5748. (progn
  5749. (or (looking-at outline-regexp)
  5750. (outline-next-visible-heading 1))
  5751. (if (looking-at re)
  5752. (- (match-end 0) (match-beginning 0) 1)
  5753. 1))
  5754. (error 1))))
  5755. (new-level (or force-level (max previous-level next-level)))
  5756. (shift (if (or (= old-level -1)
  5757. (= new-level -1)
  5758. (= old-level new-level))
  5759. 0
  5760. (- new-level old-level)))
  5761. (delta (if (> shift 0) -1 1))
  5762. (func (if (> shift 0) 'org-demote 'org-promote))
  5763. (org-odd-levels-only nil)
  5764. beg end newend)
  5765. ;; Remove the forced level indicator
  5766. (if force-level
  5767. (delete-region (point-at-bol) (point)))
  5768. ;; Paste
  5769. (beginning-of-line 1)
  5770. (unless for-yank (org-back-over-empty-lines))
  5771. (setq beg (point))
  5772. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5773. (insert-before-markers txt)
  5774. (unless (string-match "\n\\'" txt) (insert "\n"))
  5775. (setq newend (point))
  5776. (org-reinstall-markers-in-region beg)
  5777. (setq end (point))
  5778. (goto-char beg)
  5779. (skip-chars-forward " \t\n\r")
  5780. (setq beg (point))
  5781. (if (and (org-invisible-p) visp)
  5782. (save-excursion (outline-show-heading)))
  5783. ;; Shift if necessary
  5784. (unless (= shift 0)
  5785. (save-restriction
  5786. (narrow-to-region beg end)
  5787. (while (not (= shift 0))
  5788. (org-map-region func (point-min) (point-max))
  5789. (setq shift (+ delta shift)))
  5790. (goto-char (point-min))
  5791. (setq newend (point-max))))
  5792. (when (or (interactive-p) for-yank)
  5793. (message "Clipboard pasted as level %d subtree" new-level))
  5794. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5795. kill-ring
  5796. (eq org-subtree-clip (current-kill 0))
  5797. org-subtree-clip-folded)
  5798. ;; The tree was folded before it was killed/copied
  5799. (hide-subtree))
  5800. (and for-yank (goto-char newend))))
  5801. (defun org-kill-is-subtree-p (&optional txt)
  5802. "Check if the current kill is an outline subtree, or a set of trees.
  5803. Returns nil if kill does not start with a headline, or if the first
  5804. headline level is not the largest headline level in the tree.
  5805. So this will actually accept several entries of equal levels as well,
  5806. which is OK for `org-paste-subtree'.
  5807. If optional TXT is given, check this string instead of the current kill."
  5808. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5809. (start-level (and kill
  5810. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5811. org-outline-regexp "\\)")
  5812. kill)
  5813. (- (match-end 2) (match-beginning 2) 1)))
  5814. (re (concat "^" org-outline-regexp))
  5815. (start (1+ (or (match-beginning 2) -1))))
  5816. (if (not start-level)
  5817. (progn
  5818. nil) ;; does not even start with a heading
  5819. (catch 'exit
  5820. (while (setq start (string-match re kill (1+ start)))
  5821. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5822. (throw 'exit nil)))
  5823. t))))
  5824. (defvar org-markers-to-move nil
  5825. "Markers that should be moved with a cut-and-paste operation.
  5826. Those markers are stored together with their positions relative to
  5827. the start of the region.")
  5828. (defun org-save-markers-in-region (beg end)
  5829. "Check markers in region.
  5830. If these markers are between BEG and END, record their position relative
  5831. to BEG, so that after moving the block of text, we can put the markers back
  5832. into place.
  5833. This function gets called just before an entry or tree gets cut from the
  5834. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5835. called immediately, to move the markers with the entries."
  5836. (setq org-markers-to-move nil)
  5837. (when (featurep 'org-clock)
  5838. (org-clock-save-markers-for-cut-and-paste beg end))
  5839. (when (featurep 'org-agenda)
  5840. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5841. (defun org-check-and-save-marker (marker beg end)
  5842. "Check if MARKER is between BEG and END.
  5843. If yes, remember the marker and the distance to BEG."
  5844. (when (and (marker-buffer marker)
  5845. (equal (marker-buffer marker) (current-buffer)))
  5846. (if (and (>= marker beg) (< marker end))
  5847. (push (cons marker (- marker beg)) org-markers-to-move))))
  5848. (defun org-reinstall-markers-in-region (beg)
  5849. "Move all remembered markers to their position relative to BEG."
  5850. (mapc (lambda (x)
  5851. (move-marker (car x) (+ beg (cdr x))))
  5852. org-markers-to-move)
  5853. (setq org-markers-to-move nil))
  5854. (defun org-narrow-to-subtree ()
  5855. "Narrow buffer to the current subtree."
  5856. (interactive)
  5857. (save-excursion
  5858. (save-match-data
  5859. (narrow-to-region
  5860. (progn (org-back-to-heading t) (point))
  5861. (progn (org-end-of-subtree t t) (point))))))
  5862. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5863. "Clone the task (subtree) at point N times.
  5864. The clones will be inserted as siblings.
  5865. In interactive use, the user will be prompted for the number of clones
  5866. to be produced, and for a time SHIFT, which may be a repeater as used
  5867. in time stamps, for example `+3d'.
  5868. When a valid repeater is given and the entry contains any time stamps,
  5869. the clones will become a sequence in time, with time stamps in the
  5870. subtree shifted for each clone produced. If SHIFT is nil or the
  5871. empty string, time stamps will be left alone.
  5872. If the original subtree did contain time stamps with a repeater,
  5873. the following will happen:
  5874. - the repeater will be removed in each clone
  5875. - an additional clone will be produced, with the current, unshifted
  5876. date(s) in the entry.
  5877. - the original entry will be placed *after* all the clones, with
  5878. repeater intact.
  5879. - the start days in the repeater in the original entry will be shifted
  5880. to past the last clone.
  5881. I this way you can spell out a number of instances of a repeating task,
  5882. and still retain the repeater to cover future instances of the task."
  5883. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5884. (let (beg end template task
  5885. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5886. (if (not (and (integerp n) (> n 0)))
  5887. (error "Invalid number of replications %s" n))
  5888. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5889. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5890. shift)))
  5891. (error "Invalid shift specification %s" shift))
  5892. (when doshift
  5893. (setq shift-n (string-to-number (match-string 1 shift))
  5894. shift-what (cdr (assoc (match-string 2 shift)
  5895. '(("d" . day) ("w" . week)
  5896. ("m" . month) ("y" . year))))))
  5897. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5898. (setq nmin 1 nmax n)
  5899. (org-back-to-heading t)
  5900. (setq beg (point))
  5901. (org-end-of-subtree t t)
  5902. (setq end (point))
  5903. (setq template (buffer-substring beg end))
  5904. (when (and doshift
  5905. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5906. (delete-region beg end)
  5907. (setq end beg)
  5908. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5909. (goto-char end)
  5910. (loop for n from nmin to nmax do
  5911. (if (not doshift)
  5912. (setq task template)
  5913. (with-temp-buffer
  5914. (insert template)
  5915. (org-mode)
  5916. (goto-char (point-min))
  5917. (while (re-search-forward org-ts-regexp-both nil t)
  5918. (org-timestamp-change (* n shift-n) shift-what))
  5919. (unless (= n n-no-remove)
  5920. (goto-char (point-min))
  5921. (while (re-search-forward org-ts-regexp nil t)
  5922. (save-excursion
  5923. (goto-char (match-beginning 0))
  5924. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5925. (delete-region (match-beginning 1) (match-end 1))))))
  5926. (setq task (buffer-string))))
  5927. (insert task))
  5928. (goto-char beg)))
  5929. ;;; Outline Sorting
  5930. (defun org-sort (with-case)
  5931. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5932. Optional argument WITH-CASE means sort case-sensitively.
  5933. With a double prefix argument, also remove duplicate entries."
  5934. (interactive "P")
  5935. (if (org-at-table-p)
  5936. (org-call-with-arg 'org-table-sort-lines with-case)
  5937. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5938. (defun org-sort-remove-invisible (s)
  5939. (remove-text-properties 0 (length s) org-rm-props s)
  5940. (while (string-match org-bracket-link-regexp s)
  5941. (setq s (replace-match (if (match-end 2)
  5942. (match-string 3 s)
  5943. (match-string 1 s)) t t s)))
  5944. s)
  5945. (defvar org-priority-regexp) ; defined later in the file
  5946. (defvar org-after-sorting-entries-or-items-hook nil
  5947. "Hook that is run after a bunch of entries or items have been sorted.
  5948. When children are sorted, the cursor is in the parent line when this
  5949. hook gets called. When a region or a plain list is sorted, the cursor
  5950. will be in the first entry of the sorted region/list.")
  5951. (defun org-sort-entries-or-items
  5952. (&optional with-case sorting-type getkey-func compare-func property)
  5953. "Sort entries on a certain level of an outline tree, or plain list items.
  5954. If there is an active region, the entries in the region are sorted.
  5955. Else, if the cursor is before the first entry, sort the top-level items.
  5956. Else, the children of the entry at point are sorted.
  5957. If the cursor is at the first item in a plain list, the list items will be
  5958. sorted.
  5959. Sorting can be alphabetically, numerically, by date/time as given by
  5960. a time stamp, by a property or by priority.
  5961. The command prompts for the sorting type unless it has been given to the
  5962. function through the SORTING-TYPE argument, which needs to a character,
  5963. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5964. precise meaning of each character:
  5965. n Numerically, by converting the beginning of the entry/item to a number.
  5966. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5967. t By date/time, either the first active time stamp in the entry, or, if
  5968. none exist, by the first inactive one.
  5969. In items, only the first line will be chekced.
  5970. s By the scheduled date/time.
  5971. d By deadline date/time.
  5972. c By creation time, which is assumed to be the first inactive time stamp
  5973. at the beginning of a line.
  5974. p By priority according to the cookie.
  5975. r By the value of a property.
  5976. Capital letters will reverse the sort order.
  5977. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5978. called with point at the beginning of the record. It must return either
  5979. a string or a number that should serve as the sorting key for that record.
  5980. Comparing entries ignores case by default. However, with an optional argument
  5981. WITH-CASE, the sorting considers case as well."
  5982. (interactive "P")
  5983. (let ((case-func (if with-case 'identity 'downcase))
  5984. start beg end stars re re2
  5985. txt what tmp plain-list-p)
  5986. ;; Find beginning and end of region to sort
  5987. (cond
  5988. ((org-region-active-p)
  5989. ;; we will sort the region
  5990. (setq end (region-end)
  5991. what "region")
  5992. (goto-char (region-beginning))
  5993. (if (not (org-on-heading-p)) (outline-next-heading))
  5994. (setq start (point)))
  5995. ((org-at-item-p)
  5996. ;; we will sort this plain list
  5997. (org-beginning-of-item-list) (setq start (point))
  5998. (org-end-of-item-list) (setq end (point))
  5999. (goto-char start)
  6000. (setq plain-list-p t
  6001. what "plain list"))
  6002. ((or (org-on-heading-p)
  6003. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6004. ;; we will sort the children of the current headline
  6005. (org-back-to-heading)
  6006. (setq start (point)
  6007. end (progn (org-end-of-subtree t t)
  6008. (org-back-over-empty-lines)
  6009. (point))
  6010. what "children")
  6011. (goto-char start)
  6012. (show-subtree)
  6013. (outline-next-heading))
  6014. (t
  6015. ;; we will sort the top-level entries in this file
  6016. (goto-char (point-min))
  6017. (or (org-on-heading-p) (outline-next-heading))
  6018. (setq start (point) end (point-max) what "top-level")
  6019. (goto-char start)
  6020. (show-all)))
  6021. (setq beg (point))
  6022. (if (>= beg end) (error "Nothing to sort"))
  6023. (unless plain-list-p
  6024. (looking-at "\\(\\*+\\)")
  6025. (setq stars (match-string 1)
  6026. re (concat "^" (regexp-quote stars) " +")
  6027. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6028. txt (buffer-substring beg end))
  6029. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6030. (if (and (not (equal stars "*")) (string-match re2 txt))
  6031. (error "Region to sort contains a level above the first entry")))
  6032. (unless sorting-type
  6033. (message
  6034. (if plain-list-p
  6035. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6036. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6037. [t]ime [s]cheduled [d]eadline [c]reated
  6038. A/N/T/S/D/C/P/O/F means reversed:")
  6039. what)
  6040. (setq sorting-type (read-char-exclusive))
  6041. (and (= (downcase sorting-type) ?f)
  6042. (setq getkey-func
  6043. (org-icompleting-read "Sort using function: "
  6044. obarray 'fboundp t nil nil))
  6045. (setq getkey-func (intern getkey-func)))
  6046. (and (= (downcase sorting-type) ?r)
  6047. (setq property
  6048. (org-icompleting-read "Property: "
  6049. (mapcar 'list (org-buffer-property-keys t))
  6050. nil t))))
  6051. (message "Sorting entries...")
  6052. (save-restriction
  6053. (narrow-to-region start end)
  6054. (let ((dcst (downcase sorting-type))
  6055. (case-fold-search nil)
  6056. (now (current-time)))
  6057. (sort-subr
  6058. (/= dcst sorting-type)
  6059. ;; This function moves to the beginning character of the "record" to
  6060. ;; be sorted.
  6061. (if plain-list-p
  6062. (lambda nil
  6063. (if (org-at-item-p) t (goto-char (point-max))))
  6064. (lambda nil
  6065. (if (re-search-forward re nil t)
  6066. (goto-char (match-beginning 0))
  6067. (goto-char (point-max)))))
  6068. ;; This function moves to the last character of the "record" being
  6069. ;; sorted.
  6070. (if plain-list-p
  6071. 'org-end-of-item
  6072. (lambda nil
  6073. (save-match-data
  6074. (condition-case nil
  6075. (outline-forward-same-level 1)
  6076. (error
  6077. (goto-char (point-max)))))))
  6078. ;; This function returns the value that gets sorted against.
  6079. (if plain-list-p
  6080. (lambda nil
  6081. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6082. (cond
  6083. ((= dcst ?n)
  6084. (string-to-number (buffer-substring (match-end 0)
  6085. (point-at-eol))))
  6086. ((= dcst ?a)
  6087. (buffer-substring (match-end 0) (point-at-eol)))
  6088. ((= dcst ?t)
  6089. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6090. (re-search-forward org-ts-regexp-both
  6091. (point-at-eol) t))
  6092. (org-time-string-to-seconds (match-string 0))
  6093. (org-float-time now)))
  6094. ((= dcst ?f)
  6095. (if getkey-func
  6096. (progn
  6097. (setq tmp (funcall getkey-func))
  6098. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6099. tmp)
  6100. (error "Invalid key function `%s'" getkey-func)))
  6101. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6102. (lambda nil
  6103. (cond
  6104. ((= dcst ?n)
  6105. (if (looking-at org-complex-heading-regexp)
  6106. (string-to-number (match-string 4))
  6107. nil))
  6108. ((= dcst ?a)
  6109. (if (looking-at org-complex-heading-regexp)
  6110. (funcall case-func (match-string 4))
  6111. nil))
  6112. ((= dcst ?t)
  6113. (let ((end (save-excursion (outline-next-heading) (point))))
  6114. (if (or (re-search-forward org-ts-regexp end t)
  6115. (re-search-forward org-ts-regexp-both end t))
  6116. (org-time-string-to-seconds (match-string 0))
  6117. (org-float-time now))))
  6118. ((= dcst ?c)
  6119. (let ((end (save-excursion (outline-next-heading) (point))))
  6120. (if (re-search-forward
  6121. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6122. end t)
  6123. (org-time-string-to-seconds (match-string 0))
  6124. (org-float-time now))))
  6125. ((= dcst ?s)
  6126. (let ((end (save-excursion (outline-next-heading) (point))))
  6127. (if (re-search-forward org-scheduled-time-regexp end t)
  6128. (org-time-string-to-seconds (match-string 1))
  6129. (org-float-time now))))
  6130. ((= dcst ?d)
  6131. (let ((end (save-excursion (outline-next-heading) (point))))
  6132. (if (re-search-forward org-deadline-time-regexp end t)
  6133. (org-time-string-to-seconds (match-string 1))
  6134. (org-float-time now))))
  6135. ((= dcst ?p)
  6136. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6137. (string-to-char (match-string 2))
  6138. org-default-priority))
  6139. ((= dcst ?r)
  6140. (or (org-entry-get nil property) ""))
  6141. ((= dcst ?o)
  6142. (if (looking-at org-complex-heading-regexp)
  6143. (- 9999 (length (member (match-string 2)
  6144. org-todo-keywords-1)))))
  6145. ((= dcst ?f)
  6146. (if getkey-func
  6147. (progn
  6148. (setq tmp (funcall getkey-func))
  6149. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6150. tmp)
  6151. (error "Invalid key function `%s'" getkey-func)))
  6152. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6153. nil
  6154. (cond
  6155. ((= dcst ?a) 'string<)
  6156. ((= dcst ?f) compare-func)
  6157. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6158. (t nil)))))
  6159. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6160. (message "Sorting entries...done")))
  6161. (defun org-do-sort (table what &optional with-case sorting-type)
  6162. "Sort TABLE of WHAT according to SORTING-TYPE.
  6163. The user will be prompted for the SORTING-TYPE if the call to this
  6164. function does not specify it. WHAT is only for the prompt, to indicate
  6165. what is being sorted. The sorting key will be extracted from
  6166. the car of the elements of the table.
  6167. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6168. (unless sorting-type
  6169. (message
  6170. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6171. what)
  6172. (setq sorting-type (read-char-exclusive)))
  6173. (let ((dcst (downcase sorting-type))
  6174. extractfun comparefun)
  6175. ;; Define the appropriate functions
  6176. (cond
  6177. ((= dcst ?n)
  6178. (setq extractfun 'string-to-number
  6179. comparefun (if (= dcst sorting-type) '< '>)))
  6180. ((= dcst ?a)
  6181. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6182. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6183. comparefun (if (= dcst sorting-type)
  6184. 'string<
  6185. (lambda (a b) (and (not (string< a b))
  6186. (not (string= a b)))))))
  6187. ((= dcst ?t)
  6188. (setq extractfun
  6189. (lambda (x)
  6190. (if (or (string-match org-ts-regexp x)
  6191. (string-match org-ts-regexp-both x))
  6192. (org-float-time
  6193. (org-time-string-to-time (match-string 0 x)))
  6194. 0))
  6195. comparefun (if (= dcst sorting-type) '< '>)))
  6196. (t (error "Invalid sorting type `%c'" sorting-type)))
  6197. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6198. table)
  6199. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6200. ;;; The orgstruct minor mode
  6201. ;; Define a minor mode which can be used in other modes in order to
  6202. ;; integrate the org-mode structure editing commands.
  6203. ;; This is really a hack, because the org-mode structure commands use
  6204. ;; keys which normally belong to the major mode. Here is how it
  6205. ;; works: The minor mode defines all the keys necessary to operate the
  6206. ;; structure commands, but wraps the commands into a function which
  6207. ;; tests if the cursor is currently at a headline or a plain list
  6208. ;; item. If that is the case, the structure command is used,
  6209. ;; temporarily setting many Org-mode variables like regular
  6210. ;; expressions for filling etc. However, when any of those keys is
  6211. ;; used at a different location, function uses `key-binding' to look
  6212. ;; up if the key has an associated command in another currently active
  6213. ;; keymap (minor modes, major mode, global), and executes that
  6214. ;; command. There might be problems if any of the keys is otherwise
  6215. ;; used as a prefix key.
  6216. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6217. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6218. ;; addresses this by checking explicitly for both bindings.
  6219. (defvar orgstruct-mode-map (make-sparse-keymap)
  6220. "Keymap for the minor `orgstruct-mode'.")
  6221. (defvar org-local-vars nil
  6222. "List of local variables, for use by `orgstruct-mode'")
  6223. ;;;###autoload
  6224. (define-minor-mode orgstruct-mode
  6225. "Toggle the minor more `orgstruct-mode'.
  6226. This mode is for using Org-mode structure commands in other modes.
  6227. The following key behave as if Org-mode was active, if the cursor
  6228. is on a headline, or on a plain list item (both in the definition
  6229. of Org-mode).
  6230. M-up Move entry/item up
  6231. M-down Move entry/item down
  6232. M-left Promote
  6233. M-right Demote
  6234. M-S-up Move entry/item up
  6235. M-S-down Move entry/item down
  6236. M-S-left Promote subtree
  6237. M-S-right Demote subtree
  6238. M-q Fill paragraph and items like in Org-mode
  6239. C-c ^ Sort entries
  6240. C-c - Cycle list bullet
  6241. TAB Cycle item visibility
  6242. M-RET Insert new heading/item
  6243. S-M-RET Insert new TODO heading / Checkbox item
  6244. C-c C-c Set tags / toggle checkbox"
  6245. nil " OrgStruct" nil
  6246. (org-load-modules-maybe)
  6247. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6248. ;;;###autoload
  6249. (defun turn-on-orgstruct ()
  6250. "Unconditionally turn on `orgstruct-mode'."
  6251. (orgstruct-mode 1))
  6252. (defun orgstruct++-mode (&optional arg)
  6253. "Toggle `orgstruct-mode', the enhanced version of it.
  6254. In addition to setting orgstruct-mode, this also exports all indentation
  6255. and autofilling variables from org-mode into the buffer. It will also
  6256. recognize item context in multiline items.
  6257. Note that turning off orgstruct-mode will *not* remove the
  6258. indentation/paragraph settings. This can only be done by refreshing the
  6259. major mode, for example with \\[normal-mode]."
  6260. (interactive "P")
  6261. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6262. (if (< arg 1)
  6263. (orgstruct-mode -1)
  6264. (orgstruct-mode 1)
  6265. (let (var val)
  6266. (mapc
  6267. (lambda (x)
  6268. (when (string-match
  6269. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6270. (symbol-name (car x)))
  6271. (setq var (car x) val (nth 1 x))
  6272. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6273. org-local-vars)
  6274. (org-set-local 'orgstruct-is-++ t))))
  6275. (defvar orgstruct-is-++ nil
  6276. "Is orgstruct-mode in ++ version in the current-buffer?")
  6277. (make-variable-buffer-local 'orgstruct-is-++)
  6278. ;;;###autoload
  6279. (defun turn-on-orgstruct++ ()
  6280. "Unconditionally turn on `orgstruct++-mode'."
  6281. (orgstruct++-mode 1))
  6282. (defun orgstruct-error ()
  6283. "Error when there is no default binding for a structure key."
  6284. (interactive)
  6285. (error "This key has no function outside structure elements"))
  6286. (defun orgstruct-setup ()
  6287. "Setup orgstruct keymaps."
  6288. (let ((nfunc 0)
  6289. (bindings
  6290. (list
  6291. '([(meta up)] org-metaup)
  6292. '([(meta down)] org-metadown)
  6293. '([(meta left)] org-metaleft)
  6294. '([(meta right)] org-metaright)
  6295. '([(meta shift up)] org-shiftmetaup)
  6296. '([(meta shift down)] org-shiftmetadown)
  6297. '([(meta shift left)] org-shiftmetaleft)
  6298. '([(meta shift right)] org-shiftmetaright)
  6299. '([?\e (up)] org-metaup)
  6300. '([?\e (down)] org-metadown)
  6301. '([?\e (left)] org-metaleft)
  6302. '([?\e (right)] org-metaright)
  6303. '([?\e (shift up)] org-shiftmetaup)
  6304. '([?\e (shift down)] org-shiftmetadown)
  6305. '([?\e (shift left)] org-shiftmetaleft)
  6306. '([?\e (shift right)] org-shiftmetaright)
  6307. '([(shift up)] org-shiftup)
  6308. '([(shift down)] org-shiftdown)
  6309. '([(shift left)] org-shiftleft)
  6310. '([(shift right)] org-shiftright)
  6311. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6312. '("\M-q" fill-paragraph)
  6313. '("\C-c^" org-sort)
  6314. '("\C-c-" org-cycle-list-bullet)))
  6315. elt key fun cmd)
  6316. (while (setq elt (pop bindings))
  6317. (setq nfunc (1+ nfunc))
  6318. (setq key (org-key (car elt))
  6319. fun (nth 1 elt)
  6320. cmd (orgstruct-make-binding fun nfunc key))
  6321. (org-defkey orgstruct-mode-map key cmd))
  6322. ;; Special treatment needed for TAB and RET
  6323. (org-defkey orgstruct-mode-map [(tab)]
  6324. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6325. (org-defkey orgstruct-mode-map "\C-i"
  6326. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6327. (org-defkey orgstruct-mode-map "\M-\C-m"
  6328. (orgstruct-make-binding 'org-insert-heading 105
  6329. "\M-\C-m" [(meta return)]))
  6330. (org-defkey orgstruct-mode-map [(meta return)]
  6331. (orgstruct-make-binding 'org-insert-heading 106
  6332. [(meta return)] "\M-\C-m"))
  6333. (org-defkey orgstruct-mode-map [(shift meta return)]
  6334. (orgstruct-make-binding 'org-insert-todo-heading 107
  6335. [(meta return)] "\M-\C-m"))
  6336. (org-defkey orgstruct-mode-map "\e\C-m"
  6337. (orgstruct-make-binding 'org-insert-heading 108
  6338. "\e\C-m" [?\e (return)]))
  6339. (org-defkey orgstruct-mode-map [?\e (return)]
  6340. (orgstruct-make-binding 'org-insert-heading 109
  6341. [?\e (return)] "\e\C-m"))
  6342. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6343. (orgstruct-make-binding 'org-insert-todo-heading 110
  6344. [?\e (return)] "\e\C-m"))
  6345. (unless org-local-vars
  6346. (setq org-local-vars (org-get-local-variables)))
  6347. t))
  6348. (defun orgstruct-make-binding (fun n &rest keys)
  6349. "Create a function for binding in the structure minor mode.
  6350. FUN is the command to call inside a table. N is used to create a unique
  6351. command name. KEYS are keys that should be checked in for a command
  6352. to execute outside of tables."
  6353. (eval
  6354. (list 'defun
  6355. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6356. '(arg)
  6357. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6358. "Outside of structure, run the binding of `"
  6359. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6360. "'.")
  6361. '(interactive "p")
  6362. (list 'if
  6363. `(org-context-p 'headline 'item
  6364. (and orgstruct-is-++
  6365. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6366. 'item-body))
  6367. (list 'org-run-like-in-org-mode (list 'quote fun))
  6368. (list 'let '(orgstruct-mode)
  6369. (list 'call-interactively
  6370. (append '(or)
  6371. (mapcar (lambda (k)
  6372. (list 'key-binding k))
  6373. keys)
  6374. '('orgstruct-error))))))))
  6375. (defun org-context-p (&rest contexts)
  6376. "Check if local context is any of CONTEXTS.
  6377. Possible values in the list of contexts are `table', `headline', and `item'."
  6378. (let ((pos (point)))
  6379. (goto-char (point-at-bol))
  6380. (prog1 (or (and (memq 'table contexts)
  6381. (looking-at "[ \t]*|"))
  6382. (and (memq 'headline contexts)
  6383. ;;????????? (looking-at "\\*+"))
  6384. (looking-at outline-regexp))
  6385. (and (memq 'item contexts)
  6386. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6387. (and (memq 'item-body contexts)
  6388. (org-in-item-p)))
  6389. (goto-char pos))))
  6390. (defun org-get-local-variables ()
  6391. "Return a list of all local variables in an org-mode buffer."
  6392. (let (varlist)
  6393. (with-current-buffer (get-buffer-create "*Org tmp*")
  6394. (erase-buffer)
  6395. (org-mode)
  6396. (setq varlist (buffer-local-variables)))
  6397. (kill-buffer "*Org tmp*")
  6398. (delq nil
  6399. (mapcar
  6400. (lambda (x)
  6401. (setq x
  6402. (if (symbolp x)
  6403. (list x)
  6404. (list (car x) (list 'quote (cdr x)))))
  6405. (if (string-match
  6406. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6407. (symbol-name (car x)))
  6408. x nil))
  6409. varlist))))
  6410. ;;;###autoload
  6411. (defun org-run-like-in-org-mode (cmd)
  6412. "Run a command, pretending that the current buffer is in Org-mode.
  6413. This will temporarily bind local variables that are typically bound in
  6414. Org-mode to the values they have in Org-mode, and then interactively
  6415. call CMD."
  6416. (org-load-modules-maybe)
  6417. (unless org-local-vars
  6418. (setq org-local-vars (org-get-local-variables)))
  6419. (eval (list 'let org-local-vars
  6420. (list 'call-interactively (list 'quote cmd)))))
  6421. ;;;; Archiving
  6422. (defun org-get-category (&optional pos)
  6423. "Get the category applying to position POS."
  6424. (get-text-property (or pos (point)) 'org-category))
  6425. (defun org-refresh-category-properties ()
  6426. "Refresh category text properties in the buffer."
  6427. (let ((def-cat (cond
  6428. ((null org-category)
  6429. (if buffer-file-name
  6430. (file-name-sans-extension
  6431. (file-name-nondirectory buffer-file-name))
  6432. "???"))
  6433. ((symbolp org-category) (symbol-name org-category))
  6434. (t org-category)))
  6435. beg end cat pos optionp)
  6436. (org-unmodified
  6437. (save-excursion
  6438. (save-restriction
  6439. (widen)
  6440. (goto-char (point-min))
  6441. (put-text-property (point) (point-max) 'org-category def-cat)
  6442. (while (re-search-forward
  6443. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6444. (setq pos (match-end 0)
  6445. optionp (equal (char-after (match-beginning 0)) ?#)
  6446. cat (org-trim (match-string 2)))
  6447. (if optionp
  6448. (setq beg (point-at-bol) end (point-max))
  6449. (org-back-to-heading t)
  6450. (setq beg (point) end (org-end-of-subtree t t)))
  6451. (put-text-property beg end 'org-category cat)
  6452. (goto-char pos)))))))
  6453. ;;;; Link Stuff
  6454. ;;; Link abbreviations
  6455. (defun org-link-expand-abbrev (link)
  6456. "Apply replacements as defined in `org-link-abbrev-alist."
  6457. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6458. (let* ((key (match-string 1 link))
  6459. (as (or (assoc key org-link-abbrev-alist-local)
  6460. (assoc key org-link-abbrev-alist)))
  6461. (tag (and (match-end 2) (match-string 3 link)))
  6462. rpl)
  6463. (if (not as)
  6464. link
  6465. (setq rpl (cdr as))
  6466. (cond
  6467. ((symbolp rpl) (funcall rpl tag))
  6468. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6469. ((string-match "%h" rpl)
  6470. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6471. (t (concat rpl tag)))))
  6472. link))
  6473. ;;; Storing and inserting links
  6474. (defvar org-insert-link-history nil
  6475. "Minibuffer history for links inserted with `org-insert-link'.")
  6476. (defvar org-stored-links nil
  6477. "Contains the links stored with `org-store-link'.")
  6478. (defvar org-store-link-plist nil
  6479. "Plist with info about the most recently link created with `org-store-link'.")
  6480. (defvar org-link-protocols nil
  6481. "Link protocols added to Org-mode using `org-add-link-type'.")
  6482. (defvar org-store-link-functions nil
  6483. "List of functions that are called to create and store a link.
  6484. Each function will be called in turn until one returns a non-nil
  6485. value. Each function should check if it is responsible for creating
  6486. this link (for example by looking at the major mode).
  6487. If not, it must exit and return nil.
  6488. If yes, it should return a non-nil value after a calling
  6489. `org-store-link-props' with a list of properties and values.
  6490. Special properties are:
  6491. :type The link prefix. like \"http\". This must be given.
  6492. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6493. This is obligatory as well.
  6494. :description Optional default description for the second pair
  6495. of brackets in an Org-mode link. The user can still change
  6496. this when inserting this link into an Org-mode buffer.
  6497. In addition to these, any additional properties can be specified
  6498. and then used in remember templates.")
  6499. (defun org-add-link-type (type &optional follow export)
  6500. "Add TYPE to the list of `org-link-types'.
  6501. Re-compute all regular expressions depending on `org-link-types'
  6502. FOLLOW and EXPORT are two functions.
  6503. FOLLOW should take the link path as the single argument and do whatever
  6504. is necessary to follow the link, for example find a file or display
  6505. a mail message.
  6506. EXPORT should format the link path for export to one of the export formats.
  6507. It should be a function accepting three arguments:
  6508. path the path of the link, the text after the prefix (like \"http:\")
  6509. desc the description of the link, if any, nil if there was no description
  6510. format the export format, a symbol like `html' or `latex'.
  6511. The function may use the FORMAT information to return different values
  6512. depending on the format. The return value will be put literally into
  6513. the exported file.
  6514. Org-mode has a built-in default for exporting links. If you are happy with
  6515. this default, there is no need to define an export function for the link
  6516. type. For a simple example of an export function, see `org-bbdb.el'."
  6517. (add-to-list 'org-link-types type t)
  6518. (org-make-link-regexps)
  6519. (if (assoc type org-link-protocols)
  6520. (setcdr (assoc type org-link-protocols) (list follow export))
  6521. (push (list type follow export) org-link-protocols)))
  6522. (defvar org-agenda-buffer-name)
  6523. ;;;###autoload
  6524. (defun org-store-link (arg)
  6525. "\\<org-mode-map>Store an org-link to the current location.
  6526. This link is added to `org-stored-links' and can later be inserted
  6527. into an org-buffer with \\[org-insert-link].
  6528. For some link types, a prefix arg is interpreted:
  6529. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6530. For file links, arg negates `org-context-in-file-links'."
  6531. (interactive "P")
  6532. (org-load-modules-maybe)
  6533. (setq org-store-link-plist nil) ; reset
  6534. (let ((outline-regexp (org-get-limited-outline-regexp))
  6535. link cpltxt desc description search txt custom-id)
  6536. (cond
  6537. ((run-hook-with-args-until-success 'org-store-link-functions)
  6538. (setq link (plist-get org-store-link-plist :link)
  6539. desc (or (plist-get org-store-link-plist :description) link)))
  6540. ((equal (buffer-name) "*Org Edit Src Example*")
  6541. (let (label gc)
  6542. (while (or (not label)
  6543. (save-excursion
  6544. (save-restriction
  6545. (widen)
  6546. (goto-char (point-min))
  6547. (re-search-forward
  6548. (regexp-quote (format org-coderef-label-format label))
  6549. nil t))))
  6550. (when label (message "Label exists already") (sit-for 2))
  6551. (setq label (read-string "Code line label: " label)))
  6552. (end-of-line 1)
  6553. (setq link (format org-coderef-label-format label))
  6554. (setq gc (- 79 (length link)))
  6555. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6556. (insert link)
  6557. (setq link (concat "(" label ")") desc nil)))
  6558. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6559. ;; We are in the agenda, link to referenced location
  6560. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6561. (get-text-property (point) 'org-marker))))
  6562. (when m
  6563. (org-with-point-at m
  6564. (call-interactively 'org-store-link)))))
  6565. ((eq major-mode 'calendar-mode)
  6566. (let ((cd (calendar-cursor-to-date)))
  6567. (setq link
  6568. (format-time-string
  6569. (car org-time-stamp-formats)
  6570. (apply 'encode-time
  6571. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6572. nil nil nil))))
  6573. (org-store-link-props :type "calendar" :date cd)))
  6574. ((eq major-mode 'w3-mode)
  6575. (setq cpltxt (if (and (buffer-name)
  6576. (not (string-match "Untitled" (buffer-name))))
  6577. (buffer-name)
  6578. (url-view-url t))
  6579. link (org-make-link (url-view-url t)))
  6580. (org-store-link-props :type "w3" :url (url-view-url t)))
  6581. ((eq major-mode 'w3m-mode)
  6582. (setq cpltxt (or w3m-current-title w3m-current-url)
  6583. link (org-make-link w3m-current-url))
  6584. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6585. ((setq search (run-hook-with-args-until-success
  6586. 'org-create-file-search-functions))
  6587. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6588. "::" search))
  6589. (setq cpltxt (or description link)))
  6590. ((eq major-mode 'image-mode)
  6591. (setq cpltxt (concat "file:"
  6592. (abbreviate-file-name buffer-file-name))
  6593. link (org-make-link cpltxt))
  6594. (org-store-link-props :type "image" :file buffer-file-name))
  6595. ((eq major-mode 'dired-mode)
  6596. ;; link to the file in the current line
  6597. (setq cpltxt (concat "file:"
  6598. (abbreviate-file-name
  6599. (expand-file-name
  6600. (dired-get-filename nil t))))
  6601. link (org-make-link cpltxt)))
  6602. ((and buffer-file-name (org-mode-p))
  6603. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6604. (cond
  6605. ((org-in-regexp "<<\\(.*?\\)>>")
  6606. (setq cpltxt
  6607. (concat "file:"
  6608. (abbreviate-file-name buffer-file-name)
  6609. "::" (match-string 1))
  6610. link (org-make-link cpltxt)))
  6611. ((and (featurep 'org-id)
  6612. (or (eq org-link-to-org-use-id t)
  6613. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6614. (interactive-p))
  6615. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6616. (interactive-p)
  6617. (not custom-id))
  6618. (and org-link-to-org-use-id
  6619. (condition-case nil
  6620. (org-entry-get nil "ID")
  6621. (error nil)))))
  6622. ;; We can make a link using the ID.
  6623. (setq link (condition-case nil
  6624. (prog1 (org-id-store-link)
  6625. (setq desc (plist-get org-store-link-plist
  6626. :description)))
  6627. (error
  6628. ;; probably before first headline, link to file only
  6629. (concat "file:"
  6630. (abbreviate-file-name buffer-file-name))))))
  6631. (t
  6632. ;; Just link to current headline
  6633. (setq cpltxt (concat "file:"
  6634. (abbreviate-file-name buffer-file-name)))
  6635. ;; Add a context search string
  6636. (when (org-xor org-context-in-file-links arg)
  6637. (setq txt (cond
  6638. ((org-on-heading-p) nil)
  6639. ((org-region-active-p)
  6640. (buffer-substring (region-beginning) (region-end)))
  6641. (t nil)))
  6642. (when (or (null txt) (string-match "\\S-" txt))
  6643. (setq cpltxt
  6644. (concat cpltxt "::"
  6645. (condition-case nil
  6646. (org-make-org-heading-search-string txt)
  6647. (error "")))
  6648. desc (or (nth 4 (ignore-errors
  6649. (org-heading-components))) "NONE"))))
  6650. (if (string-match "::\\'" cpltxt)
  6651. (setq cpltxt (substring cpltxt 0 -2)))
  6652. (setq link (org-make-link cpltxt)))))
  6653. ((buffer-file-name (buffer-base-buffer))
  6654. ;; Just link to this file here.
  6655. (setq cpltxt (concat "file:"
  6656. (abbreviate-file-name
  6657. (buffer-file-name (buffer-base-buffer)))))
  6658. ;; Add a context string
  6659. (when (org-xor org-context-in-file-links arg)
  6660. (setq txt (if (org-region-active-p)
  6661. (buffer-substring (region-beginning) (region-end))
  6662. (buffer-substring (point-at-bol) (point-at-eol))))
  6663. ;; Only use search option if there is some text.
  6664. (when (string-match "\\S-" txt)
  6665. (setq cpltxt
  6666. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6667. desc "NONE")))
  6668. (setq link (org-make-link cpltxt)))
  6669. ((interactive-p)
  6670. (error "Cannot link to a buffer which is not visiting a file"))
  6671. (t (setq link nil)))
  6672. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6673. (setq link (or link cpltxt)
  6674. desc (or desc cpltxt))
  6675. (if (equal desc "NONE") (setq desc nil))
  6676. (if (and (or (interactive-p) executing-kbd-macro) link)
  6677. (progn
  6678. (setq org-stored-links
  6679. (cons (list link desc) org-stored-links))
  6680. (message "Stored: %s" (or desc link))
  6681. (when custom-id
  6682. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6683. "::#" custom-id))
  6684. (setq org-stored-links
  6685. (cons (list link desc) org-stored-links))))
  6686. (and link (org-make-link-string link desc)))))
  6687. (defun org-store-link-props (&rest plist)
  6688. "Store link properties, extract names and addresses."
  6689. (let (x adr)
  6690. (when (setq x (plist-get plist :from))
  6691. (setq adr (mail-extract-address-components x))
  6692. (setq plist (plist-put plist :fromname (car adr)))
  6693. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6694. (when (setq x (plist-get plist :to))
  6695. (setq adr (mail-extract-address-components x))
  6696. (setq plist (plist-put plist :toname (car adr)))
  6697. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6698. (let ((from (plist-get plist :from))
  6699. (to (plist-get plist :to)))
  6700. (when (and from to org-from-is-user-regexp)
  6701. (setq plist
  6702. (plist-put plist :fromto
  6703. (if (string-match org-from-is-user-regexp from)
  6704. (concat "to %t")
  6705. (concat "from %f"))))))
  6706. (setq org-store-link-plist plist))
  6707. (defun org-add-link-props (&rest plist)
  6708. "Add these properties to the link property list."
  6709. (let (key value)
  6710. (while plist
  6711. (setq key (pop plist) value (pop plist))
  6712. (setq org-store-link-plist
  6713. (plist-put org-store-link-plist key value)))))
  6714. (defun org-email-link-description (&optional fmt)
  6715. "Return the description part of an email link.
  6716. This takes information from `org-store-link-plist' and formats it
  6717. according to FMT (default from `org-email-link-description-format')."
  6718. (setq fmt (or fmt org-email-link-description-format))
  6719. (let* ((p org-store-link-plist)
  6720. (to (plist-get p :toaddress))
  6721. (from (plist-get p :fromaddress))
  6722. (table
  6723. (list
  6724. (cons "%c" (plist-get p :fromto))
  6725. (cons "%F" (plist-get p :from))
  6726. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6727. (cons "%T" (plist-get p :to))
  6728. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6729. (cons "%s" (plist-get p :subject))
  6730. (cons "%m" (plist-get p :message-id)))))
  6731. (when (string-match "%c" fmt)
  6732. ;; Check if the user wrote this message
  6733. (if (and org-from-is-user-regexp from to
  6734. (save-match-data (string-match org-from-is-user-regexp from)))
  6735. (setq fmt (replace-match "to %t" t t fmt))
  6736. (setq fmt (replace-match "from %f" t t fmt))))
  6737. (org-replace-escapes fmt table)))
  6738. (defun org-make-org-heading-search-string (&optional string heading)
  6739. "Make search string for STRING or current headline."
  6740. (interactive)
  6741. (let ((s (or string (org-get-heading))))
  6742. (unless (and string (not heading))
  6743. ;; We are using a headline, clean up garbage in there.
  6744. (if (string-match org-todo-regexp s)
  6745. (setq s (replace-match "" t t s)))
  6746. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6747. (setq s (replace-match "" t t s)))
  6748. (setq s (org-trim s))
  6749. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6750. org-comment-string "\\)") s)
  6751. (setq s (replace-match "" t t s)))
  6752. (while (string-match org-ts-regexp s)
  6753. (setq s (replace-match "" t t s))))
  6754. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6755. (setq s (replace-match " " t t s)))
  6756. (or string (setq s (concat "*" s))) ; Add * for headlines
  6757. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6758. (defun org-make-link (&rest strings)
  6759. "Concatenate STRINGS."
  6760. (apply 'concat strings))
  6761. (defun org-make-link-string (link &optional description)
  6762. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6763. (unless (string-match "\\S-" link)
  6764. (error "Empty link"))
  6765. (when (stringp description)
  6766. ;; Remove brackets from the description, they are fatal.
  6767. (while (string-match "\\[" description)
  6768. (setq description (replace-match "{" t t description)))
  6769. (while (string-match "\\]" description)
  6770. (setq description (replace-match "}" t t description))))
  6771. (when (equal (org-link-escape link) description)
  6772. ;; No description needed, it is identical
  6773. (setq description nil))
  6774. (when (and (not description)
  6775. (not (equal link (org-link-escape link))))
  6776. (setq description (org-extract-attributes link)))
  6777. (concat "[[" (org-link-escape link) "]"
  6778. (if description (concat "[" description "]") "")
  6779. "]"))
  6780. (defconst org-link-escape-chars
  6781. '((?\ . "%20")
  6782. (?\[ . "%5B")
  6783. (?\] . "%5D")
  6784. (?\340 . "%E0") ; `a
  6785. (?\342 . "%E2") ; ^a
  6786. (?\347 . "%E7") ; ,c
  6787. (?\350 . "%E8") ; `e
  6788. (?\351 . "%E9") ; 'e
  6789. (?\352 . "%EA") ; ^e
  6790. (?\356 . "%EE") ; ^i
  6791. (?\364 . "%F4") ; ^o
  6792. (?\371 . "%F9") ; `u
  6793. (?\373 . "%FB") ; ^u
  6794. (?\; . "%3B")
  6795. (?? . "%3F")
  6796. (?= . "%3D")
  6797. (?+ . "%2B")
  6798. )
  6799. "Association list of escapes for some characters problematic in links.
  6800. This is the list that is used for internal purposes.")
  6801. (defvar org-url-encoding-use-url-hexify nil)
  6802. (defconst org-link-escape-chars-browser
  6803. '((?\ . "%20")) ; 32 for the SPC char
  6804. "Association list of escapes for some characters problematic in links.
  6805. This is the list that is used before handing over to the browser.")
  6806. (defun org-link-escape (text &optional table)
  6807. "Escape characters in TEXT that are problematic for links."
  6808. (if org-url-encoding-use-url-hexify
  6809. (url-hexify-string text)
  6810. (setq table (or table org-link-escape-chars))
  6811. (when text
  6812. (let ((re (mapconcat (lambda (x) (regexp-quote
  6813. (char-to-string (car x))))
  6814. table "\\|")))
  6815. (while (string-match re text)
  6816. (setq text
  6817. (replace-match
  6818. (cdr (assoc (string-to-char (match-string 0 text))
  6819. table))
  6820. t t text)))
  6821. text))))
  6822. (defun org-link-unescape (text &optional table)
  6823. "Reverse the action of `org-link-escape'."
  6824. (if org-url-encoding-use-url-hexify
  6825. (url-unhex-string text)
  6826. (setq table (or table org-link-escape-chars))
  6827. (when text
  6828. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6829. table "\\|")))
  6830. (while (string-match re text)
  6831. (setq text
  6832. (replace-match
  6833. (char-to-string (car (rassoc (match-string 0 text) table)))
  6834. t t text)))
  6835. text))))
  6836. (defun org-xor (a b)
  6837. "Exclusive or."
  6838. (if a (not b) b))
  6839. (defun org-fixup-message-id-for-http (s)
  6840. "Replace special characters in a message id, so it can be used in an http query."
  6841. (while (string-match "<" s)
  6842. (setq s (replace-match "%3C" t t s)))
  6843. (while (string-match ">" s)
  6844. (setq s (replace-match "%3E" t t s)))
  6845. (while (string-match "@" s)
  6846. (setq s (replace-match "%40" t t s)))
  6847. s)
  6848. ;;;###autoload
  6849. (defun org-insert-link-global ()
  6850. "Insert a link like Org-mode does.
  6851. This command can be called in any mode to insert a link in Org-mode syntax."
  6852. (interactive)
  6853. (org-load-modules-maybe)
  6854. (org-run-like-in-org-mode 'org-insert-link))
  6855. (defun org-insert-link (&optional complete-file link-location)
  6856. "Insert a link. At the prompt, enter the link.
  6857. Completion can be used to insert any of the link protocol prefixes like
  6858. http or ftp in use.
  6859. The history can be used to select a link previously stored with
  6860. `org-store-link'. When the empty string is entered (i.e. if you just
  6861. press RET at the prompt), the link defaults to the most recently
  6862. stored link. As SPC triggers completion in the minibuffer, you need to
  6863. use M-SPC or C-q SPC to force the insertion of a space character.
  6864. You will also be prompted for a description, and if one is given, it will
  6865. be displayed in the buffer instead of the link.
  6866. If there is already a link at point, this command will allow you to edit link
  6867. and description parts.
  6868. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6869. be selected using completion. The path to the file will be relative to the
  6870. current directory if the file is in the current directory or a subdirectory.
  6871. Otherwise, the link will be the absolute path as completed in the minibuffer
  6872. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6873. option `org-link-file-path-type'.
  6874. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6875. the current directory or below.
  6876. With three \\[universal-argument] prefixes, negate the meaning of
  6877. `org-keep-stored-link-after-insertion'.
  6878. If `org-make-link-description-function' is non-nil, this function will be
  6879. called with the link target, and the result will be the default
  6880. link description.
  6881. If the LINK-LOCATION parameter is non-nil, this value will be
  6882. used as the link location instead of reading one interactively."
  6883. (interactive "P")
  6884. (let* ((wcf (current-window-configuration))
  6885. (region (if (org-region-active-p)
  6886. (buffer-substring (region-beginning) (region-end))))
  6887. (remove (and region (list (region-beginning) (region-end))))
  6888. (desc region)
  6889. tmphist ; byte-compile incorrectly complains about this
  6890. (link link-location)
  6891. entry file all-prefixes)
  6892. (cond
  6893. (link-location) ; specified by arg, just use it.
  6894. ((org-in-regexp org-bracket-link-regexp 1)
  6895. ;; We do have a link at point, and we are going to edit it.
  6896. (setq remove (list (match-beginning 0) (match-end 0)))
  6897. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6898. (setq link (read-string "Link: "
  6899. (org-link-unescape
  6900. (org-match-string-no-properties 1)))))
  6901. ((or (org-in-regexp org-angle-link-re)
  6902. (org-in-regexp org-plain-link-re))
  6903. ;; Convert to bracket link
  6904. (setq remove (list (match-beginning 0) (match-end 0))
  6905. link (read-string "Link: "
  6906. (org-remove-angle-brackets (match-string 0)))))
  6907. ((member complete-file '((4) (16)))
  6908. ;; Completing read for file names.
  6909. (setq link (org-file-complete-link complete-file)))
  6910. (t
  6911. ;; Read link, with completion for stored links.
  6912. (with-output-to-temp-buffer "*Org Links*"
  6913. (princ "Insert a link.
  6914. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6915. (when org-stored-links
  6916. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6917. (princ (mapconcat
  6918. (lambda (x)
  6919. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6920. (reverse org-stored-links) "\n"))))
  6921. (let ((cw (selected-window)))
  6922. (select-window (get-buffer-window "*Org Links*"))
  6923. (setq truncate-lines t)
  6924. (unless (pos-visible-in-window-p (point-max))
  6925. (org-fit-window-to-buffer))
  6926. (and (window-live-p cw) (select-window cw)))
  6927. ;; Fake a link history, containing the stored links.
  6928. (setq tmphist (append (mapcar 'car org-stored-links)
  6929. org-insert-link-history))
  6930. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6931. (mapcar 'car org-link-abbrev-alist)
  6932. org-link-types))
  6933. (unwind-protect
  6934. (progn
  6935. (setq link
  6936. (let ((org-completion-use-ido nil)
  6937. (org-completion-use-iswitchb nil))
  6938. (org-completing-read
  6939. "Link: "
  6940. (append
  6941. (mapcar (lambda (x) (list (concat x ":")))
  6942. all-prefixes)
  6943. (mapcar 'car org-stored-links))
  6944. nil nil nil
  6945. 'tmphist
  6946. (car (car org-stored-links)))))
  6947. (if (or (member link all-prefixes)
  6948. (and (equal ":" (substring link -1))
  6949. (member (substring link 0 -1) all-prefixes)
  6950. (setq link (substring link 0 -1))))
  6951. (setq link (org-link-try-special-completion link))))
  6952. (set-window-configuration wcf)
  6953. (kill-buffer "*Org Links*"))
  6954. (setq entry (assoc link org-stored-links))
  6955. (or entry (push link org-insert-link-history))
  6956. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6957. (not org-keep-stored-link-after-insertion))
  6958. (setq org-stored-links (delq (assoc link org-stored-links)
  6959. org-stored-links)))
  6960. (setq desc (or desc (nth 1 entry)))))
  6961. (if (string-match org-plain-link-re link)
  6962. ;; URL-like link, normalize the use of angular brackets.
  6963. (setq link (org-make-link (org-remove-angle-brackets link))))
  6964. ;; Check if we are linking to the current file with a search option
  6965. ;; If yes, simplify the link by using only the search option.
  6966. (when (and buffer-file-name
  6967. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6968. (let* ((path (match-string 1 link))
  6969. (case-fold-search nil)
  6970. (search (match-string 2 link)))
  6971. (save-match-data
  6972. (if (equal (file-truename buffer-file-name) (file-truename path))
  6973. ;; We are linking to this same file, with a search option
  6974. (setq link search)))))
  6975. ;; Check if we can/should use a relative path. If yes, simplify the link
  6976. (when (string-match "^file:\\(.*\\)" link)
  6977. (let* ((path (match-string 1 link))
  6978. (origpath path)
  6979. (case-fold-search nil))
  6980. (cond
  6981. ((or (eq org-link-file-path-type 'absolute)
  6982. (equal complete-file '(16)))
  6983. (setq path (abbreviate-file-name (expand-file-name path))))
  6984. ((eq org-link-file-path-type 'noabbrev)
  6985. (setq path (expand-file-name path)))
  6986. ((eq org-link-file-path-type 'relative)
  6987. (setq path (file-relative-name path)))
  6988. (t
  6989. (save-match-data
  6990. (if (string-match (concat "^" (regexp-quote
  6991. (file-name-as-directory
  6992. (expand-file-name "."))))
  6993. (expand-file-name path))
  6994. ;; We are linking a file with relative path name.
  6995. (setq path (substring (expand-file-name path)
  6996. (match-end 0)))
  6997. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6998. (setq link (concat "file:" path))
  6999. (if (equal desc origpath)
  7000. (setq desc path))))
  7001. (if org-make-link-description-function
  7002. (setq desc (funcall org-make-link-description-function link desc)))
  7003. (setq desc (read-string "Description: " desc))
  7004. (unless (string-match "\\S-" desc) (setq desc nil))
  7005. (if remove (apply 'delete-region remove))
  7006. (insert (org-make-link-string link desc))))
  7007. (defun org-link-try-special-completion (type)
  7008. "If there is completion support for link type TYPE, offer it."
  7009. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7010. (if (functionp fun)
  7011. (funcall fun)
  7012. (read-string "Link (no completion support): " (concat type ":")))))
  7013. (defun org-file-complete-link (&optional arg)
  7014. "Create a file link using completion."
  7015. (let (file link)
  7016. (setq file (read-file-name "File: "))
  7017. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7018. (pwd1 (file-name-as-directory (abbreviate-file-name
  7019. (expand-file-name ".")))))
  7020. (cond
  7021. ((equal arg '(16))
  7022. (setq link (org-make-link
  7023. "file:"
  7024. (abbreviate-file-name (expand-file-name file)))))
  7025. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7026. (setq link (org-make-link "file:" (match-string 1 file))))
  7027. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7028. (expand-file-name file))
  7029. (setq link (org-make-link
  7030. "file:" (match-string 1 (expand-file-name file)))))
  7031. (t (setq link (org-make-link "file:" file)))))
  7032. link))
  7033. (defun org-completing-read (&rest args)
  7034. "Completing-read with SPACE being a normal character."
  7035. (let ((minibuffer-local-completion-map
  7036. (copy-keymap minibuffer-local-completion-map)))
  7037. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7038. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7039. (apply 'org-icompleting-read args)))
  7040. (defun org-completing-read-no-i (&rest args)
  7041. (let (org-completion-use-ido org-completion-use-iswitchb)
  7042. (apply 'org-completing-read args)))
  7043. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7044. "Use iswitch as a completing-read replacement to choose from choices.
  7045. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7046. from."
  7047. (let* ((iswitchb-use-virtual-buffers nil)
  7048. (iswitchb-make-buflist-hook
  7049. (lambda ()
  7050. (setq iswitchb-temp-buflist choices))))
  7051. (iswitchb-read-buffer prompt)))
  7052. (defun org-icompleting-read (&rest args)
  7053. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7054. (org-without-partial-completion
  7055. (if (and org-completion-use-ido
  7056. (fboundp 'ido-completing-read)
  7057. (boundp 'ido-mode) ido-mode
  7058. (listp (second args)))
  7059. (let ((ido-enter-matching-directory nil))
  7060. (apply 'ido-completing-read (concat (car args))
  7061. (if (consp (car (nth 1 args)))
  7062. (mapcar (lambda (x) (car x)) (nth 1 args))
  7063. (nth 1 args))
  7064. (cddr args)))
  7065. (if (and org-completion-use-iswitchb
  7066. (boundp 'iswitchb-mode) iswitchb-mode
  7067. (listp (second args)))
  7068. (apply 'org-iswitchb-completing-read (concat (car args))
  7069. (if (consp (car (nth 1 args)))
  7070. (mapcar (lambda (x) (car x)) (nth 1 args))
  7071. (nth 1 args))
  7072. (cddr args))
  7073. (apply 'completing-read args)))))
  7074. (defun org-extract-attributes (s)
  7075. "Extract the attributes cookie from a string and set as text property."
  7076. (let (a attr (start 0) key value)
  7077. (save-match-data
  7078. (when (string-match "{{\\([^}]+\\)}}$" s)
  7079. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7080. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7081. (setq key (match-string 1 a) value (match-string 2 a)
  7082. start (match-end 0)
  7083. attr (plist-put attr (intern key) value))))
  7084. (org-add-props s nil 'org-attr attr))
  7085. s))
  7086. (defun org-extract-attributes-from-string (tag)
  7087. (let (key value attr)
  7088. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7089. (setq key (match-string 1 tag) value (match-string 2 tag)
  7090. tag (replace-match "" t t tag)
  7091. attr (plist-put attr (intern key) value)))
  7092. (cons tag attr)))
  7093. (defun org-attributes-to-string (plist)
  7094. "Format a property list into an HTML attribute list."
  7095. (let ((s "") key value)
  7096. (while plist
  7097. (setq key (pop plist) value (pop plist))
  7098. (and value
  7099. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7100. s))
  7101. ;;; Opening/following a link
  7102. (defvar org-link-search-failed nil)
  7103. (defun org-next-link ()
  7104. "Move forward to the next link.
  7105. If the link is in hidden text, expose it."
  7106. (interactive)
  7107. (when (and org-link-search-failed (eq this-command last-command))
  7108. (goto-char (point-min))
  7109. (message "Link search wrapped back to beginning of buffer"))
  7110. (setq org-link-search-failed nil)
  7111. (let* ((pos (point))
  7112. (ct (org-context))
  7113. (a (assoc :link ct)))
  7114. (if a (goto-char (nth 2 a)))
  7115. (if (re-search-forward org-any-link-re nil t)
  7116. (progn
  7117. (goto-char (match-beginning 0))
  7118. (if (org-invisible-p) (org-show-context)))
  7119. (goto-char pos)
  7120. (setq org-link-search-failed t)
  7121. (error "No further link found"))))
  7122. (defun org-previous-link ()
  7123. "Move backward to the previous link.
  7124. If the link is in hidden text, expose it."
  7125. (interactive)
  7126. (when (and org-link-search-failed (eq this-command last-command))
  7127. (goto-char (point-max))
  7128. (message "Link search wrapped back to end of buffer"))
  7129. (setq org-link-search-failed nil)
  7130. (let* ((pos (point))
  7131. (ct (org-context))
  7132. (a (assoc :link ct)))
  7133. (if a (goto-char (nth 1 a)))
  7134. (if (re-search-backward org-any-link-re nil t)
  7135. (progn
  7136. (goto-char (match-beginning 0))
  7137. (if (org-invisible-p) (org-show-context)))
  7138. (goto-char pos)
  7139. (setq org-link-search-failed t)
  7140. (error "No further link found"))))
  7141. (defun org-translate-link (s)
  7142. "Translate a link string if a translation function has been defined."
  7143. (if (and org-link-translation-function
  7144. (fboundp org-link-translation-function)
  7145. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7146. (progn
  7147. (setq s (funcall org-link-translation-function
  7148. (match-string 1) (match-string 2)))
  7149. (concat (car s) ":" (cdr s)))
  7150. s))
  7151. (defun org-translate-link-from-planner (type path)
  7152. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7153. This is still an experimental function, your mileage may vary."
  7154. (cond
  7155. ((member type '("http" "https" "news" "ftp"))
  7156. ;; standard Internet links are the same.
  7157. nil)
  7158. ((and (equal type "irc") (string-match "^//" path))
  7159. ;; Planner has two / at the beginning of an irc link, we have 1.
  7160. ;; We should have zero, actually....
  7161. (setq path (substring path 1)))
  7162. ((and (equal type "lisp") (string-match "^/" path))
  7163. ;; Planner has a slash, we do not.
  7164. (setq type "elisp" path (substring path 1)))
  7165. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7166. ;; A typical message link. Planner has the id after the fina slash,
  7167. ;; we separate it with a hash mark
  7168. (setq path (concat (match-string 1 path) "#"
  7169. (org-remove-angle-brackets (match-string 2 path)))))
  7170. )
  7171. (cons type path))
  7172. (defun org-find-file-at-mouse (ev)
  7173. "Open file link or URL at mouse."
  7174. (interactive "e")
  7175. (mouse-set-point ev)
  7176. (org-open-at-point 'in-emacs))
  7177. (defun org-open-at-mouse (ev)
  7178. "Open file link or URL at mouse."
  7179. (interactive "e")
  7180. (mouse-set-point ev)
  7181. (if (eq major-mode 'org-agenda-mode)
  7182. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7183. (org-open-at-point))
  7184. (defvar org-window-config-before-follow-link nil
  7185. "The window configuration before following a link.
  7186. This is saved in case the need arises to restore it.")
  7187. (defvar org-open-link-marker (make-marker)
  7188. "Marker pointing to the location where `org-open-at-point; was called.")
  7189. ;;;###autoload
  7190. (defun org-open-at-point-global ()
  7191. "Follow a link like Org-mode does.
  7192. This command can be called in any mode to follow a link that has
  7193. Org-mode syntax."
  7194. (interactive)
  7195. (org-run-like-in-org-mode 'org-open-at-point))
  7196. ;;;###autoload
  7197. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7198. "Open a link in the string S, as if it was in Org-mode."
  7199. (interactive "sLink: \nP")
  7200. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7201. (with-temp-buffer
  7202. (let ((org-inhibit-startup t))
  7203. (org-mode)
  7204. (insert s)
  7205. (goto-char (point-min))
  7206. (org-open-at-point arg reference-buffer)))))
  7207. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7208. "Open link at or after point.
  7209. If there is no link at point, this function will search forward up to
  7210. the end of the current line.
  7211. Normally, files will be opened by an appropriate application. If the
  7212. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7213. With a double prefix argument, try to open outside of Emacs, in the
  7214. application the system uses for this file type."
  7215. (interactive "P")
  7216. (org-load-modules-maybe)
  7217. (move-marker org-open-link-marker (point))
  7218. (setq org-window-config-before-follow-link (current-window-configuration))
  7219. (org-remove-occur-highlights nil nil t)
  7220. (cond
  7221. ((and (org-on-heading-p)
  7222. (not (org-in-regexp
  7223. (concat org-plain-link-re "\\|"
  7224. org-bracket-link-regexp "\\|"
  7225. org-angle-link-re "\\|"
  7226. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7227. (or (org-offer-links-in-entry in-emacs)
  7228. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7229. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7230. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7231. (org-footnote-action))
  7232. (t
  7233. (let (type path link line search (pos (point)))
  7234. (catch 'match
  7235. (save-excursion
  7236. (skip-chars-forward "^]\n\r")
  7237. (when (org-in-regexp org-bracket-link-regexp)
  7238. (setq link (org-extract-attributes
  7239. (org-link-unescape (org-match-string-no-properties 1))))
  7240. (while (string-match " *\n *" link)
  7241. (setq link (replace-match " " t t link)))
  7242. (setq link (org-link-expand-abbrev link))
  7243. (cond
  7244. ((or (file-name-absolute-p link)
  7245. (string-match "^\\.\\.?/" link))
  7246. (setq type "file" path link))
  7247. ((string-match org-link-re-with-space3 link)
  7248. (setq type (match-string 1 link) path (match-string 2 link)))
  7249. (t (setq type "thisfile" path link)))
  7250. (throw 'match t)))
  7251. (when (get-text-property (point) 'org-linked-text)
  7252. (setq type "thisfile"
  7253. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7254. (1+ (point)) (point))
  7255. path (buffer-substring
  7256. (previous-single-property-change pos 'org-linked-text)
  7257. (next-single-property-change pos 'org-linked-text)))
  7258. (throw 'match t))
  7259. (save-excursion
  7260. (when (or (org-in-regexp org-angle-link-re)
  7261. (org-in-regexp org-plain-link-re))
  7262. (setq type (match-string 1) path (match-string 2))
  7263. (throw 'match t)))
  7264. (save-excursion
  7265. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7266. (setq type "tags"
  7267. path (match-string 1))
  7268. (while (string-match ":" path)
  7269. (setq path (replace-match "+" t t path)))
  7270. (throw 'match t)))
  7271. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7272. (setq type "tree-match"
  7273. path (match-string 1))
  7274. (throw 'match t)))
  7275. (unless path
  7276. (error "No link found"))
  7277. ;; switch back to reference buffer
  7278. ;; needed when if called in a temporary buffer through
  7279. ;; org-open-link-from-string
  7280. (with-current-buffer (or reference-buffer (current-buffer))
  7281. ;; Remove any trailing spaces in path
  7282. (if (string-match " +\\'" path)
  7283. (setq path (replace-match "" t t path)))
  7284. (if (and org-link-translation-function
  7285. (fboundp org-link-translation-function))
  7286. ;; Check if we need to translate the link
  7287. (let ((tmp (funcall org-link-translation-function type path)))
  7288. (setq type (car tmp) path (cdr tmp))))
  7289. (cond
  7290. ((assoc type org-link-protocols)
  7291. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7292. ((equal type "mailto")
  7293. (let ((cmd (car org-link-mailto-program))
  7294. (args (cdr org-link-mailto-program)) args1
  7295. (address path) (subject "") a)
  7296. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7297. (setq address (match-string 1 path)
  7298. subject (org-link-escape (match-string 2 path))))
  7299. (while args
  7300. (cond
  7301. ((not (stringp (car args))) (push (pop args) args1))
  7302. (t (setq a (pop args))
  7303. (if (string-match "%a" a)
  7304. (setq a (replace-match address t t a)))
  7305. (if (string-match "%s" a)
  7306. (setq a (replace-match subject t t a)))
  7307. (push a args1))))
  7308. (apply cmd (nreverse args1))))
  7309. ((member type '("http" "https" "ftp" "news"))
  7310. (browse-url (concat type ":" (org-link-escape
  7311. path org-link-escape-chars-browser))))
  7312. ((member type '("message"))
  7313. (browse-url (concat type ":" path)))
  7314. ((string= type "tags")
  7315. (org-tags-view in-emacs path))
  7316. ((string= type "thisfile")
  7317. (if in-emacs
  7318. (switch-to-buffer-other-window
  7319. (org-get-buffer-for-internal-link (current-buffer)))
  7320. (org-mark-ring-push))
  7321. (let ((cmd `(org-link-search
  7322. ,path
  7323. ,(cond ((equal in-emacs '(4)) 'occur)
  7324. ((equal in-emacs '(16)) 'org-occur)
  7325. (t nil))
  7326. ,pos)))
  7327. (condition-case nil (eval cmd)
  7328. (error (progn (widen) (eval cmd))))))
  7329. ((string= type "tree-match")
  7330. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7331. ((string= type "file")
  7332. (if (string-match "::\\([0-9]+\\)\\'" path)
  7333. (setq line (string-to-number (match-string 1 path))
  7334. path (substring path 0 (match-beginning 0)))
  7335. (if (string-match "::\\(.+\\)\\'" path)
  7336. (setq search (match-string 1 path)
  7337. path (substring path 0 (match-beginning 0)))))
  7338. (if (string-match "[*?{]" (file-name-nondirectory path))
  7339. (dired path)
  7340. (org-open-file path in-emacs line search)))
  7341. ((string= type "news")
  7342. (require 'org-gnus)
  7343. (org-gnus-follow-link path))
  7344. ((string= type "shell")
  7345. (let ((cmd path))
  7346. (if (or (not org-confirm-shell-link-function)
  7347. (funcall org-confirm-shell-link-function
  7348. (format "Execute \"%s\" in shell? "
  7349. (org-add-props cmd nil
  7350. 'face 'org-warning))))
  7351. (progn
  7352. (message "Executing %s" cmd)
  7353. (shell-command cmd))
  7354. (error "Abort"))))
  7355. ((string= type "elisp")
  7356. (let ((cmd path))
  7357. (if (or (not org-confirm-elisp-link-function)
  7358. (funcall org-confirm-elisp-link-function
  7359. (format "Execute \"%s\" as elisp? "
  7360. (org-add-props cmd nil
  7361. 'face 'org-warning))))
  7362. (message "%s => %s" cmd
  7363. (if (equal (string-to-char cmd) ?\()
  7364. (eval (read cmd))
  7365. (call-interactively (read cmd))))
  7366. (error "Abort"))))
  7367. (t
  7368. (browse-url-at-point)))))))
  7369. (move-marker org-open-link-marker nil)
  7370. (run-hook-with-args 'org-follow-link-hook))
  7371. (defun org-offer-links-in-entry (&optional nth zero)
  7372. "Offer links in the curren entry and follow the selected link.
  7373. If there is only one link, follow it immediately as well.
  7374. If NTH is an integer, immediately pick the NTH link found.
  7375. If ZERO is a string, check also this string for a link, and if
  7376. there is one, offer it as link number zero."
  7377. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7378. "\\(" org-angle-link-re "\\)\\|"
  7379. "\\(" org-plain-link-re "\\)"))
  7380. (cnt ?0)
  7381. (in-emacs (if (integerp nth) nil nth))
  7382. have-zero end links link c)
  7383. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7384. (push (match-string 0 zero) links)
  7385. (setq cnt (1- cnt) have-zero t))
  7386. (save-excursion
  7387. (org-back-to-heading t)
  7388. (setq end (save-excursion (outline-next-heading) (point)))
  7389. (while (re-search-forward re end t)
  7390. (push (match-string 0) links))
  7391. (setq links (org-uniquify (reverse links))))
  7392. (cond
  7393. ((null links)
  7394. (message "No links"))
  7395. ((equal (length links) 1)
  7396. (setq link (car links)))
  7397. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7398. (setq link (nth (if have-zero nth (1- nth)) links)))
  7399. (t ; we have to select a link
  7400. (save-excursion
  7401. (save-window-excursion
  7402. (delete-other-windows)
  7403. (with-output-to-temp-buffer "*Select Link*"
  7404. (mapc (lambda (l)
  7405. (if (not (string-match org-bracket-link-regexp l))
  7406. (princ (format "[%c] %s\n" (incf cnt)
  7407. (org-remove-angle-brackets l)))
  7408. (if (match-end 3)
  7409. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7410. (match-string 3 l) (match-string 1 l)))
  7411. (princ (format "[%c] %s\n" (incf cnt)
  7412. (match-string 1 l))))))
  7413. links))
  7414. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7415. (message "Select link to open:")
  7416. (setq c (read-char-exclusive))
  7417. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7418. (when (equal c ?q) (error "Abort"))
  7419. (setq nth (- c ?0))
  7420. (if have-zero (setq nth (1+ nth)))
  7421. (unless (and (integerp nth) (>= (length links) nth))
  7422. (error "Invalid link selection"))
  7423. (setq link (nth (1- nth) links))))
  7424. (if link
  7425. (progn (org-open-link-from-string link in-emacs (current-buffer)) t)
  7426. nil)))
  7427. ;;;; Time estimates
  7428. (defun org-get-effort (&optional pom)
  7429. "Get the effort estimate for the current entry."
  7430. (org-entry-get pom org-effort-property))
  7431. ;;; File search
  7432. (defvar org-create-file-search-functions nil
  7433. "List of functions to construct the right search string for a file link.
  7434. These functions are called in turn with point at the location to
  7435. which the link should point.
  7436. A function in the hook should first test if it would like to
  7437. handle this file type, for example by checking the major-mode or
  7438. the file extension. If it decides not to handle this file, it
  7439. should just return nil to give other functions a chance. If it
  7440. does handle the file, it must return the search string to be used
  7441. when following the link. The search string will be part of the
  7442. file link, given after a double colon, and `org-open-at-point'
  7443. will automatically search for it. If special measures must be
  7444. taken to make the search successful, another function should be
  7445. added to the companion hook `org-execute-file-search-functions',
  7446. which see.
  7447. A function in this hook may also use `setq' to set the variable
  7448. `description' to provide a suggestion for the descriptive text to
  7449. be used for this link when it gets inserted into an Org-mode
  7450. buffer with \\[org-insert-link].")
  7451. (defvar org-execute-file-search-functions nil
  7452. "List of functions to execute a file search triggered by a link.
  7453. Functions added to this hook must accept a single argument, the
  7454. search string that was part of the file link, the part after the
  7455. double colon. The function must first check if it would like to
  7456. handle this search, for example by checking the major-mode or the
  7457. file extension. If it decides not to handle this search, it
  7458. should just return nil to give other functions a chance. If it
  7459. does handle the search, it must return a non-nil value to keep
  7460. other functions from trying.
  7461. Each function can access the current prefix argument through the
  7462. variable `current-prefix-argument'. Note that a single prefix is
  7463. used to force opening a link in Emacs, so it may be good to only
  7464. use a numeric or double prefix to guide the search function.
  7465. In case this is needed, a function in this hook can also restore
  7466. the window configuration before `org-open-at-point' was called using:
  7467. (set-window-configuration org-window-config-before-follow-link)")
  7468. (defun org-link-search (s &optional type avoid-pos)
  7469. "Search for a link search option.
  7470. If S is surrounded by forward slashes, it is interpreted as a
  7471. regular expression. In org-mode files, this will create an `org-occur'
  7472. sparse tree. In ordinary files, `occur' will be used to list matches.
  7473. If the current buffer is in `dired-mode', grep will be used to search
  7474. in all files. If AVOID-POS is given, ignore matches near that position."
  7475. (let ((case-fold-search t)
  7476. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7477. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7478. (append '(("") (" ") ("\t") ("\n"))
  7479. org-emphasis-alist)
  7480. "\\|") "\\)"))
  7481. (pos (point))
  7482. (pre nil) (post nil)
  7483. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7484. (cond
  7485. ;; First check if there are any special
  7486. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7487. ;; Now try the builtin stuff
  7488. ((and (equal (string-to-char s0) ?#)
  7489. (> (length s0) 1)
  7490. (save-excursion
  7491. (goto-char (point-min))
  7492. (and
  7493. (re-search-forward
  7494. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7495. (setq type 'dedicated
  7496. pos (match-beginning 0))))
  7497. ;; There is an exact target for this
  7498. (goto-char pos)
  7499. (org-back-to-heading t)))
  7500. ((save-excursion
  7501. (goto-char (point-min))
  7502. (and
  7503. (re-search-forward
  7504. (concat "<<" (regexp-quote s0) ">>") nil t)
  7505. (setq type 'dedicated
  7506. pos (match-beginning 0))))
  7507. ;; There is an exact target for this
  7508. (goto-char pos))
  7509. ((and (string-match "^(\\(.*\\))$" s0)
  7510. (save-excursion
  7511. (goto-char (point-min))
  7512. (and
  7513. (re-search-forward
  7514. (concat "[^[]" (regexp-quote
  7515. (format org-coderef-label-format
  7516. (match-string 1 s0))))
  7517. nil t)
  7518. (setq type 'dedicated
  7519. pos (1+ (match-beginning 0))))))
  7520. ;; There is a coderef target for this
  7521. (goto-char pos))
  7522. ((string-match "^/\\(.*\\)/$" s)
  7523. ;; A regular expression
  7524. (cond
  7525. ((org-mode-p)
  7526. (org-occur (match-string 1 s)))
  7527. ;;((eq major-mode 'dired-mode)
  7528. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7529. (t (org-do-occur (match-string 1 s)))))
  7530. (t
  7531. ;; A normal search strings
  7532. (when (equal (string-to-char s) ?*)
  7533. ;; Anchor on headlines, post may include tags.
  7534. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7535. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7536. s (substring s 1)))
  7537. (remove-text-properties
  7538. 0 (length s)
  7539. '(face nil mouse-face nil keymap nil fontified nil) s)
  7540. ;; Make a series of regular expressions to find a match
  7541. (setq words (org-split-string s "[ \n\r\t]+")
  7542. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7543. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7544. "\\)" markers)
  7545. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7546. re2a (concat "[ \t\r\n]" re2a_)
  7547. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7548. re4 (concat "[^a-zA-Z_]" re4_)
  7549. re1 (concat pre re2 post)
  7550. re3 (concat pre (if pre re4_ re4) post)
  7551. re5 (concat pre ".*" re4)
  7552. re2 (concat pre re2)
  7553. re2a (concat pre (if pre re2a_ re2a))
  7554. re4 (concat pre (if pre re4_ re4))
  7555. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7556. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7557. re5 "\\)"
  7558. ))
  7559. (cond
  7560. ((eq type 'org-occur) (org-occur reall))
  7561. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7562. (t (goto-char (point-min))
  7563. (setq type 'fuzzy)
  7564. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7565. (org-search-not-self 1 re1 nil t)
  7566. (org-search-not-self 1 re2 nil t)
  7567. (org-search-not-self 1 re2a nil t)
  7568. (org-search-not-self 1 re3 nil t)
  7569. (org-search-not-self 1 re4 nil t)
  7570. (org-search-not-self 1 re5 nil t)
  7571. )
  7572. (goto-char (match-beginning 1))
  7573. (goto-char pos)
  7574. (error "No match")))))
  7575. (t
  7576. ;; Normal string-search
  7577. (goto-char (point-min))
  7578. (if (search-forward s nil t)
  7579. (goto-char (match-beginning 0))
  7580. (error "No match"))))
  7581. (and (org-mode-p) (org-show-context 'link-search))
  7582. type))
  7583. (defun org-search-not-self (group &rest args)
  7584. "Execute `re-search-forward', but only accept matches that do not
  7585. enclose the position of `org-open-link-marker'."
  7586. (let ((m org-open-link-marker))
  7587. (catch 'exit
  7588. (while (apply 're-search-forward args)
  7589. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7590. (goto-char (match-end group))
  7591. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7592. (> (match-beginning 0) (marker-position m))
  7593. (< (match-end 0) (marker-position m)))
  7594. (save-match-data
  7595. (or (not (org-in-regexp
  7596. org-bracket-link-analytic-regexp 1))
  7597. (not (match-end 4)) ; no description
  7598. (and (<= (match-beginning 4) (point))
  7599. (>= (match-end 4) (point))))))
  7600. (throw 'exit (point))))))))
  7601. (defun org-get-buffer-for-internal-link (buffer)
  7602. "Return a buffer to be used for displaying the link target of internal links."
  7603. (cond
  7604. ((not org-display-internal-link-with-indirect-buffer)
  7605. buffer)
  7606. ((string-match "(Clone)$" (buffer-name buffer))
  7607. (message "Buffer is already a clone, not making another one")
  7608. ;; we also do not modify visibility in this case
  7609. buffer)
  7610. (t ; make a new indirect buffer for displaying the link
  7611. (let* ((bn (buffer-name buffer))
  7612. (ibn (concat bn "(Clone)"))
  7613. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7614. (with-current-buffer ib (org-overview))
  7615. ib))))
  7616. (defun org-do-occur (regexp &optional cleanup)
  7617. "Call the Emacs command `occur'.
  7618. If CLEANUP is non-nil, remove the printout of the regular expression
  7619. in the *Occur* buffer. This is useful if the regex is long and not useful
  7620. to read."
  7621. (occur regexp)
  7622. (when cleanup
  7623. (let ((cwin (selected-window)) win beg end)
  7624. (when (setq win (get-buffer-window "*Occur*"))
  7625. (select-window win))
  7626. (goto-char (point-min))
  7627. (when (re-search-forward "match[a-z]+" nil t)
  7628. (setq beg (match-end 0))
  7629. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7630. (setq end (1- (match-beginning 0)))))
  7631. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7632. (goto-char (point-min))
  7633. (select-window cwin))))
  7634. ;;; The mark ring for links jumps
  7635. (defvar org-mark-ring nil
  7636. "Mark ring for positions before jumps in Org-mode.")
  7637. (defvar org-mark-ring-last-goto nil
  7638. "Last position in the mark ring used to go back.")
  7639. ;; Fill and close the ring
  7640. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7641. (loop for i from 1 to org-mark-ring-length do
  7642. (push (make-marker) org-mark-ring))
  7643. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7644. org-mark-ring)
  7645. (defun org-mark-ring-push (&optional pos buffer)
  7646. "Put the current position or POS into the mark ring and rotate it."
  7647. (interactive)
  7648. (setq pos (or pos (point)))
  7649. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7650. (move-marker (car org-mark-ring)
  7651. (or pos (point))
  7652. (or buffer (current-buffer)))
  7653. (message "%s"
  7654. (substitute-command-keys
  7655. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7656. (defun org-mark-ring-goto (&optional n)
  7657. "Jump to the previous position in the mark ring.
  7658. With prefix arg N, jump back that many stored positions. When
  7659. called several times in succession, walk through the entire ring.
  7660. Org-mode commands jumping to a different position in the current file,
  7661. or to another Org-mode file, automatically push the old position
  7662. onto the ring."
  7663. (interactive "p")
  7664. (let (p m)
  7665. (if (eq last-command this-command)
  7666. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7667. (setq p org-mark-ring))
  7668. (setq org-mark-ring-last-goto p)
  7669. (setq m (car p))
  7670. (switch-to-buffer (marker-buffer m))
  7671. (goto-char m)
  7672. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7673. (defun org-remove-angle-brackets (s)
  7674. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7675. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7676. s)
  7677. (defun org-add-angle-brackets (s)
  7678. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7679. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7680. s)
  7681. (defun org-remove-double-quotes (s)
  7682. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7683. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7684. s)
  7685. ;;; Following specific links
  7686. (defun org-follow-timestamp-link ()
  7687. (cond
  7688. ((org-at-date-range-p t)
  7689. (let ((org-agenda-start-on-weekday)
  7690. (t1 (match-string 1))
  7691. (t2 (match-string 2)))
  7692. (setq t1 (time-to-days (org-time-string-to-time t1))
  7693. t2 (time-to-days (org-time-string-to-time t2)))
  7694. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7695. ((org-at-timestamp-p t)
  7696. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7697. (substring (match-string 1) 0 10)))
  7698. 1))
  7699. (t (error "This should not happen"))))
  7700. ;;; Following file links
  7701. (defvar org-wait nil)
  7702. (defun org-open-file (path &optional in-emacs line search)
  7703. "Open the file at PATH.
  7704. First, this expands any special file name abbreviations. Then the
  7705. configuration variable `org-file-apps' is checked if it contains an
  7706. entry for this file type, and if yes, the corresponding command is launched.
  7707. If no application is found, Emacs simply visits the file.
  7708. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7709. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7710. and o use an external application to visit the file.
  7711. Optional LINE specifies a line to go to, optional SEARCH a string to
  7712. search for. If LINE or SEARCH is given, the file will always be
  7713. opened in Emacs.
  7714. If the file does not exist, an error is thrown."
  7715. (setq in-emacs (or in-emacs line search))
  7716. (let* ((file (if (equal path "")
  7717. buffer-file-name
  7718. (substitute-in-file-name (expand-file-name path))))
  7719. (apps (append org-file-apps (org-default-apps)))
  7720. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7721. (dirp (if remp nil (file-directory-p file)))
  7722. (file (if (and dirp org-open-directory-means-index-dot-org)
  7723. (concat (file-name-as-directory file) "index.org")
  7724. file))
  7725. (a-m-a-p (assq 'auto-mode apps))
  7726. (dfile (downcase file))
  7727. (old-buffer (current-buffer))
  7728. (old-pos (point))
  7729. (old-mode major-mode)
  7730. ext cmd)
  7731. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7732. (setq ext (match-string 1 dfile))
  7733. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7734. (setq ext (match-string 1 dfile))))
  7735. (cond
  7736. ((equal in-emacs '(16))
  7737. (setq cmd (cdr (assoc 'system apps))))
  7738. (in-emacs (setq cmd 'emacs))
  7739. (t
  7740. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7741. (and dirp (cdr (assoc 'directory apps)))
  7742. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7743. 'string-match)
  7744. (cdr (assoc ext apps))
  7745. (cdr (assoc t apps))))))
  7746. (when (eq cmd 'system)
  7747. (setq cmd (cdr (assoc 'system apps))))
  7748. (when (eq cmd 'default)
  7749. (setq cmd (cdr (assoc t apps))))
  7750. (when (eq cmd 'mailcap)
  7751. (require 'mailcap)
  7752. (mailcap-parse-mailcaps)
  7753. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7754. (command (mailcap-mime-info mime-type)))
  7755. (if (stringp command)
  7756. (setq cmd command)
  7757. (setq cmd 'emacs))))
  7758. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7759. (not (file-exists-p file))
  7760. (not org-open-non-existing-files))
  7761. (error "No such file: %s" file))
  7762. (cond
  7763. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7764. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7765. (while (string-match "['\"]%s['\"]" cmd)
  7766. (setq cmd (replace-match "%s" t t cmd)))
  7767. (while (string-match "%s" cmd)
  7768. (setq cmd (replace-match
  7769. (save-match-data
  7770. (shell-quote-argument
  7771. (convert-standard-filename file)))
  7772. t t cmd)))
  7773. (save-window-excursion
  7774. (start-process-shell-command cmd nil cmd)
  7775. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7776. ))
  7777. ((or (stringp cmd)
  7778. (eq cmd 'emacs))
  7779. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7780. (widen)
  7781. (if line (org-goto-line line)
  7782. (if search (org-link-search search))))
  7783. ((consp cmd)
  7784. (let ((file (convert-standard-filename file)))
  7785. (eval cmd)))
  7786. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7787. (and (org-mode-p) (eq old-mode 'org-mode)
  7788. (or (not (equal old-buffer (current-buffer)))
  7789. (not (equal old-pos (point))))
  7790. (org-mark-ring-push old-pos old-buffer))))
  7791. (defun org-default-apps ()
  7792. "Return the default applications for this operating system."
  7793. (cond
  7794. ((eq system-type 'darwin)
  7795. org-file-apps-defaults-macosx)
  7796. ((eq system-type 'windows-nt)
  7797. org-file-apps-defaults-windowsnt)
  7798. (t org-file-apps-defaults-gnu)))
  7799. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7800. "Convert extensions to regular expressions in the cars of LIST.
  7801. Also, weed out any non-string entries, because the return value is used
  7802. only for regexp matching.
  7803. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7804. point to the symbol `emacs', indicating that the file should
  7805. be opened in Emacs."
  7806. (append
  7807. (delq nil
  7808. (mapcar (lambda (x)
  7809. (if (not (stringp (car x)))
  7810. nil
  7811. (if (string-match "\\W" (car x))
  7812. x
  7813. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7814. list))
  7815. (if add-auto-mode
  7816. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7817. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7818. (defun org-file-remote-p (file)
  7819. "Test whether FILE specifies a location on a remote system.
  7820. Return non-nil if the location is indeed remote.
  7821. For example, the filename \"/user@host:/foo\" specifies a location
  7822. on the system \"/user@host:\"."
  7823. (cond ((fboundp 'file-remote-p)
  7824. (file-remote-p file))
  7825. ((fboundp 'tramp-handle-file-remote-p)
  7826. (tramp-handle-file-remote-p file))
  7827. ((and (boundp 'ange-ftp-name-format)
  7828. (string-match (car ange-ftp-name-format) file))
  7829. t)
  7830. (t nil)))
  7831. ;;;; Refiling
  7832. (defun org-get-org-file ()
  7833. "Read a filename, with default directory `org-directory'."
  7834. (let ((default (or org-default-notes-file remember-data-file)))
  7835. (read-file-name (format "File name [%s]: " default)
  7836. (file-name-as-directory org-directory)
  7837. default)))
  7838. (defun org-notes-order-reversed-p ()
  7839. "Check if the current file should receive notes in reversed order."
  7840. (cond
  7841. ((not org-reverse-note-order) nil)
  7842. ((eq t org-reverse-note-order) t)
  7843. ((not (listp org-reverse-note-order)) nil)
  7844. (t (catch 'exit
  7845. (let ((all org-reverse-note-order)
  7846. entry)
  7847. (while (setq entry (pop all))
  7848. (if (string-match (car entry) buffer-file-name)
  7849. (throw 'exit (cdr entry))))
  7850. nil)))))
  7851. (defvar org-refile-target-table nil
  7852. "The list of refile targets, created by `org-refile'.")
  7853. (defvar org-agenda-new-buffers nil
  7854. "Buffers created to visit agenda files.")
  7855. (defun org-get-refile-targets (&optional default-buffer)
  7856. "Produce a table with refile targets."
  7857. (let ((case-fold-search nil)
  7858. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7859. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7860. targets txt re files f desc descre fast-path-p level pos0)
  7861. (message "Getting targets...")
  7862. (with-current-buffer (or default-buffer (current-buffer))
  7863. (while (setq entry (pop entries))
  7864. (setq files (car entry) desc (cdr entry))
  7865. (setq fast-path-p nil)
  7866. (cond
  7867. ((null files) (setq files (list (current-buffer))))
  7868. ((eq files 'org-agenda-files)
  7869. (setq files (org-agenda-files 'unrestricted)))
  7870. ((and (symbolp files) (fboundp files))
  7871. (setq files (funcall files)))
  7872. ((and (symbolp files) (boundp files))
  7873. (setq files (symbol-value files))))
  7874. (if (stringp files) (setq files (list files)))
  7875. (cond
  7876. ((eq (car desc) :tag)
  7877. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7878. ((eq (car desc) :todo)
  7879. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7880. ((eq (car desc) :regexp)
  7881. (setq descre (cdr desc)))
  7882. ((eq (car desc) :level)
  7883. (setq descre (concat "^\\*\\{" (number-to-string
  7884. (if org-odd-levels-only
  7885. (1- (* 2 (cdr desc)))
  7886. (cdr desc)))
  7887. "\\}[ \t]")))
  7888. ((eq (car desc) :maxlevel)
  7889. (setq fast-path-p t)
  7890. (setq descre (concat "^\\*\\{1," (number-to-string
  7891. (if org-odd-levels-only
  7892. (1- (* 2 (cdr desc)))
  7893. (cdr desc)))
  7894. "\\}[ \t]")))
  7895. (t (error "Bad refiling target description %s" desc)))
  7896. (while (setq f (pop files))
  7897. (save-excursion
  7898. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7899. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7900. (setq f (expand-file-name f))
  7901. (if (eq org-refile-use-outline-path 'file)
  7902. (push (list (file-name-nondirectory f) f nil nil) targets))
  7903. (save-excursion
  7904. (save-restriction
  7905. (widen)
  7906. (goto-char (point-min))
  7907. (while (re-search-forward descre nil t)
  7908. (goto-char (setq pos0 (point-at-bol)))
  7909. (catch 'next
  7910. (when org-refile-target-verify-function
  7911. (save-match-data
  7912. (or (funcall org-refile-target-verify-function)
  7913. (throw 'next t))))
  7914. (when (looking-at org-complex-heading-regexp)
  7915. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7916. txt (org-link-display-format (match-string 4))
  7917. re (concat "^" (regexp-quote
  7918. (buffer-substring (match-beginning 1)
  7919. (match-end 4)))))
  7920. (if (match-end 5) (setq re (concat re "[ \t]+"
  7921. (regexp-quote
  7922. (match-string 5)))))
  7923. (setq re (concat re "[ \t]*$"))
  7924. (when org-refile-use-outline-path
  7925. (setq txt (mapconcat 'org-protect-slash
  7926. (append
  7927. (if (eq org-refile-use-outline-path 'file)
  7928. (list (file-name-nondirectory
  7929. (buffer-file-name (buffer-base-buffer))))
  7930. (if (eq org-refile-use-outline-path 'full-file-path)
  7931. (list (buffer-file-name (buffer-base-buffer)))))
  7932. (org-get-outline-path fast-path-p level txt)
  7933. (list txt))
  7934. "/")))
  7935. (push (list txt f re (point)) targets)))
  7936. (when (= (point) pos0)
  7937. ;; verification function has not moved point
  7938. (goto-char (point-at-eol))))))))))
  7939. (message "Getting targets...done")
  7940. (nreverse targets)))
  7941. (defun org-protect-slash (s)
  7942. (while (string-match "/" s)
  7943. (setq s (replace-match "\\" t t s)))
  7944. s)
  7945. (defvar org-olpa (make-vector 20 nil))
  7946. (defun org-get-outline-path (&optional fastp level heading)
  7947. "Return the outline path to the current entry, as a list."
  7948. (if fastp
  7949. (progn
  7950. (if (> level 19)
  7951. (error "Outline path failure, more than 19 levels."))
  7952. (loop for i from level upto 19 do
  7953. (aset org-olpa i nil))
  7954. (prog1
  7955. (delq nil (append org-olpa nil))
  7956. (aset org-olpa level heading)))
  7957. (let (rtn)
  7958. (save-excursion
  7959. (while (org-up-heading-safe)
  7960. (when (looking-at org-complex-heading-regexp)
  7961. (push (org-match-string-no-properties 4) rtn)))
  7962. rtn))))
  7963. (defvar org-refile-history nil
  7964. "History for refiling operations.")
  7965. (defvar org-after-refile-insert-hook nil
  7966. "Hook run after `org-refile' has inserted its stuff at the new location.
  7967. Note that this is still *before* the stuff will be removed from
  7968. the *old* location.")
  7969. (defun org-refile (&optional goto default-buffer rfloc)
  7970. "Move the entry at point to another heading.
  7971. The list of target headings is compiled using the information in
  7972. `org-refile-targets', which see. This list is created before each use
  7973. and will therefore always be up-to-date.
  7974. At the target location, the entry is filed as a subitem of the target heading.
  7975. Depending on `org-reverse-note-order', the new subitem will either be the
  7976. first or the last subitem.
  7977. If there is an active region, all entries in that region will be moved.
  7978. However, the region must fulfil the requirement that the first heading
  7979. is the first one sets the top-level of the moved text - at most siblings
  7980. below it are allowed.
  7981. With prefix arg GOTO, the command will only visit the target location,
  7982. not actually move anything.
  7983. With a double prefix `C-u C-u', go to the location where the last refiling
  7984. operation has put the subtree.
  7985. RFLOC can be a refile location obtained in a different way.
  7986. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7987. (interactive "P")
  7988. (let* ((cbuf (current-buffer))
  7989. (regionp (org-region-active-p))
  7990. (region-start (and regionp (region-beginning)))
  7991. (region-end (and regionp (region-end)))
  7992. (region-length (and regionp (- region-end region-start)))
  7993. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7994. pos it nbuf file re level reversed)
  7995. (when regionp
  7996. (goto-char region-start)
  7997. (or (bolp) (goto-char (point-at-bol)))
  7998. (setq region-start (point))
  7999. (unless (org-kill-is-subtree-p
  8000. (buffer-substring region-start region-end))
  8001. (error "The region is not a (sequence of) subtree(s)")))
  8002. (if (equal goto '(16))
  8003. (org-refile-goto-last-stored)
  8004. (when (setq it (or rfloc
  8005. (save-excursion
  8006. (org-refile-get-location
  8007. (if goto "Goto: " "Refile to: ") default-buffer
  8008. org-refile-allow-creating-parent-nodes))))
  8009. (setq file (nth 1 it)
  8010. re (nth 2 it)
  8011. pos (nth 3 it))
  8012. (if (and (not goto)
  8013. pos
  8014. (equal (buffer-file-name) file)
  8015. (if regionp
  8016. (and (>= pos region-start)
  8017. (<= pos region-end))
  8018. (and (>= pos (point))
  8019. (< pos (save-excursion
  8020. (org-end-of-subtree t t))))))
  8021. (error "Cannot refile to position inside the tree or region"))
  8022. (setq nbuf (or (find-buffer-visiting file)
  8023. (find-file-noselect file)))
  8024. (if goto
  8025. (progn
  8026. (switch-to-buffer nbuf)
  8027. (goto-char pos)
  8028. (org-show-context 'org-goto))
  8029. (if regionp
  8030. (progn
  8031. (org-kill-new (buffer-substring region-start region-end))
  8032. (org-save-markers-in-region region-start region-end))
  8033. (org-copy-subtree 1 nil t))
  8034. (save-excursion
  8035. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  8036. (find-file-noselect file))))
  8037. (setq reversed (org-notes-order-reversed-p))
  8038. (save-excursion
  8039. (save-restriction
  8040. (widen)
  8041. (if pos
  8042. (progn
  8043. (goto-char pos)
  8044. (looking-at outline-regexp)
  8045. (setq level (org-get-valid-level (funcall outline-level) 1))
  8046. (goto-char
  8047. (if reversed
  8048. (or (outline-next-heading) (point-max))
  8049. (or (save-excursion (org-get-next-sibling))
  8050. (org-end-of-subtree t t)
  8051. (point-max)))))
  8052. (setq level 1)
  8053. (if (not reversed)
  8054. (goto-char (point-max))
  8055. (goto-char (point-min))
  8056. (or (outline-next-heading) (goto-char (point-max)))))
  8057. (if (not (bolp)) (newline))
  8058. (bookmark-set "org-refile-last-stored")
  8059. (org-paste-subtree level)
  8060. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8061. (run-hooks 'org-after-refile-insert-hook))))
  8062. (if regionp
  8063. (delete-region (point) (+ (point) region-length))
  8064. (org-cut-subtree))
  8065. (when (featurep 'org-inlinetask)
  8066. (org-inlinetask-remove-END-maybe))
  8067. (setq org-markers-to-move nil)
  8068. (message "Refiled to \"%s\"" (car it))))))
  8069. (org-reveal))
  8070. (defun org-refile-goto-last-stored ()
  8071. "Go to the location where the last refile was stored."
  8072. (interactive)
  8073. (bookmark-jump "org-refile-last-stored")
  8074. (message "This is the location of the last refile"))
  8075. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8076. "Prompt the user for a refile location, using PROMPT."
  8077. (let ((org-refile-targets org-refile-targets)
  8078. (org-refile-use-outline-path org-refile-use-outline-path))
  8079. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8080. (unless org-refile-target-table
  8081. (error "No refile targets"))
  8082. (let* ((cbuf (current-buffer))
  8083. (partial-completion-mode nil)
  8084. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8085. (cfunc (if (and org-refile-use-outline-path
  8086. org-outline-path-complete-in-steps)
  8087. 'org-olpath-completing-read
  8088. 'org-icompleting-read))
  8089. (extra (if org-refile-use-outline-path "/" ""))
  8090. (filename (and cfn (expand-file-name cfn)))
  8091. (tbl (mapcar
  8092. (lambda (x)
  8093. (if (and (not (member org-refile-use-outline-path
  8094. '(file full-file-path)))
  8095. (not (equal filename (nth 1 x))))
  8096. (cons (concat (car x) extra " ("
  8097. (file-name-nondirectory (nth 1 x)) ")")
  8098. (cdr x))
  8099. (cons (concat (car x) extra) (cdr x))))
  8100. org-refile-target-table))
  8101. (completion-ignore-case t)
  8102. pa answ parent-target child parent old-hist)
  8103. (setq old-hist org-refile-history)
  8104. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8105. nil 'org-refile-history))
  8106. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8107. (if pa
  8108. (progn
  8109. (when (or (not org-refile-history)
  8110. (not (eq old-hist org-refile-history))
  8111. (not (equal (car pa) (car org-refile-history))))
  8112. (setq org-refile-history
  8113. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8114. org-refile-history
  8115. (cdr org-refile-history))))
  8116. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8117. (pop org-refile-history)))
  8118. pa)
  8119. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8120. (setq parent (match-string 1 answ)
  8121. child (match-string 2 answ))
  8122. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8123. (when (and parent-target
  8124. (or (eq new-nodes t)
  8125. (and (eq new-nodes 'confirm)
  8126. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8127. (org-refile-new-child parent-target child))))))
  8128. (defun org-refile-new-child (parent-target child)
  8129. "Use refile target PARENT-TARGET to add new CHILD below it."
  8130. (unless parent-target
  8131. (error "Cannot find parent for new node"))
  8132. (let ((file (nth 1 parent-target))
  8133. (pos (nth 3 parent-target))
  8134. level)
  8135. (with-current-buffer (or (find-buffer-visiting file)
  8136. (find-file-noselect file))
  8137. (save-excursion
  8138. (save-restriction
  8139. (widen)
  8140. (if pos
  8141. (goto-char pos)
  8142. (goto-char (point-max))
  8143. (if (not (bolp)) (newline)))
  8144. (when (looking-at outline-regexp)
  8145. (setq level (funcall outline-level))
  8146. (org-end-of-subtree t t))
  8147. (org-back-over-empty-lines)
  8148. (insert "\n" (make-string
  8149. (if pos (org-get-valid-level level 1) 1) ?*)
  8150. " " child "\n")
  8151. (beginning-of-line 0)
  8152. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8153. (defun org-olpath-completing-read (prompt collection &rest args)
  8154. "Read an outline path like a file name."
  8155. (let ((thetable collection)
  8156. (org-completion-use-ido nil) ; does not work with ido.
  8157. (org-completion-use-iswitchb nil)) ; or iswitchb
  8158. (apply
  8159. 'org-icompleting-read prompt
  8160. (lambda (string predicate &optional flag)
  8161. (let (rtn r f (l (length string)))
  8162. (cond
  8163. ((eq flag nil)
  8164. ;; try completion
  8165. (try-completion string thetable))
  8166. ((eq flag t)
  8167. ;; all-completions
  8168. (setq rtn (all-completions string thetable predicate))
  8169. (mapcar
  8170. (lambda (x)
  8171. (setq r (substring x l))
  8172. (if (string-match " ([^)]*)$" x)
  8173. (setq f (match-string 0 x))
  8174. (setq f ""))
  8175. (if (string-match "/" r)
  8176. (concat string (substring r 0 (match-end 0)) f)
  8177. x))
  8178. rtn))
  8179. ((eq flag 'lambda)
  8180. ;; exact match?
  8181. (assoc string thetable)))
  8182. ))
  8183. args)))
  8184. ;;;; Dynamic blocks
  8185. (defun org-find-dblock (name)
  8186. "Find the first dynamic block with name NAME in the buffer.
  8187. If not found, stay at current position and return nil."
  8188. (let (pos)
  8189. (save-excursion
  8190. (goto-char (point-min))
  8191. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8192. nil t)
  8193. (match-beginning 0))))
  8194. (if pos (goto-char pos))
  8195. pos))
  8196. (defconst org-dblock-start-re
  8197. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8198. "Matches the startline of a dynamic block, with parameters.")
  8199. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8200. "Matches the end of a dynamic block.")
  8201. (defun org-create-dblock (plist)
  8202. "Create a dynamic block section, with parameters taken from PLIST.
  8203. PLIST must contain a :name entry which is used as name of the block."
  8204. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8205. (end-of-line 1)
  8206. (newline))
  8207. (let ((col (current-column))
  8208. (name (plist-get plist :name)))
  8209. (insert "#+BEGIN: " name)
  8210. (while plist
  8211. (if (eq (car plist) :name)
  8212. (setq plist (cddr plist))
  8213. (insert " " (prin1-to-string (pop plist)))))
  8214. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8215. (beginning-of-line -2)))
  8216. (defun org-prepare-dblock ()
  8217. "Prepare dynamic block for refresh.
  8218. This empties the block, puts the cursor at the insert position and returns
  8219. the property list including an extra property :name with the block name."
  8220. (unless (looking-at org-dblock-start-re)
  8221. (error "Not at a dynamic block"))
  8222. (let* ((begdel (1+ (match-end 0)))
  8223. (name (org-no-properties (match-string 1)))
  8224. (params (append (list :name name)
  8225. (read (concat "(" (match-string 3) ")")))))
  8226. (save-excursion
  8227. (beginning-of-line 1)
  8228. (skip-chars-forward " \t")
  8229. (setq params (plist-put params :indentation-column (current-column))))
  8230. (unless (re-search-forward org-dblock-end-re nil t)
  8231. (error "Dynamic block not terminated"))
  8232. (setq params
  8233. (append params
  8234. (list :content (buffer-substring
  8235. begdel (match-beginning 0)))))
  8236. (delete-region begdel (match-beginning 0))
  8237. (goto-char begdel)
  8238. (open-line 1)
  8239. params))
  8240. (defun org-map-dblocks (&optional command)
  8241. "Apply COMMAND to all dynamic blocks in the current buffer.
  8242. If COMMAND is not given, use `org-update-dblock'."
  8243. (let ((cmd (or command 'org-update-dblock))
  8244. pos)
  8245. (save-excursion
  8246. (goto-char (point-min))
  8247. (while (re-search-forward org-dblock-start-re nil t)
  8248. (goto-char (setq pos (match-beginning 0)))
  8249. (condition-case nil
  8250. (funcall cmd)
  8251. (error (message "Error during update of dynamic block")))
  8252. (goto-char pos)
  8253. (unless (re-search-forward org-dblock-end-re nil t)
  8254. (error "Dynamic block not terminated"))))))
  8255. (defun org-dblock-update (&optional arg)
  8256. "User command for updating dynamic blocks.
  8257. Update the dynamic block at point. With prefix ARG, update all dynamic
  8258. blocks in the buffer."
  8259. (interactive "P")
  8260. (if arg
  8261. (org-update-all-dblocks)
  8262. (or (looking-at org-dblock-start-re)
  8263. (org-beginning-of-dblock))
  8264. (org-update-dblock)))
  8265. (defun org-update-dblock ()
  8266. "Update the dynamic block at point
  8267. This means to empty the block, parse for parameters and then call
  8268. the correct writing function."
  8269. (save-window-excursion
  8270. (let* ((pos (point))
  8271. (line (org-current-line))
  8272. (params (org-prepare-dblock))
  8273. (name (plist-get params :name))
  8274. (indent (plist-get params :indentation-column))
  8275. (cmd (intern (concat "org-dblock-write:" name))))
  8276. (message "Updating dynamic block `%s' at line %d..." name line)
  8277. (funcall cmd params)
  8278. (message "Updating dynamic block `%s' at line %d...done" name line)
  8279. (goto-char pos)
  8280. (when (and indent (> indent 0))
  8281. (setq indent (make-string indent ?\ ))
  8282. (save-excursion
  8283. (org-beginning-of-dblock)
  8284. (forward-line 1)
  8285. (while (not (looking-at org-dblock-end-re))
  8286. (insert indent)
  8287. (beginning-of-line 2))
  8288. (when (looking-at org-dblock-end-re)
  8289. (and (looking-at "[ \t]+")
  8290. (replace-match ""))
  8291. (insert indent)))))))
  8292. (defun org-beginning-of-dblock ()
  8293. "Find the beginning of the dynamic block at point.
  8294. Error if there is no such block at point."
  8295. (let ((pos (point))
  8296. beg)
  8297. (end-of-line 1)
  8298. (if (and (re-search-backward org-dblock-start-re nil t)
  8299. (setq beg (match-beginning 0))
  8300. (re-search-forward org-dblock-end-re nil t)
  8301. (> (match-end 0) pos))
  8302. (goto-char beg)
  8303. (goto-char pos)
  8304. (error "Not in a dynamic block"))))
  8305. (defun org-update-all-dblocks ()
  8306. "Update all dynamic blocks in the buffer.
  8307. This function can be used in a hook."
  8308. (when (org-mode-p)
  8309. (org-map-dblocks 'org-update-dblock)))
  8310. ;;;; Completion
  8311. (defconst org-additional-option-like-keywords
  8312. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8313. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8314. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
  8315. "BEGIN:" "END:"
  8316. "ORGTBL" "TBLFM:" "TBLNAME:"
  8317. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8318. "BEGIN_QUOTE" "END_QUOTE"
  8319. "BEGIN_VERSE" "END_VERSE"
  8320. "BEGIN_CENTER" "END_CENTER"
  8321. "BEGIN_SRC" "END_SRC"
  8322. "CATEGORY" "COLUMNS"
  8323. "CAPTION" "LABEL"
  8324. "SETUPFILE"
  8325. "BIND"
  8326. "MACRO"))
  8327. (defcustom org-structure-template-alist
  8328. '(
  8329. ("s" "#+begin_src ?\n\n#+end_src"
  8330. "<src lang=\"?\">\n\n</src>")
  8331. ("e" "#+begin_example\n?\n#+end_example"
  8332. "<example>\n?\n</example>")
  8333. ("q" "#+begin_quote\n?\n#+end_quote"
  8334. "<quote>\n?\n</quote>")
  8335. ("v" "#+begin_verse\n?\n#+end_verse"
  8336. "<verse>\n?\n/verse>")
  8337. ("c" "#+begin_center\n?\n#+end_center"
  8338. "<center>\n?\n/center>")
  8339. ("l" "#+begin_latex\n?\n#+end_latex"
  8340. "<literal style=\"latex\">\n?\n</literal>")
  8341. ("L" "#+latex: "
  8342. "<literal style=\"latex\">?</literal>")
  8343. ("h" "#+begin_html\n?\n#+end_html"
  8344. "<literal style=\"html\">\n?\n</literal>")
  8345. ("H" "#+html: "
  8346. "<literal style=\"html\">?</literal>")
  8347. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8348. ("A" "#+ascii: ")
  8349. ("i" "#+include %file ?"
  8350. "<include file=%file markup=\"?\">")
  8351. )
  8352. "Structure completion elements.
  8353. This is a list of abbreviation keys and values. The value gets inserted
  8354. it you type @samp{.} followed by the key and then the completion key,
  8355. usually `M-TAB'. %file will be replaced by a file name after prompting
  8356. for the file using completion.
  8357. There are two templates for each key, the first uses the original Org syntax,
  8358. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8359. the default when the /org-mtags.el/ module has been loaded. See also the
  8360. variable `org-mtags-prefer-muse-templates'.
  8361. This is an experimental feature, it is undecided if it is going to stay in."
  8362. :group 'org-completion
  8363. :type '(repeat
  8364. (string :tag "Key")
  8365. (string :tag "Template")
  8366. (string :tag "Muse Template")))
  8367. (defun org-try-structure-completion ()
  8368. "Try to complete a structure template before point.
  8369. This looks for strings like \"<e\" on an otherwise empty line and
  8370. expands them."
  8371. (let ((l (buffer-substring (point-at-bol) (point)))
  8372. a)
  8373. (when (and (looking-at "[ \t]*$")
  8374. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8375. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8376. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8377. (match-beginning 1)) a)
  8378. t)))
  8379. (defun org-complete-expand-structure-template (start cell)
  8380. "Expand a structure template."
  8381. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8382. (rpl (nth (if musep 2 1) cell))
  8383. (ind ""))
  8384. (delete-region start (point))
  8385. (when (string-match "\\`#\\+" rpl)
  8386. (cond
  8387. ((bolp))
  8388. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8389. (setq ind (buffer-substring (point-at-bol) (point))))
  8390. (t (newline))))
  8391. (setq start (point))
  8392. (if (string-match "%file" rpl)
  8393. (setq rpl (replace-match
  8394. (concat
  8395. "\""
  8396. (save-match-data
  8397. (abbreviate-file-name (read-file-name "Include file: ")))
  8398. "\"")
  8399. t t rpl)))
  8400. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8401. (concat "\n" ind)))
  8402. (insert rpl)
  8403. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8404. (defun org-complete (&optional arg)
  8405. "Perform completion on word at point.
  8406. At the beginning of a headline, this completes TODO keywords as given in
  8407. `org-todo-keywords'.
  8408. If the current word is preceded by a backslash, completes the TeX symbols
  8409. that are supported for HTML support.
  8410. If the current word is preceded by \"#+\", completes special words for
  8411. setting file options.
  8412. In the line after \"#+STARTUP:, complete valid keywords.\"
  8413. At all other locations, this simply calls the value of
  8414. `org-completion-fallback-command'."
  8415. (interactive "P")
  8416. (org-without-partial-completion
  8417. (catch 'exit
  8418. (let* ((a nil)
  8419. (end (point))
  8420. (beg1 (save-excursion
  8421. (skip-chars-backward (org-re "[:alnum:]_@"))
  8422. (point)))
  8423. (beg (save-excursion
  8424. (skip-chars-backward "a-zA-Z0-9_:$")
  8425. (point)))
  8426. (confirm (lambda (x) (stringp (car x))))
  8427. (searchhead (equal (char-before beg) ?*))
  8428. (struct
  8429. (when (and (member (char-before beg1) '(?. ?<))
  8430. (setq a (assoc (buffer-substring beg1 (point))
  8431. org-structure-template-alist)))
  8432. (org-complete-expand-structure-template (1- beg1) a)
  8433. (throw 'exit t)))
  8434. (tag (and (equal (char-before beg1) ?:)
  8435. (equal (char-after (point-at-bol)) ?*)))
  8436. (prop (and (equal (char-before beg1) ?:)
  8437. (not (equal (char-after (point-at-bol)) ?*))))
  8438. (texp (equal (char-before beg) ?\\))
  8439. (link (equal (char-before beg) ?\[))
  8440. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8441. beg)
  8442. "#+"))
  8443. (startup (string-match "^#\\+STARTUP:.*"
  8444. (buffer-substring (point-at-bol) (point))))
  8445. (completion-ignore-case opt)
  8446. (type nil)
  8447. (tbl nil)
  8448. (table (cond
  8449. (opt
  8450. (setq type :opt)
  8451. (require 'org-exp)
  8452. (append
  8453. (delq nil
  8454. (mapcar
  8455. (lambda (x)
  8456. (if (string-match
  8457. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8458. (cons (match-string 2 x)
  8459. (match-string 1 x))))
  8460. (org-split-string (org-get-current-options) "\n")))
  8461. (mapcar 'list org-additional-option-like-keywords)))
  8462. (startup
  8463. (setq type :startup)
  8464. org-startup-options)
  8465. (link (append org-link-abbrev-alist-local
  8466. org-link-abbrev-alist))
  8467. (texp
  8468. (setq type :tex)
  8469. org-html-entities)
  8470. ((string-match "\\`\\*+[ \t]+\\'"
  8471. (buffer-substring (point-at-bol) beg))
  8472. (setq type :todo)
  8473. (mapcar 'list org-todo-keywords-1))
  8474. (searchhead
  8475. (setq type :searchhead)
  8476. (save-excursion
  8477. (goto-char (point-min))
  8478. (while (re-search-forward org-todo-line-regexp nil t)
  8479. (push (list
  8480. (org-make-org-heading-search-string
  8481. (match-string 3) t))
  8482. tbl)))
  8483. tbl)
  8484. (tag (setq type :tag beg beg1)
  8485. (or org-tag-alist (org-get-buffer-tags)))
  8486. (prop (setq type :prop beg beg1)
  8487. (mapcar 'list (org-buffer-property-keys nil t t)))
  8488. (t (progn
  8489. (call-interactively org-completion-fallback-command)
  8490. (throw 'exit nil)))))
  8491. (pattern (buffer-substring-no-properties beg end))
  8492. (completion (try-completion pattern table confirm)))
  8493. (cond ((eq completion t)
  8494. (if (not (assoc (upcase pattern) table))
  8495. (message "Already complete")
  8496. (if (and (equal type :opt)
  8497. (not (member (car (assoc (upcase pattern) table))
  8498. org-additional-option-like-keywords)))
  8499. (insert (substring (cdr (assoc (upcase pattern) table))
  8500. (length pattern)))
  8501. (if (memq type '(:tag :prop)) (insert ":")))))
  8502. ((null completion)
  8503. (message "Can't find completion for \"%s\"" pattern)
  8504. (ding))
  8505. ((not (string= pattern completion))
  8506. (delete-region beg end)
  8507. (if (string-match " +$" completion)
  8508. (setq completion (replace-match "" t t completion)))
  8509. (insert completion)
  8510. (if (get-buffer-window "*Completions*")
  8511. (delete-window (get-buffer-window "*Completions*")))
  8512. (if (assoc completion table)
  8513. (if (eq type :todo) (insert " ")
  8514. (if (memq type '(:tag :prop)) (insert ":"))))
  8515. (if (and (equal type :opt) (assoc completion table))
  8516. (message "%s" (substitute-command-keys
  8517. "Press \\[org-complete] again to insert example settings"))))
  8518. (t
  8519. (message "Making completion list...")
  8520. (let ((list (sort (all-completions pattern table confirm)
  8521. 'string<)))
  8522. (with-output-to-temp-buffer "*Completions*"
  8523. (condition-case nil
  8524. ;; Protection needed for XEmacs and emacs 21
  8525. (display-completion-list list pattern)
  8526. (error (display-completion-list list)))))
  8527. (message "Making completion list...%s" "done")))))))
  8528. ;;;; TODO, DEADLINE, Comments
  8529. (defun org-toggle-comment ()
  8530. "Change the COMMENT state of an entry."
  8531. (interactive)
  8532. (save-excursion
  8533. (org-back-to-heading)
  8534. (let (case-fold-search)
  8535. (if (looking-at (concat outline-regexp
  8536. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8537. (replace-match "" t t nil 1)
  8538. (if (looking-at outline-regexp)
  8539. (progn
  8540. (goto-char (match-end 0))
  8541. (insert org-comment-string " ")))))))
  8542. (defvar org-last-todo-state-is-todo nil
  8543. "This is non-nil when the last TODO state change led to a TODO state.
  8544. If the last change removed the TODO tag or switched to DONE, then
  8545. this is nil.")
  8546. (defvar org-setting-tags nil) ; dynamically skipped
  8547. (defun org-parse-local-options (string var)
  8548. "Parse STRING for startup setting relevant for variable VAR."
  8549. (let ((rtn (symbol-value var))
  8550. e opts)
  8551. (save-match-data
  8552. (if (or (not string) (not (string-match "\\S-" string)))
  8553. rtn
  8554. (setq opts (delq nil (mapcar (lambda (x)
  8555. (setq e (assoc x org-startup-options))
  8556. (if (eq (nth 1 e) var) e nil))
  8557. (org-split-string string "[ \t]+"))))
  8558. (if (not opts)
  8559. rtn
  8560. (setq rtn nil)
  8561. (while (setq e (pop opts))
  8562. (if (not (nth 3 e))
  8563. (setq rtn (nth 2 e))
  8564. (if (not (listp rtn)) (setq rtn nil))
  8565. (push (nth 2 e) rtn)))
  8566. rtn)))))
  8567. (defvar org-todo-setup-filter-hook nil
  8568. "Hook for functions that pre-filter todo specs.
  8569. Each function takes a todo spec and returns either `nil' or the spec
  8570. transformed into canonical form." )
  8571. (defvar org-todo-get-default-hook nil
  8572. "Hook for functions that get a default item for todo.
  8573. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8574. `nil' or a string to be used for the todo mark." )
  8575. (defvar org-agenda-headline-snapshot-before-repeat)
  8576. (defun org-todo (&optional arg)
  8577. "Change the TODO state of an item.
  8578. The state of an item is given by a keyword at the start of the heading,
  8579. like
  8580. *** TODO Write paper
  8581. *** DONE Call mom
  8582. The different keywords are specified in the variable `org-todo-keywords'.
  8583. By default the available states are \"TODO\" and \"DONE\".
  8584. So for this example: when the item starts with TODO, it is changed to DONE.
  8585. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8586. DONE are present, add TODO at the beginning of the heading.
  8587. With C-u prefix arg, use completion to determine the new state.
  8588. With numeric prefix arg, switch to that state.
  8589. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8590. With a tripple C-u prefix, circumvent any state blocking.
  8591. For calling through lisp, arg is also interpreted in the following way:
  8592. 'none -> empty state
  8593. \"\"(empty string) -> switch to empty state
  8594. 'done -> switch to DONE
  8595. 'nextset -> switch to the next set of keywords
  8596. 'previousset -> switch to the previous set of keywords
  8597. \"WAITING\" -> switch to the specified keyword, but only if it
  8598. really is a member of `org-todo-keywords'."
  8599. (interactive "P")
  8600. (if (equal arg '(16)) (setq arg 'nextset))
  8601. (let ((org-blocker-hook org-blocker-hook)
  8602. (case-fold-search nil))
  8603. (when (equal arg '(64))
  8604. (setq arg nil org-blocker-hook nil))
  8605. (when (and org-blocker-hook
  8606. (or org-inhibit-blocking
  8607. (org-entry-get nil "NOBLOCKING")))
  8608. (setq org-blocker-hook nil))
  8609. (save-excursion
  8610. (catch 'exit
  8611. (org-back-to-heading t)
  8612. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8613. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8614. (looking-at " *"))
  8615. (let* ((match-data (match-data))
  8616. (startpos (point-at-bol))
  8617. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8618. (org-log-done org-log-done)
  8619. (org-log-repeat org-log-repeat)
  8620. (org-todo-log-states org-todo-log-states)
  8621. (this (match-string 1))
  8622. (hl-pos (match-beginning 0))
  8623. (head (org-get-todo-sequence-head this))
  8624. (ass (assoc head org-todo-kwd-alist))
  8625. (interpret (nth 1 ass))
  8626. (done-word (nth 3 ass))
  8627. (final-done-word (nth 4 ass))
  8628. (last-state (or this ""))
  8629. (completion-ignore-case t)
  8630. (member (member this org-todo-keywords-1))
  8631. (tail (cdr member))
  8632. (state (cond
  8633. ((and org-todo-key-trigger
  8634. (or (and (equal arg '(4))
  8635. (eq org-use-fast-todo-selection 'prefix))
  8636. (and (not arg) org-use-fast-todo-selection
  8637. (not (eq org-use-fast-todo-selection
  8638. 'prefix)))))
  8639. ;; Use fast selection
  8640. (org-fast-todo-selection))
  8641. ((and (equal arg '(4))
  8642. (or (not org-use-fast-todo-selection)
  8643. (not org-todo-key-trigger)))
  8644. ;; Read a state with completion
  8645. (org-icompleting-read
  8646. "State: " (mapcar (lambda(x) (list x))
  8647. org-todo-keywords-1)
  8648. nil t))
  8649. ((eq arg 'right)
  8650. (if this
  8651. (if tail (car tail) nil)
  8652. (car org-todo-keywords-1)))
  8653. ((eq arg 'left)
  8654. (if (equal member org-todo-keywords-1)
  8655. nil
  8656. (if this
  8657. (nth (- (length org-todo-keywords-1)
  8658. (length tail) 2)
  8659. org-todo-keywords-1)
  8660. (org-last org-todo-keywords-1))))
  8661. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8662. (setq arg nil))) ; hack to fall back to cycling
  8663. (arg
  8664. ;; user or caller requests a specific state
  8665. (cond
  8666. ((equal arg "") nil)
  8667. ((eq arg 'none) nil)
  8668. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8669. ((eq arg 'nextset)
  8670. (or (car (cdr (member head org-todo-heads)))
  8671. (car org-todo-heads)))
  8672. ((eq arg 'previousset)
  8673. (let ((org-todo-heads (reverse org-todo-heads)))
  8674. (or (car (cdr (member head org-todo-heads)))
  8675. (car org-todo-heads))))
  8676. ((car (member arg org-todo-keywords-1)))
  8677. ((stringp arg)
  8678. (error "State `%s' not valid in this file" arg))
  8679. ((nth (1- (prefix-numeric-value arg))
  8680. org-todo-keywords-1))))
  8681. ((null member) (or head (car org-todo-keywords-1)))
  8682. ((equal this final-done-word) nil) ;; -> make empty
  8683. ((null tail) nil) ;; -> first entry
  8684. ((memq interpret '(type priority))
  8685. (if (eq this-command last-command)
  8686. (car tail)
  8687. (if (> (length tail) 0)
  8688. (or done-word (car org-done-keywords))
  8689. nil)))
  8690. (t
  8691. (car tail))))
  8692. (state (or
  8693. (run-hook-with-args-until-success
  8694. 'org-todo-get-default-hook state last-state)
  8695. state))
  8696. (next (if state (concat " " state " ") " "))
  8697. (change-plist (list :type 'todo-state-change :from this :to state
  8698. :position startpos))
  8699. dolog now-done-p)
  8700. (when org-blocker-hook
  8701. (setq org-last-todo-state-is-todo
  8702. (not (member this org-done-keywords)))
  8703. (unless (save-excursion
  8704. (save-match-data
  8705. (run-hook-with-args-until-failure
  8706. 'org-blocker-hook change-plist)))
  8707. (if (interactive-p)
  8708. (error "TODO state change from %s to %s blocked" this state)
  8709. ;; fail silently
  8710. (message "TODO state change from %s to %s blocked" this state)
  8711. (throw 'exit nil))))
  8712. (store-match-data match-data)
  8713. (replace-match next t t)
  8714. (unless (pos-visible-in-window-p hl-pos)
  8715. (message "TODO state changed to %s" (org-trim next)))
  8716. (unless head
  8717. (setq head (org-get-todo-sequence-head state)
  8718. ass (assoc head org-todo-kwd-alist)
  8719. interpret (nth 1 ass)
  8720. done-word (nth 3 ass)
  8721. final-done-word (nth 4 ass)))
  8722. (when (memq arg '(nextset previousset))
  8723. (message "Keyword-Set %d/%d: %s"
  8724. (- (length org-todo-sets) -1
  8725. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8726. (length org-todo-sets)
  8727. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8728. (setq org-last-todo-state-is-todo
  8729. (not (member state org-done-keywords)))
  8730. (setq now-done-p (and (member state org-done-keywords)
  8731. (not (member this org-done-keywords))))
  8732. (and logging (org-local-logging logging))
  8733. (when (and (or org-todo-log-states org-log-done)
  8734. (not (eq org-inhibit-logging t))
  8735. (not (memq arg '(nextset previousset))))
  8736. ;; we need to look at recording a time and note
  8737. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8738. (nth 2 (assoc this org-todo-log-states))))
  8739. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8740. (setq dolog 'time))
  8741. (when (and state
  8742. (member state org-not-done-keywords)
  8743. (not (member this org-not-done-keywords)))
  8744. ;; This is now a todo state and was not one before
  8745. ;; If there was a CLOSED time stamp, get rid of it.
  8746. (org-add-planning-info nil nil 'closed))
  8747. (when (and now-done-p org-log-done)
  8748. ;; It is now done, and it was not done before
  8749. (org-add-planning-info 'closed (org-current-time))
  8750. (if (and (not dolog) (eq 'note org-log-done))
  8751. (org-add-log-setup 'done state this 'findpos 'note)))
  8752. (when (and state dolog)
  8753. ;; This is a non-nil state, and we need to log it
  8754. (org-add-log-setup 'state state this 'findpos dolog)))
  8755. ;; Fixup tag positioning
  8756. (org-todo-trigger-tag-changes state)
  8757. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8758. (when org-provide-todo-statistics
  8759. (org-update-parent-todo-statistics))
  8760. (run-hooks 'org-after-todo-state-change-hook)
  8761. (if (and arg (not (member state org-done-keywords)))
  8762. (setq head (org-get-todo-sequence-head state)))
  8763. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8764. ;; Do we need to trigger a repeat?
  8765. (when now-done-p
  8766. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8767. ;; This is for the agenda, take a snapshot of the headline.
  8768. (save-match-data
  8769. (setq org-agenda-headline-snapshot-before-repeat
  8770. (org-get-heading))))
  8771. (org-auto-repeat-maybe state))
  8772. ;; Fixup cursor location if close to the keyword
  8773. (if (and (outline-on-heading-p)
  8774. (not (bolp))
  8775. (save-excursion (beginning-of-line 1)
  8776. (looking-at org-todo-line-regexp))
  8777. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8778. (progn
  8779. (goto-char (or (match-end 2) (match-end 1)))
  8780. (and (looking-at " ") (just-one-space))))
  8781. (when org-trigger-hook
  8782. (save-excursion
  8783. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8784. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8785. "Block turning an entry into a TODO, using the hierarchy.
  8786. This checks whether the current task should be blocked from state
  8787. changes. Such blocking occurs when:
  8788. 1. The task has children which are not all in a completed state.
  8789. 2. A task has a parent with the property :ORDERED:, and there
  8790. are siblings prior to the current task with incomplete
  8791. status.
  8792. 3. The parent of the task is blocked because it has siblings that should
  8793. be done first, or is child of a block grandparent TODO entry."
  8794. (catch 'dont-block
  8795. ;; If this is not a todo state change, or if this entry is already DONE,
  8796. ;; do not block
  8797. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8798. (member (plist-get change-plist :from)
  8799. (cons 'done org-done-keywords))
  8800. (member (plist-get change-plist :to)
  8801. (cons 'todo org-not-done-keywords)))
  8802. (throw 'dont-block t))
  8803. ;; If this task has children, and any are undone, it's blocked
  8804. (save-excursion
  8805. (org-back-to-heading t)
  8806. (let ((this-level (funcall outline-level)))
  8807. (outline-next-heading)
  8808. (let ((child-level (funcall outline-level)))
  8809. (while (and (not (eobp))
  8810. (> child-level this-level))
  8811. ;; this todo has children, check whether they are all
  8812. ;; completed
  8813. (if (and (not (org-entry-is-done-p))
  8814. (org-entry-is-todo-p))
  8815. (throw 'dont-block nil))
  8816. (outline-next-heading)
  8817. (setq child-level (funcall outline-level))))))
  8818. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8819. ;; any previous siblings are undone, it's blocked
  8820. (save-excursion
  8821. (org-back-to-heading t)
  8822. (let* ((pos (point))
  8823. (parent-pos (and (org-up-heading-safe) (point))))
  8824. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8825. (when (and (org-entry-get (point) "ORDERED")
  8826. (forward-line 1)
  8827. (re-search-forward org-not-done-heading-regexp pos t))
  8828. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8829. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8830. (while t
  8831. (goto-char parent-pos)
  8832. (if (not (looking-at org-not-done-heading-regexp))
  8833. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8834. (setq pos (point))
  8835. (setq parent-pos (and (org-up-heading-safe) (point)))
  8836. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8837. (when (and (org-entry-get (point) "ORDERED")
  8838. (forward-line 1)
  8839. (re-search-forward org-not-done-heading-regexp pos t))
  8840. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8841. (defcustom org-track-ordered-property-with-tag nil
  8842. "Should the ORDERED property also be shown as a tag?
  8843. The ORDERED property decides if an entry should require subtasks to be
  8844. completed in sequence. Since a property is not very visible, setting
  8845. this option means that toggling the ORDERED property with the command
  8846. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8847. not relevant for the behavior, but it makes things more visible.
  8848. Note that toggling the tag with tags commands will not change the property
  8849. and therefore not influence behavior!
  8850. This can be t, meaning the tag ORDERED should be used, It can also be a
  8851. string to select a different tag for this task."
  8852. :group 'org-todo
  8853. :type '(choice
  8854. (const :tag "No tracking" nil)
  8855. (const :tag "Track with ORDERED tag" t)
  8856. (string :tag "Use other tag")))
  8857. (defun org-toggle-ordered-property ()
  8858. "Toggle the ORDERED property of the current entry.
  8859. For better visibility, you can track the value of this property with a tag.
  8860. See variable `org-track-ordered-property-with-tag'."
  8861. (interactive)
  8862. (let* ((t1 org-track-ordered-property-with-tag)
  8863. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8864. (save-excursion
  8865. (org-back-to-heading)
  8866. (if (org-entry-get nil "ORDERED")
  8867. (progn
  8868. (org-delete-property "ORDERED")
  8869. (and tag (org-toggle-tag tag 'off))
  8870. (message "Subtasks can be completed in arbitrary order"))
  8871. (org-entry-put nil "ORDERED" "t")
  8872. (and tag (org-toggle-tag tag 'on))
  8873. (message "Subtasks must be completed in sequence")))))
  8874. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8875. (defun org-block-todo-from-checkboxes (change-plist)
  8876. "Block turning an entry into a TODO, using checkboxes.
  8877. This checks whether the current task should be blocked from state
  8878. changes because there are uncheckd boxes in this entry."
  8879. (catch 'dont-block
  8880. ;; If this is not a todo state change, or if this entry is already DONE,
  8881. ;; do not block
  8882. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8883. (member (plist-get change-plist :from)
  8884. (cons 'done org-done-keywords))
  8885. (member (plist-get change-plist :to)
  8886. (cons 'todo org-not-done-keywords)))
  8887. (throw 'dont-block t))
  8888. ;; If this task has checkboxes that are not checked, it's blocked
  8889. (save-excursion
  8890. (org-back-to-heading t)
  8891. (let ((beg (point)) end)
  8892. (outline-next-heading)
  8893. (setq end (point))
  8894. (goto-char beg)
  8895. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8896. end t)
  8897. (progn
  8898. (if (boundp 'org-blocked-by-checkboxes)
  8899. (setq org-blocked-by-checkboxes t))
  8900. (throw 'dont-block nil)))))
  8901. t)) ; do not block
  8902. (defun org-update-statistics-cookies (all)
  8903. "Update the statistics cookie, either from TODO or from checkboxes.
  8904. This should be called with the cursor in a line with a statistics cookie."
  8905. (interactive "P")
  8906. (if all
  8907. (progn
  8908. (org-update-checkbox-count 'all)
  8909. (org-map-entries 'org-update-parent-todo-statistics))
  8910. (if (not (org-on-heading-p))
  8911. (org-update-checkbox-count)
  8912. (let ((pos (move-marker (make-marker) (point)))
  8913. end l1 l2)
  8914. (ignore-errors (org-back-to-heading t))
  8915. (if (not (org-on-heading-p))
  8916. (org-update-checkbox-count)
  8917. (setq l1 (org-outline-level))
  8918. (setq end (save-excursion
  8919. (outline-next-heading)
  8920. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  8921. (point)))
  8922. (if (and (save-excursion (re-search-forward
  8923. "^[ \t]*[-+*] \\[[- X]\\]" end t))
  8924. (not (save-excursion (re-search-forward
  8925. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  8926. (org-update-checkbox-count)
  8927. (if (and l2 (> l2 l1))
  8928. (progn
  8929. (goto-char end)
  8930. (org-update-parent-todo-statistics))
  8931. (error "No data for statistics cookie"))))
  8932. (goto-char pos)
  8933. (move-marker pos nil)))))
  8934. (defvar org-entry-property-inherited-from) ;; defined below
  8935. (defun org-update-parent-todo-statistics ()
  8936. "Update any statistics cookie in the parent of the current headline.
  8937. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8938. the entire subtree and this will travel up the hierarchy and update
  8939. statistics everywhere."
  8940. (interactive)
  8941. (let* ((lim 0) prop
  8942. (recursive (or (not org-hierarchical-todo-statistics)
  8943. (string-match
  8944. "\\<recursive\\>"
  8945. (or (setq prop (org-entry-get
  8946. nil "COOKIE_DATA" 'inherit)) ""))))
  8947. (lim (or (and prop (marker-position
  8948. org-entry-property-inherited-from))
  8949. lim))
  8950. (first t)
  8951. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8952. level ltoggle l1 new ndel
  8953. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8954. (catch 'exit
  8955. (save-excursion
  8956. (beginning-of-line 1)
  8957. (if (org-at-heading-p)
  8958. (setq ltoggle (funcall outline-level))
  8959. (error "This should not happen"))
  8960. (while (and (setq level (org-up-heading-safe))
  8961. (or recursive first)
  8962. (>= (point) lim))
  8963. (setq first nil)
  8964. (unless (and level
  8965. (not (string-match
  8966. "\\<checkbox\\>"
  8967. (downcase
  8968. (or (org-entry-get
  8969. nil "COOKIE_DATA")
  8970. "")))))
  8971. (throw 'exit nil))
  8972. (while (re-search-forward box-re (point-at-eol) t)
  8973. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8974. (setq is-percent (match-end 2))
  8975. (save-match-data
  8976. (unless (outline-next-heading) (throw 'exit nil))
  8977. (while (and (looking-at org-complex-heading-regexp)
  8978. (> (setq l1 (length (match-string 1))) level))
  8979. (setq kwd (and (or recursive (= l1 ltoggle))
  8980. (match-string 2)))
  8981. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8982. (and (listp org-provide-todo-statistics)
  8983. (or (member kwd org-provide-todo-statistics)
  8984. (member kwd org-done-keywords))))
  8985. (setq cnt-all (1+ cnt-all))
  8986. (if (eq org-provide-todo-statistics t)
  8987. (and kwd (setq cnt-all (1+ cnt-all)))))
  8988. (and (member kwd org-done-keywords)
  8989. (setq cnt-done (1+ cnt-done)))
  8990. (outline-next-heading)))
  8991. (setq new
  8992. (if is-percent
  8993. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8994. (format "[%d/%d]" cnt-done cnt-all))
  8995. ndel (- (match-end 0) (match-beginning 0)))
  8996. (goto-char (match-beginning 0))
  8997. (insert new)
  8998. (delete-region (point) (+ (point) ndel))))
  8999. (when cookie-present
  9000. (run-hook-with-args 'org-after-todo-statistics-hook
  9001. cnt-done (- cnt-all cnt-done)))))
  9002. (run-hooks 'org-todo-statistics-hook)))
  9003. (defvar org-after-todo-statistics-hook nil
  9004. "Hook that is called after a TODO statistics cookie has been updated.
  9005. Each function is called with two arguments: the number of not-done entries
  9006. and the number of done entries.
  9007. For example, the following function, when added to this hook, will switch
  9008. an entry to DONE when all children are done, and back to TODO when new
  9009. entries are set to a TODO status. Note that this hook is only called
  9010. when there is a statistics cookie in the headline!
  9011. (defun org-summary-todo (n-done n-not-done)
  9012. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9013. (let (org-log-done org-log-states) ; turn off logging
  9014. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9015. ")
  9016. (defvar org-todo-statistics-hook nil
  9017. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9018. This hook runs even if there is no statisics cookie present, in which case
  9019. `org-after-todo-statistics-hook' would not run.")
  9020. (defun org-todo-trigger-tag-changes (state)
  9021. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9022. (let ((l org-todo-state-tags-triggers)
  9023. changes)
  9024. (when (or (not state) (equal state ""))
  9025. (setq changes (append changes (cdr (assoc "" l)))))
  9026. (when (and (stringp state) (> (length state) 0))
  9027. (setq changes (append changes (cdr (assoc state l)))))
  9028. (when (member state org-not-done-keywords)
  9029. (setq changes (append changes (cdr (assoc 'todo l)))))
  9030. (when (member state org-done-keywords)
  9031. (setq changes (append changes (cdr (assoc 'done l)))))
  9032. (dolist (c changes)
  9033. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9034. (defun org-local-logging (value)
  9035. "Get logging settings from a property VALUE."
  9036. (let* (words w a)
  9037. ;; directly set the variables, they are already local.
  9038. (setq org-log-done nil
  9039. org-log-repeat nil
  9040. org-todo-log-states nil)
  9041. (setq words (org-split-string value))
  9042. (while (setq w (pop words))
  9043. (cond
  9044. ((setq a (assoc w org-startup-options))
  9045. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9046. (set (nth 1 a) (nth 2 a))))
  9047. ((setq a (org-extract-log-state-settings w))
  9048. (and (member (car a) org-todo-keywords-1)
  9049. (push a org-todo-log-states)))))))
  9050. (defun org-get-todo-sequence-head (kwd)
  9051. "Return the head of the TODO sequence to which KWD belongs.
  9052. If KWD is not set, check if there is a text property remembering the
  9053. right sequence."
  9054. (let (p)
  9055. (cond
  9056. ((not kwd)
  9057. (or (get-text-property (point-at-bol) 'org-todo-head)
  9058. (progn
  9059. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9060. nil (point-at-eol)))
  9061. (get-text-property p 'org-todo-head))))
  9062. ((not (member kwd org-todo-keywords-1))
  9063. (car org-todo-keywords-1))
  9064. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9065. (defun org-fast-todo-selection ()
  9066. "Fast TODO keyword selection with single keys.
  9067. Returns the new TODO keyword, or nil if no state change should occur."
  9068. (let* ((fulltable org-todo-key-alist)
  9069. (done-keywords org-done-keywords) ;; needed for the faces.
  9070. (maxlen (apply 'max (mapcar
  9071. (lambda (x)
  9072. (if (stringp (car x)) (string-width (car x)) 0))
  9073. fulltable)))
  9074. (expert nil)
  9075. (fwidth (+ maxlen 3 1 3))
  9076. (ncol (/ (- (window-width) 4) fwidth))
  9077. tg cnt e c tbl
  9078. groups ingroup)
  9079. (save-excursion
  9080. (save-window-excursion
  9081. (if expert
  9082. (set-buffer (get-buffer-create " *Org todo*"))
  9083. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9084. (erase-buffer)
  9085. (org-set-local 'org-done-keywords done-keywords)
  9086. (setq tbl fulltable cnt 0)
  9087. (while (setq e (pop tbl))
  9088. (cond
  9089. ((equal e '(:startgroup))
  9090. (push '() groups) (setq ingroup t)
  9091. (when (not (= cnt 0))
  9092. (setq cnt 0)
  9093. (insert "\n"))
  9094. (insert "{ "))
  9095. ((equal e '(:endgroup))
  9096. (setq ingroup nil cnt 0)
  9097. (insert "}\n"))
  9098. ((equal e '(:newline))
  9099. (when (not (= cnt 0))
  9100. (setq cnt 0)
  9101. (insert "\n")
  9102. (setq e (car tbl))
  9103. (while (equal (car tbl) '(:newline))
  9104. (insert "\n")
  9105. (setq tbl (cdr tbl)))))
  9106. (t
  9107. (setq tg (car e) c (cdr e))
  9108. (if ingroup (push tg (car groups)))
  9109. (setq tg (org-add-props tg nil 'face
  9110. (org-get-todo-face tg)))
  9111. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9112. (insert "[" c "] " tg (make-string
  9113. (- fwidth 4 (length tg)) ?\ ))
  9114. (when (= (setq cnt (1+ cnt)) ncol)
  9115. (insert "\n")
  9116. (if ingroup (insert " "))
  9117. (setq cnt 0)))))
  9118. (insert "\n")
  9119. (goto-char (point-min))
  9120. (if (not expert) (org-fit-window-to-buffer))
  9121. (message "[a-z..]:Set [SPC]:clear")
  9122. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9123. (cond
  9124. ((or (= c ?\C-g)
  9125. (and (= c ?q) (not (rassoc c fulltable))))
  9126. (setq quit-flag t))
  9127. ((= c ?\ ) nil)
  9128. ((setq e (rassoc c fulltable) tg (car e))
  9129. tg)
  9130. (t (setq quit-flag t)))))))
  9131. (defun org-entry-is-todo-p ()
  9132. (member (org-get-todo-state) org-not-done-keywords))
  9133. (defun org-entry-is-done-p ()
  9134. (member (org-get-todo-state) org-done-keywords))
  9135. (defun org-get-todo-state ()
  9136. (save-excursion
  9137. (org-back-to-heading t)
  9138. (and (looking-at org-todo-line-regexp)
  9139. (match-end 2)
  9140. (match-string 2))))
  9141. (defun org-at-date-range-p (&optional inactive-ok)
  9142. "Is the cursor inside a date range?"
  9143. (interactive)
  9144. (save-excursion
  9145. (catch 'exit
  9146. (let ((pos (point)))
  9147. (skip-chars-backward "^[<\r\n")
  9148. (skip-chars-backward "<[")
  9149. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9150. (>= (match-end 0) pos)
  9151. (throw 'exit t))
  9152. (skip-chars-backward "^<[\r\n")
  9153. (skip-chars-backward "<[")
  9154. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9155. (>= (match-end 0) pos)
  9156. (throw 'exit t)))
  9157. nil)))
  9158. (defun org-get-repeat (&optional tagline)
  9159. "Check if there is a deadline/schedule with repeater in this entry."
  9160. (save-match-data
  9161. (save-excursion
  9162. (org-back-to-heading t)
  9163. (and (re-search-forward (if tagline
  9164. (concat tagline "\\s-*" org-repeat-re)
  9165. org-repeat-re)
  9166. (org-entry-end-position) t)
  9167. (match-string-no-properties 1)))))
  9168. (defvar org-last-changed-timestamp)
  9169. (defvar org-last-inserted-timestamp)
  9170. (defvar org-log-post-message)
  9171. (defvar org-log-note-purpose)
  9172. (defvar org-log-note-how)
  9173. (defvar org-log-note-extra)
  9174. (defun org-auto-repeat-maybe (done-word)
  9175. "Check if the current headline contains a repeated deadline/schedule.
  9176. If yes, set TODO state back to what it was and change the base date
  9177. of repeating deadline/scheduled time stamps to new date.
  9178. This function is run automatically after each state change to a DONE state."
  9179. ;; last-state is dynamically scoped into this function
  9180. (let* ((repeat (org-get-repeat))
  9181. (aa (assoc last-state org-todo-kwd-alist))
  9182. (interpret (nth 1 aa))
  9183. (head (nth 2 aa))
  9184. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9185. (msg "Entry repeats: ")
  9186. (org-log-done nil)
  9187. (org-todo-log-states nil)
  9188. (nshiftmax 10) (nshift 0)
  9189. re type n what ts time)
  9190. (when repeat
  9191. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9192. (org-todo (if (eq interpret 'type) last-state head))
  9193. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9194. (org-time-stamp-format t t)))
  9195. (when org-log-repeat
  9196. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9197. (memq 'org-add-log-note post-command-hook))
  9198. ;; OK, we are already setup for some record
  9199. (if (eq org-log-repeat 'note)
  9200. ;; make sure we take a note, not only a time stamp
  9201. (setq org-log-note-how 'note))
  9202. ;; Set up for taking a record
  9203. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9204. last-state
  9205. 'findpos org-log-repeat)))
  9206. (org-back-to-heading t)
  9207. (org-add-planning-info nil nil 'closed)
  9208. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9209. org-deadline-time-regexp "\\)\\|\\("
  9210. org-ts-regexp "\\)"))
  9211. (while (re-search-forward
  9212. re (save-excursion (outline-next-heading) (point)) t)
  9213. (setq type (if (match-end 1) org-scheduled-string
  9214. (if (match-end 3) org-deadline-string "Plain:"))
  9215. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9216. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9217. (setq n (string-to-number (match-string 2 ts))
  9218. what (match-string 3 ts))
  9219. (if (equal what "w") (setq n (* n 7) what "d"))
  9220. ;; Preparation, see if we need to modify the start date for the change
  9221. (when (match-end 1)
  9222. (setq time (save-match-data (org-time-string-to-time ts)))
  9223. (cond
  9224. ((equal (match-string 1 ts) ".")
  9225. ;; Shift starting date to today
  9226. (org-timestamp-change
  9227. (- (time-to-days (current-time)) (time-to-days time))
  9228. 'day))
  9229. ((equal (match-string 1 ts) "+")
  9230. (while (or (= nshift 0)
  9231. (<= (time-to-days time) (time-to-days (current-time))))
  9232. (when (= (incf nshift) nshiftmax)
  9233. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9234. (error "Abort")))
  9235. (org-timestamp-change n (cdr (assoc what whata)))
  9236. (org-at-timestamp-p t)
  9237. (setq ts (match-string 1))
  9238. (setq time (save-match-data (org-time-string-to-time ts))))
  9239. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9240. ;; rematch, so that we have everything in place for the real shift
  9241. (org-at-timestamp-p t)
  9242. (setq ts (match-string 1))
  9243. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9244. (org-timestamp-change n (cdr (assoc what whata)))
  9245. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9246. (setq org-log-post-message msg)
  9247. (message "%s" msg))))
  9248. (defun org-show-todo-tree (arg)
  9249. "Make a compact tree which shows all headlines marked with TODO.
  9250. The tree will show the lines where the regexp matches, and all higher
  9251. headlines above the match.
  9252. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9253. With a numeric prefix N, construct a sparse tree for the Nth element
  9254. of `org-todo-keywords-1'."
  9255. (interactive "P")
  9256. (let ((case-fold-search nil)
  9257. (kwd-re
  9258. (cond ((null arg) org-not-done-regexp)
  9259. ((equal arg '(4))
  9260. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9261. (mapcar 'list org-todo-keywords-1))))
  9262. (concat "\\("
  9263. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9264. "\\)\\>")))
  9265. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9266. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9267. org-todo-keywords-1)))
  9268. (t (error "Invalid prefix argument: %s" arg)))))
  9269. (message "%d TODO entries found"
  9270. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9271. (defun org-deadline (&optional remove time)
  9272. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9273. With argument REMOVE, remove any deadline from the item.
  9274. When TIME is set, it should be an internal time specification, and the
  9275. scheduling will use the corresponding date."
  9276. (interactive "P")
  9277. (if remove
  9278. (progn
  9279. (org-remove-timestamp-with-keyword org-deadline-string)
  9280. (message "Item no longer has a deadline."))
  9281. (if (org-get-repeat)
  9282. (error "Cannot change deadline on task with repeater, please do that by hand")
  9283. (org-add-planning-info 'deadline time 'closed)
  9284. (message "Deadline on %s" org-last-inserted-timestamp))))
  9285. (defun org-schedule (&optional remove time)
  9286. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9287. With argument REMOVE, remove any scheduling date from the item.
  9288. When TIME is set, it should be an internal time specification, and the
  9289. scheduling will use the corresponding date."
  9290. (interactive "P")
  9291. (if remove
  9292. (progn
  9293. (org-remove-timestamp-with-keyword org-scheduled-string)
  9294. (message "Item is no longer scheduled."))
  9295. (if (org-get-repeat)
  9296. (error "Cannot reschedule task with repeater, please do that by hand")
  9297. (org-add-planning-info 'scheduled time 'closed)
  9298. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9299. (defun org-get-scheduled-time (pom &optional inherit)
  9300. "Get the scheduled time as a time tuple, of a format suitable
  9301. for calling org-schedule with, or if there is no scheduling,
  9302. returns nil."
  9303. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9304. (when time
  9305. (apply 'encode-time (org-parse-time-string time)))))
  9306. (defun org-get-deadline-time (pom &optional inherit)
  9307. "Get the deadine as a time tuple, of a format suitable for
  9308. calling org-deadlin with, or if there is no scheduling, returns
  9309. nil."
  9310. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9311. (when time
  9312. (apply 'encode-time (org-parse-time-string time)))))
  9313. (defun org-remove-timestamp-with-keyword (keyword)
  9314. "Remove all time stamps with KEYWORD in the current entry."
  9315. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9316. beg)
  9317. (save-excursion
  9318. (org-back-to-heading t)
  9319. (setq beg (point))
  9320. (outline-next-heading)
  9321. (while (re-search-backward re beg t)
  9322. (replace-match "")
  9323. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9324. (equal (char-before) ?\ ))
  9325. (backward-delete-char 1)
  9326. (if (string-match "^[ \t]*$" (buffer-substring
  9327. (point-at-bol) (point-at-eol)))
  9328. (delete-region (point-at-bol)
  9329. (min (point-max) (1+ (point-at-eol))))))))))
  9330. (defun org-add-planning-info (what &optional time &rest remove)
  9331. "Insert new timestamp with keyword in the line directly after the headline.
  9332. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9333. If non is given, the user is prompted for a date.
  9334. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9335. be removed."
  9336. (interactive)
  9337. (let (org-time-was-given org-end-time-was-given ts
  9338. end default-time default-input)
  9339. (catch 'exit
  9340. (when (and (not time) (memq what '(scheduled deadline)))
  9341. ;; Try to get a default date/time from existing timestamp
  9342. (save-excursion
  9343. (org-back-to-heading t)
  9344. (setq end (save-excursion (outline-next-heading) (point)))
  9345. (when (re-search-forward (if (eq what 'scheduled)
  9346. org-scheduled-time-regexp
  9347. org-deadline-time-regexp)
  9348. end t)
  9349. (setq ts (match-string 1)
  9350. default-time
  9351. (apply 'encode-time (org-parse-time-string ts))
  9352. default-input (and ts (org-get-compact-tod ts))))))
  9353. (when what
  9354. ;; If necessary, get the time from the user
  9355. (setq time (or time (org-read-date nil 'to-time nil nil
  9356. default-time default-input))))
  9357. (when (and org-insert-labeled-timestamps-at-point
  9358. (member what '(scheduled deadline)))
  9359. (insert
  9360. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9361. (org-insert-time-stamp time org-time-was-given
  9362. nil nil nil (list org-end-time-was-given))
  9363. (setq what nil))
  9364. (save-excursion
  9365. (save-restriction
  9366. (let (col list elt ts buffer-invisibility-spec)
  9367. (org-back-to-heading t)
  9368. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9369. (goto-char (match-end 1))
  9370. (setq col (current-column))
  9371. (goto-char (match-end 0))
  9372. (if (eobp) (insert "\n") (forward-char 1))
  9373. (when (and (not what)
  9374. (not (looking-at
  9375. (concat "[ \t]*"
  9376. org-keyword-time-not-clock-regexp))))
  9377. ;; Nothing to add, nothing to remove...... :-)
  9378. (throw 'exit nil))
  9379. (if (and (not (looking-at outline-regexp))
  9380. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9381. "[^\r\n]*"))
  9382. (not (equal (match-string 1) org-clock-string)))
  9383. (narrow-to-region (match-beginning 0) (match-end 0))
  9384. (insert-before-markers "\n")
  9385. (backward-char 1)
  9386. (narrow-to-region (point) (point))
  9387. (and org-adapt-indentation (org-indent-to-column col)))
  9388. ;; Check if we have to remove something.
  9389. (setq list (cons what remove))
  9390. (while list
  9391. (setq elt (pop list))
  9392. (goto-char (point-min))
  9393. (when (or (and (eq elt 'scheduled)
  9394. (re-search-forward org-scheduled-time-regexp nil t))
  9395. (and (eq elt 'deadline)
  9396. (re-search-forward org-deadline-time-regexp nil t))
  9397. (and (eq elt 'closed)
  9398. (re-search-forward org-closed-time-regexp nil t)))
  9399. (replace-match "")
  9400. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9401. (skip-chars-backward " ")
  9402. (if (looking-at " +") (replace-match ""))))
  9403. (goto-char (point-max))
  9404. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  9405. (when what
  9406. (insert
  9407. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9408. (cond ((eq what 'scheduled) org-scheduled-string)
  9409. ((eq what 'deadline) org-deadline-string)
  9410. ((eq what 'closed) org-closed-string))
  9411. " ")
  9412. (setq ts (org-insert-time-stamp
  9413. time
  9414. (or org-time-was-given
  9415. (and (eq what 'closed) org-log-done-with-time))
  9416. (eq what 'closed)
  9417. nil nil (list org-end-time-was-given)))
  9418. (end-of-line 1))
  9419. (goto-char (point-min))
  9420. (widen)
  9421. (if (and (looking-at "[ \t]+\n")
  9422. (equal (char-before) ?\n))
  9423. (delete-region (1- (point)) (point-at-eol)))
  9424. ts))))))
  9425. (defvar org-log-note-marker (make-marker))
  9426. (defvar org-log-note-purpose nil)
  9427. (defvar org-log-note-state nil)
  9428. (defvar org-log-note-previous-state nil)
  9429. (defvar org-log-note-how nil)
  9430. (defvar org-log-note-extra nil)
  9431. (defvar org-log-note-window-configuration nil)
  9432. (defvar org-log-note-return-to (make-marker))
  9433. (defvar org-log-post-message nil
  9434. "Message to be displayed after a log note has been stored.
  9435. The auto-repeater uses this.")
  9436. (defun org-add-note ()
  9437. "Add a note to the current entry.
  9438. This is done in the same way as adding a state change note."
  9439. (interactive)
  9440. (org-add-log-setup 'note nil nil 'findpos nil))
  9441. (defvar org-property-end-re)
  9442. (defun org-add-log-setup (&optional purpose state prev-state
  9443. findpos how &optional extra)
  9444. "Set up the post command hook to take a note.
  9445. If this is about to TODO state change, the new state is expected in STATE.
  9446. When FINDPOS is non-nil, find the correct position for the note in
  9447. the current entry. If not, assume that it can be inserted at point.
  9448. HOW is an indicator what kind of note should be created.
  9449. EXTRA is additional text that will be inserted into the notes buffer."
  9450. (let* ((org-log-into-drawer (org-log-into-drawer))
  9451. (drawer (cond ((stringp org-log-into-drawer)
  9452. org-log-into-drawer)
  9453. (org-log-into-drawer "LOGBOOK")
  9454. (t nil))))
  9455. (save-restriction
  9456. (save-excursion
  9457. (when findpos
  9458. (org-back-to-heading t)
  9459. (narrow-to-region (point) (save-excursion
  9460. (outline-next-heading) (point)))
  9461. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9462. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9463. "[^\r\n]*\\)?"))
  9464. (goto-char (match-end 0))
  9465. (cond
  9466. (drawer
  9467. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9468. nil t)
  9469. (progn
  9470. (goto-char (match-end 0))
  9471. (or org-log-states-order-reversed
  9472. (and (re-search-forward org-property-end-re nil t)
  9473. (goto-char (1- (match-beginning 0))))))
  9474. (insert "\n:" drawer ":\n:END:")
  9475. (beginning-of-line 0)
  9476. (org-indent-line-function)
  9477. (beginning-of-line 2)
  9478. (org-indent-line-function)
  9479. (end-of-line 0)))
  9480. ((and org-log-state-notes-insert-after-drawers
  9481. (save-excursion
  9482. (forward-line) (looking-at org-drawer-regexp)))
  9483. (forward-line)
  9484. (while (looking-at org-drawer-regexp)
  9485. (goto-char (match-end 0))
  9486. (re-search-forward org-property-end-re (point-max) t)
  9487. (forward-line))
  9488. (forward-line -1)))
  9489. (unless org-log-states-order-reversed
  9490. (and (= (char-after) ?\n) (forward-char 1))
  9491. (org-skip-over-state-notes)
  9492. (skip-chars-backward " \t\n\r")))
  9493. (move-marker org-log-note-marker (point))
  9494. (setq org-log-note-purpose purpose
  9495. org-log-note-state state
  9496. org-log-note-previous-state prev-state
  9497. org-log-note-how how
  9498. org-log-note-extra extra)
  9499. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9500. (defun org-skip-over-state-notes ()
  9501. "Skip past the list of State notes in an entry."
  9502. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9503. (while (looking-at "[ \t]*- State")
  9504. (condition-case nil
  9505. (org-next-item)
  9506. (error (org-end-of-item)))))
  9507. (defun org-add-log-note (&optional purpose)
  9508. "Pop up a window for taking a note, and add this note later at point."
  9509. (remove-hook 'post-command-hook 'org-add-log-note)
  9510. (setq org-log-note-window-configuration (current-window-configuration))
  9511. (delete-other-windows)
  9512. (move-marker org-log-note-return-to (point))
  9513. (switch-to-buffer (marker-buffer org-log-note-marker))
  9514. (goto-char org-log-note-marker)
  9515. (org-switch-to-buffer-other-window "*Org Note*")
  9516. (erase-buffer)
  9517. (if (memq org-log-note-how '(time state))
  9518. (let (current-prefix-arg) (org-store-log-note))
  9519. (let ((org-inhibit-startup t)) (org-mode))
  9520. (insert (format "# Insert note for %s.
  9521. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9522. (cond
  9523. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9524. ((eq org-log-note-purpose 'done) "closed todo item")
  9525. ((eq org-log-note-purpose 'state)
  9526. (format "state change from \"%s\" to \"%s\""
  9527. (or org-log-note-previous-state "")
  9528. (or org-log-note-state "")))
  9529. ((eq org-log-note-purpose 'note)
  9530. "this entry")
  9531. (t (error "This should not happen")))))
  9532. (if org-log-note-extra (insert org-log-note-extra))
  9533. (org-set-local 'org-finish-function 'org-store-log-note)))
  9534. (defvar org-note-abort nil) ; dynamically scoped
  9535. (defun org-store-log-note ()
  9536. "Finish taking a log note, and insert it to where it belongs."
  9537. (let ((txt (buffer-string))
  9538. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9539. lines ind)
  9540. (kill-buffer (current-buffer))
  9541. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9542. (setq txt (replace-match "" t t txt)))
  9543. (if (string-match "\\s-+\\'" txt)
  9544. (setq txt (replace-match "" t t txt)))
  9545. (setq lines (org-split-string txt "\n"))
  9546. (when (and note (string-match "\\S-" note))
  9547. (setq note
  9548. (org-replace-escapes
  9549. note
  9550. (list (cons "%u" (user-login-name))
  9551. (cons "%U" user-full-name)
  9552. (cons "%t" (format-time-string
  9553. (org-time-stamp-format 'long 'inactive)
  9554. (current-time)))
  9555. (cons "%s" (if org-log-note-state
  9556. (concat "\"" org-log-note-state "\"")
  9557. ""))
  9558. (cons "%S" (if org-log-note-previous-state
  9559. (concat "\"" org-log-note-previous-state "\"")
  9560. "\"\"")))))
  9561. (if lines (setq note (concat note " \\\\")))
  9562. (push note lines))
  9563. (when (or current-prefix-arg org-note-abort)
  9564. (when org-log-into-drawer
  9565. (org-remove-empty-drawer-at
  9566. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9567. org-log-note-marker))
  9568. (setq lines nil))
  9569. (when lines
  9570. (save-excursion
  9571. (set-buffer (marker-buffer org-log-note-marker))
  9572. (save-excursion
  9573. (goto-char org-log-note-marker)
  9574. (move-marker org-log-note-marker nil)
  9575. (end-of-line 1)
  9576. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9577. (insert "- " (pop lines))
  9578. (org-indent-line-function)
  9579. (beginning-of-line 1)
  9580. (looking-at "[ \t]*")
  9581. (setq ind (concat (match-string 0) " "))
  9582. (end-of-line 1)
  9583. (while lines (insert "\n" ind (pop lines)))
  9584. (message "Note stored")
  9585. (org-back-to-heading t)
  9586. (org-cycle-hide-drawers 'children)))))
  9587. (set-window-configuration org-log-note-window-configuration)
  9588. (with-current-buffer (marker-buffer org-log-note-return-to)
  9589. (goto-char org-log-note-return-to))
  9590. (move-marker org-log-note-return-to nil)
  9591. (and org-log-post-message (message "%s" org-log-post-message)))
  9592. (defun org-remove-empty-drawer-at (drawer pos)
  9593. "Remove an emptyr DARWER drawer at position POS.
  9594. POS may also be a marker."
  9595. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9596. (save-excursion
  9597. (save-restriction
  9598. (widen)
  9599. (goto-char pos)
  9600. (if (org-in-regexp
  9601. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9602. (replace-match ""))))))
  9603. (defun org-sparse-tree (&optional arg)
  9604. "Create a sparse tree, prompt for the details.
  9605. This command can create sparse trees. You first need to select the type
  9606. of match used to create the tree:
  9607. t Show entries with a specific TODO keyword.
  9608. m Show entries selected by a tags/property match.
  9609. p Enter a property name and its value (both with completion on existing
  9610. names/values) and show entries with that property.
  9611. r Show entries matching a regular expression.
  9612. d Show deadlines due within `org-deadline-warning-days'.
  9613. b Show deadlines and scheduled items before a date.
  9614. a Show deadlines and scheduled items after a date."
  9615. (interactive "P")
  9616. (let (ans kwd value)
  9617. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9618. (setq ans (read-char-exclusive))
  9619. (cond
  9620. ((equal ans ?d)
  9621. (call-interactively 'org-check-deadlines))
  9622. ((equal ans ?b)
  9623. (call-interactively 'org-check-before-date))
  9624. ((equal ans ?a)
  9625. (call-interactively 'org-check-after-date))
  9626. ((equal ans ?t)
  9627. (org-show-todo-tree '(4)))
  9628. ((member ans '(?T ?m))
  9629. (call-interactively 'org-match-sparse-tree))
  9630. ((member ans '(?p ?P))
  9631. (setq kwd (org-icompleting-read "Property: "
  9632. (mapcar 'list (org-buffer-property-keys))))
  9633. (setq value (org-icompleting-read "Value: "
  9634. (mapcar 'list (org-property-values kwd))))
  9635. (unless (string-match "\\`{.*}\\'" value)
  9636. (setq value (concat "\"" value "\"")))
  9637. (org-match-sparse-tree arg (concat kwd "=" value)))
  9638. ((member ans '(?r ?R ?/))
  9639. (call-interactively 'org-occur))
  9640. (t (error "No such sparse tree command \"%c\"" ans)))))
  9641. (defvar org-occur-highlights nil
  9642. "List of overlays used for occur matches.")
  9643. (make-variable-buffer-local 'org-occur-highlights)
  9644. (defvar org-occur-parameters nil
  9645. "Parameters of the active org-occur calls.
  9646. This is a list, each call to org-occur pushes as cons cell,
  9647. containing the regular expression and the callback, onto the list.
  9648. The list can contain several entries if `org-occur' has been called
  9649. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9650. will only contain one set of parameters. When the highlights are
  9651. removed (for example with `C-c C-c', or with the next edit (depending
  9652. on `org-remove-highlights-with-change'), this variable is emptied
  9653. as well.")
  9654. (make-variable-buffer-local 'org-occur-parameters)
  9655. (defun org-occur (regexp &optional keep-previous callback)
  9656. "Make a compact tree which shows all matches of REGEXP.
  9657. The tree will show the lines where the regexp matches, and all higher
  9658. headlines above the match. It will also show the heading after the match,
  9659. to make sure editing the matching entry is easy.
  9660. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9661. call to `org-occur' will be kept, to allow stacking of calls to this
  9662. command.
  9663. If CALLBACK is non-nil, it is a function which is called to confirm
  9664. that the match should indeed be shown."
  9665. (interactive "sRegexp: \nP")
  9666. (when (equal regexp "")
  9667. (error "Regexp cannot be empty"))
  9668. (unless keep-previous
  9669. (org-remove-occur-highlights nil nil t))
  9670. (push (cons regexp callback) org-occur-parameters)
  9671. (let ((cnt 0))
  9672. (save-excursion
  9673. (goto-char (point-min))
  9674. (if (or (not keep-previous) ; do not want to keep
  9675. (not org-occur-highlights)) ; no previous matches
  9676. ;; hide everything
  9677. (org-overview))
  9678. (while (re-search-forward regexp nil t)
  9679. (when (or (not callback)
  9680. (save-match-data (funcall callback)))
  9681. (setq cnt (1+ cnt))
  9682. (when org-highlight-sparse-tree-matches
  9683. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9684. (org-show-context 'occur-tree))))
  9685. (when org-remove-highlights-with-change
  9686. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9687. nil 'local))
  9688. (unless org-sparse-tree-open-archived-trees
  9689. (org-hide-archived-subtrees (point-min) (point-max)))
  9690. (run-hooks 'org-occur-hook)
  9691. (if (interactive-p)
  9692. (message "%d match(es) for regexp %s" cnt regexp))
  9693. cnt))
  9694. (defun org-show-context (&optional key)
  9695. "Make sure point and context and visible.
  9696. How much context is shown depends upon the variables
  9697. `org-show-hierarchy-above', `org-show-following-heading'. and
  9698. `org-show-siblings'."
  9699. (let ((heading-p (org-on-heading-p t))
  9700. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9701. (following-p (org-get-alist-option org-show-following-heading key))
  9702. (entry-p (org-get-alist-option org-show-entry-below key))
  9703. (siblings-p (org-get-alist-option org-show-siblings key)))
  9704. (catch 'exit
  9705. ;; Show heading or entry text
  9706. (if (and heading-p (not entry-p))
  9707. (org-flag-heading nil) ; only show the heading
  9708. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9709. (org-show-hidden-entry))) ; show entire entry
  9710. (when following-p
  9711. ;; Show next sibling, or heading below text
  9712. (save-excursion
  9713. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9714. (org-flag-heading nil))))
  9715. (when siblings-p (org-show-siblings))
  9716. (when hierarchy-p
  9717. ;; show all higher headings, possibly with siblings
  9718. (save-excursion
  9719. (while (and (condition-case nil
  9720. (progn (org-up-heading-all 1) t)
  9721. (error nil))
  9722. (not (bobp)))
  9723. (org-flag-heading nil)
  9724. (when siblings-p (org-show-siblings))))))))
  9725. (defun org-reveal (&optional siblings)
  9726. "Show current entry, hierarchy above it, and the following headline.
  9727. This can be used to show a consistent set of context around locations
  9728. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9729. not t for the search context.
  9730. With optional argument SIBLINGS, on each level of the hierarchy all
  9731. siblings are shown. This repairs the tree structure to what it would
  9732. look like when opened with hierarchical calls to `org-cycle'."
  9733. (interactive "P")
  9734. (let ((org-show-hierarchy-above t)
  9735. (org-show-following-heading t)
  9736. (org-show-siblings (if siblings t org-show-siblings)))
  9737. (org-show-context nil)))
  9738. (defun org-highlight-new-match (beg end)
  9739. "Highlight from BEG to END and mark the highlight is an occur headline."
  9740. (let ((ov (org-make-overlay beg end)))
  9741. (org-overlay-put ov 'face 'secondary-selection)
  9742. (push ov org-occur-highlights)))
  9743. (defun org-remove-occur-highlights (&optional beg end noremove)
  9744. "Remove the occur highlights from the buffer.
  9745. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9746. from the `before-change-functions' in the current buffer."
  9747. (interactive)
  9748. (unless org-inhibit-highlight-removal
  9749. (mapc 'org-delete-overlay org-occur-highlights)
  9750. (setq org-occur-highlights nil)
  9751. (setq org-occur-parameters nil)
  9752. (unless noremove
  9753. (remove-hook 'before-change-functions
  9754. 'org-remove-occur-highlights 'local))))
  9755. ;;;; Priorities
  9756. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9757. "Regular expression matching the priority indicator.")
  9758. (defvar org-remove-priority-next-time nil)
  9759. (defun org-priority-up ()
  9760. "Increase the priority of the current item."
  9761. (interactive)
  9762. (org-priority 'up))
  9763. (defun org-priority-down ()
  9764. "Decrease the priority of the current item."
  9765. (interactive)
  9766. (org-priority 'down))
  9767. (defun org-priority (&optional action)
  9768. "Change the priority of an item by ARG.
  9769. ACTION can be `set', `up', `down', or a character."
  9770. (interactive)
  9771. (unless org-enable-priority-commands
  9772. (error "Priority commands are disabled"))
  9773. (setq action (or action 'set))
  9774. (let (current new news have remove)
  9775. (save-excursion
  9776. (org-back-to-heading t)
  9777. (if (looking-at org-priority-regexp)
  9778. (setq current (string-to-char (match-string 2))
  9779. have t)
  9780. (setq current org-default-priority))
  9781. (cond
  9782. ((or (eq action 'set)
  9783. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9784. (if (not (eq action 'set))
  9785. (setq new action)
  9786. (message "Priority %c-%c, SPC to remove: "
  9787. org-highest-priority org-lowest-priority)
  9788. (setq new (read-char-exclusive)))
  9789. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9790. (= (upcase org-lowest-priority) org-lowest-priority))
  9791. (setq new (upcase new)))
  9792. (cond ((equal new ?\ ) (setq remove t))
  9793. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9794. (error "Priority must be between `%c' and `%c'"
  9795. org-highest-priority org-lowest-priority))))
  9796. ((eq action 'up)
  9797. (if (and (not have) (eq last-command this-command))
  9798. (setq new org-lowest-priority)
  9799. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9800. org-default-priority (1- current)))))
  9801. ((eq action 'down)
  9802. (if (and (not have) (eq last-command this-command))
  9803. (setq new org-highest-priority)
  9804. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9805. org-default-priority (1+ current)))))
  9806. (t (error "Invalid action")))
  9807. (if (or (< (upcase new) org-highest-priority)
  9808. (> (upcase new) org-lowest-priority))
  9809. (setq remove t))
  9810. (setq news (format "%c" new))
  9811. (if have
  9812. (if remove
  9813. (replace-match "" t t nil 1)
  9814. (replace-match news t t nil 2))
  9815. (if remove
  9816. (error "No priority cookie found in line")
  9817. (let ((case-fold-search nil))
  9818. (looking-at org-todo-line-regexp))
  9819. (if (match-end 2)
  9820. (progn
  9821. (goto-char (match-end 2))
  9822. (insert " [#" news "]"))
  9823. (goto-char (match-beginning 3))
  9824. (insert "[#" news "] "))))
  9825. (org-preserve-lc (org-set-tags nil 'align)))
  9826. (if remove
  9827. (message "Priority removed")
  9828. (message "Priority of current item set to %s" news))))
  9829. (defun org-get-priority (s)
  9830. "Find priority cookie and return priority."
  9831. (save-match-data
  9832. (if (not (string-match org-priority-regexp s))
  9833. (* 1000 (- org-lowest-priority org-default-priority))
  9834. (* 1000 (- org-lowest-priority
  9835. (string-to-char (match-string 2 s)))))))
  9836. ;;;; Tags
  9837. (defvar org-agenda-archives-mode)
  9838. (defvar org-map-continue-from nil
  9839. "Position from where mapping should continue.
  9840. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9841. (defvar org-scanner-tags nil
  9842. "The current tag list while the tags scanner is running.")
  9843. (defvar org-trust-scanner-tags nil
  9844. "Should `org-get-tags-at' use the tags fro the scanner.
  9845. This is for internal dynamical scoping only.
  9846. When this is non-nil, the function `org-get-tags-at' will return the value
  9847. of `org-scanner-tags' instead of building the list by itself. This
  9848. can lead to large speed-ups when the tags scanner is used in a file with
  9849. many entries, and when the list of tags is retrieved, for example to
  9850. obtain a list of properties. Building the tags list for each entry in such
  9851. a file becomes an N^2 operation - but with this variable set, it scales
  9852. as N.")
  9853. (defun org-scan-tags (action matcher &optional todo-only)
  9854. "Scan headline tags with inheritance and produce output ACTION.
  9855. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9856. or `agenda' to produce an entry list for an agenda view. It can also be
  9857. a Lisp form or a function that should be called at each matched headline, in
  9858. this case the return value is a list of all return values from these calls.
  9859. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9860. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9861. only lines with a TODO keyword are included in the output."
  9862. (require 'org-agenda)
  9863. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9864. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9865. (org-re
  9866. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9867. (props (list 'face 'default
  9868. 'done-face 'org-agenda-done
  9869. 'undone-face 'default
  9870. 'mouse-face 'highlight
  9871. 'org-not-done-regexp org-not-done-regexp
  9872. 'org-todo-regexp org-todo-regexp
  9873. 'keymap org-agenda-keymap
  9874. 'help-echo
  9875. (format "mouse-2 or RET jump to org file %s"
  9876. (abbreviate-file-name
  9877. (or (buffer-file-name (buffer-base-buffer))
  9878. (buffer-name (buffer-base-buffer)))))))
  9879. (case-fold-search nil)
  9880. (org-map-continue-from nil)
  9881. lspos tags tags-list
  9882. (tags-alist (list (cons 0 org-file-tags)))
  9883. (llast 0) rtn rtn1 level category i txt
  9884. todo marker entry priority)
  9885. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9886. (setq action (list 'lambda nil action)))
  9887. (save-excursion
  9888. (goto-char (point-min))
  9889. (when (eq action 'sparse-tree)
  9890. (org-overview)
  9891. (org-remove-occur-highlights))
  9892. (while (re-search-forward re nil t)
  9893. (catch :skip
  9894. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9895. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9896. (goto-char (setq lspos (match-beginning 0)))
  9897. (setq level (org-reduced-level (funcall outline-level))
  9898. category (org-get-category))
  9899. (setq i llast llast level)
  9900. ;; remove tag lists from same and sublevels
  9901. (while (>= i level)
  9902. (when (setq entry (assoc i tags-alist))
  9903. (setq tags-alist (delete entry tags-alist)))
  9904. (setq i (1- i)))
  9905. ;; add the next tags
  9906. (when tags
  9907. (setq tags (org-split-string tags ":")
  9908. tags-alist
  9909. (cons (cons level tags) tags-alist)))
  9910. ;; compile tags for current headline
  9911. (setq tags-list
  9912. (if org-use-tag-inheritance
  9913. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9914. tags)
  9915. org-scanner-tags tags-list)
  9916. (when org-use-tag-inheritance
  9917. (setcdr (car tags-alist)
  9918. (mapcar (lambda (x)
  9919. (setq x (copy-sequence x))
  9920. (org-add-prop-inherited x))
  9921. (cdar tags-alist))))
  9922. (when (and tags org-use-tag-inheritance
  9923. (or (not (eq t org-use-tag-inheritance))
  9924. org-tags-exclude-from-inheritance))
  9925. ;; selective inheritance, remove uninherited ones
  9926. (setcdr (car tags-alist)
  9927. (org-remove-uniherited-tags (cdar tags-alist))))
  9928. (when (and (or (not todo-only)
  9929. (and (member todo org-not-done-keywords)
  9930. (or (not org-agenda-tags-todo-honor-ignore-options)
  9931. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9932. (let ((case-fold-search t)) (eval matcher))
  9933. (or
  9934. (not (member org-archive-tag tags-list))
  9935. ;; we have an archive tag, should we use this anyway?
  9936. (or (not org-agenda-skip-archived-trees)
  9937. (and (eq action 'agenda) org-agenda-archives-mode))))
  9938. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9939. ;; select this headline
  9940. (cond
  9941. ((eq action 'sparse-tree)
  9942. (and org-highlight-sparse-tree-matches
  9943. (org-get-heading) (match-end 0)
  9944. (org-highlight-new-match
  9945. (match-beginning 0) (match-beginning 1)))
  9946. (org-show-context 'tags-tree))
  9947. ((eq action 'agenda)
  9948. (setq txt (org-format-agenda-item
  9949. ""
  9950. (concat
  9951. (if (eq org-tags-match-list-sublevels 'indented)
  9952. (make-string (1- level) ?.) "")
  9953. (org-get-heading))
  9954. category
  9955. tags-list
  9956. )
  9957. priority (org-get-priority txt))
  9958. (goto-char lspos)
  9959. (setq marker (org-agenda-new-marker))
  9960. (org-add-props txt props
  9961. 'org-marker marker 'org-hd-marker marker 'org-category category
  9962. 'todo-state todo
  9963. 'priority priority 'type "tagsmatch")
  9964. (push txt rtn))
  9965. ((functionp action)
  9966. (setq org-map-continue-from nil)
  9967. (save-excursion
  9968. (setq rtn1 (funcall action))
  9969. (push rtn1 rtn)))
  9970. (t (error "Invalid action")))
  9971. ;; if we are to skip sublevels, jump to end of subtree
  9972. (unless org-tags-match-list-sublevels
  9973. (org-end-of-subtree t)
  9974. (backward-char 1))))
  9975. ;; Get the correct position from where to continue
  9976. (if org-map-continue-from
  9977. (goto-char org-map-continue-from)
  9978. (and (= (point) lspos) (end-of-line 1)))))
  9979. (when (and (eq action 'sparse-tree)
  9980. (not org-sparse-tree-open-archived-trees))
  9981. (org-hide-archived-subtrees (point-min) (point-max)))
  9982. (nreverse rtn)))
  9983. (defun org-remove-uniherited-tags (tags)
  9984. "Remove all tags that are not inherited from the list TAGS."
  9985. (cond
  9986. ((eq org-use-tag-inheritance t)
  9987. (if org-tags-exclude-from-inheritance
  9988. (org-delete-all org-tags-exclude-from-inheritance tags)
  9989. tags))
  9990. ((not org-use-tag-inheritance) nil)
  9991. ((stringp org-use-tag-inheritance)
  9992. (delq nil (mapcar
  9993. (lambda (x)
  9994. (if (and (string-match org-use-tag-inheritance x)
  9995. (not (member x org-tags-exclude-from-inheritance)))
  9996. x nil))
  9997. tags)))
  9998. ((listp org-use-tag-inheritance)
  9999. (delq nil (mapcar
  10000. (lambda (x)
  10001. (if (member x org-use-tag-inheritance) x nil))
  10002. tags)))))
  10003. (defvar todo-only) ;; dynamically scoped
  10004. (defun org-match-sparse-tree (&optional todo-only match)
  10005. "Create a sparse tree according to tags string MATCH.
  10006. MATCH can contain positive and negative selection of tags, like
  10007. \"+WORK+URGENT-WITHBOSS\".
  10008. If optional argument TODO-ONLY is non-nil, only select lines that are
  10009. also TODO lines."
  10010. (interactive "P")
  10011. (org-prepare-agenda-buffers (list (current-buffer)))
  10012. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10013. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10014. (defvar org-cached-props nil)
  10015. (defun org-cached-entry-get (pom property)
  10016. (if (or (eq t org-use-property-inheritance)
  10017. (and (stringp org-use-property-inheritance)
  10018. (string-match org-use-property-inheritance property))
  10019. (and (listp org-use-property-inheritance)
  10020. (member property org-use-property-inheritance)))
  10021. ;; Caching is not possible, check it directly
  10022. (org-entry-get pom property 'inherit)
  10023. ;; Get all properties, so that we can do complicated checks easily
  10024. (cdr (assoc property (or org-cached-props
  10025. (setq org-cached-props
  10026. (org-entry-properties pom)))))))
  10027. (defun org-global-tags-completion-table (&optional files)
  10028. "Return the list of all tags in all agenda buffer/files."
  10029. (save-excursion
  10030. (org-uniquify
  10031. (delq nil
  10032. (apply 'append
  10033. (mapcar
  10034. (lambda (file)
  10035. (set-buffer (find-file-noselect file))
  10036. (append (org-get-buffer-tags)
  10037. (mapcar (lambda (x) (if (stringp (car-safe x))
  10038. (list (car-safe x)) nil))
  10039. org-tag-alist)))
  10040. (if (and files (car files))
  10041. files
  10042. (org-agenda-files))))))))
  10043. (defun org-make-tags-matcher (match)
  10044. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10045. ;; todo-only is scoped dynamically into this function, and the function
  10046. ;; may change it if the matcher asks for it.
  10047. (unless match
  10048. ;; Get a new match request, with completion
  10049. (let ((org-last-tags-completion-table
  10050. (org-global-tags-completion-table)))
  10051. (setq match (org-completing-read-no-i
  10052. "Match: " 'org-tags-completion-function nil nil nil
  10053. 'org-tags-history))))
  10054. ;; Parse the string and create a lisp form
  10055. (let ((match0 match)
  10056. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10057. minus tag mm
  10058. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10059. orterms term orlist re-p str-p level-p level-op time-p
  10060. prop-p pn pv po cat-p gv rest)
  10061. (if (string-match "/+" match)
  10062. ;; match contains also a todo-matching request
  10063. (progn
  10064. (setq tagsmatch (substring match 0 (match-beginning 0))
  10065. todomatch (substring match (match-end 0)))
  10066. (if (string-match "^!" todomatch)
  10067. (setq todo-only t todomatch (substring todomatch 1)))
  10068. (if (string-match "^\\s-*$" todomatch)
  10069. (setq todomatch nil)))
  10070. ;; only matching tags
  10071. (setq tagsmatch match todomatch nil))
  10072. ;; Make the tags matcher
  10073. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10074. (setq tagsmatcher t)
  10075. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10076. (while (setq term (pop orterms))
  10077. (while (and (equal (substring term -1) "\\") orterms)
  10078. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10079. (while (string-match re term)
  10080. (setq rest (substring term (match-end 0))
  10081. minus (and (match-end 1)
  10082. (equal (match-string 1 term) "-"))
  10083. tag (match-string 2 term)
  10084. re-p (equal (string-to-char tag) ?{)
  10085. level-p (match-end 4)
  10086. prop-p (match-end 5)
  10087. mm (cond
  10088. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10089. (level-p
  10090. (setq level-op (org-op-to-function (match-string 3 term)))
  10091. `(,level-op level ,(string-to-number
  10092. (match-string 4 term))))
  10093. (prop-p
  10094. (setq pn (match-string 5 term)
  10095. po (match-string 6 term)
  10096. pv (match-string 7 term)
  10097. cat-p (equal pn "CATEGORY")
  10098. re-p (equal (string-to-char pv) ?{)
  10099. str-p (equal (string-to-char pv) ?\")
  10100. time-p (save-match-data
  10101. (string-match "^\"[[<].*[]>]\"$" pv))
  10102. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10103. (if time-p (setq pv (org-matcher-time pv)))
  10104. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10105. (cond
  10106. ((equal pn "CATEGORY")
  10107. (setq gv '(get-text-property (point) 'org-category)))
  10108. ((equal pn "TODO")
  10109. (setq gv 'todo))
  10110. (t
  10111. (setq gv `(org-cached-entry-get nil ,pn))))
  10112. (if re-p
  10113. (if (eq po 'org<>)
  10114. `(not (string-match ,pv (or ,gv "")))
  10115. `(string-match ,pv (or ,gv "")))
  10116. (if str-p
  10117. `(,po (or ,gv "") ,pv)
  10118. `(,po (string-to-number (or ,gv ""))
  10119. ,(string-to-number pv) ))))
  10120. (t `(member ,tag tags-list)))
  10121. mm (if minus (list 'not mm) mm)
  10122. term rest)
  10123. (push mm tagsmatcher))
  10124. (push (if (> (length tagsmatcher) 1)
  10125. (cons 'and tagsmatcher)
  10126. (car tagsmatcher))
  10127. orlist)
  10128. (setq tagsmatcher nil))
  10129. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10130. (setq tagsmatcher
  10131. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10132. ;; Make the todo matcher
  10133. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10134. (setq todomatcher t)
  10135. (setq orterms (org-split-string todomatch "|") orlist nil)
  10136. (while (setq term (pop orterms))
  10137. (while (string-match re term)
  10138. (setq minus (and (match-end 1)
  10139. (equal (match-string 1 term) "-"))
  10140. kwd (match-string 2 term)
  10141. re-p (equal (string-to-char kwd) ?{)
  10142. term (substring term (match-end 0))
  10143. mm (if re-p
  10144. `(string-match ,(substring kwd 1 -1) todo)
  10145. (list 'equal 'todo kwd))
  10146. mm (if minus (list 'not mm) mm))
  10147. (push mm todomatcher))
  10148. (push (if (> (length todomatcher) 1)
  10149. (cons 'and todomatcher)
  10150. (car todomatcher))
  10151. orlist)
  10152. (setq todomatcher nil))
  10153. (setq todomatcher (if (> (length orlist) 1)
  10154. (cons 'or orlist) (car orlist))))
  10155. ;; Return the string and lisp forms of the matcher
  10156. (setq matcher (if todomatcher
  10157. (list 'and tagsmatcher todomatcher)
  10158. tagsmatcher))
  10159. (cons match0 matcher)))
  10160. (defun org-op-to-function (op &optional stringp)
  10161. "Turn an operator into the appropriate function."
  10162. (setq op
  10163. (cond
  10164. ((equal op "<" ) '(< string< org-time<))
  10165. ((equal op ">" ) '(> org-string> org-time>))
  10166. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10167. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10168. ((member op '("=" "==")) '(= string= org-time=))
  10169. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10170. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10171. (defun org<> (a b) (not (= a b)))
  10172. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10173. (defun org-string>= (a b) (not (string< a b)))
  10174. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10175. (defun org-string<> (a b) (not (string= a b)))
  10176. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10177. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10178. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10179. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10180. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10181. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10182. (defun org-2ft (s)
  10183. "Convert S to a floating point time.
  10184. If S is already a number, just return it. If it is a string, parse
  10185. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10186. (cond
  10187. ((numberp s) s)
  10188. ((stringp s)
  10189. (condition-case nil
  10190. (float-time (apply 'encode-time (org-parse-time-string s)))
  10191. (error 0.)))
  10192. (t 0.)))
  10193. (defun org-time-today ()
  10194. "Time in seconds today at 0:00.
  10195. Returns the float number of seconds since the beginning of the
  10196. epoch to the beginning of today (00:00)."
  10197. (float-time (apply 'encode-time
  10198. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10199. (defun org-matcher-time (s)
  10200. "Interpret a time comparison value."
  10201. (save-match-data
  10202. (cond
  10203. ((string= s "<now>") (float-time))
  10204. ((string= s "<today>") (org-time-today))
  10205. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10206. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10207. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10208. (+ (org-time-today)
  10209. (* (string-to-number (match-string 1 s))
  10210. (cdr (assoc (match-string 2 s)
  10211. '(("d" . 86400.0) ("w" . 604800.0)
  10212. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10213. (t (org-2ft s)))))
  10214. (defun org-match-any-p (re list)
  10215. "Does re match any element of list?"
  10216. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10217. (delq nil list))
  10218. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10219. (defvar org-tags-overlay (org-make-overlay 1 1))
  10220. (org-detach-overlay org-tags-overlay)
  10221. (defun org-get-local-tags-at (&optional pos)
  10222. "Get a list of tags defined in the current headline."
  10223. (org-get-tags-at pos 'local))
  10224. (defun org-get-local-tags ()
  10225. "Get a list of tags defined in the current headline."
  10226. (org-get-tags-at nil 'local))
  10227. (defun org-get-tags-at (&optional pos local)
  10228. "Get a list of all headline tags applicable at POS.
  10229. POS defaults to point. If tags are inherited, the list contains
  10230. the targets in the same sequence as the headlines appear, i.e.
  10231. the tags of the current headline come last.
  10232. When LOCAL is non-nil, only return tags from the current headline,
  10233. ignore inherited ones."
  10234. (interactive)
  10235. (if (and org-trust-scanner-tags
  10236. (or (not pos) (equal pos (point)))
  10237. (not local))
  10238. org-scanner-tags
  10239. (let (tags ltags lastpos parent)
  10240. (save-excursion
  10241. (save-restriction
  10242. (widen)
  10243. (goto-char (or pos (point)))
  10244. (save-match-data
  10245. (catch 'done
  10246. (condition-case nil
  10247. (progn
  10248. (org-back-to-heading t)
  10249. (while (not (equal lastpos (point)))
  10250. (setq lastpos (point))
  10251. (when (looking-at
  10252. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10253. (setq ltags (org-split-string
  10254. (org-match-string-no-properties 1) ":"))
  10255. (when parent
  10256. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10257. (setq tags (append
  10258. (if parent
  10259. (org-remove-uniherited-tags ltags)
  10260. ltags)
  10261. tags)))
  10262. (or org-use-tag-inheritance (throw 'done t))
  10263. (if local (throw 'done t))
  10264. (or (org-up-heading-safe) (error nil))
  10265. (setq parent t)))
  10266. (error nil)))))
  10267. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10268. (defun org-add-prop-inherited (s)
  10269. (add-text-properties 0 (length s) '(inherited t) s)
  10270. s)
  10271. (defun org-toggle-tag (tag &optional onoff)
  10272. "Toggle the tag TAG for the current line.
  10273. If ONOFF is `on' or `off', don't toggle but set to this state."
  10274. (let (res current)
  10275. (save-excursion
  10276. (org-back-to-heading t)
  10277. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10278. (point-at-eol) t)
  10279. (progn
  10280. (setq current (match-string 1))
  10281. (replace-match ""))
  10282. (setq current ""))
  10283. (setq current (nreverse (org-split-string current ":")))
  10284. (cond
  10285. ((eq onoff 'on)
  10286. (setq res t)
  10287. (or (member tag current) (push tag current)))
  10288. ((eq onoff 'off)
  10289. (or (not (member tag current)) (setq current (delete tag current))))
  10290. (t (if (member tag current)
  10291. (setq current (delete tag current))
  10292. (setq res t)
  10293. (push tag current))))
  10294. (end-of-line 1)
  10295. (if current
  10296. (progn
  10297. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10298. (org-set-tags nil t))
  10299. (delete-horizontal-space))
  10300. (run-hooks 'org-after-tags-change-hook))
  10301. res))
  10302. (defun org-align-tags-here (to-col)
  10303. ;; Assumes that this is a headline
  10304. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10305. (beginning-of-line 1)
  10306. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10307. (< pos (match-beginning 2)))
  10308. (progn
  10309. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10310. (goto-char (match-beginning 1))
  10311. (insert " ")
  10312. (delete-region (point) (1+ (match-beginning 2)))
  10313. (setq ncol (max (1+ (current-column))
  10314. (1+ col)
  10315. (if (> to-col 0)
  10316. to-col
  10317. (- (abs to-col) tags-l))))
  10318. (setq p (point))
  10319. (insert (make-string (- ncol (current-column)) ?\ ))
  10320. (setq ncol (current-column))
  10321. (when indent-tabs-mode (tabify p (point-at-eol)))
  10322. (org-move-to-column (min ncol col) t))
  10323. (goto-char pos))))
  10324. (defun org-set-tags-command (&optional arg just-align)
  10325. "Call the set-tags command for the current entry."
  10326. (interactive "P")
  10327. (if (org-on-heading-p)
  10328. (org-set-tags arg just-align)
  10329. (save-excursion
  10330. (org-back-to-heading t)
  10331. (org-set-tags arg just-align))))
  10332. (defun org-set-tags-to (data)
  10333. "Set the tags of the current entry to DATA, replacing the current tags.
  10334. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10335. If DATA is nil or the empty string, any tags will be removed."
  10336. (interactive "sTags: ")
  10337. (setq data
  10338. (cond
  10339. ((eq data nil) "")
  10340. ((equal data "") "")
  10341. ((stringp data)
  10342. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10343. ":"))
  10344. ((listp data)
  10345. (concat ":" (mapconcat 'identity data ":") ":"))
  10346. (t nil)))
  10347. (when data
  10348. (save-excursion
  10349. (org-back-to-heading t)
  10350. (when (looking-at org-complex-heading-regexp)
  10351. (if (match-end 5)
  10352. (progn
  10353. (goto-char (match-beginning 5))
  10354. (insert data)
  10355. (delete-region (point) (point-at-eol))
  10356. (org-set-tags nil 'align))
  10357. (goto-char (point-at-eol))
  10358. (insert " " data)
  10359. (org-set-tags nil 'align)))
  10360. (beginning-of-line 1)
  10361. (if (looking-at ".*?\\([ \t]+\\)$")
  10362. (delete-region (match-beginning 1) (match-end 1))))))
  10363. (defun org-set-tags (&optional arg just-align)
  10364. "Set the tags for the current headline.
  10365. With prefix ARG, realign all tags in headings in the current buffer."
  10366. (interactive "P")
  10367. (let* ((re (concat "^" outline-regexp))
  10368. (current (org-get-tags-string))
  10369. (col (current-column))
  10370. (org-setting-tags t)
  10371. table current-tags inherited-tags ; computed below when needed
  10372. tags p0 c0 c1 rpl)
  10373. (if arg
  10374. (save-excursion
  10375. (goto-char (point-min))
  10376. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10377. (while (re-search-forward re nil t)
  10378. (org-set-tags nil t)
  10379. (end-of-line 1)))
  10380. (message "All tags realigned to column %d" org-tags-column))
  10381. (if just-align
  10382. (setq tags current)
  10383. ;; Get a new set of tags from the user
  10384. (save-excursion
  10385. (setq table (append org-tag-persistent-alist
  10386. (or org-tag-alist (org-get-buffer-tags)))
  10387. org-last-tags-completion-table table
  10388. current-tags (org-split-string current ":")
  10389. inherited-tags (nreverse
  10390. (nthcdr (length current-tags)
  10391. (nreverse (org-get-tags-at))))
  10392. tags
  10393. (if (or (eq t org-use-fast-tag-selection)
  10394. (and org-use-fast-tag-selection
  10395. (delq nil (mapcar 'cdr table))))
  10396. (org-fast-tag-selection
  10397. current-tags inherited-tags table
  10398. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10399. (let ((org-add-colon-after-tag-completion t))
  10400. (org-trim
  10401. (org-without-partial-completion
  10402. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10403. nil nil current 'org-tags-history)))))))
  10404. (while (string-match "[-+&]+" tags)
  10405. ;; No boolean logic, just a list
  10406. (setq tags (replace-match ":" t t tags))))
  10407. (if org-tags-sort-function
  10408. (setq tags (mapconcat 'identity
  10409. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10410. org-tags-sort-function) ":")))
  10411. (if (string-match "\\`[\t ]*\\'" tags)
  10412. (setq tags "")
  10413. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10414. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10415. ;; Insert new tags at the correct column
  10416. (beginning-of-line 1)
  10417. (cond
  10418. ((and (equal current "") (equal tags "")))
  10419. ((re-search-forward
  10420. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10421. (point-at-eol) t)
  10422. (if (equal tags "")
  10423. (setq rpl "")
  10424. (goto-char (match-beginning 0))
  10425. (setq c0 (current-column) p0 (point)
  10426. c1 (max (1+ c0) (if (> org-tags-column 0)
  10427. org-tags-column
  10428. (- (- org-tags-column) (length tags))))
  10429. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10430. (replace-match rpl t t)
  10431. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10432. tags)
  10433. (t (error "Tags alignment failed")))
  10434. (org-move-to-column col)
  10435. (unless just-align
  10436. (run-hooks 'org-after-tags-change-hook)))))
  10437. (defun org-change-tag-in-region (beg end tag off)
  10438. "Add or remove TAG for each entry in the region.
  10439. This works in the agenda, and also in an org-mode buffer."
  10440. (interactive
  10441. (list (region-beginning) (region-end)
  10442. (let ((org-last-tags-completion-table
  10443. (if (org-mode-p)
  10444. (org-get-buffer-tags)
  10445. (org-global-tags-completion-table))))
  10446. (org-icompleting-read
  10447. "Tag: " 'org-tags-completion-function nil nil nil
  10448. 'org-tags-history))
  10449. (progn
  10450. (message "[s]et or [r]emove? ")
  10451. (equal (read-char-exclusive) ?r))))
  10452. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10453. (let ((agendap (equal major-mode 'org-agenda-mode))
  10454. l1 l2 m buf pos newhead (cnt 0))
  10455. (goto-char end)
  10456. (setq l2 (1- (org-current-line)))
  10457. (goto-char beg)
  10458. (setq l1 (org-current-line))
  10459. (loop for l from l1 to l2 do
  10460. (org-goto-line l)
  10461. (setq m (get-text-property (point) 'org-hd-marker))
  10462. (when (or (and (org-mode-p) (org-on-heading-p))
  10463. (and agendap m))
  10464. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10465. pos (if agendap m (point)))
  10466. (with-current-buffer buf
  10467. (save-excursion
  10468. (save-restriction
  10469. (goto-char pos)
  10470. (setq cnt (1+ cnt))
  10471. (org-toggle-tag tag (if off 'off 'on))
  10472. (setq newhead (org-get-heading)))))
  10473. (and agendap (org-agenda-change-all-lines newhead m))))
  10474. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10475. (defun org-tags-completion-function (string predicate &optional flag)
  10476. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10477. (confirm (lambda (x) (stringp (car x)))))
  10478. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10479. (setq s1 (match-string 1 string)
  10480. s2 (match-string 2 string))
  10481. (setq s1 "" s2 string))
  10482. (cond
  10483. ((eq flag nil)
  10484. ;; try completion
  10485. (setq rtn (try-completion s2 ctable confirm))
  10486. (if (stringp rtn)
  10487. (setq rtn
  10488. (concat s1 s2 (substring rtn (length s2))
  10489. (if (and org-add-colon-after-tag-completion
  10490. (assoc rtn ctable))
  10491. ":" ""))))
  10492. rtn)
  10493. ((eq flag t)
  10494. ;; all-completions
  10495. (all-completions s2 ctable confirm)
  10496. )
  10497. ((eq flag 'lambda)
  10498. ;; exact match?
  10499. (assoc s2 ctable)))
  10500. ))
  10501. (defun org-fast-tag-insert (kwd tags face &optional end)
  10502. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10503. (insert (format "%-12s" (concat kwd ":"))
  10504. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10505. (or end "")))
  10506. (defun org-fast-tag-show-exit (flag)
  10507. (save-excursion
  10508. (org-goto-line 3)
  10509. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10510. (replace-match ""))
  10511. (when flag
  10512. (end-of-line 1)
  10513. (org-move-to-column (- (window-width) 19) t)
  10514. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10515. (defun org-set-current-tags-overlay (current prefix)
  10516. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10517. (if (featurep 'xemacs)
  10518. (org-overlay-display org-tags-overlay (concat prefix s)
  10519. 'secondary-selection)
  10520. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10521. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10522. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10523. "Fast tag selection with single keys.
  10524. CURRENT is the current list of tags in the headline, INHERITED is the
  10525. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10526. possibly with grouping information. TODO-TABLE is a similar table with
  10527. TODO keywords, should these have keys assigned to them.
  10528. If the keys are nil, a-z are automatically assigned.
  10529. Returns the new tags string, or nil to not change the current settings."
  10530. (let* ((fulltable (append table todo-table))
  10531. (maxlen (apply 'max (mapcar
  10532. (lambda (x)
  10533. (if (stringp (car x)) (string-width (car x)) 0))
  10534. fulltable)))
  10535. (buf (current-buffer))
  10536. (expert (eq org-fast-tag-selection-single-key 'expert))
  10537. (buffer-tags nil)
  10538. (fwidth (+ maxlen 3 1 3))
  10539. (ncol (/ (- (window-width) 4) fwidth))
  10540. (i-face 'org-done)
  10541. (c-face 'org-todo)
  10542. tg cnt e c char c1 c2 ntable tbl rtn
  10543. ov-start ov-end ov-prefix
  10544. (exit-after-next org-fast-tag-selection-single-key)
  10545. (done-keywords org-done-keywords)
  10546. groups ingroup)
  10547. (save-excursion
  10548. (beginning-of-line 1)
  10549. (if (looking-at
  10550. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10551. (setq ov-start (match-beginning 1)
  10552. ov-end (match-end 1)
  10553. ov-prefix "")
  10554. (setq ov-start (1- (point-at-eol))
  10555. ov-end (1+ ov-start))
  10556. (skip-chars-forward "^\n\r")
  10557. (setq ov-prefix
  10558. (concat
  10559. (buffer-substring (1- (point)) (point))
  10560. (if (> (current-column) org-tags-column)
  10561. " "
  10562. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10563. (org-move-overlay org-tags-overlay ov-start ov-end)
  10564. (save-window-excursion
  10565. (if expert
  10566. (set-buffer (get-buffer-create " *Org tags*"))
  10567. (delete-other-windows)
  10568. (split-window-vertically)
  10569. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10570. (erase-buffer)
  10571. (org-set-local 'org-done-keywords done-keywords)
  10572. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10573. (org-fast-tag-insert "Current" current c-face "\n\n")
  10574. (org-fast-tag-show-exit exit-after-next)
  10575. (org-set-current-tags-overlay current ov-prefix)
  10576. (setq tbl fulltable char ?a cnt 0)
  10577. (while (setq e (pop tbl))
  10578. (cond
  10579. ((equal (car e) :startgroup)
  10580. (push '() groups) (setq ingroup t)
  10581. (when (not (= cnt 0))
  10582. (setq cnt 0)
  10583. (insert "\n"))
  10584. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  10585. ((equal (car e) :endgroup)
  10586. (setq ingroup nil cnt 0)
  10587. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  10588. ((equal e '(:newline))
  10589. (when (not (= cnt 0))
  10590. (setq cnt 0)
  10591. (insert "\n")
  10592. (setq e (car tbl))
  10593. (while (equal (car tbl) '(:newline))
  10594. (insert "\n")
  10595. (setq tbl (cdr tbl)))))
  10596. (t
  10597. (setq tg (copy-sequence (car e)) c2 nil)
  10598. (if (cdr e)
  10599. (setq c (cdr e))
  10600. ;; automatically assign a character.
  10601. (setq c1 (string-to-char
  10602. (downcase (substring
  10603. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10604. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10605. (while (or (rassoc char ntable) (rassoc char table))
  10606. (setq char (1+ char)))
  10607. (setq c2 c1))
  10608. (setq c (or c2 char)))
  10609. (if ingroup (push tg (car groups)))
  10610. (setq tg (org-add-props tg nil 'face
  10611. (cond
  10612. ((not (assoc tg table))
  10613. (org-get-todo-face tg))
  10614. ((member tg current) c-face)
  10615. ((member tg inherited) i-face)
  10616. (t nil))))
  10617. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10618. (insert "[" c "] " tg (make-string
  10619. (- fwidth 4 (length tg)) ?\ ))
  10620. (push (cons tg c) ntable)
  10621. (when (= (setq cnt (1+ cnt)) ncol)
  10622. (insert "\n")
  10623. (if ingroup (insert " "))
  10624. (setq cnt 0)))))
  10625. (setq ntable (nreverse ntable))
  10626. (insert "\n")
  10627. (goto-char (point-min))
  10628. (if (not expert) (org-fit-window-to-buffer))
  10629. (setq rtn
  10630. (catch 'exit
  10631. (while t
  10632. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  10633. (if (not groups) "no " "")
  10634. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10635. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10636. (cond
  10637. ((= c ?\r) (throw 'exit t))
  10638. ((= c ?!)
  10639. (setq groups (not groups))
  10640. (goto-char (point-min))
  10641. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10642. ((= c ?\C-c)
  10643. (if (not expert)
  10644. (org-fast-tag-show-exit
  10645. (setq exit-after-next (not exit-after-next)))
  10646. (setq expert nil)
  10647. (delete-other-windows)
  10648. (split-window-vertically)
  10649. (org-switch-to-buffer-other-window " *Org tags*")
  10650. (org-fit-window-to-buffer)))
  10651. ((or (= c ?\C-g)
  10652. (and (= c ?q) (not (rassoc c ntable))))
  10653. (org-detach-overlay org-tags-overlay)
  10654. (setq quit-flag t))
  10655. ((= c ?\ )
  10656. (setq current nil)
  10657. (if exit-after-next (setq exit-after-next 'now)))
  10658. ((= c ?\t)
  10659. (condition-case nil
  10660. (setq tg (org-icompleting-read
  10661. "Tag: "
  10662. (or buffer-tags
  10663. (with-current-buffer buf
  10664. (org-get-buffer-tags)))))
  10665. (quit (setq tg "")))
  10666. (when (string-match "\\S-" tg)
  10667. (add-to-list 'buffer-tags (list tg))
  10668. (if (member tg current)
  10669. (setq current (delete tg current))
  10670. (push tg current)))
  10671. (if exit-after-next (setq exit-after-next 'now)))
  10672. ((setq e (rassoc c todo-table) tg (car e))
  10673. (with-current-buffer buf
  10674. (save-excursion (org-todo tg)))
  10675. (if exit-after-next (setq exit-after-next 'now)))
  10676. ((setq e (rassoc c ntable) tg (car e))
  10677. (if (member tg current)
  10678. (setq current (delete tg current))
  10679. (loop for g in groups do
  10680. (if (member tg g)
  10681. (mapc (lambda (x)
  10682. (setq current (delete x current)))
  10683. g)))
  10684. (push tg current))
  10685. (if exit-after-next (setq exit-after-next 'now))))
  10686. ;; Create a sorted list
  10687. (setq current
  10688. (sort current
  10689. (lambda (a b)
  10690. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10691. (if (eq exit-after-next 'now) (throw 'exit t))
  10692. (goto-char (point-min))
  10693. (beginning-of-line 2)
  10694. (delete-region (point) (point-at-eol))
  10695. (org-fast-tag-insert "Current" current c-face)
  10696. (org-set-current-tags-overlay current ov-prefix)
  10697. (while (re-search-forward
  10698. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10699. (setq tg (match-string 1))
  10700. (add-text-properties
  10701. (match-beginning 1) (match-end 1)
  10702. (list 'face
  10703. (cond
  10704. ((member tg current) c-face)
  10705. ((member tg inherited) i-face)
  10706. (t (get-text-property (match-beginning 1) 'face))))))
  10707. (goto-char (point-min)))))
  10708. (org-detach-overlay org-tags-overlay)
  10709. (if rtn
  10710. (mapconcat 'identity current ":")
  10711. nil))))
  10712. (defun org-get-tags-string ()
  10713. "Get the TAGS string in the current headline."
  10714. (unless (org-on-heading-p t)
  10715. (error "Not on a heading"))
  10716. (save-excursion
  10717. (beginning-of-line 1)
  10718. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10719. (org-match-string-no-properties 1)
  10720. "")))
  10721. (defun org-get-tags ()
  10722. "Get the list of tags specified in the current headline."
  10723. (org-split-string (org-get-tags-string) ":"))
  10724. (defun org-get-buffer-tags ()
  10725. "Get a table of all tags used in the buffer, for completion."
  10726. (let (tags)
  10727. (save-excursion
  10728. (goto-char (point-min))
  10729. (while (re-search-forward
  10730. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10731. (when (equal (char-after (point-at-bol 0)) ?*)
  10732. (mapc (lambda (x) (add-to-list 'tags x))
  10733. (org-split-string (org-match-string-no-properties 1) ":")))))
  10734. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  10735. (mapcar 'list tags)))
  10736. ;;;; The mapping API
  10737. ;;;###autoload
  10738. (defun org-map-entries (func &optional match scope &rest skip)
  10739. "Call FUNC at each headline selected by MATCH in SCOPE.
  10740. FUNC is a function or a lisp form. The function will be called without
  10741. arguments, with the cursor positioned at the beginning of the headline.
  10742. The return values of all calls to the function will be collected and
  10743. returned as a list.
  10744. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10745. does not need to preserve point. After evaluation, the cursor will be
  10746. moved to the end of the line (presumably of the headline of the
  10747. processed entry) and search continues from there. Under some
  10748. circumstances, this may not produce the wanted results. For example,
  10749. if you have removed (e.g. archived) the current (sub)tree it could
  10750. mean that the next entry will be skipped entirely. In such cases, you
  10751. can specify the position from where search should continue by making
  10752. FUNC set the variable `org-map-continue-from' to the desired buffer
  10753. position.
  10754. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10755. Only headlines that are matched by this query will be considered during
  10756. the iteration. When MATCH is nil or t, all headlines will be
  10757. visited by the iteration.
  10758. SCOPE determines the scope of this command. It can be any of:
  10759. nil The current buffer, respecting the restriction if any
  10760. tree The subtree started with the entry at point
  10761. file The current buffer, without restriction
  10762. file-with-archives
  10763. The current buffer, and any archives associated with it
  10764. agenda All agenda files
  10765. agenda-with-archives
  10766. All agenda files with any archive files associated with them
  10767. \(file1 file2 ...)
  10768. If this is a list, all files in the list will be scanned
  10769. The remaining args are treated as settings for the skipping facilities of
  10770. the scanner. The following items can be given here:
  10771. archive skip trees with the archive tag.
  10772. comment skip trees with the COMMENT keyword
  10773. function or Emacs Lisp form:
  10774. will be used as value for `org-agenda-skip-function', so whenever
  10775. the function returns t, FUNC will not be called for that
  10776. entry and search will continue from the point where the
  10777. function leaves it.
  10778. If your function needs to retrieve the tags including inherited tags
  10779. at the *current* entry, you can use the value of the variable
  10780. `org-scanner-tags' which will be much faster than getting the value
  10781. with `org-get-tags-at'. If your function gets properties with
  10782. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10783. to t around the call to `org-entry-properties' to get the same speedup.
  10784. Note that if your function moves around to retrieve tags and properties at
  10785. a *different* entry, you cannot use these techniques."
  10786. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10787. (org-agenda-skip-archived-trees (memq 'archive skip))
  10788. (org-agenda-skip-comment-trees (memq 'comment skip))
  10789. (org-agenda-skip-function
  10790. (car (org-delete-all '(comment archive) skip)))
  10791. (org-tags-match-list-sublevels t)
  10792. matcher file res
  10793. org-todo-keywords-for-agenda
  10794. org-done-keywords-for-agenda
  10795. org-todo-keyword-alist-for-agenda
  10796. org-drawers-for-agenda
  10797. org-tag-alist-for-agenda)
  10798. (cond
  10799. ((eq match t) (setq matcher t))
  10800. ((eq match nil) (setq matcher t))
  10801. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10802. (save-excursion
  10803. (save-restriction
  10804. (when (eq scope 'tree)
  10805. (org-back-to-heading t)
  10806. (org-narrow-to-subtree)
  10807. (setq scope nil))
  10808. (if (not scope)
  10809. (progn
  10810. (org-prepare-agenda-buffers
  10811. (list (buffer-file-name (current-buffer))))
  10812. (setq res (org-scan-tags func matcher)))
  10813. ;; Get the right scope
  10814. (cond
  10815. ((and scope (listp scope) (symbolp (car scope)))
  10816. (setq scope (eval scope)))
  10817. ((eq scope 'agenda)
  10818. (setq scope (org-agenda-files t)))
  10819. ((eq scope 'agenda-with-archives)
  10820. (setq scope (org-agenda-files t))
  10821. (setq scope (org-add-archive-files scope)))
  10822. ((eq scope 'file)
  10823. (setq scope (list (buffer-file-name))))
  10824. ((eq scope 'file-with-archives)
  10825. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10826. (org-prepare-agenda-buffers scope)
  10827. (while (setq file (pop scope))
  10828. (with-current-buffer (org-find-base-buffer-visiting file)
  10829. (save-excursion
  10830. (save-restriction
  10831. (widen)
  10832. (goto-char (point-min))
  10833. (setq res (append res (org-scan-tags func matcher))))))))))
  10834. res))
  10835. ;;;; Properties
  10836. ;;; Setting and retrieving properties
  10837. (defconst org-special-properties
  10838. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10839. "TIMESTAMP" "TIMESTAMP_IA")
  10840. "The special properties valid in Org-mode.
  10841. These are properties that are not defined in the property drawer,
  10842. but in some other way.")
  10843. (defconst org-default-properties
  10844. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10845. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10846. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10847. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10848. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10849. "CLOCK_MODELINE_TOTAL" "STYLE")
  10850. "Some properties that are used by Org-mode for various purposes.
  10851. Being in this list makes sure that they are offered for completion.")
  10852. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10853. "Regular expression matching the first line of a property drawer.")
  10854. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10855. "Regular expression matching the first line of a property drawer.")
  10856. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10857. "Regular expression matching the first line of a property drawer.")
  10858. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10859. "Regular expression matching the first line of a property drawer.")
  10860. (defconst org-property-drawer-re
  10861. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10862. org-property-end-re "\\)\n?")
  10863. "Matches an entire property drawer.")
  10864. (defconst org-clock-drawer-re
  10865. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10866. org-property-end-re "\\)\n?")
  10867. "Matches an entire clock drawer.")
  10868. (defun org-property-action ()
  10869. "Do an action on properties."
  10870. (interactive)
  10871. (let (c)
  10872. (org-at-property-p)
  10873. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10874. (setq c (read-char-exclusive))
  10875. (cond
  10876. ((equal c ?s)
  10877. (call-interactively 'org-set-property))
  10878. ((equal c ?d)
  10879. (call-interactively 'org-delete-property))
  10880. ((equal c ?D)
  10881. (call-interactively 'org-delete-property-globally))
  10882. ((equal c ?c)
  10883. (call-interactively 'org-compute-property-at-point))
  10884. (t (error "No such property action %c" c)))))
  10885. (defun org-set-effort (&optional value)
  10886. "Set the effort property of the current entry.
  10887. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  10888. allowed value."
  10889. (interactive "P")
  10890. (if (equal value 0) (setq value 10))
  10891. (let* ((completion-ignore-case t)
  10892. (prop org-effort-property)
  10893. (cur (org-entry-get nil prop))
  10894. (allowed (org-property-get-allowed-values nil prop 'table))
  10895. (existing (mapcar 'list (org-property-values prop)))
  10896. (val (cond
  10897. ((stringp value) value)
  10898. ((and allowed (integerp value))
  10899. (or (car (nth (1- value) allowed))
  10900. (car (org-last allowed))))
  10901. (allowed
  10902. (org-completing-read "Value: " allowed nil 'req-match))
  10903. (t
  10904. (let (org-completion-use-ido org-completion-use-iswitchb)
  10905. (org-completing-read
  10906. (concat "Value " (if (and cur (string-match "\\S-" cur))
  10907. (concat "[" cur "]") "")
  10908. ": ")
  10909. existing nil nil "" nil cur))))))
  10910. (unless (equal (org-entry-get nil prop) val)
  10911. (org-entry-put nil prop val))
  10912. (message "%s is now %s" prop val)))
  10913. (defun org-at-property-p ()
  10914. "Is the cursor in a property line?"
  10915. ;; FIXME: Does not check if we are actually in the drawer.
  10916. ;; FIXME: also returns true on any drawers.....
  10917. ;; This is used by C-c C-c for property action.
  10918. (save-excursion
  10919. (beginning-of-line 1)
  10920. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10921. (defun org-get-property-block (&optional beg end force)
  10922. "Return the (beg . end) range of the body of the property drawer.
  10923. BEG and END can be beginning and end of subtree, if not given
  10924. they will be found.
  10925. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10926. (catch 'exit
  10927. (save-excursion
  10928. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10929. (end (or end (progn (outline-next-heading) (point)))))
  10930. (goto-char beg)
  10931. (if (re-search-forward org-property-start-re end t)
  10932. (setq beg (1+ (match-end 0)))
  10933. (if force
  10934. (save-excursion
  10935. (org-insert-property-drawer)
  10936. (setq end (progn (outline-next-heading) (point))))
  10937. (throw 'exit nil))
  10938. (goto-char beg)
  10939. (if (re-search-forward org-property-start-re end t)
  10940. (setq beg (1+ (match-end 0)))))
  10941. (if (re-search-forward org-property-end-re end t)
  10942. (setq end (match-beginning 0))
  10943. (or force (throw 'exit nil))
  10944. (goto-char beg)
  10945. (setq end beg)
  10946. (org-indent-line-function)
  10947. (insert ":END:\n"))
  10948. (cons beg end)))))
  10949. (defun org-entry-properties (&optional pom which)
  10950. "Get all properties of the entry at point-or-marker POM.
  10951. This includes the TODO keyword, the tags, time strings for deadline,
  10952. scheduled, and clocking, and any additional properties defined in the
  10953. entry. The return value is an alist, keys may occur multiple times
  10954. if the property key was used several times.
  10955. POM may also be nil, in which case the current entry is used.
  10956. If WHICH is nil or `all', get all properties. If WHICH is
  10957. `special' or `standard', only get that subclass."
  10958. (setq which (or which 'all))
  10959. (org-with-point-at pom
  10960. (let ((clockstr (substring org-clock-string 0 -1))
  10961. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10962. beg end range props sum-props key value string clocksum)
  10963. (save-excursion
  10964. (when (condition-case nil
  10965. (and (org-mode-p) (org-back-to-heading t))
  10966. (error nil))
  10967. (setq beg (point))
  10968. (setq sum-props (get-text-property (point) 'org-summaries))
  10969. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10970. (outline-next-heading)
  10971. (setq end (point))
  10972. (when (memq which '(all special))
  10973. ;; Get the special properties, like TODO and tags
  10974. (goto-char beg)
  10975. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10976. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10977. (when (looking-at org-priority-regexp)
  10978. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10979. (when (and (setq value (org-get-tags-string))
  10980. (string-match "\\S-" value))
  10981. (push (cons "TAGS" value) props))
  10982. (when (setq value (org-get-tags-at))
  10983. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10984. props))
  10985. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10986. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10987. string (if (equal key clockstr)
  10988. (org-no-properties
  10989. (org-trim
  10990. (buffer-substring
  10991. (match-beginning 3) (goto-char (point-at-eol)))))
  10992. (substring (org-match-string-no-properties 3) 1 -1)))
  10993. (unless key
  10994. (if (= (char-after (match-beginning 3)) ?\[)
  10995. (setq key "TIMESTAMP_IA")
  10996. (setq key "TIMESTAMP")))
  10997. (when (or (equal key clockstr) (not (assoc key props)))
  10998. (push (cons key string) props)))
  10999. )
  11000. (when (memq which '(all standard))
  11001. ;; Get the standard properties, like :PROP: ...
  11002. (setq range (org-get-property-block beg end))
  11003. (when range
  11004. (goto-char (car range))
  11005. (while (re-search-forward
  11006. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11007. (cdr range) t)
  11008. (setq key (org-match-string-no-properties 1)
  11009. value (org-trim (or (org-match-string-no-properties 2) "")))
  11010. (unless (member key excluded)
  11011. (push (cons key (or value "")) props)))))
  11012. (if clocksum
  11013. (push (cons "CLOCKSUM"
  11014. (org-columns-number-to-string (/ (float clocksum) 60.)
  11015. 'add_times))
  11016. props))
  11017. (unless (assoc "CATEGORY" props)
  11018. (setq value (or (org-get-category)
  11019. (progn (org-refresh-category-properties)
  11020. (org-get-category))))
  11021. (push (cons "CATEGORY" value) props))
  11022. (append sum-props (nreverse props)))))))
  11023. (defun org-entry-get (pom property &optional inherit)
  11024. "Get value of PROPERTY for entry at point-or-marker POM.
  11025. If INHERIT is non-nil and the entry does not have the property,
  11026. then also check higher levels of the hierarchy.
  11027. If INHERIT is the symbol `selective', use inheritance only if the setting
  11028. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11029. If the property is present but empty, the return value is the empty string.
  11030. If the property is not present at all, nil is returned."
  11031. (org-with-point-at pom
  11032. (if (and inherit (if (eq inherit 'selective)
  11033. (org-property-inherit-p property)
  11034. t))
  11035. (org-entry-get-with-inheritance property)
  11036. (if (member property org-special-properties)
  11037. ;; We need a special property. Use brute force, get all properties.
  11038. (cdr (assoc property (org-entry-properties nil 'special)))
  11039. (let ((range (org-get-property-block)))
  11040. (if (and range
  11041. (goto-char (car range))
  11042. (re-search-forward
  11043. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11044. (cdr range) t))
  11045. ;; Found the property, return it.
  11046. (if (match-end 1)
  11047. (org-match-string-no-properties 1)
  11048. "")))))))
  11049. (defun org-property-or-variable-value (var &optional inherit)
  11050. "Check if there is a property fixing the value of VAR.
  11051. If yes, return this value. If not, return the current value of the variable."
  11052. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11053. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11054. (read prop)
  11055. (symbol-value var))))
  11056. (defun org-entry-delete (pom property)
  11057. "Delete the property PROPERTY from entry at point-or-marker POM."
  11058. (org-with-point-at pom
  11059. (if (member property org-special-properties)
  11060. nil ; cannot delete these properties.
  11061. (let ((range (org-get-property-block)))
  11062. (if (and range
  11063. (goto-char (car range))
  11064. (re-search-forward
  11065. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11066. (cdr range) t))
  11067. (progn
  11068. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11069. t)
  11070. nil)))))
  11071. ;; Multi-values properties are properties that contain multiple values
  11072. ;; These values are assumed to be single words, separated by whitespace.
  11073. (defun org-entry-add-to-multivalued-property (pom property value)
  11074. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11075. (let* ((old (org-entry-get pom property))
  11076. (values (and old (org-split-string old "[ \t]"))))
  11077. (setq value (org-entry-protect-space value))
  11078. (unless (member value values)
  11079. (setq values (cons value values))
  11080. (org-entry-put pom property
  11081. (mapconcat 'identity values " ")))))
  11082. (defun org-entry-remove-from-multivalued-property (pom property value)
  11083. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11084. (let* ((old (org-entry-get pom property))
  11085. (values (and old (org-split-string old "[ \t]"))))
  11086. (setq value (org-entry-protect-space value))
  11087. (when (member value values)
  11088. (setq values (delete value values))
  11089. (org-entry-put pom property
  11090. (mapconcat 'identity values " ")))))
  11091. (defun org-entry-member-in-multivalued-property (pom property value)
  11092. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11093. (let* ((old (org-entry-get pom property))
  11094. (values (and old (org-split-string old "[ \t]"))))
  11095. (setq value (org-entry-protect-space value))
  11096. (member value values)))
  11097. (defun org-entry-get-multivalued-property (pom property)
  11098. "Return a list of values in a multivalued property."
  11099. (let* ((value (org-entry-get pom property))
  11100. (values (and value (org-split-string value "[ \t]"))))
  11101. (mapcar 'org-entry-restore-space values)))
  11102. (defun org-entry-put-multivalued-property (pom property &rest values)
  11103. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11104. VALUES should be a list of strings. Spaces will be protected."
  11105. (org-entry-put pom property
  11106. (mapconcat 'org-entry-protect-space values " "))
  11107. (let* ((value (org-entry-get pom property))
  11108. (values (and value (org-split-string value "[ \t]"))))
  11109. (mapcar 'org-entry-restore-space values)))
  11110. (defun org-entry-protect-space (s)
  11111. "Protect spaces and newline in string S."
  11112. (while (string-match " " s)
  11113. (setq s (replace-match "%20" t t s)))
  11114. (while (string-match "\n" s)
  11115. (setq s (replace-match "%0A" t t s)))
  11116. s)
  11117. (defun org-entry-restore-space (s)
  11118. "Restore spaces and newline in string S."
  11119. (while (string-match "%20" s)
  11120. (setq s (replace-match " " t t s)))
  11121. (while (string-match "%0A" s)
  11122. (setq s (replace-match "\n" t t s)))
  11123. s)
  11124. (defvar org-entry-property-inherited-from (make-marker)
  11125. "Marker pointing to the entry from where a property was inherited.
  11126. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11127. location of the entry where the inheritance search matched. If there was
  11128. no match, the marker will point nowhere.
  11129. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11130. is set.")
  11131. (defun org-entry-get-with-inheritance (property)
  11132. "Get entry property, and search higher levels if not present."
  11133. (move-marker org-entry-property-inherited-from nil)
  11134. (let (tmp)
  11135. (save-excursion
  11136. (save-restriction
  11137. (widen)
  11138. (catch 'ex
  11139. (while t
  11140. (when (setq tmp (org-entry-get nil property))
  11141. (org-back-to-heading t)
  11142. (move-marker org-entry-property-inherited-from (point))
  11143. (throw 'ex tmp))
  11144. (or (org-up-heading-safe) (throw 'ex nil)))))
  11145. (or tmp
  11146. (cdr (assoc property org-file-properties))
  11147. (cdr (assoc property org-global-properties))
  11148. (cdr (assoc property org-global-properties-fixed))))))
  11149. (defun org-entry-put (pom property value)
  11150. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11151. (org-with-point-at pom
  11152. (org-back-to-heading t)
  11153. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11154. range)
  11155. (cond
  11156. ((equal property "TODO")
  11157. (when (and (stringp value) (string-match "\\S-" value)
  11158. (not (member value org-todo-keywords-1)))
  11159. (error "\"%s\" is not a valid TODO state" value))
  11160. (if (or (not value)
  11161. (not (string-match "\\S-" value)))
  11162. (setq value 'none))
  11163. (org-todo value)
  11164. (org-set-tags nil 'align))
  11165. ((equal property "PRIORITY")
  11166. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11167. (string-to-char value) ?\ ))
  11168. (org-set-tags nil 'align))
  11169. ((equal property "SCHEDULED")
  11170. (if (re-search-forward org-scheduled-time-regexp end t)
  11171. (cond
  11172. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11173. ((eq value 'later) (org-timestamp-change 1 'day))
  11174. (t (call-interactively 'org-schedule)))
  11175. (call-interactively 'org-schedule)))
  11176. ((equal property "DEADLINE")
  11177. (if (re-search-forward org-deadline-time-regexp end t)
  11178. (cond
  11179. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11180. ((eq value 'later) (org-timestamp-change 1 'day))
  11181. (t (call-interactively 'org-deadline)))
  11182. (call-interactively 'org-deadline)))
  11183. ((member property org-special-properties)
  11184. (error "The %s property can not yet be set with `org-entry-put'"
  11185. property))
  11186. (t ; a non-special property
  11187. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11188. (setq range (org-get-property-block beg end 'force))
  11189. (goto-char (car range))
  11190. (if (re-search-forward
  11191. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11192. (progn
  11193. (delete-region (match-beginning 1) (match-end 1))
  11194. (goto-char (match-beginning 1)))
  11195. (goto-char (cdr range))
  11196. (insert "\n")
  11197. (backward-char 1)
  11198. (org-indent-line-function)
  11199. (insert ":" property ":"))
  11200. (and value (insert " " value))
  11201. (org-indent-line-function)))))))
  11202. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11203. "Get all property keys in the current buffer.
  11204. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11205. like tags and TODO state.
  11206. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11207. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11208. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11209. formats in the current buffer."
  11210. (let (rtn range cfmt s p)
  11211. (save-excursion
  11212. (save-restriction
  11213. (widen)
  11214. (goto-char (point-min))
  11215. (while (re-search-forward org-property-start-re nil t)
  11216. (setq range (org-get-property-block))
  11217. (goto-char (car range))
  11218. (while (re-search-forward
  11219. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11220. (cdr range) t)
  11221. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11222. (outline-next-heading))))
  11223. (when include-specials
  11224. (setq rtn (append org-special-properties rtn)))
  11225. (when include-defaults
  11226. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11227. (add-to-list 'rtn org-effort-property))
  11228. (when include-columns
  11229. (save-excursion
  11230. (save-restriction
  11231. (widen)
  11232. (goto-char (point-min))
  11233. (while (re-search-forward
  11234. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11235. nil t)
  11236. (setq cfmt (match-string 2) s 0)
  11237. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11238. cfmt s)
  11239. (setq s (match-end 0)
  11240. p (match-string 1 cfmt))
  11241. (unless (or (equal p "ITEM")
  11242. (member p org-special-properties))
  11243. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11244. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11245. (defun org-property-values (key)
  11246. "Return a list of all values of property KEY."
  11247. (save-excursion
  11248. (save-restriction
  11249. (widen)
  11250. (goto-char (point-min))
  11251. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11252. values)
  11253. (while (re-search-forward re nil t)
  11254. (add-to-list 'values (org-trim (match-string 1))))
  11255. (delete "" values)))))
  11256. (defun org-insert-property-drawer ()
  11257. "Insert a property drawer into the current entry."
  11258. (interactive)
  11259. (org-back-to-heading t)
  11260. (looking-at outline-regexp)
  11261. (let ((indent (if org-adapt-indentation
  11262. (- (match-end 0)(match-beginning 0))
  11263. 0))
  11264. (beg (point))
  11265. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11266. end hiddenp)
  11267. (outline-next-heading)
  11268. (setq end (point))
  11269. (goto-char beg)
  11270. (while (re-search-forward re end t))
  11271. (setq hiddenp (org-invisible-p))
  11272. (end-of-line 1)
  11273. (and (equal (char-after) ?\n) (forward-char 1))
  11274. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11275. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11276. ;; just skip this line
  11277. (beginning-of-line 2)
  11278. ;; Drawer start, find the end
  11279. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11280. (beginning-of-line 1)))
  11281. (org-skip-over-state-notes)
  11282. (skip-chars-backward " \t\n\r")
  11283. (if (eq (char-before) ?*) (forward-char 1))
  11284. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11285. (beginning-of-line 0)
  11286. (org-indent-to-column indent)
  11287. (beginning-of-line 2)
  11288. (org-indent-to-column indent)
  11289. (beginning-of-line 0)
  11290. (if hiddenp
  11291. (save-excursion
  11292. (org-back-to-heading t)
  11293. (hide-entry))
  11294. (org-flag-drawer t))))
  11295. (defun org-set-property (property value)
  11296. "In the current entry, set PROPERTY to VALUE.
  11297. When called interactively, this will prompt for a property name, offering
  11298. completion on existing and default properties. And then it will prompt
  11299. for a value, offering completion either on allowed values (via an inherited
  11300. xxx_ALL property) or on existing values in other instances of this property
  11301. in the current file."
  11302. (interactive
  11303. (let* ((completion-ignore-case t)
  11304. (keys (org-buffer-property-keys nil t t))
  11305. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11306. (prop (if (member prop0 keys)
  11307. prop0
  11308. (or (cdr (assoc (downcase prop0)
  11309. (mapcar (lambda (x) (cons (downcase x) x))
  11310. keys)))
  11311. prop0)))
  11312. (cur (org-entry-get nil prop))
  11313. (allowed (org-property-get-allowed-values nil prop 'table))
  11314. (existing (mapcar 'list (org-property-values prop)))
  11315. (val (if allowed
  11316. (org-completing-read "Value: " allowed nil 'req-match)
  11317. (let (org-completion-use-ido org-completion-use-iswitchb)
  11318. (org-completing-read
  11319. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11320. (concat "[" cur "]") "")
  11321. ": ")
  11322. existing nil nil "" nil cur)))))
  11323. (list prop (if (equal val "") cur val))))
  11324. (unless (equal (org-entry-get nil property) value)
  11325. (org-entry-put nil property value)))
  11326. (defun org-delete-property (property)
  11327. "In the current entry, delete PROPERTY."
  11328. (interactive
  11329. (let* ((completion-ignore-case t)
  11330. (prop (org-icompleting-read
  11331. "Property: " (org-entry-properties nil 'standard))))
  11332. (list prop)))
  11333. (message "Property %s %s" property
  11334. (if (org-entry-delete nil property)
  11335. "deleted"
  11336. "was not present in the entry")))
  11337. (defun org-delete-property-globally (property)
  11338. "Remove PROPERTY globally, from all entries."
  11339. (interactive
  11340. (let* ((completion-ignore-case t)
  11341. (prop (org-icompleting-read
  11342. "Globally remove property: "
  11343. (mapcar 'list (org-buffer-property-keys)))))
  11344. (list prop)))
  11345. (save-excursion
  11346. (save-restriction
  11347. (widen)
  11348. (goto-char (point-min))
  11349. (let ((cnt 0))
  11350. (while (re-search-forward
  11351. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11352. nil t)
  11353. (setq cnt (1+ cnt))
  11354. (replace-match ""))
  11355. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11356. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11357. (defun org-compute-property-at-point ()
  11358. "Compute the property at point.
  11359. This looks for an enclosing column format, extracts the operator and
  11360. then applies it to the property in the column format's scope."
  11361. (interactive)
  11362. (unless (org-at-property-p)
  11363. (error "Not at a property"))
  11364. (let ((prop (org-match-string-no-properties 2)))
  11365. (org-columns-get-format-and-top-level)
  11366. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11367. (error "No operator defined for property %s" prop))
  11368. (org-columns-compute prop)))
  11369. (defun org-property-get-allowed-values (pom property &optional table)
  11370. "Get allowed values for the property PROPERTY.
  11371. When TABLE is non-nil, return an alist that can directly be used for
  11372. completion."
  11373. (let (vals)
  11374. (cond
  11375. ((equal property "TODO")
  11376. (setq vals (org-with-point-at pom
  11377. (append org-todo-keywords-1 '("")))))
  11378. ((equal property "PRIORITY")
  11379. (let ((n org-lowest-priority))
  11380. (while (>= n org-highest-priority)
  11381. (push (char-to-string n) vals)
  11382. (setq n (1- n)))))
  11383. ((member property org-special-properties))
  11384. (t
  11385. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11386. (when (and vals (string-match "\\S-" vals))
  11387. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11388. (setq vals (mapcar (lambda (x)
  11389. (cond ((stringp x) x)
  11390. ((numberp x) (number-to-string x))
  11391. ((symbolp x) (symbol-name x))
  11392. (t "???")))
  11393. vals)))))
  11394. (if table (mapcar 'list vals) vals)))
  11395. (defun org-property-previous-allowed-value (&optional previous)
  11396. "Switch to the next allowed value for this property."
  11397. (interactive)
  11398. (org-property-next-allowed-value t))
  11399. (defun org-property-next-allowed-value (&optional previous)
  11400. "Switch to the next allowed value for this property."
  11401. (interactive)
  11402. (unless (org-at-property-p)
  11403. (error "Not at a property"))
  11404. (let* ((key (match-string 2))
  11405. (value (match-string 3))
  11406. (allowed (or (org-property-get-allowed-values (point) key)
  11407. (and (member value '("[ ]" "[-]" "[X]"))
  11408. '("[ ]" "[X]"))))
  11409. nval)
  11410. (unless allowed
  11411. (error "Allowed values for this property have not been defined"))
  11412. (if previous (setq allowed (reverse allowed)))
  11413. (if (member value allowed)
  11414. (setq nval (car (cdr (member value allowed)))))
  11415. (setq nval (or nval (car allowed)))
  11416. (if (equal nval value)
  11417. (error "Only one allowed value for this property"))
  11418. (org-at-property-p)
  11419. (replace-match (concat " :" key ": " nval) t t)
  11420. (org-indent-line-function)
  11421. (beginning-of-line 1)
  11422. (skip-chars-forward " \t")))
  11423. (defun org-find-entry-with-id (ident)
  11424. "Locate the entry that contains the ID property with exact value IDENT.
  11425. IDENT can be a string, a symbol or a number, this function will search for
  11426. the string representation of it.
  11427. Return the position where this entry starts, or nil if there is no such entry."
  11428. (interactive "sID: ")
  11429. (let ((id (cond
  11430. ((stringp ident) ident)
  11431. ((symbol-name ident) (symbol-name ident))
  11432. ((numberp ident) (number-to-string ident))
  11433. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11434. (case-fold-search nil))
  11435. (save-excursion
  11436. (save-restriction
  11437. (widen)
  11438. (goto-char (point-min))
  11439. (when (re-search-forward
  11440. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11441. nil t)
  11442. (org-back-to-heading t)
  11443. (point))))))
  11444. ;;;; Timestamps
  11445. (defvar org-last-changed-timestamp nil)
  11446. (defvar org-last-inserted-timestamp nil
  11447. "The last time stamp inserted with `org-insert-time-stamp'.")
  11448. (defvar org-time-was-given) ; dynamically scoped parameter
  11449. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11450. (defvar org-ts-what) ; dynamically scoped parameter
  11451. (defun org-time-stamp (arg &optional inactive)
  11452. "Prompt for a date/time and insert a time stamp.
  11453. If the user specifies a time like HH:MM, or if this command is called
  11454. with a prefix argument, the time stamp will contain date and time.
  11455. Otherwise, only the date will be included. All parts of a date not
  11456. specified by the user will be filled in from the current date/time.
  11457. So if you press just return without typing anything, the time stamp
  11458. will represent the current date/time. If there is already a timestamp
  11459. at the cursor, it will be modified."
  11460. (interactive "P")
  11461. (let* ((ts nil)
  11462. (default-time
  11463. ;; Default time is either today, or, when entering a range,
  11464. ;; the range start.
  11465. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11466. (save-excursion
  11467. (re-search-backward
  11468. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11469. (- (point) 20) t)))
  11470. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11471. (current-time)))
  11472. (default-input (and ts (org-get-compact-tod ts)))
  11473. org-time-was-given org-end-time-was-given time)
  11474. (cond
  11475. ((and (org-at-timestamp-p t)
  11476. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11477. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11478. (insert "--")
  11479. (setq time (let ((this-command this-command))
  11480. (org-read-date arg 'totime nil nil
  11481. default-time default-input)))
  11482. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11483. ((org-at-timestamp-p t)
  11484. (setq time (let ((this-command this-command))
  11485. (org-read-date arg 'totime nil nil default-time default-input)))
  11486. (when (org-at-timestamp-p t) ; just to get the match data
  11487. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11488. (replace-match "")
  11489. (setq org-last-changed-timestamp
  11490. (org-insert-time-stamp
  11491. time (or org-time-was-given arg)
  11492. inactive nil nil (list org-end-time-was-given))))
  11493. (message "Timestamp updated"))
  11494. (t
  11495. (setq time (let ((this-command this-command))
  11496. (org-read-date arg 'totime nil nil default-time default-input)))
  11497. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11498. nil nil (list org-end-time-was-given))))))
  11499. ;; FIXME: can we use this for something else, like computing time differences?
  11500. (defun org-get-compact-tod (s)
  11501. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11502. (let* ((t1 (match-string 1 s))
  11503. (h1 (string-to-number (match-string 2 s)))
  11504. (m1 (string-to-number (match-string 3 s)))
  11505. (t2 (and (match-end 4) (match-string 5 s)))
  11506. (h2 (and t2 (string-to-number (match-string 6 s))))
  11507. (m2 (and t2 (string-to-number (match-string 7 s))))
  11508. dh dm)
  11509. (if (not t2)
  11510. t1
  11511. (setq dh (- h2 h1) dm (- m2 m1))
  11512. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11513. (concat t1 "+" (number-to-string dh)
  11514. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11515. (defun org-time-stamp-inactive (&optional arg)
  11516. "Insert an inactive time stamp.
  11517. An inactive time stamp is enclosed in square brackets instead of angle
  11518. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11519. does not link to the calendar and cannot be changed with the S-cursor keys.
  11520. So these are more for recording a certain time/date."
  11521. (interactive "P")
  11522. (org-time-stamp arg 'inactive))
  11523. (defvar org-date-ovl (org-make-overlay 1 1))
  11524. (org-overlay-put org-date-ovl 'face 'org-warning)
  11525. (org-detach-overlay org-date-ovl)
  11526. (defvar org-ans1) ; dynamically scoped parameter
  11527. (defvar org-ans2) ; dynamically scoped parameter
  11528. (defvar org-plain-time-of-day-regexp) ; defined below
  11529. (defvar org-overriding-default-time nil) ; dynamically scoped
  11530. (defvar org-read-date-overlay nil)
  11531. (defvar org-dcst nil) ; dynamically scoped
  11532. (defvar org-read-date-history nil)
  11533. (defvar org-read-date-final-answer nil)
  11534. (defun org-read-date (&optional with-time to-time from-string prompt
  11535. default-time default-input)
  11536. "Read a date, possibly a time, and make things smooth for the user.
  11537. The prompt will suggest to enter an ISO date, but you can also enter anything
  11538. which will at least partially be understood by `parse-time-string'.
  11539. Unrecognized parts of the date will default to the current day, month, year,
  11540. hour and minute. If this command is called to replace a timestamp at point,
  11541. of to enter the second timestamp of a range, the default time is taken from the
  11542. existing stamp. For example,
  11543. 3-2-5 --> 2003-02-05
  11544. feb 15 --> currentyear-02-15
  11545. sep 12 9 --> 2009-09-12
  11546. 12:45 --> today 12:45
  11547. 22 sept 0:34 --> currentyear-09-22 0:34
  11548. 12 --> currentyear-currentmonth-12
  11549. Fri --> nearest Friday (today or later)
  11550. etc.
  11551. Furthermore you can specify a relative date by giving, as the *first* thing
  11552. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11553. change in days weeks, months, years.
  11554. With a single plus or minus, the date is relative to today. With a double
  11555. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11556. +4d --> four days from today
  11557. +4 --> same as above
  11558. +2w --> two weeks from today
  11559. ++5 --> five days from default date
  11560. The function understands only English month and weekday abbreviations,
  11561. but this can be configured with the variables `parse-time-months' and
  11562. `parse-time-weekdays'.
  11563. While prompting, a calendar is popped up - you can also select the
  11564. date with the mouse (button 1). The calendar shows a period of three
  11565. months. To scroll it to other months, use the keys `>' and `<'.
  11566. If you don't like the calendar, turn it off with
  11567. \(setq org-read-date-popup-calendar nil)
  11568. With optional argument TO-TIME, the date will immediately be converted
  11569. to an internal time.
  11570. With an optional argument WITH-TIME, the prompt will suggest to also
  11571. insert a time. Note that when WITH-TIME is not set, you can still
  11572. enter a time, and this function will inform the calling routine about
  11573. this change. The calling routine may then choose to change the format
  11574. used to insert the time stamp into the buffer to include the time.
  11575. With optional argument FROM-STRING, read from this string instead from
  11576. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11577. the time/date that is used for everything that is not specified by the
  11578. user."
  11579. (require 'parse-time)
  11580. (let* ((org-time-stamp-rounding-minutes
  11581. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11582. (org-dcst org-display-custom-times)
  11583. (ct (org-current-time))
  11584. (def (or org-overriding-default-time default-time ct))
  11585. (defdecode (decode-time def))
  11586. (dummy (progn
  11587. (when (< (nth 2 defdecode) org-extend-today-until)
  11588. (setcar (nthcdr 2 defdecode) -1)
  11589. (setcar (nthcdr 1 defdecode) 59)
  11590. (setq def (apply 'encode-time defdecode)
  11591. defdecode (decode-time def)))))
  11592. (calendar-frame-setup nil)
  11593. (calendar-move-hook nil)
  11594. (calendar-view-diary-initially-flag nil)
  11595. (view-diary-entries-initially nil)
  11596. (calendar-view-holidays-initially-flag nil)
  11597. (view-calendar-holidays-initially nil)
  11598. (timestr (format-time-string
  11599. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11600. (prompt (concat (if prompt (concat prompt " ") "")
  11601. (format "Date+time [%s]: " timestr)))
  11602. ans (org-ans0 "") org-ans1 org-ans2 final)
  11603. (cond
  11604. (from-string (setq ans from-string))
  11605. (org-read-date-popup-calendar
  11606. (save-excursion
  11607. (save-window-excursion
  11608. (calendar)
  11609. (calendar-forward-day (- (time-to-days def)
  11610. (calendar-absolute-from-gregorian
  11611. (calendar-current-date))))
  11612. (org-eval-in-calendar nil t)
  11613. (let* ((old-map (current-local-map))
  11614. (map (copy-keymap calendar-mode-map))
  11615. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11616. (org-defkey map (kbd "RET") 'org-calendar-select)
  11617. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11618. 'org-calendar-select-mouse)
  11619. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11620. 'org-calendar-select-mouse)
  11621. (org-defkey minibuffer-local-map [(meta shift left)]
  11622. (lambda () (interactive)
  11623. (org-eval-in-calendar '(calendar-backward-month 1))))
  11624. (org-defkey minibuffer-local-map [(meta shift right)]
  11625. (lambda () (interactive)
  11626. (org-eval-in-calendar '(calendar-forward-month 1))))
  11627. (org-defkey minibuffer-local-map [(meta shift up)]
  11628. (lambda () (interactive)
  11629. (org-eval-in-calendar '(calendar-backward-year 1))))
  11630. (org-defkey minibuffer-local-map [(meta shift down)]
  11631. (lambda () (interactive)
  11632. (org-eval-in-calendar '(calendar-forward-year 1))))
  11633. (org-defkey minibuffer-local-map [?\e (shift left)]
  11634. (lambda () (interactive)
  11635. (org-eval-in-calendar '(calendar-backward-month 1))))
  11636. (org-defkey minibuffer-local-map [?\e (shift right)]
  11637. (lambda () (interactive)
  11638. (org-eval-in-calendar '(calendar-forward-month 1))))
  11639. (org-defkey minibuffer-local-map [?\e (shift up)]
  11640. (lambda () (interactive)
  11641. (org-eval-in-calendar '(calendar-backward-year 1))))
  11642. (org-defkey minibuffer-local-map [?\e (shift down)]
  11643. (lambda () (interactive)
  11644. (org-eval-in-calendar '(calendar-forward-year 1))))
  11645. (org-defkey minibuffer-local-map [(shift up)]
  11646. (lambda () (interactive)
  11647. (org-eval-in-calendar '(calendar-backward-week 1))))
  11648. (org-defkey minibuffer-local-map [(shift down)]
  11649. (lambda () (interactive)
  11650. (org-eval-in-calendar '(calendar-forward-week 1))))
  11651. (org-defkey minibuffer-local-map [(shift left)]
  11652. (lambda () (interactive)
  11653. (org-eval-in-calendar '(calendar-backward-day 1))))
  11654. (org-defkey minibuffer-local-map [(shift right)]
  11655. (lambda () (interactive)
  11656. (org-eval-in-calendar '(calendar-forward-day 1))))
  11657. (org-defkey minibuffer-local-map ">"
  11658. (lambda () (interactive)
  11659. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11660. (org-defkey minibuffer-local-map "<"
  11661. (lambda () (interactive)
  11662. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11663. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11664. (unwind-protect
  11665. (progn
  11666. (use-local-map map)
  11667. (add-hook 'post-command-hook 'org-read-date-display)
  11668. (setq org-ans0 (read-string prompt default-input
  11669. 'org-read-date-history nil))
  11670. ;; org-ans0: from prompt
  11671. ;; org-ans1: from mouse click
  11672. ;; org-ans2: from calendar motion
  11673. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11674. (remove-hook 'post-command-hook 'org-read-date-display)
  11675. (use-local-map old-map)
  11676. (when org-read-date-overlay
  11677. (org-delete-overlay org-read-date-overlay)
  11678. (setq org-read-date-overlay nil)))))))
  11679. (t ; Naked prompt only
  11680. (unwind-protect
  11681. (setq ans (read-string prompt default-input
  11682. 'org-read-date-history timestr))
  11683. (when org-read-date-overlay
  11684. (org-delete-overlay org-read-date-overlay)
  11685. (setq org-read-date-overlay nil)))))
  11686. (setq final (org-read-date-analyze ans def defdecode))
  11687. (setq org-read-date-final-answer ans)
  11688. (if to-time
  11689. (apply 'encode-time final)
  11690. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11691. (format "%04d-%02d-%02d %02d:%02d"
  11692. (nth 5 final) (nth 4 final) (nth 3 final)
  11693. (nth 2 final) (nth 1 final))
  11694. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11695. (defvar def)
  11696. (defvar defdecode)
  11697. (defvar with-time)
  11698. (defvar org-read-date-analyze-futurep nil)
  11699. (defun org-read-date-display ()
  11700. "Display the current date prompt interpretation in the minibuffer."
  11701. (when org-read-date-display-live
  11702. (when org-read-date-overlay
  11703. (org-delete-overlay org-read-date-overlay))
  11704. (let ((p (point)))
  11705. (end-of-line 1)
  11706. (while (not (equal (buffer-substring
  11707. (max (point-min) (- (point) 4)) (point))
  11708. " "))
  11709. (insert " "))
  11710. (goto-char p))
  11711. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11712. " " (or org-ans1 org-ans2)))
  11713. (org-end-time-was-given nil)
  11714. (f (org-read-date-analyze ans def defdecode))
  11715. (fmts (if org-dcst
  11716. org-time-stamp-custom-formats
  11717. org-time-stamp-formats))
  11718. (fmt (if (or with-time
  11719. (and (boundp 'org-time-was-given) org-time-was-given))
  11720. (cdr fmts)
  11721. (car fmts)))
  11722. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11723. (when (and org-end-time-was-given
  11724. (string-match org-plain-time-of-day-regexp txt))
  11725. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11726. org-end-time-was-given
  11727. (substring txt (match-end 0)))))
  11728. (when org-read-date-analyze-futurep
  11729. (setq txt (concat txt " (=>F)")))
  11730. (setq org-read-date-overlay
  11731. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11732. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11733. (defun org-read-date-analyze (ans def defdecode)
  11734. "Analyse the combined answer of the date prompt."
  11735. ;; FIXME: cleanup and comment
  11736. (let (delta deltan deltaw deltadef year month day
  11737. hour minute second wday pm h2 m2 tl wday1
  11738. iso-year iso-weekday iso-week iso-year iso-date futurep)
  11739. (setq org-read-date-analyze-futurep nil)
  11740. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11741. (setq ans "+0"))
  11742. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11743. (setq ans (replace-match "" t t ans)
  11744. deltan (car delta)
  11745. deltaw (nth 1 delta)
  11746. deltadef (nth 2 delta)))
  11747. ;; Check if there is an iso week date in there
  11748. ;; If yes, sore the info and postpone interpreting it until the rest
  11749. ;; of the parsing is done
  11750. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11751. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11752. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11753. iso-week (string-to-number (match-string 2 ans)))
  11754. (setq ans (replace-match "" t t ans)))
  11755. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11756. (when (string-match
  11757. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11758. (setq year (if (match-end 2)
  11759. (string-to-number (match-string 2 ans))
  11760. (string-to-number (format-time-string "%Y")))
  11761. month (string-to-number (match-string 3 ans))
  11762. day (string-to-number (match-string 4 ans)))
  11763. (if (< year 100) (setq year (+ 2000 year)))
  11764. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11765. t nil ans)))
  11766. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11767. ;; If there is a time with am/pm, and *no* time without it, we convert
  11768. ;; so that matching will be successful.
  11769. (loop for i from 1 to 2 do ; twice, for end time as well
  11770. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11771. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11772. (setq hour (string-to-number (match-string 1 ans))
  11773. minute (if (match-end 3)
  11774. (string-to-number (match-string 3 ans))
  11775. 0)
  11776. pm (equal ?p
  11777. (string-to-char (downcase (match-string 4 ans)))))
  11778. (if (and (= hour 12) (not pm))
  11779. (setq hour 0)
  11780. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11781. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11782. t t ans))))
  11783. ;; Check if a time range is given as a duration
  11784. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11785. (setq hour (string-to-number (match-string 1 ans))
  11786. h2 (+ hour (string-to-number (match-string 3 ans)))
  11787. minute (string-to-number (match-string 2 ans))
  11788. m2 (+ minute (if (match-end 5) (string-to-number
  11789. (match-string 5 ans))0)))
  11790. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11791. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11792. t t ans)))
  11793. ;; Check if there is a time range
  11794. (when (boundp 'org-end-time-was-given)
  11795. (setq org-time-was-given nil)
  11796. (when (and (string-match org-plain-time-of-day-regexp ans)
  11797. (match-end 8))
  11798. (setq org-end-time-was-given (match-string 8 ans))
  11799. (setq ans (concat (substring ans 0 (match-beginning 7))
  11800. (substring ans (match-end 7))))))
  11801. (setq tl (parse-time-string ans)
  11802. day (or (nth 3 tl) (nth 3 defdecode))
  11803. month (or (nth 4 tl)
  11804. (if (and org-read-date-prefer-future
  11805. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11806. (prog1 (1+ (nth 4 defdecode)) (setq futurep t))
  11807. (nth 4 defdecode)))
  11808. year (or (nth 5 tl)
  11809. (if (and org-read-date-prefer-future
  11810. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11811. (prog1 (1+ (nth 5 defdecode)) (setq futurep t))
  11812. (nth 5 defdecode)))
  11813. hour (or (nth 2 tl) (nth 2 defdecode))
  11814. minute (or (nth 1 tl) (nth 1 defdecode))
  11815. second (or (nth 0 tl) 0)
  11816. wday (nth 6 tl))
  11817. (when (and (eq org-read-date-prefer-future 'time)
  11818. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  11819. (equal day (nth 3 defdecode))
  11820. (equal month (nth 4 defdecode))
  11821. (equal year (nth 5 defdecode))
  11822. (nth 2 tl)
  11823. (or (< (nth 2 tl) (nth 2 defdecode))
  11824. (and (= (nth 2 tl) (nth 2 defdecode))
  11825. (nth 1 tl)
  11826. (< (nth 1 tl) (nth 1 defdecode)))))
  11827. (setq day (1+ day)
  11828. futurep t))
  11829. ;; Special date definitions below
  11830. (cond
  11831. (iso-week
  11832. ;; There was an iso week
  11833. (setq futurep nil)
  11834. (setq year (or iso-year year)
  11835. day (or iso-weekday wday 1)
  11836. wday nil ; to make sure that the trigger below does not match
  11837. iso-date (calendar-gregorian-from-absolute
  11838. (calendar-absolute-from-iso
  11839. (list iso-week day year))))
  11840. ; FIXME: Should we also push ISO weeks into the future?
  11841. ; (when (and org-read-date-prefer-future
  11842. ; (not iso-year)
  11843. ; (< (calendar-absolute-from-gregorian iso-date)
  11844. ; (time-to-days (current-time))))
  11845. ; (setq year (1+ year)
  11846. ; iso-date (calendar-gregorian-from-absolute
  11847. ; (calendar-absolute-from-iso
  11848. ; (list iso-week day year)))))
  11849. (setq month (car iso-date)
  11850. year (nth 2 iso-date)
  11851. day (nth 1 iso-date)))
  11852. (deltan
  11853. (setq futurep nil)
  11854. (unless deltadef
  11855. (let ((now (decode-time (current-time))))
  11856. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11857. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11858. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11859. ((equal deltaw "m") (setq month (+ month deltan)))
  11860. ((equal deltaw "y") (setq year (+ year deltan)))))
  11861. ((and wday (not (nth 3 tl)))
  11862. (setq futurep nil)
  11863. ;; Weekday was given, but no day, so pick that day in the week
  11864. ;; on or after the derived date.
  11865. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11866. (unless (equal wday wday1)
  11867. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11868. (if (and (boundp 'org-time-was-given)
  11869. (nth 2 tl))
  11870. (setq org-time-was-given t))
  11871. (if (< year 100) (setq year (+ 2000 year)))
  11872. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11873. (setq org-read-date-analyze-futurep futurep)
  11874. (list second minute hour day month year)))
  11875. (defvar parse-time-weekdays)
  11876. (defun org-read-date-get-relative (s today default)
  11877. "Check string S for special relative date string.
  11878. TODAY and DEFAULT are internal times, for today and for a default.
  11879. Return shift list (N what def-flag)
  11880. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11881. N is the number of WHATs to shift.
  11882. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11883. the DEFAULT date rather than TODAY."
  11884. (when (and
  11885. (string-match
  11886. (concat
  11887. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11888. "\\([0-9]+\\)?"
  11889. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11890. "\\([ \t]\\|$\\)") s)
  11891. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11892. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11893. (string-to-char (substring (match-string 1 s) -1))
  11894. ?+))
  11895. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11896. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11897. (what (if (match-end 3) (match-string 3 s) "d"))
  11898. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11899. (date (if rel default today))
  11900. (wday (nth 6 (decode-time date)))
  11901. delta)
  11902. (if wday1
  11903. (progn
  11904. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11905. (if (= dir ?-) (setq delta (- delta 7)))
  11906. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11907. (list delta "d" rel))
  11908. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11909. (defun org-eval-in-calendar (form &optional keepdate)
  11910. "Eval FORM in the calendar window and return to current window.
  11911. Also, store the cursor date in variable org-ans2."
  11912. (let ((sf (selected-frame))
  11913. (sw (selected-window)))
  11914. (select-window (get-buffer-window "*Calendar*" t))
  11915. (eval form)
  11916. (when (and (not keepdate) (calendar-cursor-to-date))
  11917. (let* ((date (calendar-cursor-to-date))
  11918. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11919. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11920. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11921. (select-window sw)
  11922. (org-select-frame-set-input-focus sf)))
  11923. (defun org-calendar-select ()
  11924. "Return to `org-read-date' with the date currently selected.
  11925. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11926. (interactive)
  11927. (when (calendar-cursor-to-date)
  11928. (let* ((date (calendar-cursor-to-date))
  11929. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11930. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11931. (if (active-minibuffer-window) (exit-minibuffer))))
  11932. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11933. "Insert a date stamp for the date given by the internal TIME.
  11934. WITH-HM means, use the stamp format that includes the time of the day.
  11935. INACTIVE means use square brackets instead of angular ones, so that the
  11936. stamp will not contribute to the agenda.
  11937. PRE and POST are optional strings to be inserted before and after the
  11938. stamp.
  11939. The command returns the inserted time stamp."
  11940. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11941. stamp)
  11942. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11943. (insert-before-markers (or pre ""))
  11944. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11945. (when (listp extra)
  11946. (setq extra (car extra))
  11947. (if (and (stringp extra)
  11948. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11949. (setq extra (format "-%02d:%02d"
  11950. (string-to-number (match-string 1 extra))
  11951. (string-to-number (match-string 2 extra))))
  11952. (setq extra nil)))
  11953. (when extra
  11954. (backward-char 1)
  11955. (insert-before-markers extra)
  11956. (forward-char 1))
  11957. (insert-before-markers (or post ""))
  11958. (setq org-last-inserted-timestamp stamp)))
  11959. (defun org-toggle-time-stamp-overlays ()
  11960. "Toggle the use of custom time stamp formats."
  11961. (interactive)
  11962. (setq org-display-custom-times (not org-display-custom-times))
  11963. (unless org-display-custom-times
  11964. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11965. (while (setq p (next-single-property-change p 'display))
  11966. (if (and (get-text-property p 'display)
  11967. (eq (get-text-property p 'face) 'org-date))
  11968. (remove-text-properties
  11969. p (setq p (next-single-property-change p 'display))
  11970. '(display t))))
  11971. (set-buffer-modified-p bmp)))
  11972. (if (featurep 'xemacs)
  11973. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11974. (org-restart-font-lock)
  11975. (setq org-table-may-need-update t)
  11976. (if org-display-custom-times
  11977. (message "Time stamps are overlayed with custom format")
  11978. (message "Time stamp overlays removed")))
  11979. (defun org-display-custom-time (beg end)
  11980. "Overlay modified time stamp format over timestamp between BEG and END."
  11981. (let* ((ts (buffer-substring beg end))
  11982. t1 w1 with-hm tf time str w2 (off 0))
  11983. (save-match-data
  11984. (setq t1 (org-parse-time-string ts t))
  11985. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  11986. (setq off (- (match-end 0) (match-beginning 0)))))
  11987. (setq end (- end off))
  11988. (setq w1 (- end beg)
  11989. with-hm (and (nth 1 t1) (nth 2 t1))
  11990. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11991. time (org-fix-decoded-time t1)
  11992. str (org-add-props
  11993. (format-time-string
  11994. (substring tf 1 -1) (apply 'encode-time time))
  11995. nil 'mouse-face 'highlight)
  11996. w2 (length str))
  11997. (if (not (= w2 w1))
  11998. (add-text-properties (1+ beg) (+ 2 beg)
  11999. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12000. (if (featurep 'xemacs)
  12001. (progn
  12002. (put-text-property beg end 'invisible t)
  12003. (put-text-property beg end 'end-glyph (make-glyph str)))
  12004. (put-text-property beg end 'display str))))
  12005. (defun org-translate-time (string)
  12006. "Translate all timestamps in STRING to custom format.
  12007. But do this only if the variable `org-display-custom-times' is set."
  12008. (when org-display-custom-times
  12009. (save-match-data
  12010. (let* ((start 0)
  12011. (re org-ts-regexp-both)
  12012. t1 with-hm inactive tf time str beg end)
  12013. (while (setq start (string-match re string start))
  12014. (setq beg (match-beginning 0)
  12015. end (match-end 0)
  12016. t1 (save-match-data
  12017. (org-parse-time-string (substring string beg end) t))
  12018. with-hm (and (nth 1 t1) (nth 2 t1))
  12019. inactive (equal (substring string beg (1+ beg)) "[")
  12020. tf (funcall (if with-hm 'cdr 'car)
  12021. org-time-stamp-custom-formats)
  12022. time (org-fix-decoded-time t1)
  12023. str (format-time-string
  12024. (concat
  12025. (if inactive "[" "<") (substring tf 1 -1)
  12026. (if inactive "]" ">"))
  12027. (apply 'encode-time time))
  12028. string (replace-match str t t string)
  12029. start (+ start (length str)))))))
  12030. string)
  12031. (defun org-fix-decoded-time (time)
  12032. "Set 0 instead of nil for the first 6 elements of time.
  12033. Don't touch the rest."
  12034. (let ((n 0))
  12035. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12036. (defun org-days-to-time (timestamp-string)
  12037. "Difference between TIMESTAMP-STRING and now in days."
  12038. (- (time-to-days (org-time-string-to-time timestamp-string))
  12039. (time-to-days (current-time))))
  12040. (defun org-deadline-close (timestamp-string &optional ndays)
  12041. "Is the time in TIMESTAMP-STRING close to the current date?"
  12042. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12043. (and (< (org-days-to-time timestamp-string) ndays)
  12044. (not (org-entry-is-done-p))))
  12045. (defun org-get-wdays (ts)
  12046. "Get the deadline lead time appropriate for timestring TS."
  12047. (cond
  12048. ((<= org-deadline-warning-days 0)
  12049. ;; 0 or negative, enforce this value no matter what
  12050. (- org-deadline-warning-days))
  12051. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12052. ;; lead time is specified.
  12053. (floor (* (string-to-number (match-string 1 ts))
  12054. (cdr (assoc (match-string 2 ts)
  12055. '(("d" . 1) ("w" . 7)
  12056. ("m" . 30.4) ("y" . 365.25)))))))
  12057. ;; go for the default.
  12058. (t org-deadline-warning-days)))
  12059. (defun org-calendar-select-mouse (ev)
  12060. "Return to `org-read-date' with the date currently selected.
  12061. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12062. (interactive "e")
  12063. (mouse-set-point ev)
  12064. (when (calendar-cursor-to-date)
  12065. (let* ((date (calendar-cursor-to-date))
  12066. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12067. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12068. (if (active-minibuffer-window) (exit-minibuffer))))
  12069. (defun org-check-deadlines (ndays)
  12070. "Check if there are any deadlines due or past due.
  12071. A deadline is considered due if it happens within `org-deadline-warning-days'
  12072. days from today's date. If the deadline appears in an entry marked DONE,
  12073. it is not shown. The prefix arg NDAYS can be used to test that many
  12074. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12075. (interactive "P")
  12076. (let* ((org-warn-days
  12077. (cond
  12078. ((equal ndays '(4)) 100000)
  12079. (ndays (prefix-numeric-value ndays))
  12080. (t (abs org-deadline-warning-days))))
  12081. (case-fold-search nil)
  12082. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12083. (callback
  12084. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12085. (message "%d deadlines past-due or due within %d days"
  12086. (org-occur regexp nil callback)
  12087. org-warn-days)))
  12088. (defun org-check-before-date (date)
  12089. "Check if there are deadlines or scheduled entries before DATE."
  12090. (interactive (list (org-read-date)))
  12091. (let ((case-fold-search nil)
  12092. (regexp (concat "\\<\\(" org-deadline-string
  12093. "\\|" org-scheduled-string
  12094. "\\) *<\\([^>]+\\)>"))
  12095. (callback
  12096. (lambda () (time-less-p
  12097. (org-time-string-to-time (match-string 2))
  12098. (org-time-string-to-time date)))))
  12099. (message "%d entries before %s"
  12100. (org-occur regexp nil callback) date)))
  12101. (defun org-check-after-date (date)
  12102. "Check if there are deadlines or scheduled entries after DATE."
  12103. (interactive (list (org-read-date)))
  12104. (let ((case-fold-search nil)
  12105. (regexp (concat "\\<\\(" org-deadline-string
  12106. "\\|" org-scheduled-string
  12107. "\\) *<\\([^>]+\\)>"))
  12108. (callback
  12109. (lambda () (not
  12110. (time-less-p
  12111. (org-time-string-to-time (match-string 2))
  12112. (org-time-string-to-time date))))))
  12113. (message "%d entries after %s"
  12114. (org-occur regexp nil callback) date)))
  12115. (defun org-evaluate-time-range (&optional to-buffer)
  12116. "Evaluate a time range by computing the difference between start and end.
  12117. Normally the result is just printed in the echo area, but with prefix arg
  12118. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12119. If the time range is actually in a table, the result is inserted into the
  12120. next column.
  12121. For time difference computation, a year is assumed to be exactly 365
  12122. days in order to avoid rounding problems."
  12123. (interactive "P")
  12124. (or
  12125. (org-clock-update-time-maybe)
  12126. (save-excursion
  12127. (unless (org-at-date-range-p t)
  12128. (goto-char (point-at-bol))
  12129. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12130. (if (not (org-at-date-range-p t))
  12131. (error "Not at a time-stamp range, and none found in current line")))
  12132. (let* ((ts1 (match-string 1))
  12133. (ts2 (match-string 2))
  12134. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12135. (match-end (match-end 0))
  12136. (time1 (org-time-string-to-time ts1))
  12137. (time2 (org-time-string-to-time ts2))
  12138. (t1 (org-float-time time1))
  12139. (t2 (org-float-time time2))
  12140. (diff (abs (- t2 t1)))
  12141. (negative (< (- t2 t1) 0))
  12142. ;; (ys (floor (* 365 24 60 60)))
  12143. (ds (* 24 60 60))
  12144. (hs (* 60 60))
  12145. (fy "%dy %dd %02d:%02d")
  12146. (fy1 "%dy %dd")
  12147. (fd "%dd %02d:%02d")
  12148. (fd1 "%dd")
  12149. (fh "%02d:%02d")
  12150. y d h m align)
  12151. (if havetime
  12152. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12153. y 0
  12154. d (floor (/ diff ds)) diff (mod diff ds)
  12155. h (floor (/ diff hs)) diff (mod diff hs)
  12156. m (floor (/ diff 60)))
  12157. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12158. y 0
  12159. d (floor (+ (/ diff ds) 0.5))
  12160. h 0 m 0))
  12161. (if (not to-buffer)
  12162. (message "%s" (org-make-tdiff-string y d h m))
  12163. (if (org-at-table-p)
  12164. (progn
  12165. (goto-char match-end)
  12166. (setq align t)
  12167. (and (looking-at " *|") (goto-char (match-end 0))))
  12168. (goto-char match-end))
  12169. (if (looking-at
  12170. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12171. (replace-match ""))
  12172. (if negative (insert " -"))
  12173. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12174. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12175. (insert " " (format fh h m))))
  12176. (if align (org-table-align))
  12177. (message "Time difference inserted")))))
  12178. (defun org-make-tdiff-string (y d h m)
  12179. (let ((fmt "")
  12180. (l nil))
  12181. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12182. l (push y l)))
  12183. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12184. l (push d l)))
  12185. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12186. l (push h l)))
  12187. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12188. l (push m l)))
  12189. (apply 'format fmt (nreverse l))))
  12190. (defun org-time-string-to-time (s)
  12191. (apply 'encode-time (org-parse-time-string s)))
  12192. (defun org-time-string-to-seconds (s)
  12193. (org-float-time (org-time-string-to-time s)))
  12194. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12195. "Convert a time stamp to an absolute day number.
  12196. If there is a specifyer for a cyclic time stamp, get the closest date to
  12197. DAYNR.
  12198. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12199. the variable date is bound by the calendar when this is called."
  12200. (cond
  12201. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12202. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12203. daynr
  12204. (+ daynr 1000)))
  12205. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12206. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12207. (time-to-days (current-time))) (match-string 0 s)
  12208. prefer show-all))
  12209. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12210. (defun org-days-to-iso-week (days)
  12211. "Return the iso week number."
  12212. (require 'cal-iso)
  12213. (car (calendar-iso-from-absolute days)))
  12214. (defun org-small-year-to-year (year)
  12215. "Convert 2-digit years into 4-digit years.
  12216. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12217. The year 2000 cannot be abbreviated. Any year larger than 99
  12218. is returned unchanged."
  12219. (if (< year 38)
  12220. (setq year (+ 2000 year))
  12221. (if (< year 100)
  12222. (setq year (+ 1900 year))))
  12223. year)
  12224. (defun org-time-from-absolute (d)
  12225. "Return the time corresponding to date D.
  12226. D may be an absolute day number, or a calendar-type list (month day year)."
  12227. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12228. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12229. (defun org-calendar-holiday ()
  12230. "List of holidays, for Diary display in Org-mode."
  12231. (require 'holidays)
  12232. (let ((hl (funcall
  12233. (if (fboundp 'calendar-check-holidays)
  12234. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12235. (if hl (mapconcat 'identity hl "; "))))
  12236. (defun org-diary-sexp-entry (sexp entry date)
  12237. "Process a SEXP diary ENTRY for DATE."
  12238. (require 'diary-lib)
  12239. (let ((result (if calendar-debug-sexp
  12240. (let ((stack-trace-on-error t))
  12241. (eval (car (read-from-string sexp))))
  12242. (condition-case nil
  12243. (eval (car (read-from-string sexp)))
  12244. (error
  12245. (beep)
  12246. (message "Bad sexp at line %d in %s: %s"
  12247. (org-current-line)
  12248. (buffer-file-name) sexp)
  12249. (sleep-for 2))))))
  12250. (cond ((stringp result) result)
  12251. ((and (consp result)
  12252. (stringp (cdr result))) (cdr result))
  12253. (result entry)
  12254. (t nil))))
  12255. (defun org-diary-to-ical-string (frombuf)
  12256. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12257. This uses the icalendar.el library."
  12258. (let* ((tmpdir (if (featurep 'xemacs)
  12259. (temp-directory)
  12260. temporary-file-directory))
  12261. (tmpfile (make-temp-name
  12262. (expand-file-name "orgics" tmpdir)))
  12263. buf rtn b e)
  12264. (save-excursion
  12265. (set-buffer frombuf)
  12266. (icalendar-export-region (point-min) (point-max) tmpfile)
  12267. (setq buf (find-buffer-visiting tmpfile))
  12268. (set-buffer buf)
  12269. (goto-char (point-min))
  12270. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12271. (setq b (match-beginning 0)))
  12272. (goto-char (point-max))
  12273. (if (re-search-backward "^END:VEVENT" nil t)
  12274. (setq e (match-end 0)))
  12275. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12276. (kill-buffer buf)
  12277. (delete-file tmpfile)
  12278. rtn))
  12279. (defun org-closest-date (start current change prefer show-all)
  12280. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12281. When PREFER is `past' return a date that is either CURRENT or past.
  12282. When PREFER is `future', return a date that is either CURRENT or future.
  12283. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12284. ;; Make the proper lists from the dates
  12285. (catch 'exit
  12286. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12287. dn dw sday cday n1 n2 n0
  12288. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12289. (setq start (org-date-to-gregorian start)
  12290. current (org-date-to-gregorian
  12291. (if show-all
  12292. current
  12293. (time-to-days (current-time))))
  12294. sday (calendar-absolute-from-gregorian start)
  12295. cday (calendar-absolute-from-gregorian current))
  12296. (if (<= cday sday) (throw 'exit sday))
  12297. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12298. (setq dn (string-to-number (match-string 1 change))
  12299. dw (cdr (assoc (match-string 2 change) a1)))
  12300. (error "Invalid change specifyer: %s" change))
  12301. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12302. (cond
  12303. ((eq dw 'day)
  12304. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12305. n2 (+ n1 dn)))
  12306. ((eq dw 'year)
  12307. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12308. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12309. (setq date1 (list m d y1)
  12310. n1 (calendar-absolute-from-gregorian date1)
  12311. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12312. n2 (calendar-absolute-from-gregorian date2)))
  12313. ((eq dw 'month)
  12314. ;; approx number of month between the two dates
  12315. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12316. ;; How often does dn fit in there?
  12317. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12318. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12319. m (+ m nm)
  12320. ny (floor (/ m 12))
  12321. y (+ y ny)
  12322. m (- m (* ny 12)))
  12323. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12324. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12325. (setq m2 (+ m dn) y2 y)
  12326. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12327. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12328. (while (<= n2 cday)
  12329. (setq n1 n2 m m2 y y2)
  12330. (setq m2 (+ m dn) y2 y)
  12331. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12332. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12333. ;; Make sure n1 is the earlier date
  12334. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12335. (if show-all
  12336. (cond
  12337. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12338. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12339. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12340. (cond
  12341. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12342. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12343. (t (if (= cday n1) n1 n2)))))))
  12344. (defun org-date-to-gregorian (date)
  12345. "Turn any specification of DATE into a gregorian date for the calendar."
  12346. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12347. ((and (listp date) (= (length date) 3)) date)
  12348. ((stringp date)
  12349. (setq date (org-parse-time-string date))
  12350. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12351. ((listp date)
  12352. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12353. (defun org-parse-time-string (s &optional nodefault)
  12354. "Parse the standard Org-mode time string.
  12355. This should be a lot faster than the normal `parse-time-string'.
  12356. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12357. hour and minute fields will be nil if not given."
  12358. (if (string-match org-ts-regexp0 s)
  12359. (list 0
  12360. (if (or (match-beginning 8) (not nodefault))
  12361. (string-to-number (or (match-string 8 s) "0")))
  12362. (if (or (match-beginning 7) (not nodefault))
  12363. (string-to-number (or (match-string 7 s) "0")))
  12364. (string-to-number (match-string 4 s))
  12365. (string-to-number (match-string 3 s))
  12366. (string-to-number (match-string 2 s))
  12367. nil nil nil)
  12368. (error "Not a standard Org-mode time string: %s" s)))
  12369. (defun org-timestamp-up (&optional arg)
  12370. "Increase the date item at the cursor by one.
  12371. If the cursor is on the year, change the year. If it is on the month or
  12372. the day, change that.
  12373. With prefix ARG, change by that many units."
  12374. (interactive "p")
  12375. (org-timestamp-change (prefix-numeric-value arg)))
  12376. (defun org-timestamp-down (&optional arg)
  12377. "Decrease the date item at the cursor by one.
  12378. If the cursor is on the year, change the year. If it is on the month or
  12379. the day, change that.
  12380. With prefix ARG, change by that many units."
  12381. (interactive "p")
  12382. (org-timestamp-change (- (prefix-numeric-value arg))))
  12383. (defun org-timestamp-up-day (&optional arg)
  12384. "Increase the date in the time stamp by one day.
  12385. With prefix ARG, change that many days."
  12386. (interactive "p")
  12387. (if (and (not (org-at-timestamp-p t))
  12388. (org-on-heading-p))
  12389. (org-todo 'up)
  12390. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12391. (defun org-timestamp-down-day (&optional arg)
  12392. "Decrease the date in the time stamp by one day.
  12393. With prefix ARG, change that many days."
  12394. (interactive "p")
  12395. (if (and (not (org-at-timestamp-p t))
  12396. (org-on-heading-p))
  12397. (org-todo 'down)
  12398. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12399. (defun org-at-timestamp-p (&optional inactive-ok)
  12400. "Determine if the cursor is in or at a timestamp."
  12401. (interactive)
  12402. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12403. (pos (point))
  12404. (ans (or (looking-at tsr)
  12405. (save-excursion
  12406. (skip-chars-backward "^[<\n\r\t")
  12407. (if (> (point) (point-min)) (backward-char 1))
  12408. (and (looking-at tsr)
  12409. (> (- (match-end 0) pos) -1))))))
  12410. (and ans
  12411. (boundp 'org-ts-what)
  12412. (setq org-ts-what
  12413. (cond
  12414. ((= pos (match-beginning 0)) 'bracket)
  12415. ((= pos (1- (match-end 0))) 'bracket)
  12416. ((org-pos-in-match-range pos 2) 'year)
  12417. ((org-pos-in-match-range pos 3) 'month)
  12418. ((org-pos-in-match-range pos 7) 'hour)
  12419. ((org-pos-in-match-range pos 8) 'minute)
  12420. ((or (org-pos-in-match-range pos 4)
  12421. (org-pos-in-match-range pos 5)) 'day)
  12422. ((and (> pos (or (match-end 8) (match-end 5)))
  12423. (< pos (match-end 0)))
  12424. (- pos (or (match-end 8) (match-end 5))))
  12425. (t 'day))))
  12426. ans))
  12427. (defun org-toggle-timestamp-type ()
  12428. "Toggle the type (<active> or [inactive]) of a time stamp."
  12429. (interactive)
  12430. (when (org-at-timestamp-p t)
  12431. (let ((beg (match-beginning 0)) (end (match-end 0))
  12432. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12433. (save-excursion
  12434. (goto-char beg)
  12435. (while (re-search-forward "[][<>]" end t)
  12436. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12437. t t)))
  12438. (message "Timestamp is now %sactive"
  12439. (if (equal (char-after beg) ?<) "" "in")))))
  12440. (defun org-timestamp-change (n &optional what)
  12441. "Change the date in the time stamp at point.
  12442. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12443. `year', `minute', `second'. If WHAT is not given, the cursor position
  12444. in the timestamp determines what will be changed."
  12445. (let ((pos (point))
  12446. with-hm inactive
  12447. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12448. org-ts-what
  12449. extra rem
  12450. ts time time0)
  12451. (if (not (org-at-timestamp-p t))
  12452. (error "Not at a timestamp"))
  12453. (if (and (not what) (eq org-ts-what 'bracket))
  12454. (org-toggle-timestamp-type)
  12455. (if (and (not what) (not (eq org-ts-what 'day))
  12456. org-display-custom-times
  12457. (get-text-property (point) 'display)
  12458. (not (get-text-property (1- (point)) 'display)))
  12459. (setq org-ts-what 'day))
  12460. (setq org-ts-what (or what org-ts-what)
  12461. inactive (= (char-after (match-beginning 0)) ?\[)
  12462. ts (match-string 0))
  12463. (replace-match "")
  12464. (if (string-match
  12465. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  12466. ts)
  12467. (setq extra (match-string 1 ts)))
  12468. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12469. (setq with-hm t))
  12470. (setq time0 (org-parse-time-string ts))
  12471. (when (and (eq org-ts-what 'minute)
  12472. (eq current-prefix-arg nil))
  12473. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12474. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12475. (setcar (cdr time0) (+ (nth 1 time0)
  12476. (if (> n 0) (- rem) (- dm rem))))))
  12477. (setq time
  12478. (encode-time (or (car time0) 0)
  12479. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12480. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12481. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12482. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12483. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12484. (nthcdr 6 time0)))
  12485. (when (and (member org-ts-what '(hour minute))
  12486. extra
  12487. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12488. (setq extra (org-modify-ts-extra
  12489. extra
  12490. (if (eq org-ts-what 'hour) 2 5)
  12491. n dm)))
  12492. (when (integerp org-ts-what)
  12493. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12494. (if (eq what 'calendar)
  12495. (let ((cal-date (org-get-date-from-calendar)))
  12496. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12497. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12498. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12499. (setcar time0 (or (car time0) 0))
  12500. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12501. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12502. (setq time (apply 'encode-time time0))))
  12503. (setq org-last-changed-timestamp
  12504. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12505. (org-clock-update-time-maybe)
  12506. (goto-char pos)
  12507. ;; Try to recenter the calendar window, if any
  12508. (if (and org-calendar-follow-timestamp-change
  12509. (get-buffer-window "*Calendar*" t)
  12510. (memq org-ts-what '(day month year)))
  12511. (org-recenter-calendar (time-to-days time))))))
  12512. (defun org-modify-ts-extra (s pos n dm)
  12513. "Change the different parts of the lead-time and repeat fields in timestamp."
  12514. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12515. ng h m new rem)
  12516. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12517. (cond
  12518. ((or (org-pos-in-match-range pos 2)
  12519. (org-pos-in-match-range pos 3))
  12520. (setq m (string-to-number (match-string 3 s))
  12521. h (string-to-number (match-string 2 s)))
  12522. (if (org-pos-in-match-range pos 2)
  12523. (setq h (+ h n))
  12524. (setq n (* dm (org-no-warnings (signum n))))
  12525. (when (not (= 0 (setq rem (% m dm))))
  12526. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12527. (setq m (+ m n)))
  12528. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12529. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12530. (setq h (min 24 (max 0 h)))
  12531. (setq ng 1 new (format "-%02d:%02d" h m)))
  12532. ((org-pos-in-match-range pos 6)
  12533. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12534. ((org-pos-in-match-range pos 5)
  12535. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12536. ((org-pos-in-match-range pos 9)
  12537. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12538. ((org-pos-in-match-range pos 8)
  12539. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12540. (when ng
  12541. (setq s (concat
  12542. (substring s 0 (match-beginning ng))
  12543. new
  12544. (substring s (match-end ng))))))
  12545. s))
  12546. (defun org-recenter-calendar (date)
  12547. "If the calendar is visible, recenter it to DATE."
  12548. (let* ((win (selected-window))
  12549. (cwin (get-buffer-window "*Calendar*" t))
  12550. (calendar-move-hook nil))
  12551. (when cwin
  12552. (select-window cwin)
  12553. (calendar-goto-date (if (listp date) date
  12554. (calendar-gregorian-from-absolute date)))
  12555. (select-window win))))
  12556. (defun org-goto-calendar (&optional arg)
  12557. "Go to the Emacs calendar at the current date.
  12558. If there is a time stamp in the current line, go to that date.
  12559. A prefix ARG can be used to force the current date."
  12560. (interactive "P")
  12561. (let ((tsr org-ts-regexp) diff
  12562. (calendar-move-hook nil)
  12563. (calendar-view-holidays-initially-flag nil)
  12564. (view-calendar-holidays-initially nil)
  12565. (calendar-view-diary-initially-flag nil)
  12566. (view-diary-entries-initially nil))
  12567. (if (or (org-at-timestamp-p)
  12568. (save-excursion
  12569. (beginning-of-line 1)
  12570. (looking-at (concat ".*" tsr))))
  12571. (let ((d1 (time-to-days (current-time)))
  12572. (d2 (time-to-days
  12573. (org-time-string-to-time (match-string 1)))))
  12574. (setq diff (- d2 d1))))
  12575. (calendar)
  12576. (calendar-goto-today)
  12577. (if (and diff (not arg)) (calendar-forward-day diff))))
  12578. (defun org-get-date-from-calendar ()
  12579. "Return a list (month day year) of date at point in calendar."
  12580. (with-current-buffer "*Calendar*"
  12581. (save-match-data
  12582. (calendar-cursor-to-date))))
  12583. (defun org-date-from-calendar ()
  12584. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12585. If there is already a time stamp at the cursor position, update it."
  12586. (interactive)
  12587. (if (org-at-timestamp-p t)
  12588. (org-timestamp-change 0 'calendar)
  12589. (let ((cal-date (org-get-date-from-calendar)))
  12590. (org-insert-time-stamp
  12591. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12592. (defun org-minutes-to-hh:mm-string (m)
  12593. "Compute H:MM from a number of minutes."
  12594. (let ((h (/ m 60)))
  12595. (setq m (- m (* 60 h)))
  12596. (format org-time-clocksum-format h m)))
  12597. (defun org-hh:mm-string-to-minutes (s)
  12598. "Convert a string H:MM to a number of minutes.
  12599. If the string is just a number, interprete it as minutes.
  12600. In fact, the first hh:mm or number in the string will be taken,
  12601. there can be extra stuff in the string.
  12602. If no number is found, the return value is 0."
  12603. (cond
  12604. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12605. (+ (* (string-to-number (match-string 1 s)) 60)
  12606. (string-to-number (match-string 2 s))))
  12607. ((string-match "\\([0-9]+\\)" s)
  12608. (string-to-number (match-string 1 s)))
  12609. (t 0)))
  12610. ;;;; Files
  12611. (defun org-save-all-org-buffers ()
  12612. "Save all Org-mode buffers without user confirmation."
  12613. (interactive)
  12614. (message "Saving all Org-mode buffers...")
  12615. (save-some-buffers t 'org-mode-p)
  12616. (when (featurep 'org-id) (org-id-locations-save))
  12617. (message "Saving all Org-mode buffers... done"))
  12618. (defun org-revert-all-org-buffers ()
  12619. "Revert all Org-mode buffers.
  12620. Prompt for confirmation when there are unsaved changes.
  12621. Be sure you know what you are doing before letting this function
  12622. overwrite your changes.
  12623. This function is useful in a setup where one tracks org files
  12624. with a version control system, to revert on one machine after pulling
  12625. changes from another. I believe the procedure must be like this:
  12626. 1. M-x org-save-all-org-buffers
  12627. 2. Pull changes from the other machine, resolve conflicts
  12628. 3. M-x org-revert-all-org-buffers"
  12629. (interactive)
  12630. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12631. (error "Abort"))
  12632. (save-excursion
  12633. (save-window-excursion
  12634. (mapc
  12635. (lambda (b)
  12636. (when (and (with-current-buffer b (org-mode-p))
  12637. (with-current-buffer b buffer-file-name))
  12638. (switch-to-buffer b)
  12639. (revert-buffer t 'no-confirm)))
  12640. (buffer-list))
  12641. (when (and (featurep 'org-id) org-id-track-globally)
  12642. (org-id-locations-load)))))
  12643. ;;;; Agenda files
  12644. ;;;###autoload
  12645. (defun org-iswitchb (&optional arg)
  12646. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12647. With a prefix argument, restrict available to files.
  12648. With two prefix arguments, restrict available buffers to agenda files."
  12649. (interactive "P")
  12650. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12651. ((equal arg '(16)) (org-buffer-list 'agenda))
  12652. (t (org-buffer-list)))))
  12653. (switch-to-buffer
  12654. (org-icompleting-read "Org buffer: "
  12655. (mapcar 'list (mapcar 'buffer-name blist))
  12656. nil t))))
  12657. ;;;###autoload
  12658. (defalias 'org-ido-switchb 'org-iswitchb)
  12659. (defun org-buffer-list (&optional predicate exclude-tmp)
  12660. "Return a list of Org buffers.
  12661. PREDICATE can be `export', `files' or `agenda'.
  12662. export restrict the list to Export buffers.
  12663. files restrict the list to buffers visiting Org files.
  12664. agenda restrict the list to buffers visiting agenda files.
  12665. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12666. (let* ((bfn nil)
  12667. (agenda-files (and (eq predicate 'agenda)
  12668. (mapcar 'file-truename (org-agenda-files t))))
  12669. (filter
  12670. (cond
  12671. ((eq predicate 'files)
  12672. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12673. ((eq predicate 'export)
  12674. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12675. ((eq predicate 'agenda)
  12676. (lambda (b)
  12677. (with-current-buffer b
  12678. (and (eq major-mode 'org-mode)
  12679. (setq bfn (buffer-file-name b))
  12680. (member (file-truename bfn) agenda-files)))))
  12681. (t (lambda (b) (with-current-buffer b
  12682. (or (eq major-mode 'org-mode)
  12683. (string-match "\*Org .*Export"
  12684. (buffer-name b)))))))))
  12685. (delq nil
  12686. (mapcar
  12687. (lambda(b)
  12688. (if (and (funcall filter b)
  12689. (or (not exclude-tmp)
  12690. (not (string-match "tmp" (buffer-name b)))))
  12691. b
  12692. nil))
  12693. (buffer-list)))))
  12694. (defun org-agenda-files (&optional unrestricted archives)
  12695. "Get the list of agenda files.
  12696. Optional UNRESTRICTED means return the full list even if a restriction
  12697. is currently in place.
  12698. When ARCHIVES is t, include all archive files hat are really being
  12699. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12700. `org-agenda-archives-mode' is t."
  12701. (let ((files
  12702. (cond
  12703. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12704. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12705. ((listp org-agenda-files) org-agenda-files)
  12706. (t (error "Invalid value of `org-agenda-files'")))))
  12707. (setq files (apply 'append
  12708. (mapcar (lambda (f)
  12709. (if (file-directory-p f)
  12710. (directory-files
  12711. f t org-agenda-file-regexp)
  12712. (list f)))
  12713. files)))
  12714. (when org-agenda-skip-unavailable-files
  12715. (setq files (delq nil
  12716. (mapcar (function
  12717. (lambda (file)
  12718. (and (file-readable-p file) file)))
  12719. files))))
  12720. (when (or (eq archives t)
  12721. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12722. (setq files (org-add-archive-files files)))
  12723. files))
  12724. (defun org-edit-agenda-file-list ()
  12725. "Edit the list of agenda files.
  12726. Depending on setup, this either uses customize to edit the variable
  12727. `org-agenda-files', or it visits the file that is holding the list. In the
  12728. latter case, the buffer is set up in a way that saving it automatically kills
  12729. the buffer and restores the previous window configuration."
  12730. (interactive)
  12731. (if (stringp org-agenda-files)
  12732. (let ((cw (current-window-configuration)))
  12733. (find-file org-agenda-files)
  12734. (org-set-local 'org-window-configuration cw)
  12735. (org-add-hook 'after-save-hook
  12736. (lambda ()
  12737. (set-window-configuration
  12738. (prog1 org-window-configuration
  12739. (kill-buffer (current-buffer))))
  12740. (org-install-agenda-files-menu)
  12741. (message "New agenda file list installed"))
  12742. nil 'local)
  12743. (message "%s" (substitute-command-keys
  12744. "Edit list and finish with \\[save-buffer]")))
  12745. (customize-variable 'org-agenda-files)))
  12746. (defun org-store-new-agenda-file-list (list)
  12747. "Set new value for the agenda file list and save it correctly."
  12748. (if (stringp org-agenda-files)
  12749. (let ((f org-agenda-files) b)
  12750. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12751. (with-temp-file f
  12752. (insert (mapconcat 'identity list "\n") "\n")))
  12753. (let ((org-mode-hook nil) (org-inhibit-startup t)
  12754. (org-insert-mode-line-in-empty-file nil))
  12755. (setq org-agenda-files list)
  12756. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12757. (defun org-read-agenda-file-list ()
  12758. "Read the list of agenda files from a file."
  12759. (when (file-directory-p org-agenda-files)
  12760. (error "`org-agenda-files' cannot be a single directory"))
  12761. (when (stringp org-agenda-files)
  12762. (with-temp-buffer
  12763. (insert-file-contents org-agenda-files)
  12764. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12765. ;;;###autoload
  12766. (defun org-cycle-agenda-files ()
  12767. "Cycle through the files in `org-agenda-files'.
  12768. If the current buffer visits an agenda file, find the next one in the list.
  12769. If the current buffer does not, find the first agenda file."
  12770. (interactive)
  12771. (let* ((fs (org-agenda-files t))
  12772. (files (append fs (list (car fs))))
  12773. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12774. file)
  12775. (unless files (error "No agenda files"))
  12776. (catch 'exit
  12777. (while (setq file (pop files))
  12778. (if (equal (file-truename file) tcf)
  12779. (when (car files)
  12780. (find-file (car files))
  12781. (throw 'exit t))))
  12782. (find-file (car fs)))
  12783. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12784. (defun org-agenda-file-to-front (&optional to-end)
  12785. "Move/add the current file to the top of the agenda file list.
  12786. If the file is not present in the list, it is added to the front. If it is
  12787. present, it is moved there. With optional argument TO-END, add/move to the
  12788. end of the list."
  12789. (interactive "P")
  12790. (let ((org-agenda-skip-unavailable-files nil)
  12791. (file-alist (mapcar (lambda (x)
  12792. (cons (file-truename x) x))
  12793. (org-agenda-files t)))
  12794. (ctf (file-truename buffer-file-name))
  12795. x had)
  12796. (setq x (assoc ctf file-alist) had x)
  12797. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12798. (if to-end
  12799. (setq file-alist (append (delq x file-alist) (list x)))
  12800. (setq file-alist (cons x (delq x file-alist))))
  12801. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12802. (org-install-agenda-files-menu)
  12803. (message "File %s to %s of agenda file list"
  12804. (if had "moved" "added") (if to-end "end" "front"))))
  12805. (defun org-remove-file (&optional file)
  12806. "Remove current file from the list of files in variable `org-agenda-files'.
  12807. These are the files which are being checked for agenda entries.
  12808. Optional argument FILE means, use this file instead of the current."
  12809. (interactive)
  12810. (let* ((org-agenda-skip-unavailable-files nil)
  12811. (file (or file buffer-file-name))
  12812. (true-file (file-truename file))
  12813. (afile (abbreviate-file-name file))
  12814. (files (delq nil (mapcar
  12815. (lambda (x)
  12816. (if (equal true-file
  12817. (file-truename x))
  12818. nil x))
  12819. (org-agenda-files t)))))
  12820. (if (not (= (length files) (length (org-agenda-files t))))
  12821. (progn
  12822. (org-store-new-agenda-file-list files)
  12823. (org-install-agenda-files-menu)
  12824. (message "Removed file: %s" afile))
  12825. (message "File was not in list: %s (not removed)" afile))))
  12826. (defun org-file-menu-entry (file)
  12827. (vector file (list 'find-file file) t))
  12828. (defun org-check-agenda-file (file)
  12829. "Make sure FILE exists. If not, ask user what to do."
  12830. (when (not (file-exists-p file))
  12831. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  12832. (abbreviate-file-name file))
  12833. (let ((r (downcase (read-char-exclusive))))
  12834. (cond
  12835. ((equal r ?r)
  12836. (org-remove-file file)
  12837. (throw 'nextfile t))
  12838. (t (error "Abort"))))))
  12839. (defun org-get-agenda-file-buffer (file)
  12840. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12841. it to the list of buffers which might be released later."
  12842. (let ((buf (org-find-base-buffer-visiting file)))
  12843. (if buf
  12844. buf ; just return it
  12845. ;; Make a new buffer and remember it
  12846. (setq buf (find-file-noselect file))
  12847. (if buf (push buf org-agenda-new-buffers))
  12848. buf)))
  12849. (defun org-release-buffers (blist)
  12850. "Release all buffers in list, asking the user for confirmation when needed.
  12851. When a buffer is unmodified, it is just killed. When modified, it is saved
  12852. \(if the user agrees) and then killed."
  12853. (let (buf file)
  12854. (while (setq buf (pop blist))
  12855. (setq file (buffer-file-name buf))
  12856. (when (and (buffer-modified-p buf)
  12857. file
  12858. (y-or-n-p (format "Save file %s? " file)))
  12859. (with-current-buffer buf (save-buffer)))
  12860. (kill-buffer buf))))
  12861. (defun org-prepare-agenda-buffers (files)
  12862. "Create buffers for all agenda files, protect archived trees and comments."
  12863. (interactive)
  12864. (let ((pa '(:org-archived t))
  12865. (pc '(:org-comment t))
  12866. (pall '(:org-archived t :org-comment t))
  12867. (inhibit-read-only t)
  12868. (rea (concat ":" org-archive-tag ":"))
  12869. bmp file re)
  12870. (save-excursion
  12871. (save-restriction
  12872. (while (setq file (pop files))
  12873. (catch 'nextfile
  12874. (if (bufferp file)
  12875. (set-buffer file)
  12876. (org-check-agenda-file file)
  12877. (set-buffer (org-get-agenda-file-buffer file)))
  12878. (widen)
  12879. (setq bmp (buffer-modified-p))
  12880. (org-refresh-category-properties)
  12881. (setq org-todo-keywords-for-agenda
  12882. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12883. (setq org-done-keywords-for-agenda
  12884. (append org-done-keywords-for-agenda org-done-keywords))
  12885. (setq org-todo-keyword-alist-for-agenda
  12886. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12887. (setq org-drawers-for-agenda
  12888. (append org-drawers-for-agenda org-drawers))
  12889. (setq org-tag-alist-for-agenda
  12890. (append org-tag-alist-for-agenda org-tag-alist))
  12891. (save-excursion
  12892. (remove-text-properties (point-min) (point-max) pall)
  12893. (when org-agenda-skip-archived-trees
  12894. (goto-char (point-min))
  12895. (while (re-search-forward rea nil t)
  12896. (if (org-on-heading-p t)
  12897. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12898. (goto-char (point-min))
  12899. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12900. (while (re-search-forward re nil t)
  12901. (add-text-properties
  12902. (match-beginning 0) (org-end-of-subtree t) pc)))
  12903. (set-buffer-modified-p bmp)))))
  12904. (setq org-todo-keyword-alist-for-agenda
  12905. (org-uniquify org-todo-keyword-alist-for-agenda)
  12906. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12907. ;;;; Embedded LaTeX
  12908. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12909. "Keymap for the minor `org-cdlatex-mode'.")
  12910. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12911. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12912. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12913. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12914. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12915. (defvar org-cdlatex-texmathp-advice-is-done nil
  12916. "Flag remembering if we have applied the advice to texmathp already.")
  12917. (define-minor-mode org-cdlatex-mode
  12918. "Toggle the minor `org-cdlatex-mode'.
  12919. This mode supports entering LaTeX environment and math in LaTeX fragments
  12920. in Org-mode.
  12921. \\{org-cdlatex-mode-map}"
  12922. nil " OCDL" nil
  12923. (when org-cdlatex-mode (require 'cdlatex))
  12924. (unless org-cdlatex-texmathp-advice-is-done
  12925. (setq org-cdlatex-texmathp-advice-is-done t)
  12926. (defadvice texmathp (around org-math-always-on activate)
  12927. "Always return t in org-mode buffers.
  12928. This is because we want to insert math symbols without dollars even outside
  12929. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12930. an embedded LaTeX fragment, let texmathp do its job.
  12931. \\[org-cdlatex-mode-map]"
  12932. (interactive)
  12933. (let (p)
  12934. (cond
  12935. ((not (org-mode-p)) ad-do-it)
  12936. ((eq this-command 'cdlatex-math-symbol)
  12937. (setq ad-return-value t
  12938. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12939. (t
  12940. (let ((p (org-inside-LaTeX-fragment-p)))
  12941. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12942. (setq ad-return-value t
  12943. texmathp-why '("Org-mode embedded math" . 0))
  12944. (if p ad-do-it)))))))))
  12945. (defun turn-on-org-cdlatex ()
  12946. "Unconditionally turn on `org-cdlatex-mode'."
  12947. (org-cdlatex-mode 1))
  12948. (defun org-inside-LaTeX-fragment-p ()
  12949. "Test if point is inside a LaTeX fragment.
  12950. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12951. sequence appearing also before point.
  12952. Even though the matchers for math are configurable, this function assumes
  12953. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12954. delimiters are skipped when they have been removed by customization.
  12955. The return value is nil, or a cons cell with the delimiter and
  12956. and the position of this delimiter.
  12957. This function does a reasonably good job, but can locally be fooled by
  12958. for example currency specifications. For example it will assume being in
  12959. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12960. fragments that are properly closed, but during editing, we have to live
  12961. with the uncertainty caused by missing closing delimiters. This function
  12962. looks only before point, not after."
  12963. (catch 'exit
  12964. (let ((pos (point))
  12965. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12966. (lim (progn
  12967. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12968. (point)))
  12969. dd-on str (start 0) m re)
  12970. (goto-char pos)
  12971. (when dodollar
  12972. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12973. re (nth 1 (assoc "$" org-latex-regexps)))
  12974. (while (string-match re str start)
  12975. (cond
  12976. ((= (match-end 0) (length str))
  12977. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12978. ((= (match-end 0) (- (length str) 5))
  12979. (throw 'exit nil))
  12980. (t (setq start (match-end 0))))))
  12981. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12982. (goto-char pos)
  12983. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12984. (and (match-beginning 2) (throw 'exit nil))
  12985. ;; count $$
  12986. (while (re-search-backward "\\$\\$" lim t)
  12987. (setq dd-on (not dd-on)))
  12988. (goto-char pos)
  12989. (if dd-on (cons "$$" m))))))
  12990. (defun org-try-cdlatex-tab ()
  12991. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12992. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12993. - inside a LaTeX fragment, or
  12994. - after the first word in a line, where an abbreviation expansion could
  12995. insert a LaTeX environment."
  12996. (when org-cdlatex-mode
  12997. (cond
  12998. ((save-excursion
  12999. (skip-chars-backward "a-zA-Z0-9*")
  13000. (skip-chars-backward " \t")
  13001. (bolp))
  13002. (cdlatex-tab) t)
  13003. ((org-inside-LaTeX-fragment-p)
  13004. (cdlatex-tab) t)
  13005. (t nil))))
  13006. (defun org-cdlatex-underscore-caret (&optional arg)
  13007. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13008. Revert to the normal definition outside of these fragments."
  13009. (interactive "P")
  13010. (if (org-inside-LaTeX-fragment-p)
  13011. (call-interactively 'cdlatex-sub-superscript)
  13012. (let (org-cdlatex-mode)
  13013. (call-interactively (key-binding (vector last-input-event))))))
  13014. (defun org-cdlatex-math-modify (&optional arg)
  13015. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13016. Revert to the normal definition outside of these fragments."
  13017. (interactive "P")
  13018. (if (org-inside-LaTeX-fragment-p)
  13019. (call-interactively 'cdlatex-math-modify)
  13020. (let (org-cdlatex-mode)
  13021. (call-interactively (key-binding (vector last-input-event))))))
  13022. (defvar org-latex-fragment-image-overlays nil
  13023. "List of overlays carrying the images of latex fragments.")
  13024. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13025. (defun org-remove-latex-fragment-image-overlays ()
  13026. "Remove all overlays with LaTeX fragment images in current buffer."
  13027. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13028. (setq org-latex-fragment-image-overlays nil))
  13029. (defun org-preview-latex-fragment (&optional subtree)
  13030. "Preview the LaTeX fragment at point, or all locally or globally.
  13031. If the cursor is in a LaTeX fragment, create the image and overlay
  13032. it over the source code. If there is no fragment at point, display
  13033. all fragments in the current text, from one headline to the next. With
  13034. prefix SUBTREE, display all fragments in the current subtree. With a
  13035. double prefix `C-u C-u', or when the cursor is before the first headline,
  13036. display all fragments in the buffer.
  13037. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13038. (interactive "P")
  13039. (org-remove-latex-fragment-image-overlays)
  13040. (save-excursion
  13041. (save-restriction
  13042. (let (beg end at msg)
  13043. (cond
  13044. ((or (equal subtree '(16))
  13045. (not (save-excursion
  13046. (re-search-backward (concat "^" outline-regexp) nil t))))
  13047. (setq beg (point-min) end (point-max)
  13048. msg "Creating images for buffer...%s"))
  13049. ((equal subtree '(4))
  13050. (org-back-to-heading)
  13051. (setq beg (point) end (org-end-of-subtree t)
  13052. msg "Creating images for subtree...%s"))
  13053. (t
  13054. (if (setq at (org-inside-LaTeX-fragment-p))
  13055. (goto-char (max (point-min) (- (cdr at) 2)))
  13056. (org-back-to-heading))
  13057. (setq beg (point) end (progn (outline-next-heading) (point))
  13058. msg (if at "Creating image...%s"
  13059. "Creating images for entry...%s"))))
  13060. (message msg "")
  13061. (narrow-to-region beg end)
  13062. (goto-char beg)
  13063. (org-format-latex
  13064. (concat "ltxpng/" (file-name-sans-extension
  13065. (file-name-nondirectory
  13066. buffer-file-name)))
  13067. default-directory 'overlays msg at 'forbuffer)
  13068. (message msg "done. Use `C-c C-c' to remove images.")))))
  13069. (defvar org-latex-regexps
  13070. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13071. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13072. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13073. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13074. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13075. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13076. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13077. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13078. "Regular expressions for matching embedded LaTeX.")
  13079. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13080. "Replace LaTeX fragments with links to an image, and produce images.
  13081. Some of the options can be changed using the variable
  13082. `org-format-latex-options'."
  13083. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13084. (let* ((prefixnodir (file-name-nondirectory prefix))
  13085. (absprefix (expand-file-name prefix dir))
  13086. (todir (file-name-directory absprefix))
  13087. (opt org-format-latex-options)
  13088. (matchers (plist-get opt :matchers))
  13089. (re-list org-latex-regexps)
  13090. (cnt 0) txt link beg end re e checkdir
  13091. executables-checked
  13092. m n block linkfile movefile ov)
  13093. ;; Check if there are old images files with this prefix, and remove them
  13094. (when (file-directory-p todir)
  13095. (mapc 'delete-file
  13096. (directory-files
  13097. todir 'full
  13098. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  13099. ;; Check the different regular expressions
  13100. (while (setq e (pop re-list))
  13101. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13102. block (if (nth 3 e) "\n\n" ""))
  13103. (when (member m matchers)
  13104. (goto-char (point-min))
  13105. (while (re-search-forward re nil t)
  13106. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13107. (not (get-text-property (match-beginning n)
  13108. 'org-protected))
  13109. (or (not overlays)
  13110. (not (eq (get-char-property (match-beginning n)
  13111. 'org-overlay-type)
  13112. 'org-latex-overlay))))
  13113. (setq txt (match-string n)
  13114. beg (match-beginning n) end (match-end n)
  13115. cnt (1+ cnt)
  13116. linkfile (format "%s_%04d.png" prefix cnt)
  13117. movefile (format "%s_%04d.png" absprefix cnt)
  13118. link (concat block "[[file:" linkfile "]]" block))
  13119. (if msg (message msg cnt))
  13120. (goto-char beg)
  13121. (unless checkdir ; make sure the directory exists
  13122. (setq checkdir t)
  13123. (or (file-directory-p todir) (make-directory todir)))
  13124. (unless executables-checked
  13125. (org-check-external-command
  13126. "latex" "needed to convert LaTeX fragments to images")
  13127. (org-check-external-command
  13128. "dvipng" "needed to convert LaTeX fragments to images")
  13129. (setq executables-checked t))
  13130. (org-create-formula-image
  13131. txt movefile opt forbuffer)
  13132. (if overlays
  13133. (progn
  13134. (mapc (lambda (o)
  13135. (if (eq (org-overlay-get o 'org-overlay-type)
  13136. 'org-latex-overlay)
  13137. (org-delete-overlay o)))
  13138. (org-overlays-in beg end))
  13139. (setq ov (org-make-overlay beg end))
  13140. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13141. (if (featurep 'xemacs)
  13142. (progn
  13143. (org-overlay-put ov 'invisible t)
  13144. (org-overlay-put
  13145. ov 'end-glyph
  13146. (make-glyph (vector 'png :file movefile))))
  13147. (org-overlay-put
  13148. ov 'display
  13149. (list 'image :type 'png :file movefile :ascent 'center)))
  13150. (push ov org-latex-fragment-image-overlays)
  13151. (goto-char end))
  13152. (delete-region beg end)
  13153. (insert link))))))))
  13154. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  13155. ;; This function borrows from Ganesh Swami's latex2png.el
  13156. (defun org-create-formula-image (string tofile options buffer)
  13157. "This calls dvipng."
  13158. (require 'org-latex)
  13159. (let* ((tmpdir (if (featurep 'xemacs)
  13160. (temp-directory)
  13161. temporary-file-directory))
  13162. (texfilebase (make-temp-name
  13163. (expand-file-name "orgtex" tmpdir)))
  13164. (texfile (concat texfilebase ".tex"))
  13165. (dvifile (concat texfilebase ".dvi"))
  13166. (pngfile (concat texfilebase ".png"))
  13167. (fnh (if (featurep 'xemacs)
  13168. (font-height (get-face-font 'default))
  13169. (face-attribute 'default :height nil)))
  13170. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13171. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13172. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13173. "Black"))
  13174. (bg (or (plist-get options (if buffer :background :html-background))
  13175. "Transparent")))
  13176. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13177. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13178. (with-temp-file texfile
  13179. (insert org-format-latex-header
  13180. (if org-export-latex-packages-alist
  13181. (concat "\n"
  13182. (mapconcat (lambda(p)
  13183. (if (equal "" (car p))
  13184. (format "\\usepackage{%s}" (cadr p))
  13185. (format "\\usepackage[%s]{%s}"
  13186. (car p) (cadr p))))
  13187. org-export-latex-packages-alist "\n"))
  13188. "")
  13189. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13190. (let ((dir default-directory))
  13191. (condition-case nil
  13192. (progn
  13193. (cd tmpdir)
  13194. (call-process "latex" nil nil nil texfile))
  13195. (error nil))
  13196. (cd dir))
  13197. (if (not (file-exists-p dvifile))
  13198. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13199. (condition-case nil
  13200. (call-process "dvipng" nil nil nil
  13201. "-fg" fg "-bg" bg
  13202. "-D" dpi
  13203. ;;"-x" scale "-y" scale
  13204. "-T" "tight"
  13205. "-o" pngfile
  13206. dvifile)
  13207. (error nil))
  13208. (if (not (file-exists-p pngfile))
  13209. (progn (message "Failed to create png file from %s" texfile) nil)
  13210. ;; Use the requested file name and clean up
  13211. (copy-file pngfile tofile 'replace)
  13212. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13213. (delete-file (concat texfilebase e)))
  13214. pngfile))))
  13215. (defun org-dvipng-color (attr)
  13216. "Return an rgb color specification for dvipng."
  13217. (apply 'format "rgb %s %s %s"
  13218. (mapcar 'org-normalize-color
  13219. (color-values (face-attribute 'default attr nil)))))
  13220. (defun org-normalize-color (value)
  13221. "Return string to be used as color value for an RGB component."
  13222. (format "%g" (/ value 65535.0)))
  13223. ;;;; Key bindings
  13224. ;; Make `C-c C-x' a prefix key
  13225. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13226. ;; TAB key with modifiers
  13227. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13228. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13229. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13230. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13231. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13232. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13233. ;; The following line is necessary under Suse GNU/Linux
  13234. (unless (featurep 'xemacs)
  13235. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13236. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13237. (define-key org-mode-map [backtab] 'org-shifttab)
  13238. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13239. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13240. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13241. ;; Cursor keys with modifiers
  13242. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13243. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13244. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13245. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13246. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13247. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13248. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13249. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13250. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13251. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13252. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13253. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13254. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13255. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13256. ;;; Extra keys for tty access.
  13257. ;; We only set them when really needed because otherwise the
  13258. ;; menus don't show the simple keys
  13259. (when (or org-use-extra-keys
  13260. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13261. (not window-system))
  13262. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13263. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13264. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13265. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13266. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13267. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13268. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13269. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13270. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13271. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13272. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13273. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13274. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13275. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13276. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13277. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13278. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13279. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13280. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13281. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13282. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13283. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13284. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13285. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13286. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13287. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13288. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13289. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13290. ;; All the other keys
  13291. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13292. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13293. (if (boundp 'narrow-map)
  13294. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13295. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13296. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13297. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13298. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13299. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13300. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  13301. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13302. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13303. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13304. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13305. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13306. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13307. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13308. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13309. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13310. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13311. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13312. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13313. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13314. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13315. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13316. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13317. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13318. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13319. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13320. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13321. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13322. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13323. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13324. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13325. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13326. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13327. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13328. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13329. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13330. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13331. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13332. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13333. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13334. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13335. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13336. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13337. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13338. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13339. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13340. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13341. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13342. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13343. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13344. (org-defkey org-mode-map "\C-m" 'org-return)
  13345. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13346. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13347. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13348. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13349. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13350. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13351. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13352. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13353. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13354. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13355. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13356. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13357. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13358. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13359. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13360. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13361. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13362. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  13363. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  13364. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13365. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13366. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13367. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13368. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13369. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13370. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13371. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13372. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13373. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13374. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13375. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13376. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13377. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13378. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13379. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13380. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13381. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13382. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13383. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13384. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13385. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13386. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13387. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13388. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13389. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13390. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13391. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13392. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13393. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13394. (when (featurep 'xemacs)
  13395. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13396. (defconst org-speed-commands-default
  13397. '(
  13398. ("c" . org-cycle)
  13399. ("C" . org-shifttab)
  13400. ("n" . outline-next-visible-heading)
  13401. ("p" . outline-previous-visible-heading)
  13402. ("f" . org-forward-same-level)
  13403. ("b" . org-backward-same-level)
  13404. ("u" . outline-up-heading)
  13405. ("U" . org-shiftmetaup)
  13406. ("D" . org-shiftmetadown)
  13407. ("r" . org-metaright)
  13408. ("l" . org-metaleft)
  13409. ("R" . org-shiftmetaright)
  13410. ("L" . org-shiftmetaleft)
  13411. ("i" . (progn (forward-char 1) (call-interactively
  13412. 'org-insert-heading-respect-content)))
  13413. ("o" . org-open-at-point)
  13414. ("t" . org-todo)
  13415. ("j" . org-goto)
  13416. ("1" . (org-priority ?A))
  13417. ("2" . (org-priority ?B))
  13418. ("3" . (org-priority ?C))
  13419. ("." . outline-mark-subtree)
  13420. ("^" . org-sort)
  13421. ("w" . org-refile)
  13422. ("a" . org-archive-subtree-default-with-confirmation)
  13423. ("/" . org-sparse-tree)
  13424. ("?" . org-speed-command-help)
  13425. )
  13426. "The default speed commands.")
  13427. (defun org-print-speed-command (e)
  13428. (princ (car e))
  13429. (princ " ")
  13430. (if (symbolp (cdr e))
  13431. (princ (symbol-name (cdr e)))
  13432. (prin1 (cdr e)))
  13433. (princ "\n"))
  13434. (defun org-speed-command-help ()
  13435. "Show the available speed commands."
  13436. (interactive)
  13437. (if (not org-use-speed-commands)
  13438. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  13439. (with-output-to-temp-buffer "*Help*"
  13440. (princ "Speed commands\n==============\n")
  13441. (mapc 'org-print-speed-command org-speed-commands-user)
  13442. (princ "\n")
  13443. (mapc 'org-print-speed-command org-speed-commands-default))))
  13444. (defvar org-self-insert-command-undo-counter 0)
  13445. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13446. (defvar org-speed-command nil)
  13447. (defun org-self-insert-command (N)
  13448. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13449. If the cursor is in a table looking at whitespace, the whitespace is
  13450. overwritten, and the table is not marked as requiring realignment."
  13451. (interactive "p")
  13452. (cond
  13453. ((and org-use-speed-commands
  13454. (bolp)
  13455. (looking-at outline-regexp)
  13456. (setq
  13457. org-speed-command
  13458. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  13459. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  13460. (cond
  13461. ((commandp org-speed-command)
  13462. (setq this-command org-speed-command)
  13463. (call-interactively org-speed-command))
  13464. ((functionp org-speed-command)
  13465. (funcall org-speed-command))
  13466. ((and org-speed-command (listp org-speed-command))
  13467. (eval org-speed-command))
  13468. (t (let (org-use-speed-commands)
  13469. (call-interactively 'org-self-insert-command)))))
  13470. ((and
  13471. (org-table-p)
  13472. (progn
  13473. ;; check if we blank the field, and if that triggers align
  13474. (and (featurep 'org-table) org-table-auto-blank-field
  13475. (member last-command
  13476. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13477. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13478. ;; got extra space, this field does not determine column width
  13479. (let (org-table-may-need-update) (org-table-blank-field))
  13480. ;; no extra space, this field may determine column width
  13481. (org-table-blank-field)))
  13482. t)
  13483. (eq N 1)
  13484. (looking-at "[^|\n]* |"))
  13485. (let (org-table-may-need-update)
  13486. (goto-char (1- (match-end 0)))
  13487. (delete-backward-char 1)
  13488. (goto-char (match-beginning 0))
  13489. (self-insert-command N)))
  13490. (t
  13491. (setq org-table-may-need-update t)
  13492. (self-insert-command N)
  13493. (org-fix-tags-on-the-fly)
  13494. (if org-self-insert-cluster-for-undo
  13495. (if (not (eq last-command 'org-self-insert-command))
  13496. (setq org-self-insert-command-undo-counter 1)
  13497. (if (>= org-self-insert-command-undo-counter 20)
  13498. (setq org-self-insert-command-undo-counter 1)
  13499. (and (> org-self-insert-command-undo-counter 0)
  13500. buffer-undo-list
  13501. (not (cadr buffer-undo-list)) ; remove nil entry
  13502. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13503. (setq org-self-insert-command-undo-counter
  13504. (1+ org-self-insert-command-undo-counter))))))))
  13505. (defun org-fix-tags-on-the-fly ()
  13506. (when (and (equal (char-after (point-at-bol)) ?*)
  13507. (org-on-heading-p))
  13508. (org-align-tags-here org-tags-column)))
  13509. (defun org-delete-backward-char (N)
  13510. "Like `delete-backward-char', insert whitespace at field end in tables.
  13511. When deleting backwards, in tables this function will insert whitespace in
  13512. front of the next \"|\" separator, to keep the table aligned. The table will
  13513. still be marked for re-alignment if the field did fill the entire column,
  13514. because, in this case the deletion might narrow the column."
  13515. (interactive "p")
  13516. (if (and (org-table-p)
  13517. (eq N 1)
  13518. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13519. (looking-at ".*?|"))
  13520. (let ((pos (point))
  13521. (noalign (looking-at "[^|\n\r]* |"))
  13522. (c org-table-may-need-update))
  13523. (backward-delete-char N)
  13524. (skip-chars-forward "^|")
  13525. (insert " ")
  13526. (goto-char (1- pos))
  13527. ;; noalign: if there were two spaces at the end, this field
  13528. ;; does not determine the width of the column.
  13529. (if noalign (setq org-table-may-need-update c)))
  13530. (backward-delete-char N)
  13531. (org-fix-tags-on-the-fly)))
  13532. (defun org-delete-char (N)
  13533. "Like `delete-char', but insert whitespace at field end in tables.
  13534. When deleting characters, in tables this function will insert whitespace in
  13535. front of the next \"|\" separator, to keep the table aligned. The table will
  13536. still be marked for re-alignment if the field did fill the entire column,
  13537. because, in this case the deletion might narrow the column."
  13538. (interactive "p")
  13539. (if (and (org-table-p)
  13540. (not (bolp))
  13541. (not (= (char-after) ?|))
  13542. (eq N 1))
  13543. (if (looking-at ".*?|")
  13544. (let ((pos (point))
  13545. (noalign (looking-at "[^|\n\r]* |"))
  13546. (c org-table-may-need-update))
  13547. (replace-match (concat
  13548. (substring (match-string 0) 1 -1)
  13549. " |"))
  13550. (goto-char pos)
  13551. ;; noalign: if there were two spaces at the end, this field
  13552. ;; does not determine the width of the column.
  13553. (if noalign (setq org-table-may-need-update c)))
  13554. (delete-char N))
  13555. (delete-char N)
  13556. (org-fix-tags-on-the-fly)))
  13557. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13558. (put 'org-self-insert-command 'delete-selection t)
  13559. (put 'orgtbl-self-insert-command 'delete-selection t)
  13560. (put 'org-delete-char 'delete-selection 'supersede)
  13561. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13562. (put 'org-yank 'delete-selection 'yank)
  13563. ;; Make `flyspell-mode' delay after some commands
  13564. (put 'org-self-insert-command 'flyspell-delayed t)
  13565. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13566. (put 'org-delete-char 'flyspell-delayed t)
  13567. (put 'org-delete-backward-char 'flyspell-delayed t)
  13568. ;; Make pabbrev-mode expand after org-mode commands
  13569. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13570. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13571. ;; How to do this: Measure non-white length of current string
  13572. ;; If equal to column width, we should realign.
  13573. (defun org-remap (map &rest commands)
  13574. "In MAP, remap the functions given in COMMANDS.
  13575. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13576. (let (new old)
  13577. (while commands
  13578. (setq old (pop commands) new (pop commands))
  13579. (if (fboundp 'command-remapping)
  13580. (org-defkey map (vector 'remap old) new)
  13581. (substitute-key-definition old new map global-map)))))
  13582. (when (eq org-enable-table-editor 'optimized)
  13583. ;; If the user wants maximum table support, we need to hijack
  13584. ;; some standard editing functions
  13585. (org-remap org-mode-map
  13586. 'self-insert-command 'org-self-insert-command
  13587. 'delete-char 'org-delete-char
  13588. 'delete-backward-char 'org-delete-backward-char)
  13589. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13590. (defvar org-ctrl-c-ctrl-c-hook nil
  13591. "Hook for functions attaching themselves to `C-c C-c'.
  13592. This can be used to add additional functionality to the C-c C-c key which
  13593. executes context-dependent commands.
  13594. Each function will be called with no arguments. The function must check
  13595. if the context is appropriate for it to act. If yes, it should do its
  13596. thing and then return a non-nil value. If the context is wrong,
  13597. just do nothing and return nil.")
  13598. (defvar org-tab-first-hook nil
  13599. "Hook for functions to attach themselves to TAB.
  13600. See `org-ctrl-c-ctrl-c-hook' for more information.
  13601. This hook runs as the first action when TAB is pressed, even before
  13602. `org-cycle' messes around with the `outline-regexp' to cater for
  13603. inline tasks and plain list item folding.
  13604. If any function in this hook returns t, not other actions like table
  13605. field motion visibility cycling will be done.")
  13606. (defvar org-tab-after-check-for-table-hook nil
  13607. "Hook for functions to attach themselves to TAB.
  13608. See `org-ctrl-c-ctrl-c-hook' for more information.
  13609. This hook runs after it has been established that the cursor is not in a
  13610. table, but before checking if the cursor is in a headline or if global cycling
  13611. should be done.
  13612. If any function in this hook returns t, not other actions like visibility
  13613. cycling will be done.")
  13614. (defvar org-tab-after-check-for-cycling-hook nil
  13615. "Hook for functions to attach themselves to TAB.
  13616. See `org-ctrl-c-ctrl-c-hook' for more information.
  13617. This hook runs after it has been established that not table field motion and
  13618. not visibility should be done because of current context. This is probably
  13619. the place where a package like yasnippets can hook in.")
  13620. (defvar org-metaleft-hook nil
  13621. "Hook for functions attaching themselves to `M-left'.
  13622. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13623. (defvar org-metaright-hook nil
  13624. "Hook for functions attaching themselves to `M-right'.
  13625. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13626. (defvar org-metaup-hook nil
  13627. "Hook for functions attaching themselves to `M-up'.
  13628. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13629. (defvar org-metadown-hook nil
  13630. "Hook for functions attaching themselves to `M-down'.
  13631. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13632. (defvar org-shiftmetaleft-hook nil
  13633. "Hook for functions attaching themselves to `M-S-left'.
  13634. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13635. (defvar org-shiftmetaright-hook nil
  13636. "Hook for functions attaching themselves to `M-S-right'.
  13637. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13638. (defvar org-shiftmetaup-hook nil
  13639. "Hook for functions attaching themselves to `M-S-up'.
  13640. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13641. (defvar org-shiftmetadown-hook nil
  13642. "Hook for functions attaching themselves to `M-S-down'.
  13643. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13644. (defvar org-metareturn-hook nil
  13645. "Hook for functions attaching themselves to `M-RET'.
  13646. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13647. (defun org-modifier-cursor-error ()
  13648. "Throw an error, a modified cursor command was applied in wrong context."
  13649. (error "This command is active in special context like tables, headlines or items"))
  13650. (defun org-shiftselect-error ()
  13651. "Throw an error because Shift-Cursor command was applied in wrong context."
  13652. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13653. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  13654. (error "This command works only in special context like headlines or timestamps")))
  13655. (defun org-call-for-shift-select (cmd)
  13656. (let ((this-command-keys-shift-translated t))
  13657. (call-interactively cmd)))
  13658. (defun org-shifttab (&optional arg)
  13659. "Global visibility cycling or move to previous table field.
  13660. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13661. on context.
  13662. See the individual commands for more information."
  13663. (interactive "P")
  13664. (cond
  13665. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13666. ((integerp arg)
  13667. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  13668. (message "Content view to level: %d" arg)
  13669. (org-content (prefix-numeric-value arg2))
  13670. (setq org-cycle-global-status 'overview)))
  13671. (t (call-interactively 'org-global-cycle))))
  13672. (defun org-shiftmetaleft ()
  13673. "Promote subtree or delete table column.
  13674. Calls `org-promote-subtree', `org-outdent-item',
  13675. or `org-table-delete-column', depending on context.
  13676. See the individual commands for more information."
  13677. (interactive)
  13678. (cond
  13679. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13680. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13681. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13682. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13683. (t (org-modifier-cursor-error))))
  13684. (defun org-shiftmetaright ()
  13685. "Demote subtree or insert table column.
  13686. Calls `org-demote-subtree', `org-indent-item',
  13687. or `org-table-insert-column', depending on context.
  13688. See the individual commands for more information."
  13689. (interactive)
  13690. (cond
  13691. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13692. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13693. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13694. ((org-at-item-p) (call-interactively 'org-indent-item))
  13695. (t (org-modifier-cursor-error))))
  13696. (defun org-shiftmetaup (&optional arg)
  13697. "Move subtree up or kill table row.
  13698. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13699. `org-move-item-up' depending on context. See the individual commands
  13700. for more information."
  13701. (interactive "P")
  13702. (cond
  13703. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13704. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13705. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13706. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13707. (t (org-modifier-cursor-error))))
  13708. (defun org-shiftmetadown (&optional arg)
  13709. "Move subtree down or insert table row.
  13710. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13711. `org-move-item-down', depending on context. See the individual
  13712. commands for more information."
  13713. (interactive "P")
  13714. (cond
  13715. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13716. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13717. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13718. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13719. (t (org-modifier-cursor-error))))
  13720. (defun org-metaleft (&optional arg)
  13721. "Promote heading or move table column to left.
  13722. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13723. With no specific context, calls the Emacs default `backward-word'.
  13724. See the individual commands for more information."
  13725. (interactive "P")
  13726. (cond
  13727. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13728. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13729. ((or (org-on-heading-p)
  13730. (and (org-region-active-p)
  13731. (save-excursion
  13732. (goto-char (region-beginning))
  13733. (org-on-heading-p))))
  13734. (call-interactively 'org-do-promote))
  13735. ((or (org-at-item-p)
  13736. (and (org-region-active-p)
  13737. (save-excursion
  13738. (goto-char (region-beginning))
  13739. (org-at-item-p))))
  13740. (call-interactively 'org-outdent-item))
  13741. (t (call-interactively 'backward-word))))
  13742. (defun org-metaright (&optional arg)
  13743. "Demote subtree or move table column to right.
  13744. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13745. With no specific context, calls the Emacs default `forward-word'.
  13746. See the individual commands for more information."
  13747. (interactive "P")
  13748. (cond
  13749. ((run-hook-with-args-until-success 'org-metaright-hook))
  13750. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13751. ((or (org-on-heading-p)
  13752. (and (org-region-active-p)
  13753. (save-excursion
  13754. (goto-char (region-beginning))
  13755. (org-on-heading-p))))
  13756. (call-interactively 'org-do-demote))
  13757. ((or (org-at-item-p)
  13758. (and (org-region-active-p)
  13759. (save-excursion
  13760. (goto-char (region-beginning))
  13761. (org-at-item-p))))
  13762. (call-interactively 'org-indent-item))
  13763. (t (call-interactively 'forward-word))))
  13764. (defun org-metaup (&optional arg)
  13765. "Move subtree up or move table row up.
  13766. Calls `org-move-subtree-up' or `org-table-move-row' or
  13767. `org-move-item-up', depending on context. See the individual commands
  13768. for more information."
  13769. (interactive "P")
  13770. (cond
  13771. ((run-hook-with-args-until-success 'org-metaup-hook))
  13772. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13773. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13774. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13775. (t (transpose-lines 1) (beginning-of-line -1))))
  13776. (defun org-metadown (&optional arg)
  13777. "Move subtree down or move table row down.
  13778. Calls `org-move-subtree-down' or `org-table-move-row' or
  13779. `org-move-item-down', depending on context. See the individual
  13780. commands for more information."
  13781. (interactive "P")
  13782. (cond
  13783. ((run-hook-with-args-until-success 'org-metadown-hook))
  13784. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13785. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13786. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13787. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13788. (defun org-shiftup (&optional arg)
  13789. "Increase item in timestamp or increase priority of current headline.
  13790. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13791. depending on context. See the individual commands for more information."
  13792. (interactive "P")
  13793. (cond
  13794. ((and org-support-shift-select (org-region-active-p))
  13795. (org-call-for-shift-select 'previous-line))
  13796. ((org-at-timestamp-p t)
  13797. (call-interactively (if org-edit-timestamp-down-means-later
  13798. 'org-timestamp-down 'org-timestamp-up)))
  13799. ((and (not (eq org-support-shift-select 'always))
  13800. org-enable-priority-commands
  13801. (org-on-heading-p))
  13802. (call-interactively 'org-priority-up))
  13803. ((and (not org-support-shift-select) (org-at-item-p))
  13804. (call-interactively 'org-previous-item))
  13805. ((org-clocktable-try-shift 'up arg))
  13806. (org-support-shift-select
  13807. (org-call-for-shift-select 'previous-line))
  13808. (t (org-shiftselect-error))))
  13809. (defun org-shiftdown (&optional arg)
  13810. "Decrease item in timestamp or decrease priority of current headline.
  13811. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13812. depending on context. See the individual commands for more information."
  13813. (interactive "P")
  13814. (cond
  13815. ((and org-support-shift-select (org-region-active-p))
  13816. (org-call-for-shift-select 'next-line))
  13817. ((org-at-timestamp-p t)
  13818. (call-interactively (if org-edit-timestamp-down-means-later
  13819. 'org-timestamp-up 'org-timestamp-down)))
  13820. ((and (not (eq org-support-shift-select 'always))
  13821. org-enable-priority-commands
  13822. (org-on-heading-p))
  13823. (call-interactively 'org-priority-down))
  13824. ((and (not org-support-shift-select) (org-at-item-p))
  13825. (call-interactively 'org-next-item))
  13826. ((org-clocktable-try-shift 'down arg))
  13827. (org-support-shift-select
  13828. (org-call-for-shift-select 'next-line))
  13829. (t (org-shiftselect-error))))
  13830. (defun org-shiftright (&optional arg)
  13831. "Cycle the thing at point or in the current line, depending on context.
  13832. Depending on context, this does one of the following:
  13833. - switch a timestamp at point one day into the future
  13834. - on a headline, switch to the next TODO keyword.
  13835. - on an item, switch entire list to the next bullet type
  13836. - on a property line, switch to the next allowed value
  13837. - on a clocktable definition line, move time block into the future"
  13838. (interactive "P")
  13839. (cond
  13840. ((and org-support-shift-select (org-region-active-p))
  13841. (org-call-for-shift-select 'forward-char))
  13842. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13843. ((and (not (eq org-support-shift-select 'always))
  13844. (org-on-heading-p))
  13845. (let ((org-inhibit-logging
  13846. (not org-treat-S-cursor-todo-selection-as-state-change))
  13847. (org-inhibit-blocking
  13848. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13849. (org-call-with-arg 'org-todo 'right)))
  13850. ((or (and org-support-shift-select
  13851. (not (eq org-support-shift-select 'always))
  13852. (org-at-item-bullet-p))
  13853. (and (not org-support-shift-select) (org-at-item-p)))
  13854. (org-call-with-arg 'org-cycle-list-bullet nil))
  13855. ((and (not (eq org-support-shift-select 'always))
  13856. (org-at-property-p))
  13857. (call-interactively 'org-property-next-allowed-value))
  13858. ((org-clocktable-try-shift 'right arg))
  13859. (org-support-shift-select
  13860. (org-call-for-shift-select 'forward-char))
  13861. (t (org-shiftselect-error))))
  13862. (defun org-shiftleft (&optional arg)
  13863. "Cycle the thing at point or in the current line, depending on context.
  13864. Depending on context, this does one of the following:
  13865. - switch a timestamp at point one day into the past
  13866. - on a headline, switch to the previous TODO keyword.
  13867. - on an item, switch entire list to the previous bullet type
  13868. - on a property line, switch to the previous allowed value
  13869. - on a clocktable definition line, move time block into the past"
  13870. (interactive "P")
  13871. (cond
  13872. ((and org-support-shift-select (org-region-active-p))
  13873. (org-call-for-shift-select 'backward-char))
  13874. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13875. ((and (not (eq org-support-shift-select 'always))
  13876. (org-on-heading-p))
  13877. (let ((org-inhibit-logging
  13878. (not org-treat-S-cursor-todo-selection-as-state-change))
  13879. (org-inhibit-blocking
  13880. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13881. (org-call-with-arg 'org-todo 'left)))
  13882. ((or (and org-support-shift-select
  13883. (not (eq org-support-shift-select 'always))
  13884. (org-at-item-bullet-p))
  13885. (and (not org-support-shift-select) (org-at-item-p)))
  13886. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13887. ((and (not (eq org-support-shift-select 'always))
  13888. (org-at-property-p))
  13889. (call-interactively 'org-property-previous-allowed-value))
  13890. ((org-clocktable-try-shift 'left arg))
  13891. (org-support-shift-select
  13892. (org-call-for-shift-select 'backward-char))
  13893. (t (org-shiftselect-error))))
  13894. (defun org-shiftcontrolright ()
  13895. "Switch to next TODO set."
  13896. (interactive)
  13897. (cond
  13898. ((and org-support-shift-select (org-region-active-p))
  13899. (org-call-for-shift-select 'forward-word))
  13900. ((and (not (eq org-support-shift-select 'always))
  13901. (org-on-heading-p))
  13902. (org-call-with-arg 'org-todo 'nextset))
  13903. (org-support-shift-select
  13904. (org-call-for-shift-select 'forward-word))
  13905. (t (org-shiftselect-error))))
  13906. (defun org-shiftcontrolleft ()
  13907. "Switch to previous TODO set."
  13908. (interactive)
  13909. (cond
  13910. ((and org-support-shift-select (org-region-active-p))
  13911. (org-call-for-shift-select 'backward-word))
  13912. ((and (not (eq org-support-shift-select 'always))
  13913. (org-on-heading-p))
  13914. (org-call-with-arg 'org-todo 'previousset))
  13915. (org-support-shift-select
  13916. (org-call-for-shift-select 'backward-word))
  13917. (t (org-shiftselect-error))))
  13918. (defun org-ctrl-c-ret ()
  13919. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13920. (interactive)
  13921. (cond
  13922. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13923. (t (call-interactively 'org-insert-heading))))
  13924. (defun org-copy-special ()
  13925. "Copy region in table or copy current subtree.
  13926. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13927. See the individual commands for more information."
  13928. (interactive)
  13929. (call-interactively
  13930. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13931. (defun org-cut-special ()
  13932. "Cut region in table or cut current subtree.
  13933. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13934. See the individual commands for more information."
  13935. (interactive)
  13936. (call-interactively
  13937. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13938. (defun org-paste-special (arg)
  13939. "Paste rectangular region into table, or past subtree relative to level.
  13940. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13941. See the individual commands for more information."
  13942. (interactive "P")
  13943. (if (org-at-table-p)
  13944. (org-table-paste-rectangle)
  13945. (org-paste-subtree arg)))
  13946. (defun org-edit-special ()
  13947. "Call a special editor for the stuff at point.
  13948. When at a table, call the formula editor with `org-table-edit-formulas'.
  13949. When at the first line of an src example, call `org-edit-src-code'.
  13950. When in an #+include line, visit the include file. Otherwise call
  13951. `ffap' to visit the file at point."
  13952. (interactive)
  13953. (cond
  13954. ((org-at-table-p)
  13955. (call-interactively 'org-table-edit-formulas))
  13956. ((save-excursion
  13957. (beginning-of-line 1)
  13958. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13959. (find-file (org-trim (match-string 1))))
  13960. ((org-edit-src-code))
  13961. ((org-edit-fixed-width-region))
  13962. (t (call-interactively 'ffap))))
  13963. (defun org-ctrl-c-ctrl-c (&optional arg)
  13964. "Set tags in headline, or update according to changed information at point.
  13965. This command does many different things, depending on context:
  13966. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13967. this is what we do.
  13968. - If the cursor is on a statistics cookie, update it.
  13969. - If the cursor is in a headline, prompt for tags and insert them
  13970. into the current line, aligned to `org-tags-column'. When called
  13971. with prefix arg, realign all tags in the current buffer.
  13972. - If the cursor is in one of the special #+KEYWORD lines, this
  13973. triggers scanning the buffer for these lines and updating the
  13974. information.
  13975. - If the cursor is inside a table, realign the table. This command
  13976. works even if the automatic table editor has been turned off.
  13977. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13978. the entire table.
  13979. - If the cursor is at a footnote reference or definition, jump to
  13980. the corresponding definition or references, respectively.
  13981. - If the cursor is a the beginning of a dynamic block, update it.
  13982. - If the cursor is inside a table created by the table.el package,
  13983. activate that table.
  13984. - If the current buffer is a remember buffer, close note and file
  13985. it. A prefix argument of 1 files to the default location
  13986. without further interaction. A prefix argument of 2 files to
  13987. the currently clocking task.
  13988. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13989. links in this buffer.
  13990. - If the cursor is on a numbered item in a plain list, renumber the
  13991. ordered list.
  13992. - If the cursor is on a checkbox, toggle it."
  13993. (interactive "P")
  13994. (let ((org-enable-table-editor t))
  13995. (cond
  13996. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13997. org-occur-highlights
  13998. org-latex-fragment-image-overlays)
  13999. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14000. (org-remove-occur-highlights)
  14001. (org-remove-latex-fragment-image-overlays)
  14002. (message "Temporary highlights/overlays removed from current buffer"))
  14003. ((and (local-variable-p 'org-finish-function (current-buffer))
  14004. (fboundp org-finish-function))
  14005. (funcall org-finish-function))
  14006. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14007. ((org-at-property-p)
  14008. (call-interactively 'org-property-action))
  14009. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14010. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14011. (or (org-on-heading-p) (org-at-item-p)))
  14012. (call-interactively 'org-update-statistics-cookies))
  14013. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14014. ((org-at-table.el-p)
  14015. (require 'table)
  14016. (beginning-of-line 1)
  14017. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14018. (call-interactively 'table-recognize-table))
  14019. ((org-at-table-p)
  14020. (org-table-maybe-eval-formula)
  14021. (if arg
  14022. (call-interactively 'org-table-recalculate)
  14023. (org-table-maybe-recalculate-line))
  14024. (call-interactively 'org-table-align))
  14025. ((or (org-footnote-at-reference-p)
  14026. (org-footnote-at-definition-p))
  14027. (call-interactively 'org-footnote-action))
  14028. ((org-at-item-checkbox-p)
  14029. (call-interactively 'org-toggle-checkbox))
  14030. ((org-at-item-p)
  14031. (if arg
  14032. (call-interactively 'org-toggle-checkbox)
  14033. (call-interactively 'org-maybe-renumber-ordered-list)))
  14034. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14035. ;; Dynamic block
  14036. (beginning-of-line 1)
  14037. (save-excursion (org-update-dblock)))
  14038. ((save-excursion
  14039. (beginning-of-line 1)
  14040. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14041. (cond
  14042. ((equal (match-string 1) "TBLFM")
  14043. ;; Recalculate the table before this line
  14044. (save-excursion
  14045. (beginning-of-line 1)
  14046. (skip-chars-backward " \r\n\t")
  14047. (if (org-at-table-p)
  14048. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14049. (t
  14050. ; (org-set-regexps-and-options)
  14051. ; (org-restart-font-lock)
  14052. (let ((org-inhibit-startup t)) (org-mode-restart))
  14053. (message "Local setup has been refreshed"))))
  14054. ((org-clock-update-time-maybe))
  14055. (t (error "C-c C-c can do nothing useful at this location")))))
  14056. (defun org-mode-restart ()
  14057. "Restart Org-mode, to scan again for special lines.
  14058. Also updates the keyword regular expressions."
  14059. (interactive)
  14060. (org-mode)
  14061. (message "Org-mode restarted"))
  14062. (defun org-kill-note-or-show-branches ()
  14063. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14064. (interactive)
  14065. (if (not org-finish-function)
  14066. (call-interactively 'show-branches)
  14067. (let ((org-note-abort t))
  14068. (funcall org-finish-function))))
  14069. (defun org-return (&optional indent)
  14070. "Goto next table row or insert a newline.
  14071. Calls `org-table-next-row' or `newline', depending on context.
  14072. See the individual commands for more information."
  14073. (interactive)
  14074. (cond
  14075. ((bobp) (if indent (newline-and-indent) (newline)))
  14076. ((org-at-table-p)
  14077. (org-table-justify-field-maybe)
  14078. (call-interactively 'org-table-next-row))
  14079. ((and org-return-follows-link
  14080. (eq (get-text-property (point) 'face) 'org-link))
  14081. (call-interactively 'org-open-at-point))
  14082. ((and (org-at-heading-p)
  14083. (looking-at
  14084. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14085. (org-show-entry)
  14086. (end-of-line 1)
  14087. (newline))
  14088. (t (if indent (newline-and-indent) (newline)))))
  14089. (defun org-return-indent ()
  14090. "Goto next table row or insert a newline and indent.
  14091. Calls `org-table-next-row' or `newline-and-indent', depending on
  14092. context. See the individual commands for more information."
  14093. (interactive)
  14094. (org-return t))
  14095. (defun org-ctrl-c-star ()
  14096. "Compute table, or change heading status of lines.
  14097. Calls `org-table-recalculate' or `org-toggle-heading',
  14098. depending on context."
  14099. (interactive)
  14100. (cond
  14101. ((org-at-table-p)
  14102. (call-interactively 'org-table-recalculate))
  14103. (t
  14104. ;; Convert all lines in region to list items
  14105. (call-interactively 'org-toggle-heading))))
  14106. (defun org-ctrl-c-minus ()
  14107. "Insert separator line in table or modify bullet status of line.
  14108. Also turns a plain line or a region of lines into list items.
  14109. Calls `org-table-insert-hline', `org-toggle-item', or
  14110. `org-cycle-list-bullet', depending on context."
  14111. (interactive)
  14112. (cond
  14113. ((org-at-table-p)
  14114. (call-interactively 'org-table-insert-hline))
  14115. ((org-region-active-p)
  14116. (call-interactively 'org-toggle-item))
  14117. ((org-in-item-p)
  14118. (call-interactively 'org-cycle-list-bullet))
  14119. (t
  14120. (call-interactively 'org-toggle-item))))
  14121. (defun org-toggle-item ()
  14122. "Convert headings or normal lines to items, items to normal lines.
  14123. If there is no active region, only the current line is considered.
  14124. If the first line in the region is a headline, convert all headlines to items.
  14125. If the first line in the region is an item, convert all items to normal lines.
  14126. If the first line is normal text, add an item bullet to each line."
  14127. (interactive)
  14128. (let (l2 l beg end)
  14129. (if (org-region-active-p)
  14130. (setq beg (region-beginning) end (region-end))
  14131. (setq beg (point-at-bol)
  14132. end (min (1+ (point-at-eol)) (point-max))))
  14133. (save-excursion
  14134. (goto-char end)
  14135. (setq l2 (org-current-line))
  14136. (goto-char beg)
  14137. (beginning-of-line 1)
  14138. (setq l (1- (org-current-line)))
  14139. (if (org-at-item-p)
  14140. ;; We already have items, de-itemize
  14141. (while (< (setq l (1+ l)) l2)
  14142. (when (org-at-item-p)
  14143. (goto-char (match-beginning 2))
  14144. (delete-region (match-beginning 2) (match-end 2))
  14145. (and (looking-at "[ \t]+") (replace-match "")))
  14146. (beginning-of-line 2))
  14147. (if (org-on-heading-p)
  14148. ;; Headings, convert to items
  14149. (while (< (setq l (1+ l)) l2)
  14150. (if (looking-at org-outline-regexp)
  14151. (replace-match "- " t t))
  14152. (beginning-of-line 2))
  14153. ;; normal lines, turn them into items
  14154. (while (< (setq l (1+ l)) l2)
  14155. (unless (org-at-item-p)
  14156. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14157. (replace-match "\\1- \\2")))
  14158. (beginning-of-line 2)))))))
  14159. (defun org-toggle-heading (&optional nstars)
  14160. "Convert headings to normal text, or items or text to headings.
  14161. If there is no active region, only the current line is considered.
  14162. If the first line is a heading, remove the stars from all headlines
  14163. in the region.
  14164. If the first line is a plain list item, turn all plain list items
  14165. into headings.
  14166. If the first line is a normal line, turn each and every line in the
  14167. region into a heading.
  14168. When converting a line into a heading, the number of stars is chosen
  14169. such that the lines become children of the current entry. However,
  14170. when a prefix argument is given, its value determines the number of
  14171. stars to add."
  14172. (interactive "P")
  14173. (let (l2 l itemp beg end)
  14174. (if (org-region-active-p)
  14175. (setq beg (region-beginning) end (region-end))
  14176. (setq beg (point-at-bol)
  14177. end (min (1+ (point-at-eol)) (point-max))))
  14178. (save-excursion
  14179. (goto-char end)
  14180. (setq l2 (org-current-line))
  14181. (goto-char beg)
  14182. (beginning-of-line 1)
  14183. (setq l (1- (org-current-line)))
  14184. (if (org-on-heading-p)
  14185. ;; We already have headlines, de-star them
  14186. (while (< (setq l (1+ l)) l2)
  14187. (when (org-on-heading-p t)
  14188. (and (looking-at outline-regexp) (replace-match "")))
  14189. (beginning-of-line 2))
  14190. (setq itemp (org-at-item-p))
  14191. (let* ((stars
  14192. (if nstars
  14193. (make-string (prefix-numeric-value current-prefix-arg)
  14194. ?*)
  14195. (save-excursion
  14196. (if (re-search-backward org-complex-heading-regexp nil t)
  14197. (match-string 1) ""))))
  14198. (add-stars (cond (nstars "")
  14199. ((equal stars "") "*")
  14200. (org-odd-levels-only "**")
  14201. (t "*")))
  14202. (rpl (concat stars add-stars " ")))
  14203. (while (< (setq l (1+ l)) l2)
  14204. (if itemp
  14205. (and (org-at-item-p) (replace-match rpl t t))
  14206. (unless (org-on-heading-p)
  14207. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14208. (replace-match (concat rpl (match-string 2))))))
  14209. (beginning-of-line 2)))))))
  14210. (defun org-meta-return (&optional arg)
  14211. "Insert a new heading or wrap a region in a table.
  14212. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14213. See the individual commands for more information."
  14214. (interactive "P")
  14215. (cond
  14216. ((run-hook-with-args-until-success 'org-metareturn-hook))
  14217. ((org-at-table-p)
  14218. (call-interactively 'org-table-wrap-region))
  14219. (t (call-interactively 'org-insert-heading))))
  14220. ;;; Menu entries
  14221. ;; Define the Org-mode menus
  14222. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14223. '("Tbl"
  14224. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  14225. ["Next Field" org-cycle (org-at-table-p)]
  14226. ["Previous Field" org-shifttab (org-at-table-p)]
  14227. ["Next Row" org-return (org-at-table-p)]
  14228. "--"
  14229. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14230. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14231. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14232. "--"
  14233. ("Column"
  14234. ["Move Column Left" org-metaleft (org-at-table-p)]
  14235. ["Move Column Right" org-metaright (org-at-table-p)]
  14236. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14237. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  14238. ("Row"
  14239. ["Move Row Up" org-metaup (org-at-table-p)]
  14240. ["Move Row Down" org-metadown (org-at-table-p)]
  14241. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14242. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14243. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14244. "--"
  14245. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  14246. ("Rectangle"
  14247. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14248. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14249. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14250. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14251. "--"
  14252. ("Calculate"
  14253. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14254. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14255. ["Edit Formulas" org-edit-special (org-at-table-p)]
  14256. "--"
  14257. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14258. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14259. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  14260. "--"
  14261. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14262. "--"
  14263. ["Sum Column/Rectangle" org-table-sum
  14264. (or (org-at-table-p) (org-region-active-p))]
  14265. ["Which Column?" org-table-current-column (org-at-table-p)])
  14266. ["Debug Formulas"
  14267. org-table-toggle-formula-debugger
  14268. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  14269. ["Show Col/Row Numbers"
  14270. org-table-toggle-coordinate-overlays
  14271. :style toggle
  14272. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14273. "--"
  14274. ["Create" org-table-create (and (not (org-at-table-p))
  14275. org-enable-table-editor)]
  14276. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14277. ["Import from File" org-table-import (not (org-at-table-p))]
  14278. ["Export to File" org-table-export (org-at-table-p)]
  14279. "--"
  14280. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14281. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14282. '("Org"
  14283. ("Show/Hide"
  14284. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14285. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14286. ["Sparse Tree..." org-sparse-tree t]
  14287. ["Reveal Context" org-reveal t]
  14288. ["Show All" show-all t]
  14289. "--"
  14290. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14291. "--"
  14292. ["New Heading" org-insert-heading t]
  14293. ("Navigate Headings"
  14294. ["Up" outline-up-heading t]
  14295. ["Next" outline-next-visible-heading t]
  14296. ["Previous" outline-previous-visible-heading t]
  14297. ["Next Same Level" outline-forward-same-level t]
  14298. ["Previous Same Level" outline-backward-same-level t]
  14299. "--"
  14300. ["Jump" org-goto t])
  14301. ("Edit Structure"
  14302. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14303. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14304. "--"
  14305. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14306. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14307. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14308. "--"
  14309. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14310. "--"
  14311. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14312. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14313. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14314. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14315. "--"
  14316. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14317. "--"
  14318. ["Convert to odd levels" org-convert-to-odd-levels t]
  14319. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14320. ("Editing"
  14321. ["Emphasis..." org-emphasize t]
  14322. ["Edit Source Example" org-edit-special t]
  14323. "--"
  14324. ["Footnote new/jump" org-footnote-action t]
  14325. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14326. ("Archive"
  14327. ["Archive (default method)" org-archive-subtree-default t]
  14328. "--"
  14329. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  14330. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14331. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  14332. )
  14333. "--"
  14334. ("Hyperlinks"
  14335. ["Store Link (Global)" org-store-link t]
  14336. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14337. ["Insert Link" org-insert-link t]
  14338. ["Follow Link" org-open-at-point t]
  14339. "--"
  14340. ["Next link" org-next-link t]
  14341. ["Previous link" org-previous-link t]
  14342. "--"
  14343. ["Descriptive Links"
  14344. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14345. :style radio
  14346. :selected (member '(org-link) buffer-invisibility-spec)]
  14347. ["Literal Links"
  14348. (progn
  14349. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14350. :style radio
  14351. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14352. "--"
  14353. ("TODO Lists"
  14354. ["TODO/DONE/-" org-todo t]
  14355. ("Select keyword"
  14356. ["Next keyword" org-shiftright (org-on-heading-p)]
  14357. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14358. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14359. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14360. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14361. ["Show TODO Tree" org-show-todo-tree t]
  14362. ["Global TODO list" org-todo-list t]
  14363. "--"
  14364. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14365. :selected org-enforce-todo-dependencies :style toggle :active t]
  14366. "Settings for tree at point"
  14367. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14368. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14369. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14370. ["Do Children parallel" org-toggle-ordered-property :style radio
  14371. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14372. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14373. "--"
  14374. ["Set Priority" org-priority t]
  14375. ["Priority Up" org-shiftup t]
  14376. ["Priority Down" org-shiftdown t]
  14377. "--"
  14378. ["Get news from all feeds" org-feed-update-all t]
  14379. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14380. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14381. ("TAGS and Properties"
  14382. ["Set Tags" org-set-tags-command t]
  14383. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14384. "--"
  14385. ["Set property" org-set-property t]
  14386. ["Column view of properties" org-columns t]
  14387. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14388. ("Dates and Scheduling"
  14389. ["Timestamp" org-time-stamp t]
  14390. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14391. ("Change Date"
  14392. ["1 Day Later" org-shiftright t]
  14393. ["1 Day Earlier" org-shiftleft t]
  14394. ["1 ... Later" org-shiftup t]
  14395. ["1 ... Earlier" org-shiftdown t])
  14396. ["Compute Time Range" org-evaluate-time-range t]
  14397. ["Schedule Item" org-schedule t]
  14398. ["Deadline" org-deadline t]
  14399. "--"
  14400. ["Custom time format" org-toggle-time-stamp-overlays
  14401. :style radio :selected org-display-custom-times]
  14402. "--"
  14403. ["Goto Calendar" org-goto-calendar t]
  14404. ["Date from Calendar" org-date-from-calendar t]
  14405. "--"
  14406. ["Start/Restart Timer" org-timer-start t]
  14407. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14408. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14409. ["Insert Timer String" org-timer t]
  14410. ["Insert Timer Item" org-timer-item t])
  14411. ("Logging work"
  14412. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14413. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14414. ["Clock out" org-clock-out t]
  14415. ["Clock cancel" org-clock-cancel t]
  14416. "--"
  14417. ["Mark as default task" org-clock-mark-default-task t]
  14418. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14419. ["Goto running clock" org-clock-goto t]
  14420. "--"
  14421. ["Display times" org-clock-display t]
  14422. ["Create clock table" org-clock-report t]
  14423. "--"
  14424. ["Record DONE time"
  14425. (progn (setq org-log-done (not org-log-done))
  14426. (message "Switching to %s will %s record a timestamp"
  14427. (car org-done-keywords)
  14428. (if org-log-done "automatically" "not")))
  14429. :style toggle :selected org-log-done])
  14430. "--"
  14431. ["Agenda Command..." org-agenda t]
  14432. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14433. ("File List for Agenda")
  14434. ("Special views current file"
  14435. ["TODO Tree" org-show-todo-tree t]
  14436. ["Check Deadlines" org-check-deadlines t]
  14437. ["Timeline" org-timeline t]
  14438. ["Tags/Property tree" org-match-sparse-tree t])
  14439. "--"
  14440. ["Export/Publish..." org-export t]
  14441. ("LaTeX"
  14442. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14443. :selected org-cdlatex-mode]
  14444. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14445. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14446. ["Modify math symbol" org-cdlatex-math-modify
  14447. (org-inside-LaTeX-fragment-p)]
  14448. ["Insert citation" org-reftex-citation t]
  14449. "--"
  14450. ["Export LaTeX fragments as images"
  14451. (if (featurep 'org-exp)
  14452. (setq org-export-with-LaTeX-fragments
  14453. (not org-export-with-LaTeX-fragments))
  14454. (require 'org-exp))
  14455. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14456. org-export-with-LaTeX-fragments)])
  14457. "--"
  14458. ("MobileOrg"
  14459. ["Push Files and Views" org-mobile-push t]
  14460. ["Get Captured and Flagged" org-mobile-pull t]
  14461. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  14462. "--"
  14463. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  14464. "--"
  14465. ("Documentation"
  14466. ["Show Version" org-version t]
  14467. ["Info Documentation" org-info t])
  14468. ("Customize"
  14469. ["Browse Org Group" org-customize t]
  14470. "--"
  14471. ["Expand This Menu" org-create-customize-menu
  14472. (fboundp 'customize-menu-create)])
  14473. ["Send bug report" org-submit-bug-report t]
  14474. "--"
  14475. ("Refresh/Reload"
  14476. ["Refresh setup current buffer" org-mode-restart t]
  14477. ["Reload Org (after update)" org-reload t]
  14478. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14479. ))
  14480. (defun org-info (&optional node)
  14481. "Read documentation for Org-mode in the info system.
  14482. With optional NODE, go directly to that node."
  14483. (interactive)
  14484. (info (format "(org)%s" (or node ""))))
  14485. ;;;###autoload
  14486. (defun org-submit-bug-report ()
  14487. "Submit a bug report on Org-mode via mail.
  14488. Don't hesitate to report any problems or inaccurate documentation.
  14489. If you don't have setup sending mail from (X)Emacs, please copy the
  14490. output buffer into your mail program, as it gives us important
  14491. information about your Org-mode version and configuration."
  14492. (interactive)
  14493. (require 'reporter)
  14494. (org-load-modules-maybe)
  14495. (org-require-autoloaded-modules)
  14496. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14497. (reporter-submit-bug-report
  14498. "emacs-orgmode@gnu.org"
  14499. (org-version)
  14500. (let (list)
  14501. (save-window-excursion
  14502. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14503. (delete-other-windows)
  14504. (erase-buffer)
  14505. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14506. We would like to add your full Org-mode and Outline configuration to the
  14507. bug report. This greatly simplifies the work of the maintainer and
  14508. other experts on the mailing list.
  14509. HOWEVER, some variables you have customized may contain private
  14510. information. The names of customers, colleagues, or friends, might
  14511. appear in the form of file names, tags, todo states, or search strings.
  14512. If you answer yes to the prompt, you might want to check and remove
  14513. such private information before sending the email.")
  14514. (add-text-properties (point-min) (point-max) '(face org-warning))
  14515. (when (yes-or-no-p "Include your Org-mode configuration ")
  14516. (mapatoms
  14517. (lambda (v)
  14518. (and (boundp v)
  14519. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14520. (or (and (symbol-value v)
  14521. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14522. (and
  14523. (get v 'custom-type) (get v 'standard-value)
  14524. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14525. (push v list)))))
  14526. (kill-buffer (get-buffer "*Warn about privacy*"))
  14527. list))
  14528. nil nil
  14529. "Remember to cover the basics, that is, what you expected to happen and
  14530. what in fact did happen. You don't know how to make a good report? See
  14531. http://orgmode.org/manual/Feedback.html#Feedback
  14532. Your bug report will be posted to the Org-mode mailing list.
  14533. ------------------------------------------------------------------------")))
  14534. (defun org-install-agenda-files-menu ()
  14535. (let ((bl (buffer-list)))
  14536. (save-excursion
  14537. (while bl
  14538. (set-buffer (pop bl))
  14539. (if (org-mode-p) (setq bl nil)))
  14540. (when (org-mode-p)
  14541. (easy-menu-change
  14542. '("Org") "File List for Agenda"
  14543. (append
  14544. (list
  14545. ["Edit File List" (org-edit-agenda-file-list) t]
  14546. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14547. ["Remove Current File from List" org-remove-file t]
  14548. ["Cycle through agenda files" org-cycle-agenda-files t]
  14549. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14550. "--")
  14551. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14552. ;;;; Documentation
  14553. ;;;###autoload
  14554. (defun org-require-autoloaded-modules ()
  14555. (interactive)
  14556. (mapc 'require
  14557. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14558. org-docbook org-exp org-html org-icalendar
  14559. org-id org-latex
  14560. org-publish org-remember org-table
  14561. org-timer org-xoxo)))
  14562. ;;;###autoload
  14563. (defun org-reload (&optional uncompiled)
  14564. "Reload all org lisp files.
  14565. With prefix arg UNCOMPILED, load the uncompiled versions."
  14566. (interactive "P")
  14567. (require 'find-func)
  14568. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14569. (dir-org (file-name-directory (org-find-library-name "org")))
  14570. (dir-org-contrib (ignore-errors
  14571. (file-name-directory
  14572. (org-find-library-name "org-contribdir"))))
  14573. (files
  14574. (append (directory-files dir-org t file-re)
  14575. (and dir-org-contrib
  14576. (directory-files dir-org-contrib t file-re))))
  14577. (remove-re (concat (if (featurep 'xemacs)
  14578. "org-colview" "org-colview-xemacs")
  14579. "\\'")))
  14580. (setq files (mapcar 'file-name-sans-extension files))
  14581. (setq files (mapcar
  14582. (lambda (x) (if (string-match remove-re x) nil x))
  14583. files))
  14584. (setq files (delq nil files))
  14585. (mapc
  14586. (lambda (f)
  14587. (when (featurep (intern (file-name-nondirectory f)))
  14588. (if (and (not uncompiled)
  14589. (file-exists-p (concat f ".elc")))
  14590. (load (concat f ".elc") nil nil t)
  14591. (load (concat f ".el") nil nil t))))
  14592. files))
  14593. (org-version))
  14594. ;;;###autoload
  14595. (defun org-customize ()
  14596. "Call the customize function with org as argument."
  14597. (interactive)
  14598. (org-load-modules-maybe)
  14599. (org-require-autoloaded-modules)
  14600. (customize-browse 'org))
  14601. (defun org-create-customize-menu ()
  14602. "Create a full customization menu for Org-mode, insert it into the menu."
  14603. (interactive)
  14604. (org-load-modules-maybe)
  14605. (org-require-autoloaded-modules)
  14606. (if (fboundp 'customize-menu-create)
  14607. (progn
  14608. (easy-menu-change
  14609. '("Org") "Customize"
  14610. `(["Browse Org group" org-customize t]
  14611. "--"
  14612. ,(customize-menu-create 'org)
  14613. ["Set" Custom-set t]
  14614. ["Save" Custom-save t]
  14615. ["Reset to Current" Custom-reset-current t]
  14616. ["Reset to Saved" Custom-reset-saved t]
  14617. ["Reset to Standard Settings" Custom-reset-standard t]))
  14618. (message "\"Org\"-menu now contains full customization menu"))
  14619. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14620. ;;;; Miscellaneous stuff
  14621. ;;; Generally useful functions
  14622. (defun org-get-at-bol (property)
  14623. "Get text property PROPERTY at beginning of line."
  14624. (get-text-property (point-at-bol) property))
  14625. (defun org-find-text-property-in-string (prop s)
  14626. "Return the first non-nil value of property PROP in string S."
  14627. (or (get-text-property 0 prop s)
  14628. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14629. prop s)))
  14630. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14631. "Display the given MESSAGE as a warning."
  14632. (if (fboundp 'display-warning)
  14633. (display-warning 'org message
  14634. (if (featurep 'xemacs)
  14635. 'warning
  14636. :warning))
  14637. (let ((buf (get-buffer-create "*Org warnings*")))
  14638. (with-current-buffer buf
  14639. (goto-char (point-max))
  14640. (insert "Warning (Org): " message)
  14641. (unless (bolp)
  14642. (newline)))
  14643. (display-buffer buf)
  14644. (sit-for 0))))
  14645. (defun org-in-commented-line ()
  14646. "Is point in a line starting with `#'?"
  14647. (equal (char-after (point-at-bol)) ?#))
  14648. (defun org-in-verbatim-emphasis ()
  14649. (save-match-data
  14650. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  14651. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14652. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14653. (if (and marker (marker-buffer marker)
  14654. (buffer-live-p (marker-buffer marker)))
  14655. (progn
  14656. (switch-to-buffer (marker-buffer marker))
  14657. (if (or (> marker (point-max)) (< marker (point-min)))
  14658. (widen))
  14659. (goto-char marker)
  14660. (org-show-context 'org-goto))
  14661. (if bookmark
  14662. (bookmark-jump bookmark)
  14663. (error "Cannot find location"))))
  14664. (defun org-quote-csv-field (s)
  14665. "Quote field for inclusion in CSV material."
  14666. (if (string-match "[\",]" s)
  14667. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14668. s))
  14669. (defun org-plist-delete (plist property)
  14670. "Delete PROPERTY from PLIST.
  14671. This is in contrast to merely setting it to 0."
  14672. (let (p)
  14673. (while plist
  14674. (if (not (eq property (car plist)))
  14675. (setq p (plist-put p (car plist) (nth 1 plist))))
  14676. (setq plist (cddr plist)))
  14677. p))
  14678. (defun org-force-self-insert (N)
  14679. "Needed to enforce self-insert under remapping."
  14680. (interactive "p")
  14681. (self-insert-command N))
  14682. (defun org-string-width (s)
  14683. "Compute width of string, ignoring invisible characters.
  14684. This ignores character with invisibility property `org-link', and also
  14685. characters with property `org-cwidth', because these will become invisible
  14686. upon the next fontification round."
  14687. (let (b l)
  14688. (when (or (eq t buffer-invisibility-spec)
  14689. (assq 'org-link buffer-invisibility-spec))
  14690. (while (setq b (text-property-any 0 (length s)
  14691. 'invisible 'org-link s))
  14692. (setq s (concat (substring s 0 b)
  14693. (substring s (or (next-single-property-change
  14694. b 'invisible s) (length s)))))))
  14695. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14696. (setq s (concat (substring s 0 b)
  14697. (substring s (or (next-single-property-change
  14698. b 'org-cwidth s) (length s))))))
  14699. (setq l (string-width s) b -1)
  14700. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14701. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14702. l))
  14703. (defun org-get-indentation (&optional line)
  14704. "Get the indentation of the current line, interpreting tabs.
  14705. When LINE is given, assume it represents a line and compute its indentation."
  14706. (if line
  14707. (if (string-match "^ *" (org-remove-tabs line))
  14708. (match-end 0))
  14709. (save-excursion
  14710. (beginning-of-line 1)
  14711. (skip-chars-forward " \t")
  14712. (current-column))))
  14713. (defun org-remove-tabs (s &optional width)
  14714. "Replace tabulators in S with spaces.
  14715. Assumes that s is a single line, starting in column 0."
  14716. (setq width (or width tab-width))
  14717. (while (string-match "\t" s)
  14718. (setq s (replace-match
  14719. (make-string
  14720. (- (* width (/ (+ (match-beginning 0) width) width))
  14721. (match-beginning 0)) ?\ )
  14722. t t s)))
  14723. s)
  14724. (defun org-fix-indentation (line ind)
  14725. "Fix indentation in LINE.
  14726. IND is a cons cell with target and minimum indentation.
  14727. If the current indentation in LINE is smaller than the minimum,
  14728. leave it alone. If it is larger than ind, set it to the target."
  14729. (let* ((l (org-remove-tabs line))
  14730. (i (org-get-indentation l))
  14731. (i1 (car ind)) (i2 (cdr ind)))
  14732. (if (>= i i2) (setq l (substring line i2)))
  14733. (if (> i1 0)
  14734. (concat (make-string i1 ?\ ) l)
  14735. l)))
  14736. (defun org-remove-indentation (code &optional n)
  14737. "Remove the maximum common indentation from the lines in CODE.
  14738. N may optionally be the number of spaces to remove."
  14739. (with-temp-buffer
  14740. (insert code)
  14741. (org-do-remove-indentation n)
  14742. (buffer-string)))
  14743. (defun org-do-remove-indentation (&optional n)
  14744. "Remove the maximum common indentation from the buffer."
  14745. (untabify (point-min) (point-max))
  14746. (let ((min 10000) re)
  14747. (if n
  14748. (setq min n)
  14749. (goto-char (point-min))
  14750. (while (re-search-forward "^ *[^ \n]" nil t)
  14751. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14752. (unless (or (= min 0) (= min 10000))
  14753. (setq re (format "^ \\{%d\\}" min))
  14754. (goto-char (point-min))
  14755. (while (re-search-forward re nil t)
  14756. (replace-match "")
  14757. (end-of-line 1))
  14758. min)))
  14759. (defun org-fill-template (template alist)
  14760. "Find each %key of ALIST in TEMPLATE and replace it."
  14761. (let (entry key value)
  14762. (setq alist (sort (copy-sequence alist)
  14763. (lambda (a b) (< (length (car a)) (length (car b))))))
  14764. (while (setq entry (pop alist))
  14765. (setq template
  14766. (replace-regexp-in-string
  14767. (concat "%" (regexp-quote (car entry)))
  14768. (cdr entry) template t t)))
  14769. template))
  14770. (defun org-base-buffer (buffer)
  14771. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14772. (if (not buffer)
  14773. buffer
  14774. (or (buffer-base-buffer buffer)
  14775. buffer)))
  14776. (defun org-trim (s)
  14777. "Remove whitespace at beginning and end of string."
  14778. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14779. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14780. s)
  14781. (defun org-wrap (string &optional width lines)
  14782. "Wrap string to either a number of lines, or a width in characters.
  14783. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14784. that costs. If there is a word longer than WIDTH, the text is actually
  14785. wrapped to the length of that word.
  14786. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14787. many lines, whatever width that takes.
  14788. The return value is a list of lines, without newlines at the end."
  14789. (let* ((words (org-split-string string "[ \t\n]+"))
  14790. (maxword (apply 'max (mapcar 'org-string-width words)))
  14791. w ll)
  14792. (cond (width
  14793. (org-do-wrap words (max maxword width)))
  14794. (lines
  14795. (setq w maxword)
  14796. (setq ll (org-do-wrap words maxword))
  14797. (if (<= (length ll) lines)
  14798. ll
  14799. (setq ll words)
  14800. (while (> (length ll) lines)
  14801. (setq w (1+ w))
  14802. (setq ll (org-do-wrap words w)))
  14803. ll))
  14804. (t (error "Cannot wrap this")))))
  14805. (defun org-do-wrap (words width)
  14806. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14807. (let (lines line)
  14808. (while words
  14809. (setq line (pop words))
  14810. (while (and words (< (+ (length line) (length (car words))) width))
  14811. (setq line (concat line " " (pop words))))
  14812. (setq lines (push line lines)))
  14813. (nreverse lines)))
  14814. (defun org-split-string (string &optional separators)
  14815. "Splits STRING into substrings at SEPARATORS.
  14816. No empty strings are returned if there are matches at the beginning
  14817. and end of string."
  14818. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14819. (start 0)
  14820. notfirst
  14821. (list nil))
  14822. (while (and (string-match rexp string
  14823. (if (and notfirst
  14824. (= start (match-beginning 0))
  14825. (< start (length string)))
  14826. (1+ start) start))
  14827. (< (match-beginning 0) (length string)))
  14828. (setq notfirst t)
  14829. (or (eq (match-beginning 0) 0)
  14830. (and (eq (match-beginning 0) (match-end 0))
  14831. (eq (match-beginning 0) start))
  14832. (setq list
  14833. (cons (substring string start (match-beginning 0))
  14834. list)))
  14835. (setq start (match-end 0)))
  14836. (or (eq start (length string))
  14837. (setq list
  14838. (cons (substring string start)
  14839. list)))
  14840. (nreverse list)))
  14841. (defun org-quote-vert (s)
  14842. "Replace \"|\" with \"\\vert\"."
  14843. (while (string-match "|" s)
  14844. (setq s (replace-match "\\vert" t t s)))
  14845. s)
  14846. (defun org-uuidgen-p (s)
  14847. "Is S an ID created by UUIDGEN?"
  14848. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14849. (defun org-context ()
  14850. "Return a list of contexts of the current cursor position.
  14851. If several contexts apply, all are returned.
  14852. Each context entry is a list with a symbol naming the context, and
  14853. two positions indicating start and end of the context. Possible
  14854. contexts are:
  14855. :headline anywhere in a headline
  14856. :headline-stars on the leading stars in a headline
  14857. :todo-keyword on a TODO keyword (including DONE) in a headline
  14858. :tags on the TAGS in a headline
  14859. :priority on the priority cookie in a headline
  14860. :item on the first line of a plain list item
  14861. :item-bullet on the bullet/number of a plain list item
  14862. :checkbox on the checkbox in a plain list item
  14863. :table in an org-mode table
  14864. :table-special on a special filed in a table
  14865. :table-table in a table.el table
  14866. :link on a hyperlink
  14867. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14868. :target on a <<target>>
  14869. :radio-target on a <<<radio-target>>>
  14870. :latex-fragment on a LaTeX fragment
  14871. :latex-preview on a LaTeX fragment with overlayed preview image
  14872. This function expects the position to be visible because it uses font-lock
  14873. faces as a help to recognize the following contexts: :table-special, :link,
  14874. and :keyword."
  14875. (let* ((f (get-text-property (point) 'face))
  14876. (faces (if (listp f) f (list f)))
  14877. (p (point)) clist o)
  14878. ;; First the large context
  14879. (cond
  14880. ((org-on-heading-p t)
  14881. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14882. (when (progn
  14883. (beginning-of-line 1)
  14884. (looking-at org-todo-line-tags-regexp))
  14885. (push (org-point-in-group p 1 :headline-stars) clist)
  14886. (push (org-point-in-group p 2 :todo-keyword) clist)
  14887. (push (org-point-in-group p 4 :tags) clist))
  14888. (goto-char p)
  14889. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  14890. (if (looking-at "\\[#[A-Z0-9]\\]")
  14891. (push (org-point-in-group p 0 :priority) clist)))
  14892. ((org-at-item-p)
  14893. (push (org-point-in-group p 2 :item-bullet) clist)
  14894. (push (list :item (point-at-bol)
  14895. (save-excursion (org-end-of-item) (point)))
  14896. clist)
  14897. (and (org-at-item-checkbox-p)
  14898. (push (org-point-in-group p 0 :checkbox) clist)))
  14899. ((org-at-table-p)
  14900. (push (list :table (org-table-begin) (org-table-end)) clist)
  14901. (if (memq 'org-formula faces)
  14902. (push (list :table-special
  14903. (previous-single-property-change p 'face)
  14904. (next-single-property-change p 'face)) clist)))
  14905. ((org-at-table-p 'any)
  14906. (push (list :table-table) clist)))
  14907. (goto-char p)
  14908. ;; Now the small context
  14909. (cond
  14910. ((org-at-timestamp-p)
  14911. (push (org-point-in-group p 0 :timestamp) clist))
  14912. ((memq 'org-link faces)
  14913. (push (list :link
  14914. (previous-single-property-change p 'face)
  14915. (next-single-property-change p 'face)) clist))
  14916. ((memq 'org-special-keyword faces)
  14917. (push (list :keyword
  14918. (previous-single-property-change p 'face)
  14919. (next-single-property-change p 'face)) clist))
  14920. ((org-on-target-p)
  14921. (push (org-point-in-group p 0 :target) clist)
  14922. (goto-char (1- (match-beginning 0)))
  14923. (if (looking-at org-radio-target-regexp)
  14924. (push (org-point-in-group p 0 :radio-target) clist))
  14925. (goto-char p))
  14926. ((setq o (car (delq nil
  14927. (mapcar
  14928. (lambda (x)
  14929. (if (memq x org-latex-fragment-image-overlays) x))
  14930. (org-overlays-at (point))))))
  14931. (push (list :latex-fragment
  14932. (org-overlay-start o) (org-overlay-end o)) clist)
  14933. (push (list :latex-preview
  14934. (org-overlay-start o) (org-overlay-end o)) clist))
  14935. ((org-inside-LaTeX-fragment-p)
  14936. ;; FIXME: positions wrong.
  14937. (push (list :latex-fragment (point) (point)) clist)))
  14938. (setq clist (nreverse (delq nil clist)))
  14939. clist))
  14940. ;; FIXME: Compare with at-regexp-p Do we need both?
  14941. (defun org-in-regexp (re &optional nlines visually)
  14942. "Check if point is inside a match of regexp.
  14943. Normally only the current line is checked, but you can include NLINES extra
  14944. lines both before and after point into the search.
  14945. If VISUALLY is set, require that the cursor is not after the match but
  14946. really on, so that the block visually is on the match."
  14947. (catch 'exit
  14948. (let ((pos (point))
  14949. (eol (point-at-eol (+ 1 (or nlines 0))))
  14950. (inc (if visually 1 0)))
  14951. (save-excursion
  14952. (beginning-of-line (- 1 (or nlines 0)))
  14953. (while (re-search-forward re eol t)
  14954. (if (and (<= (match-beginning 0) pos)
  14955. (>= (+ inc (match-end 0)) pos))
  14956. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14957. (defun org-at-regexp-p (regexp)
  14958. "Is point inside a match of REGEXP in the current line?"
  14959. (catch 'exit
  14960. (save-excursion
  14961. (let ((pos (point)) (end (point-at-eol)))
  14962. (beginning-of-line 1)
  14963. (while (re-search-forward regexp end t)
  14964. (if (and (<= (match-beginning 0) pos)
  14965. (>= (match-end 0) pos))
  14966. (throw 'exit t)))
  14967. nil))))
  14968. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14969. "Call `multi-occur' with buffers for all agenda files."
  14970. (interactive "sOrg-files matching: \np")
  14971. (let* ((files (org-agenda-files))
  14972. (tnames (mapcar 'file-truename files))
  14973. (extra org-agenda-text-search-extra-files)
  14974. f)
  14975. (when (eq (car extra) 'agenda-archives)
  14976. (setq extra (cdr extra))
  14977. (setq files (org-add-archive-files files)))
  14978. (while (setq f (pop extra))
  14979. (unless (member (file-truename f) tnames)
  14980. (add-to-list 'files f 'append)
  14981. (add-to-list 'tnames (file-truename f) 'append)))
  14982. (multi-occur
  14983. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14984. regexp)))
  14985. (if (boundp 'occur-mode-find-occurrence-hook)
  14986. ;; Emacs 23
  14987. (add-hook 'occur-mode-find-occurrence-hook
  14988. (lambda ()
  14989. (when (org-mode-p)
  14990. (org-reveal))))
  14991. ;; Emacs 22
  14992. (defadvice occur-mode-goto-occurrence
  14993. (after org-occur-reveal activate)
  14994. (and (org-mode-p) (org-reveal)))
  14995. (defadvice occur-mode-goto-occurrence-other-window
  14996. (after org-occur-reveal activate)
  14997. (and (org-mode-p) (org-reveal)))
  14998. (defadvice occur-mode-display-occurrence
  14999. (after org-occur-reveal activate)
  15000. (when (org-mode-p)
  15001. (let ((pos (occur-mode-find-occurrence)))
  15002. (with-current-buffer (marker-buffer pos)
  15003. (save-excursion
  15004. (goto-char pos)
  15005. (org-reveal)))))))
  15006. (defun org-occur-link-in-agenda-files ()
  15007. "Create a link and search for it in the agendas.
  15008. The link is not stored in `org-stored-links', it is just created
  15009. for the search purpose."
  15010. (interactive)
  15011. (let ((link (condition-case nil
  15012. (org-store-link nil)
  15013. (error "Unable to create a link to here"))))
  15014. (org-occur-in-agenda-files (regexp-quote link))))
  15015. (defun org-uniquify (list)
  15016. "Remove duplicate elements from LIST."
  15017. (let (res)
  15018. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15019. res))
  15020. (defun org-delete-all (elts list)
  15021. "Remove all elements in ELTS from LIST."
  15022. (while elts
  15023. (setq list (delete (pop elts) list)))
  15024. list)
  15025. (defun org-back-over-empty-lines ()
  15026. "Move backwards over whitespace, to the beginning of the first empty line.
  15027. Returns the number of empty lines passed."
  15028. (let ((pos (point)))
  15029. (skip-chars-backward " \t\n\r")
  15030. (beginning-of-line 2)
  15031. (goto-char (min (point) pos))
  15032. (count-lines (point) pos)))
  15033. (defun org-skip-whitespace ()
  15034. (skip-chars-forward " \t\n\r"))
  15035. (defun org-point-in-group (point group &optional context)
  15036. "Check if POINT is in match-group GROUP.
  15037. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15038. match. If the match group does ot exist or point is not inside it,
  15039. return nil."
  15040. (and (match-beginning group)
  15041. (>= point (match-beginning group))
  15042. (<= point (match-end group))
  15043. (if context
  15044. (list context (match-beginning group) (match-end group))
  15045. t)))
  15046. (defun org-switch-to-buffer-other-window (&rest args)
  15047. "Switch to buffer in a second window on the current frame.
  15048. In particular, do not allow pop-up frames."
  15049. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15050. special-display-function)
  15051. (apply 'switch-to-buffer-other-window args)))
  15052. (defun org-combine-plists (&rest plists)
  15053. "Create a single property list from all plists in PLISTS.
  15054. The process starts by copying the first list, and then setting properties
  15055. from the other lists. Settings in the last list are the most significant
  15056. ones and overrule settings in the other lists."
  15057. (let ((rtn (copy-sequence (pop plists)))
  15058. p v ls)
  15059. (while plists
  15060. (setq ls (pop plists))
  15061. (while ls
  15062. (setq p (pop ls) v (pop ls))
  15063. (setq rtn (plist-put rtn p v))))
  15064. rtn))
  15065. (defun org-move-line-down (arg)
  15066. "Move the current line down. With prefix argument, move it past ARG lines."
  15067. (interactive "p")
  15068. (let ((col (current-column))
  15069. beg end pos)
  15070. (beginning-of-line 1) (setq beg (point))
  15071. (beginning-of-line 2) (setq end (point))
  15072. (beginning-of-line (+ 1 arg))
  15073. (setq pos (move-marker (make-marker) (point)))
  15074. (insert (delete-and-extract-region beg end))
  15075. (goto-char pos)
  15076. (org-move-to-column col)))
  15077. (defun org-move-line-up (arg)
  15078. "Move the current line up. With prefix argument, move it past ARG lines."
  15079. (interactive "p")
  15080. (let ((col (current-column))
  15081. beg end pos)
  15082. (beginning-of-line 1) (setq beg (point))
  15083. (beginning-of-line 2) (setq end (point))
  15084. (beginning-of-line (- arg))
  15085. (setq pos (move-marker (make-marker) (point)))
  15086. (insert (delete-and-extract-region beg end))
  15087. (goto-char pos)
  15088. (org-move-to-column col)))
  15089. (defun org-replace-escapes (string table)
  15090. "Replace %-escapes in STRING with values in TABLE.
  15091. TABLE is an association list with keys like \"%a\" and string values.
  15092. The sequences in STRING may contain normal field width and padding information,
  15093. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15094. so values can contain further %-escapes if they are define later in TABLE."
  15095. (let ((case-fold-search nil)
  15096. e re rpl)
  15097. (while (setq e (pop table))
  15098. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15099. (while (string-match re string)
  15100. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15101. (cdr e)))
  15102. (setq string (replace-match rpl t t string))))
  15103. string))
  15104. (defun org-sublist (list start end)
  15105. "Return a section of LIST, from START to END.
  15106. Counting starts at 1."
  15107. (let (rtn (c start))
  15108. (setq list (nthcdr (1- start) list))
  15109. (while (and list (<= c end))
  15110. (push (pop list) rtn)
  15111. (setq c (1+ c)))
  15112. (nreverse rtn)))
  15113. (defun org-find-base-buffer-visiting (file)
  15114. "Like `find-buffer-visiting' but always return the base buffer and
  15115. not an indirect buffer."
  15116. (let ((buf (or (get-file-buffer file)
  15117. (find-buffer-visiting file))))
  15118. (if buf
  15119. (or (buffer-base-buffer buf) buf)
  15120. nil)))
  15121. (defun org-image-file-name-regexp (&optional extensions)
  15122. "Return regexp matching the file names of images.
  15123. If EXTENSIONS is given, only match these."
  15124. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15125. (image-file-name-regexp)
  15126. (let ((image-file-name-extensions
  15127. (or extensions
  15128. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15129. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15130. (concat "\\."
  15131. (regexp-opt (nconc (mapcar 'upcase
  15132. image-file-name-extensions)
  15133. image-file-name-extensions)
  15134. t)
  15135. "\\'"))))
  15136. (defun org-file-image-p (file &optional extensions)
  15137. "Return non-nil if FILE is an image."
  15138. (save-match-data
  15139. (string-match (org-image-file-name-regexp extensions) file)))
  15140. (defun org-get-cursor-date ()
  15141. "Return the date at cursor in as a time.
  15142. This works in the calendar and in the agenda, anywhere else it just
  15143. returns the current time."
  15144. (let (date day defd)
  15145. (cond
  15146. ((eq major-mode 'calendar-mode)
  15147. (setq date (calendar-cursor-to-date)
  15148. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15149. ((eq major-mode 'org-agenda-mode)
  15150. (setq day (get-text-property (point) 'day))
  15151. (if day
  15152. (setq date (calendar-gregorian-from-absolute day)
  15153. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15154. (nth 2 date))))))
  15155. (or defd (current-time))))
  15156. (defvar org-agenda-action-marker (make-marker)
  15157. "Marker pointing to the entry for the next agenda action.")
  15158. (defun org-mark-entry-for-agenda-action ()
  15159. "Mark the current entry as target of an agenda action.
  15160. Agenda actions are actions executed from the agenda with the key `k',
  15161. which make use of the date at the cursor."
  15162. (interactive)
  15163. (move-marker org-agenda-action-marker
  15164. (save-excursion (org-back-to-heading t) (point))
  15165. (current-buffer))
  15166. (message
  15167. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15168. ;;; Paragraph filling stuff.
  15169. ;; We want this to be just right, so use the full arsenal.
  15170. (defun org-indent-line-function ()
  15171. "Indent line like previous, but further if previous was headline or item."
  15172. (interactive)
  15173. (let* ((pos (point))
  15174. (itemp (org-at-item-p))
  15175. (case-fold-search t)
  15176. (org-drawer-regexp (or org-drawer-regexp "\000"))
  15177. column bpos bcol tpos tcol bullet btype bullet-type)
  15178. ;; Find the previous relevant line
  15179. (beginning-of-line 1)
  15180. (cond
  15181. ((looking-at "#") (setq column 0))
  15182. ((looking-at "\\*+ ") (setq column 0))
  15183. ((and (looking-at "[ \t]*:END:")
  15184. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  15185. (save-excursion
  15186. (goto-char (1- (match-beginning 1)))
  15187. (setq column (current-column))))
  15188. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  15189. (save-excursion
  15190. (re-search-backward
  15191. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  15192. (setq column (org-get-indentation (match-string 0))))
  15193. (t
  15194. (beginning-of-line 0)
  15195. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  15196. (not (looking-at "[ \t]*:END:"))
  15197. (not (looking-at org-drawer-regexp)))
  15198. (beginning-of-line 0))
  15199. (cond
  15200. ((looking-at "\\*+[ \t]+")
  15201. (if (not org-adapt-indentation)
  15202. (setq column 0)
  15203. (goto-char (match-end 0))
  15204. (setq column (current-column))))
  15205. ((looking-at org-drawer-regexp)
  15206. (goto-char (1- (match-beginning 1)))
  15207. (setq column (current-column)))
  15208. ((looking-at "\\([ \t]*\\):END:")
  15209. (goto-char (match-end 1))
  15210. (setq column (current-column)))
  15211. ((org-in-item-p)
  15212. (org-beginning-of-item)
  15213. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  15214. (setq bpos (match-beginning 1) tpos (match-end 0)
  15215. bcol (progn (goto-char bpos) (current-column))
  15216. tcol (progn (goto-char tpos) (current-column))
  15217. bullet (match-string 1)
  15218. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  15219. (if (> tcol (+ bcol org-description-max-indent))
  15220. (setq tcol (+ bcol 5)))
  15221. (if (not itemp)
  15222. (setq column tcol)
  15223. (goto-char pos)
  15224. (beginning-of-line 1)
  15225. (if (looking-at "\\S-")
  15226. (progn
  15227. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  15228. (setq bullet (match-string 1)
  15229. btype (if (string-match "[0-9]" bullet) "n" bullet))
  15230. (setq column (if (equal btype bullet-type) bcol tcol)))
  15231. (setq column (org-get-indentation)))))
  15232. (t (setq column (org-get-indentation))))))
  15233. (goto-char pos)
  15234. (if (<= (current-column) (current-indentation))
  15235. (org-indent-line-to column)
  15236. (save-excursion (org-indent-line-to column)))
  15237. (setq column (current-column))
  15238. (beginning-of-line 1)
  15239. (if (looking-at
  15240. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  15241. (replace-match (concat (match-string 1)
  15242. (format org-property-format
  15243. (match-string 2) (match-string 3)))
  15244. t t))
  15245. (org-move-to-column column)))
  15246. (defun org-set-autofill-regexps ()
  15247. (interactive)
  15248. ;; In the paragraph separator we include headlines, because filling
  15249. ;; text in a line directly attached to a headline would otherwise
  15250. ;; fill the headline as well.
  15251. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15252. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  15253. ;; The paragraph starter includes hand-formatted lists.
  15254. (org-set-local
  15255. 'paragraph-start
  15256. (concat
  15257. "\f" "\\|"
  15258. "[ ]*$" "\\|"
  15259. "\\*+ " "\\|"
  15260. "[ \t]*#" "\\|"
  15261. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  15262. "[ \t]*[:|]" "\\|"
  15263. "\\$\\$" "\\|"
  15264. "\\\\\\(begin\\|end\\|[][]\\)"))
  15265. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15266. ;; But only if the user has not turned off tables or fixed-width regions
  15267. (org-set-local
  15268. 'auto-fill-inhibit-regexp
  15269. (concat "\\*+ \\|#\\+"
  15270. "\\|[ \t]*" org-keyword-time-regexp
  15271. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15272. (concat
  15273. "\\|[ \t]*["
  15274. (if org-enable-table-editor "|" "")
  15275. (if org-enable-fixed-width-editor ":" "")
  15276. "]"))))
  15277. ;; We use our own fill-paragraph function, to make sure that tables
  15278. ;; and fixed-width regions are not wrapped. That function will pass
  15279. ;; through to `fill-paragraph' when appropriate.
  15280. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15281. ; Adaptive filling: To get full control, first make sure that
  15282. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15283. (org-set-local 'adaptive-fill-regexp "\000")
  15284. (org-set-local 'adaptive-fill-function
  15285. 'org-adaptive-fill-function)
  15286. (org-set-local
  15287. 'align-mode-rules-list
  15288. '((org-in-buffer-settings
  15289. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15290. (modes . '(org-mode))))))
  15291. (defun org-fill-paragraph (&optional justify)
  15292. "Re-align a table, pass through to fill-paragraph if no table."
  15293. (let ((table-p (org-at-table-p))
  15294. (table.el-p (org-at-table.el-p)))
  15295. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15296. (save-excursion (goto-char (point-at-bol))
  15297. (looking-at outline-regexp)))
  15298. t) ; skip headlines
  15299. (table.el-p t) ; skip table.el tables
  15300. (table-p (org-table-align) t) ; align org-mode tables
  15301. (t nil)))) ; call paragraph-fill
  15302. ;; For reference, this is the default value of adaptive-fill-regexp
  15303. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15304. (defun org-adaptive-fill-function ()
  15305. "Return a fill prefix for org-mode files.
  15306. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15307. work correctly."
  15308. (cond ((looking-at "#[ \t]+")
  15309. (match-string 0))
  15310. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15311. (save-excursion
  15312. (if (> (match-end 1) (+ (match-beginning 1)
  15313. org-description-max-indent))
  15314. (goto-char (+ (match-beginning 1) 5))
  15315. (goto-char (match-end 0)))
  15316. (make-string (current-column) ?\ )))
  15317. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15318. (save-excursion
  15319. (goto-char (match-end 0))
  15320. (make-string (current-column) ?\ )))
  15321. (t nil)))
  15322. ;;; Other stuff.
  15323. (defun org-toggle-fixed-width-section (arg)
  15324. "Toggle the fixed-width export.
  15325. If there is no active region, the QUOTE keyword at the current headline is
  15326. inserted or removed. When present, it causes the text between this headline
  15327. and the next to be exported as fixed-width text, and unmodified.
  15328. If there is an active region, this command adds or removes a colon as the
  15329. first character of this line. If the first character of a line is a colon,
  15330. this line is also exported in fixed-width font."
  15331. (interactive "P")
  15332. (let* ((cc 0)
  15333. (regionp (org-region-active-p))
  15334. (beg (if regionp (region-beginning) (point)))
  15335. (end (if regionp (region-end)))
  15336. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15337. (case-fold-search nil)
  15338. (re "[ \t]*\\(: \\)")
  15339. off)
  15340. (if regionp
  15341. (save-excursion
  15342. (goto-char beg)
  15343. (setq cc (current-column))
  15344. (beginning-of-line 1)
  15345. (setq off (looking-at re))
  15346. (while (> nlines 0)
  15347. (setq nlines (1- nlines))
  15348. (beginning-of-line 1)
  15349. (cond
  15350. (arg
  15351. (org-move-to-column cc t)
  15352. (insert ": \n")
  15353. (forward-line -1))
  15354. ((and off (looking-at re))
  15355. (replace-match "" t t nil 1))
  15356. ((not off) (org-move-to-column cc t) (insert ": ")))
  15357. (forward-line 1)))
  15358. (save-excursion
  15359. (org-back-to-heading)
  15360. (if (looking-at (concat outline-regexp
  15361. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15362. (replace-match "" t t nil 1)
  15363. (if (looking-at outline-regexp)
  15364. (progn
  15365. (goto-char (match-end 0))
  15366. (insert org-quote-string " "))))))))
  15367. (defun org-reftex-citation ()
  15368. "Use reftex-citation to insert a citation into the buffer.
  15369. This looks for a line like
  15370. #+BIBLIOGRAPHY: foo plain option:-d
  15371. and derives from it that foo.bib is the bbliography file relevant
  15372. for this document. It then installs the necessary environment for RefTeX
  15373. to work in this buffer and calls `reftex-citation' to insert a citation
  15374. into the buffer.
  15375. Export of such citations to both LaTeX and HTML is handled by the contributed
  15376. package org-exp-bibtex by Taru Karttunen."
  15377. (interactive)
  15378. (let ((reftex-docstruct-symbol 'rds)
  15379. (reftex-cite-format "\\cite{%l}")
  15380. rds bib)
  15381. (save-excursion
  15382. (save-restriction
  15383. (widen)
  15384. (let ((case-fold-search t)
  15385. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15386. (if (not (save-excursion
  15387. (or (re-search-forward re nil t)
  15388. (re-search-backward re nil t))))
  15389. (error "No bibliography defined in file")
  15390. (setq bib (concat (match-string 1) ".bib")
  15391. rds (list (list 'bib bib)))))))
  15392. (call-interactively 'reftex-citation)))
  15393. ;;;; Functions extending outline functionality
  15394. (defun org-beginning-of-line (&optional arg)
  15395. "Go to the beginning of the current line. If that is invisible, continue
  15396. to a visible line beginning. This makes the function of C-a more intuitive.
  15397. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15398. first attempt, and only move to after the tags when the cursor is already
  15399. beyond the end of the headline."
  15400. (interactive "P")
  15401. (let ((pos (point))
  15402. (special (if (consp org-special-ctrl-a/e)
  15403. (car org-special-ctrl-a/e)
  15404. org-special-ctrl-a/e))
  15405. refpos)
  15406. (if (org-bound-and-true-p line-move-visual)
  15407. (beginning-of-visual-line 1)
  15408. (beginning-of-line 1))
  15409. (if (and arg (fboundp 'move-beginning-of-line))
  15410. (call-interactively 'move-beginning-of-line)
  15411. (if (bobp)
  15412. nil
  15413. (backward-char 1)
  15414. (if (org-invisible-p)
  15415. (while (and (not (bobp)) (org-invisible-p))
  15416. (backward-char 1)
  15417. (beginning-of-line 1))
  15418. (forward-char 1))))
  15419. (when special
  15420. (cond
  15421. ((and (looking-at org-complex-heading-regexp)
  15422. (= (char-after (match-end 1)) ?\ ))
  15423. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15424. (point-at-eol)))
  15425. (goto-char
  15426. (if (eq special t)
  15427. (cond ((> pos refpos) refpos)
  15428. ((= pos (point)) refpos)
  15429. (t (point)))
  15430. (cond ((> pos (point)) (point))
  15431. ((not (eq last-command this-command)) (point))
  15432. (t refpos)))))
  15433. ((org-at-item-p)
  15434. (goto-char
  15435. (if (eq special t)
  15436. (cond ((> pos (match-end 4)) (match-end 4))
  15437. ((= pos (point)) (match-end 4))
  15438. (t (point)))
  15439. (cond ((> pos (point)) (point))
  15440. ((not (eq last-command this-command)) (point))
  15441. (t (match-end 4))))))))
  15442. (org-no-warnings
  15443. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15444. (defun org-end-of-line (&optional arg)
  15445. "Go to the end of the line.
  15446. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15447. first attempt, and only move to after the tags when the cursor is already
  15448. beyond the end of the headline."
  15449. (interactive "P")
  15450. (let ((special (if (consp org-special-ctrl-a/e)
  15451. (cdr org-special-ctrl-a/e)
  15452. org-special-ctrl-a/e)))
  15453. (if (or (not special)
  15454. (not (org-on-heading-p))
  15455. arg)
  15456. (call-interactively
  15457. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15458. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15459. (t 'end-of-line)))
  15460. (let ((pos (point)))
  15461. (beginning-of-line 1)
  15462. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  15463. (if (eq special t)
  15464. (if (or (< pos (match-beginning 1))
  15465. (= pos (match-end 0)))
  15466. (goto-char (match-beginning 1))
  15467. (goto-char (match-end 0)))
  15468. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15469. (goto-char (match-end 0))
  15470. (goto-char (match-beginning 1))))
  15471. (call-interactively (if (fboundp 'move-end-of-line)
  15472. 'move-end-of-line
  15473. 'end-of-line)))))
  15474. (org-no-warnings
  15475. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15476. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15477. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15478. (define-key org-mode-map [home] 'org-beginning-of-line)
  15479. (define-key org-mode-map [end] 'org-end-of-line)
  15480. (defun org-backward-sentence (&optional arg)
  15481. "Go to beginning of sentence, or beginning of table field.
  15482. This will call `backward-sentence' or `org-table-beginning-of-field',
  15483. depending on context."
  15484. (interactive "P")
  15485. (cond
  15486. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15487. (t (call-interactively 'backward-sentence))))
  15488. (defun org-forward-sentence (&optional arg)
  15489. "Go to end of sentence, or end of table field.
  15490. This will call `forward-sentence' or `org-table-end-of-field',
  15491. depending on context."
  15492. (interactive "P")
  15493. (cond
  15494. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15495. (t (call-interactively 'forward-sentence))))
  15496. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15497. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15498. (defun org-kill-line (&optional arg)
  15499. "Kill line, to tags or end of line."
  15500. (interactive "P")
  15501. (cond
  15502. ((or (not org-special-ctrl-k)
  15503. (bolp)
  15504. (not (org-on-heading-p)))
  15505. (call-interactively 'kill-line))
  15506. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15507. (kill-region (point) (match-beginning 1))
  15508. (org-set-tags nil t))
  15509. (t (kill-region (point) (point-at-eol)))))
  15510. (define-key org-mode-map "\C-k" 'org-kill-line)
  15511. (defun org-yank (&optional arg)
  15512. "Yank. If the kill is a subtree, treat it specially.
  15513. This command will look at the current kill and check if is a single
  15514. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15515. cursor is at the beginning of a line or after the stars of a currently
  15516. empty headline, then the yank is handled specially. How exactly depends
  15517. on the value of the following variables, both set by default.
  15518. org-yank-folded-subtrees
  15519. When set, the subtree(s) will be folded after insertion, but only
  15520. if doing so would now swallow text after the yanked text.
  15521. org-yank-adjusted-subtrees
  15522. When set, the subtree will be promoted or demoted in order to
  15523. fit into the local outline tree structure, which means that the level
  15524. will be adjusted so that it becomes the smaller one of the two
  15525. *visible* surrounding headings.
  15526. Any prefix to this command will cause `yank' to be called directly with
  15527. no special treatment. In particular, a simple `C-u' prefix will just
  15528. plainly yank the text as it is.
  15529. \[1] The test checks if the first non-white line is a heading
  15530. and if there are no other headings with fewer stars."
  15531. (interactive "P")
  15532. (org-yank-generic 'yank arg))
  15533. (defun org-yank-generic (command arg)
  15534. "Perform some yank-like command.
  15535. This function implements the behavior described in the `org-yank'
  15536. documentation. However, it has been generalized to work for any
  15537. interactive command with similar behavior."
  15538. ;; pretend to be command COMMAND
  15539. (setq this-command command)
  15540. (if arg
  15541. (call-interactively command)
  15542. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15543. (and (org-kill-is-subtree-p)
  15544. (or (bolp)
  15545. (and (looking-at "[ \t]*$")
  15546. (string-match
  15547. "\\`\\*+\\'"
  15548. (buffer-substring (point-at-bol) (point)))))))
  15549. swallowp)
  15550. (cond
  15551. ((and subtreep org-yank-folded-subtrees)
  15552. (let ((beg (point))
  15553. end)
  15554. (if (and subtreep org-yank-adjusted-subtrees)
  15555. (org-paste-subtree nil nil 'for-yank)
  15556. (call-interactively command))
  15557. (setq end (point))
  15558. (goto-char beg)
  15559. (when (and (bolp) subtreep
  15560. (not (setq swallowp
  15561. (org-yank-folding-would-swallow-text beg end))))
  15562. (or (looking-at outline-regexp)
  15563. (re-search-forward (concat "^" outline-regexp) end t))
  15564. (while (and (< (point) end) (looking-at outline-regexp))
  15565. (hide-subtree)
  15566. (org-cycle-show-empty-lines 'folded)
  15567. (condition-case nil
  15568. (outline-forward-same-level 1)
  15569. (error (goto-char end)))))
  15570. (when swallowp
  15571. (message
  15572. "Inserted text not folded because that would swallow text"))
  15573. (goto-char end)
  15574. (skip-chars-forward " \t\n\r")
  15575. (beginning-of-line 1)
  15576. (push-mark beg 'nomsg)))
  15577. ((and subtreep org-yank-adjusted-subtrees)
  15578. (let ((beg (point-at-bol)))
  15579. (org-paste-subtree nil nil 'for-yank)
  15580. (push-mark beg 'nomsg)))
  15581. (t
  15582. (call-interactively command))))))
  15583. (defun org-yank-folding-would-swallow-text (beg end)
  15584. "Would hide-subtree at BEG swallow any text after END?"
  15585. (let (level)
  15586. (save-excursion
  15587. (goto-char beg)
  15588. (when (or (looking-at outline-regexp)
  15589. (re-search-forward (concat "^" outline-regexp) end t))
  15590. (setq level (org-outline-level)))
  15591. (goto-char end)
  15592. (skip-chars-forward " \t\r\n\v\f")
  15593. (if (or (eobp)
  15594. (and (bolp) (looking-at org-outline-regexp)
  15595. (<= (org-outline-level) level)))
  15596. nil ; Nothing would be swallowed
  15597. t)))) ; something would swallow
  15598. (define-key org-mode-map "\C-y" 'org-yank)
  15599. (defun org-invisible-p ()
  15600. "Check if point is at a character currently not visible."
  15601. ;; Early versions of noutline don't have `outline-invisible-p'.
  15602. (if (fboundp 'outline-invisible-p)
  15603. (outline-invisible-p)
  15604. (get-char-property (point) 'invisible)))
  15605. (defun org-invisible-p2 ()
  15606. "Check if point is at a character currently not visible."
  15607. (save-excursion
  15608. (if (and (eolp) (not (bobp))) (backward-char 1))
  15609. ;; Early versions of noutline don't have `outline-invisible-p'.
  15610. (if (fboundp 'outline-invisible-p)
  15611. (outline-invisible-p)
  15612. (get-char-property (point) 'invisible))))
  15613. (defun org-back-to-heading (&optional invisible-ok)
  15614. "Call `outline-back-to-heading', but provide a better error message."
  15615. (condition-case nil
  15616. (outline-back-to-heading invisible-ok)
  15617. (error (error "Before first headline at position %d in buffer %s"
  15618. (point) (current-buffer)))))
  15619. (defun org-before-first-heading-p ()
  15620. "Before first heading?"
  15621. (save-excursion
  15622. (null (re-search-backward "^\\*+ " nil t))))
  15623. (defun org-on-heading-p (&optional ignored)
  15624. (outline-on-heading-p t))
  15625. (defun org-at-heading-p (&optional ignored)
  15626. (outline-on-heading-p t))
  15627. (defun org-at-heading-or-item-p ()
  15628. (or (org-on-heading-p) (org-at-item-p)))
  15629. (defun org-on-target-p ()
  15630. (or (org-in-regexp org-radio-target-regexp)
  15631. (org-in-regexp org-target-regexp)))
  15632. (defun org-up-heading-all (arg)
  15633. "Move to the heading line of which the present line is a subheading.
  15634. This function considers both visible and invisible heading lines.
  15635. With argument, move up ARG levels."
  15636. (if (fboundp 'outline-up-heading-all)
  15637. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15638. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15639. (defun org-up-heading-safe ()
  15640. "Move to the heading line of which the present line is a subheading.
  15641. This version will not throw an error. It will return the level of the
  15642. headline found, or nil if no higher level is found.
  15643. Also, this function will be a lot faster than `outline-up-heading',
  15644. because it relies on stars being the outline starters. This can really
  15645. make a significant difference in outlines with very many siblings."
  15646. (let (start-level re)
  15647. (org-back-to-heading t)
  15648. (setq start-level (funcall outline-level))
  15649. (if (equal start-level 1)
  15650. nil
  15651. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15652. (if (re-search-backward re nil t)
  15653. (funcall outline-level)))))
  15654. (defun org-first-sibling-p ()
  15655. "Is this heading the first child of its parents?"
  15656. (interactive)
  15657. (let ((re (concat "^" outline-regexp))
  15658. level l)
  15659. (unless (org-at-heading-p t)
  15660. (error "Not at a heading"))
  15661. (setq level (funcall outline-level))
  15662. (save-excursion
  15663. (if (not (re-search-backward re nil t))
  15664. t
  15665. (setq l (funcall outline-level))
  15666. (< l level)))))
  15667. (defun org-goto-sibling (&optional previous)
  15668. "Goto the next sibling, even if it is invisible.
  15669. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15670. when a sibling was found. When none is found, return nil and don't
  15671. move point."
  15672. (let ((fun (if previous 're-search-backward 're-search-forward))
  15673. (pos (point))
  15674. (re (concat "^" outline-regexp))
  15675. level l)
  15676. (when (condition-case nil (org-back-to-heading t) (error nil))
  15677. (setq level (funcall outline-level))
  15678. (catch 'exit
  15679. (or previous (forward-char 1))
  15680. (while (funcall fun re nil t)
  15681. (setq l (funcall outline-level))
  15682. (when (< l level) (goto-char pos) (throw 'exit nil))
  15683. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15684. (goto-char pos)
  15685. nil))))
  15686. (defun org-show-siblings ()
  15687. "Show all siblings of the current headline."
  15688. (save-excursion
  15689. (while (org-goto-sibling) (org-flag-heading nil)))
  15690. (save-excursion
  15691. (while (org-goto-sibling 'previous)
  15692. (org-flag-heading nil))))
  15693. (defun org-show-hidden-entry ()
  15694. "Show an entry where even the heading is hidden."
  15695. (save-excursion
  15696. (org-show-entry)))
  15697. (defun org-flag-heading (flag &optional entry)
  15698. "Flag the current heading. FLAG non-nil means make invisible.
  15699. When ENTRY is non-nil, show the entire entry."
  15700. (save-excursion
  15701. (org-back-to-heading t)
  15702. ;; Check if we should show the entire entry
  15703. (if entry
  15704. (progn
  15705. (org-show-entry)
  15706. (save-excursion
  15707. (and (outline-next-heading)
  15708. (org-flag-heading nil))))
  15709. (outline-flag-region (max (point-min) (1- (point)))
  15710. (save-excursion (outline-end-of-heading) (point))
  15711. flag))))
  15712. (defun org-get-next-sibling ()
  15713. "Move to next heading of the same level, and return point.
  15714. If there is no such heading, return nil.
  15715. This is like outline-next-sibling, but invisible headings are ok."
  15716. (let ((level (funcall outline-level)))
  15717. (outline-next-heading)
  15718. (while (and (not (eobp)) (> (funcall outline-level) level))
  15719. (outline-next-heading))
  15720. (if (or (eobp) (< (funcall outline-level) level))
  15721. nil
  15722. (point))))
  15723. (defun org-get-last-sibling ()
  15724. "Move to previous heading of the same level, and return point.
  15725. If there is no such heading, return nil."
  15726. (let ((opoint (point))
  15727. (level (funcall outline-level)))
  15728. (outline-previous-heading)
  15729. (when (and (/= (point) opoint) (outline-on-heading-p t))
  15730. (while (and (> (funcall outline-level) level)
  15731. (not (bobp)))
  15732. (outline-previous-heading))
  15733. (if (< (funcall outline-level) level)
  15734. nil
  15735. (point)))))
  15736. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15737. ;; This contains an exact copy of the original function, but it uses
  15738. ;; `org-back-to-heading', to make it work also in invisible
  15739. ;; trees. And is uses an invisible-OK argument.
  15740. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15741. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15742. ;; with many children and grandchildren etc, this can be much faster
  15743. ;; than the outline version.
  15744. (org-back-to-heading invisible-OK)
  15745. (let ((first t)
  15746. (level (funcall outline-level)))
  15747. (if (and (org-mode-p) (< level 1000))
  15748. ;; A true heading (not a plain list item), in Org-mode
  15749. ;; This means we can easily find the end by looking
  15750. ;; only for the right number of stars. Using a regexp to do
  15751. ;; this is so much faster than using a Lisp loop.
  15752. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15753. (forward-char 1)
  15754. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15755. ;; something else, do it the slow way
  15756. (while (and (not (eobp))
  15757. (or first (> (funcall outline-level) level)))
  15758. (setq first nil)
  15759. (outline-next-heading)))
  15760. (unless to-heading
  15761. (if (memq (preceding-char) '(?\n ?\^M))
  15762. (progn
  15763. ;; Go to end of line before heading
  15764. (forward-char -1)
  15765. (if (memq (preceding-char) '(?\n ?\^M))
  15766. ;; leave blank line before heading
  15767. (forward-char -1))))))
  15768. (point))
  15769. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15770. "Use Org version in org-mode, for dramatic speed-up."
  15771. (if (eq major-mode 'org-mode)
  15772. (progn
  15773. (org-end-of-subtree nil t)
  15774. (unless (eobp) (backward-char 1)))
  15775. ad-do-it))
  15776. (defun org-forward-same-level (arg &optional invisible-ok)
  15777. "Move forward to the arg'th subheading at same level as this one.
  15778. Stop at the first and last subheadings of a superior heading."
  15779. (interactive "p")
  15780. (org-back-to-heading invisible-ok)
  15781. (org-on-heading-p)
  15782. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15783. (re (format "^\\*\\{1,%d\\} " level))
  15784. l)
  15785. (forward-char 1)
  15786. (while (> arg 0)
  15787. (while (and (re-search-forward re nil 'move)
  15788. (setq l (- (match-end 0) (match-beginning 0) 1))
  15789. (= l level)
  15790. (not invisible-ok)
  15791. (org-invisible-p))
  15792. (if (< l level) (setq arg 1)))
  15793. (setq arg (1- arg)))
  15794. (beginning-of-line 1)))
  15795. (defun org-backward-same-level (arg &optional invisible-ok)
  15796. "Move backward to the arg'th subheading at same level as this one.
  15797. Stop at the first and last subheadings of a superior heading."
  15798. (interactive "p")
  15799. (org-back-to-heading)
  15800. (org-on-heading-p)
  15801. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15802. (re (format "^\\*\\{1,%d\\} " level))
  15803. l)
  15804. (while (> arg 0)
  15805. (while (and (re-search-backward re nil 'move)
  15806. (setq l (- (match-end 0) (match-beginning 0) 1))
  15807. (= l level)
  15808. (not invisible-ok)
  15809. (org-invisible-p))
  15810. (if (< l level) (setq arg 1)))
  15811. (setq arg (1- arg)))))
  15812. (defun org-show-subtree ()
  15813. "Show everything after this heading at deeper levels."
  15814. (outline-flag-region
  15815. (point)
  15816. (save-excursion
  15817. (org-end-of-subtree t t))
  15818. nil))
  15819. (defun org-show-entry ()
  15820. "Show the body directly following this heading.
  15821. Show the heading too, if it is currently invisible."
  15822. (interactive)
  15823. (save-excursion
  15824. (condition-case nil
  15825. (progn
  15826. (org-back-to-heading t)
  15827. (outline-flag-region
  15828. (max (point-min) (1- (point)))
  15829. (save-excursion
  15830. (if (re-search-forward
  15831. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15832. (match-beginning 1)
  15833. (point-max)))
  15834. nil)
  15835. (org-cycle-hide-drawers 'children))
  15836. (error nil))))
  15837. (defun org-make-options-regexp (kwds &optional extra)
  15838. "Make a regular expression for keyword lines."
  15839. (concat
  15840. "^"
  15841. "#?[ \t]*\\+\\("
  15842. (mapconcat 'regexp-quote kwds "\\|")
  15843. (if extra (concat "\\|" extra))
  15844. "\\):[ \t]*"
  15845. "\\(.*\\)"))
  15846. ;; Make isearch reveal the necessary context
  15847. (defun org-isearch-end ()
  15848. "Reveal context after isearch exits."
  15849. (when isearch-success ; only if search was successful
  15850. (if (featurep 'xemacs)
  15851. ;; Under XEmacs, the hook is run in the correct place,
  15852. ;; we directly show the context.
  15853. (org-show-context 'isearch)
  15854. ;; In Emacs the hook runs *before* restoring the overlays.
  15855. ;; So we have to use a one-time post-command-hook to do this.
  15856. ;; (Emacs 22 has a special variable, see function `org-mode')
  15857. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15858. isearch-mode-end-hook-quit)
  15859. ;; Only when the isearch was not quitted.
  15860. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15861. 'append 'local)))))
  15862. (defun org-isearch-post-command ()
  15863. "Remove self from hook, and show context."
  15864. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15865. (org-show-context 'isearch))
  15866. ;;;; Integration with and fixes for other packages
  15867. ;;; Imenu support
  15868. (defvar org-imenu-markers nil
  15869. "All markers currently used by Imenu.")
  15870. (make-variable-buffer-local 'org-imenu-markers)
  15871. (defun org-imenu-new-marker (&optional pos)
  15872. "Return a new marker for use by Imenu, and remember the marker."
  15873. (let ((m (make-marker)))
  15874. (move-marker m (or pos (point)))
  15875. (push m org-imenu-markers)
  15876. m))
  15877. (defun org-imenu-get-tree ()
  15878. "Produce the index for Imenu."
  15879. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15880. (setq org-imenu-markers nil)
  15881. (let* ((n org-imenu-depth)
  15882. (re (concat "^" outline-regexp))
  15883. (subs (make-vector (1+ n) nil))
  15884. (last-level 0)
  15885. m level head)
  15886. (save-excursion
  15887. (save-restriction
  15888. (widen)
  15889. (goto-char (point-max))
  15890. (while (re-search-backward re nil t)
  15891. (setq level (org-reduced-level (funcall outline-level)))
  15892. (when (<= level n)
  15893. (looking-at org-complex-heading-regexp)
  15894. (setq head (org-link-display-format
  15895. (org-match-string-no-properties 4))
  15896. m (org-imenu-new-marker))
  15897. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15898. (if (>= level last-level)
  15899. (push (cons head m) (aref subs level))
  15900. (push (cons head (aref subs (1+ level))) (aref subs level))
  15901. (loop for i from (1+ level) to n do (aset subs i nil)))
  15902. (setq last-level level)))))
  15903. (aref subs 1)))
  15904. (eval-after-load "imenu"
  15905. '(progn
  15906. (add-hook 'imenu-after-jump-hook
  15907. (lambda ()
  15908. (if (eq major-mode 'org-mode)
  15909. (org-show-context 'org-goto))))))
  15910. (defun org-link-display-format (link)
  15911. "Replace a link with either the description, or the link target
  15912. if no description is present"
  15913. (save-match-data
  15914. (if (string-match org-bracket-link-analytic-regexp link)
  15915. (replace-match (or (match-string 5 link)
  15916. (concat (match-string 1 link)
  15917. (match-string 3 link)))
  15918. nil nil link)
  15919. link)))
  15920. ;; Speedbar support
  15921. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15922. "Overlay marking the agenda restriction line in speedbar.")
  15923. (org-overlay-put org-speedbar-restriction-lock-overlay
  15924. 'face 'org-agenda-restriction-lock)
  15925. (org-overlay-put org-speedbar-restriction-lock-overlay
  15926. 'help-echo "Agendas are currently limited to this item.")
  15927. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15928. (defun org-speedbar-set-agenda-restriction ()
  15929. "Restrict future agenda commands to the location at point in speedbar.
  15930. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15931. (interactive)
  15932. (require 'org-agenda)
  15933. (let (p m tp np dir txt)
  15934. (cond
  15935. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15936. 'org-imenu t))
  15937. (setq m (get-text-property p 'org-imenu-marker))
  15938. (save-excursion
  15939. (save-restriction
  15940. (set-buffer (marker-buffer m))
  15941. (goto-char m)
  15942. (org-agenda-set-restriction-lock 'subtree))))
  15943. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15944. 'speedbar-function 'speedbar-find-file))
  15945. (setq tp (previous-single-property-change
  15946. (1+ p) 'speedbar-function)
  15947. np (next-single-property-change
  15948. tp 'speedbar-function)
  15949. dir (speedbar-line-directory)
  15950. txt (buffer-substring-no-properties (or tp (point-min))
  15951. (or np (point-max))))
  15952. (save-excursion
  15953. (save-restriction
  15954. (set-buffer (find-file-noselect
  15955. (let ((default-directory dir))
  15956. (expand-file-name txt))))
  15957. (unless (org-mode-p)
  15958. (error "Cannot restrict to non-Org-mode file"))
  15959. (org-agenda-set-restriction-lock 'file))))
  15960. (t (error "Don't know how to restrict Org-mode's agenda")))
  15961. (org-move-overlay org-speedbar-restriction-lock-overlay
  15962. (point-at-bol) (point-at-eol))
  15963. (setq current-prefix-arg nil)
  15964. (org-agenda-maybe-redo)))
  15965. (eval-after-load "speedbar"
  15966. '(progn
  15967. (speedbar-add-supported-extension ".org")
  15968. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15969. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15970. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15971. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15972. (add-hook 'speedbar-visiting-tag-hook
  15973. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15974. ;;; Fixes and Hacks for problems with other packages
  15975. ;; Make flyspell not check words in links, to not mess up our keymap
  15976. (defun org-mode-flyspell-verify ()
  15977. "Don't let flyspell put overlays at active buttons."
  15978. (and (not (get-text-property (point) 'keymap))
  15979. (not (get-text-property (point) 'org-no-flyspell))))
  15980. (defun org-remove-flyspell-overlays-in (beg end)
  15981. "Remove flyspell overlays in region."
  15982. (and (org-bound-and-true-p flyspell-mode)
  15983. (fboundp 'flyspell-delete-region-overlays)
  15984. (flyspell-delete-region-overlays beg end))
  15985. (add-text-properties beg end '(org-no-flyspell t)))
  15986. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  15987. (eval-after-load "bookmark"
  15988. '(if (boundp 'bookmark-after-jump-hook)
  15989. ;; We can use the hook
  15990. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15991. ;; Hook not available, use advice
  15992. (defadvice bookmark-jump (after org-make-visible activate)
  15993. "Make the position visible."
  15994. (org-bookmark-jump-unhide))))
  15995. ;; Make sure saveplace shows the location if it was hidden
  15996. (eval-after-load "saveplace"
  15997. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15998. "Make the position visible."
  15999. (org-bookmark-jump-unhide)))
  16000. ;; Make sure ecb shows the location if it was hidden
  16001. (eval-after-load "ecb"
  16002. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16003. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16004. (if (eq major-mode 'org-mode)
  16005. (org-show-context))))
  16006. (defun org-bookmark-jump-unhide ()
  16007. "Unhide the current position, to show the bookmark location."
  16008. (and (org-mode-p)
  16009. (or (org-invisible-p)
  16010. (save-excursion (goto-char (max (point-min) (1- (point))))
  16011. (org-invisible-p)))
  16012. (org-show-context 'bookmark-jump)))
  16013. ;; Make session.el ignore our circular variable
  16014. (eval-after-load "session"
  16015. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16016. ;;;; Experimental code
  16017. (defun org-closed-in-range ()
  16018. "Sparse tree of items closed in a certain time range.
  16019. Still experimental, may disappear in the future."
  16020. (interactive)
  16021. ;; Get the time interval from the user.
  16022. (let* ((time1 (org-float-time
  16023. (org-read-date nil 'to-time nil "Starting date: ")))
  16024. (time2 (org-float-time
  16025. (org-read-date nil 'to-time nil "End date:")))
  16026. ;; callback function
  16027. (callback (lambda ()
  16028. (let ((time
  16029. (org-float-time
  16030. (apply 'encode-time
  16031. (org-parse-time-string
  16032. (match-string 1))))))
  16033. ;; check if time in interval
  16034. (and (>= time time1) (<= time time2))))))
  16035. ;; make tree, check each match with the callback
  16036. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16037. ;;;; Finish up
  16038. (provide 'org)
  16039. (run-hooks 'org-load-hook)
  16040. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16041. ;;; org.el ends here