org.el 652 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514
  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. (defvar remember-data-file)
  2632. (defvar texmathp-why)
  2633. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2634. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2635. (defvar w3m-current-url)
  2636. (defvar w3m-current-title)
  2637. (defvar org-latex-regexps)
  2638. ;;; Autoload and prepare some org modules
  2639. ;; Some table stuff that needs to be defined here, because it is used
  2640. ;; by the functions setting up org-mode or checking for table context.
  2641. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2642. "Detects an org-type or table-type table.")
  2643. (defconst org-table-line-regexp "^[ \t]*|"
  2644. "Detects an org-type table line.")
  2645. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2646. "Detects an org-type table line.")
  2647. (defconst org-table-hline-regexp "^[ \t]*|-"
  2648. "Detects an org-type table hline.")
  2649. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2650. "Detects a table-type table hline.")
  2651. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2652. "Searching from within a table (any type) this finds the first line
  2653. outside the table.")
  2654. ;; Autoload the functions in org-table.el that are needed by functions here.
  2655. (eval-and-compile
  2656. (org-autoload "org-table"
  2657. '(org-table-align org-table-begin org-table-blank-field
  2658. org-table-convert org-table-convert-region org-table-copy-down
  2659. org-table-copy-region org-table-create
  2660. org-table-create-or-convert-from-region
  2661. org-table-create-with-table.el org-table-current-dline
  2662. org-table-cut-region org-table-delete-column org-table-edit-field
  2663. org-table-edit-formulas org-table-end org-table-eval-formula
  2664. org-table-export org-table-field-info
  2665. org-table-get-stored-formulas org-table-goto-column
  2666. org-table-hline-and-move org-table-import org-table-insert-column
  2667. org-table-insert-hline org-table-insert-row org-table-iterate
  2668. org-table-justify-field-maybe org-table-kill-row
  2669. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2670. org-table-move-column org-table-move-column-left
  2671. org-table-move-column-right org-table-move-row
  2672. org-table-move-row-down org-table-move-row-up
  2673. org-table-next-field org-table-next-row org-table-paste-rectangle
  2674. org-table-previous-field org-table-recalculate
  2675. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2676. org-table-toggle-coordinate-overlays
  2677. org-table-toggle-formula-debugger org-table-wrap-region
  2678. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2679. (defun org-at-table-p (&optional table-type)
  2680. "Return t if the cursor is inside an org-type table.
  2681. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2682. (if org-enable-table-editor
  2683. (save-excursion
  2684. (beginning-of-line 1)
  2685. (looking-at (if table-type org-table-any-line-regexp
  2686. org-table-line-regexp)))
  2687. nil))
  2688. (defsubst org-table-p () (org-at-table-p))
  2689. (defun org-at-table.el-p ()
  2690. "Return t if and only if we are at a table.el table."
  2691. (and (org-at-table-p 'any)
  2692. (save-excursion
  2693. (goto-char (org-table-begin 'any))
  2694. (looking-at org-table1-hline-regexp))))
  2695. (defun org-table-recognize-table.el ()
  2696. "If there is a table.el table nearby, recognize it and move into it."
  2697. (if org-table-tab-recognizes-table.el
  2698. (if (org-at-table.el-p)
  2699. (progn
  2700. (beginning-of-line 1)
  2701. (if (looking-at org-table-dataline-regexp)
  2702. nil
  2703. (if (looking-at org-table1-hline-regexp)
  2704. (progn
  2705. (beginning-of-line 2)
  2706. (if (looking-at org-table-any-border-regexp)
  2707. (beginning-of-line -1)))))
  2708. (if (re-search-forward "|" (org-table-end t) t)
  2709. (progn
  2710. (require 'table)
  2711. (if (table--at-cell-p (point))
  2712. t
  2713. (message "recognizing table.el table...")
  2714. (table-recognize-table)
  2715. (message "recognizing table.el table...done")))
  2716. (error "This should not happen..."))
  2717. t)
  2718. nil)
  2719. nil))
  2720. (defun org-at-table-hline-p ()
  2721. "Return t if the cursor is inside a hline in a table."
  2722. (if org-enable-table-editor
  2723. (save-excursion
  2724. (beginning-of-line 1)
  2725. (looking-at org-table-hline-regexp))
  2726. nil))
  2727. (defvar org-table-clean-did-remove-column nil)
  2728. (defun org-table-map-tables (function)
  2729. "Apply FUNCTION to the start of all tables in the buffer."
  2730. (save-excursion
  2731. (save-restriction
  2732. (widen)
  2733. (goto-char (point-min))
  2734. (while (re-search-forward org-table-any-line-regexp nil t)
  2735. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2736. (beginning-of-line 1)
  2737. (when (looking-at org-table-line-regexp)
  2738. (save-excursion (funcall function))
  2739. (or (looking-at org-table-line-regexp)
  2740. (forward-char 1)))
  2741. (re-search-forward org-table-any-border-regexp nil 1))))
  2742. (message "Mapping tables: done"))
  2743. ;; Declare and autoload functions from org-exp.el & Co
  2744. (declare-function org-default-export-plist "org-exp")
  2745. (declare-function org-infile-export-plist "org-exp")
  2746. (declare-function org-get-current-options "org-exp")
  2747. (eval-and-compile
  2748. (org-autoload "org-exp"
  2749. '(org-export org-export-visible
  2750. org-insert-export-options-template
  2751. org-table-clean-before-export))
  2752. (org-autoload "org-ascii"
  2753. '(org-export-as-ascii org-export-ascii-preprocess
  2754. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2755. org-export-region-as-ascii))
  2756. (org-autoload "org-html"
  2757. '(org-export-as-html-and-open
  2758. org-export-as-html-batch org-export-as-html-to-buffer
  2759. org-replace-region-by-html org-export-region-as-html
  2760. org-export-as-html))
  2761. (org-autoload "org-icalendar"
  2762. '(org-export-icalendar-this-file
  2763. org-export-icalendar-all-agenda-files
  2764. org-export-icalendar-combine-agenda-files))
  2765. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2766. ;; Declare and autoload functions from org-agenda.el
  2767. (eval-and-compile
  2768. (org-autoload "org-agenda"
  2769. '(org-agenda org-agenda-list org-search-view
  2770. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2771. org-diary org-agenda-to-appt
  2772. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2773. ;; Autoload org-remember
  2774. (eval-and-compile
  2775. (org-autoload "org-remember"
  2776. '(org-remember-insinuate org-remember-annotation
  2777. org-remember-apply-template org-remember org-remember-handler)))
  2778. ;; Autoload org-clock.el
  2779. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2780. (beg end))
  2781. (declare-function org-clock-update-mode-line "org-clock" ())
  2782. (declare-function org-resolve-clocks "org-clock"
  2783. (&optional also-non-dangling-p prompt last-valid))
  2784. (defvar org-clock-start-time)
  2785. (defvar org-clock-marker (make-marker)
  2786. "Marker recording the last clock-in.")
  2787. (defvar org-clock-hd-marker (make-marker)
  2788. "Marker recording the last clock-in, but the headline position.")
  2789. (defun org-clock-is-active ()
  2790. "Return non-nil if clock is currently running.
  2791. The return value is actually the clock marker."
  2792. (marker-buffer org-clock-marker))
  2793. (eval-and-compile
  2794. (org-autoload
  2795. "org-clock"
  2796. '(org-clock-in org-clock-out org-clock-cancel
  2797. org-clock-goto org-clock-sum org-clock-display
  2798. org-clock-remove-overlays org-clock-report
  2799. org-clocktable-shift org-dblock-write:clocktable
  2800. org-get-clocktable org-resolve-clocks)))
  2801. (defun org-clock-update-time-maybe ()
  2802. "If this is a CLOCK line, update it and return t.
  2803. Otherwise, return nil."
  2804. (interactive)
  2805. (save-excursion
  2806. (beginning-of-line 1)
  2807. (skip-chars-forward " \t")
  2808. (when (looking-at org-clock-string)
  2809. (let ((re (concat "[ \t]*" org-clock-string
  2810. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2811. "\\([ \t]*=>.*\\)?\\)?"))
  2812. ts te h m s neg)
  2813. (cond
  2814. ((not (looking-at re))
  2815. nil)
  2816. ((not (match-end 2))
  2817. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2818. (> org-clock-marker (point))
  2819. (<= org-clock-marker (point-at-eol)))
  2820. ;; The clock is running here
  2821. (setq org-clock-start-time
  2822. (apply 'encode-time
  2823. (org-parse-time-string (match-string 1))))
  2824. (org-clock-update-mode-line)))
  2825. (t
  2826. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2827. (end-of-line 1)
  2828. (setq ts (match-string 1)
  2829. te (match-string 3))
  2830. (setq s (- (org-float-time
  2831. (apply 'encode-time (org-parse-time-string te)))
  2832. (org-float-time
  2833. (apply 'encode-time (org-parse-time-string ts))))
  2834. neg (< s 0)
  2835. s (abs s)
  2836. h (floor (/ s 3600))
  2837. s (- s (* 3600 h))
  2838. m (floor (/ s 60))
  2839. s (- s (* 60 s)))
  2840. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2841. t))))))
  2842. (defun org-check-running-clock ()
  2843. "Check if the current buffer contains the running clock.
  2844. If yes, offer to stop it and to save the buffer with the changes."
  2845. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2846. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2847. (buffer-name))))
  2848. (org-clock-out)
  2849. (when (y-or-n-p "Save changed buffer?")
  2850. (save-buffer))))
  2851. (defun org-clocktable-try-shift (dir n)
  2852. "Check if this line starts a clock table, if yes, shift the time block."
  2853. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2854. (org-clocktable-shift dir n)))
  2855. ;; Autoload org-timer.el
  2856. (eval-and-compile
  2857. (org-autoload
  2858. "org-timer"
  2859. '(org-timer-start org-timer org-timer-item
  2860. org-timer-change-times-in-region
  2861. org-timer-set-timer
  2862. org-timer-reset-timers
  2863. org-timer-show-remaining-time)))
  2864. ;; Autoload org-feed.el
  2865. (eval-and-compile
  2866. (org-autoload
  2867. "org-feed"
  2868. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2869. ;; Autoload org-indent.el
  2870. (eval-and-compile
  2871. (org-autoload
  2872. "org-indent"
  2873. '(org-indent-mode)))
  2874. ;; Autoload org-mobile.el
  2875. (eval-and-compile
  2876. (org-autoload
  2877. "org-mobile"
  2878. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  2879. ;; Autoload archiving code
  2880. ;; The stuff that is needed for cycling and tags has to be defined here.
  2881. (defgroup org-archive nil
  2882. "Options concerning archiving in Org-mode."
  2883. :tag "Org Archive"
  2884. :group 'org-structure)
  2885. (defcustom org-archive-location "%s_archive::"
  2886. "The location where subtrees should be archived.
  2887. The value of this variable is a string, consisting of two parts,
  2888. separated by a double-colon. The first part is a filename and
  2889. the second part is a headline.
  2890. When the filename is omitted, archiving happens in the same file.
  2891. %s in the filename will be replaced by the current file
  2892. name (without the directory part). Archiving to a different file
  2893. is useful to keep archived entries from contributing to the
  2894. Org-mode Agenda.
  2895. The archived entries will be filed as subtrees of the specified
  2896. headline. When the headline is omitted, the subtrees are simply
  2897. filed away at the end of the file, as top-level entries. Also in
  2898. the heading you can use %s to represent the file name, this can be
  2899. useful when using the same archive for a number of different files.
  2900. Here are a few examples:
  2901. \"%s_archive::\"
  2902. If the current file is Projects.org, archive in file
  2903. Projects.org_archive, as top-level trees. This is the default.
  2904. \"::* Archived Tasks\"
  2905. Archive in the current file, under the top-level headline
  2906. \"* Archived Tasks\".
  2907. \"~/org/archive.org::\"
  2908. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2909. \"~/org/archive.org::From %s\"
  2910. Archive in file ~/org/archive.org (absolute path), und headlines
  2911. \"From FILENAME\" where file name is the current file name.
  2912. \"basement::** Finished Tasks\"
  2913. Archive in file ./basement (relative path), as level 3 trees
  2914. below the level 2 heading \"** Finished Tasks\".
  2915. You may set this option on a per-file basis by adding to the buffer a
  2916. line like
  2917. #+ARCHIVE: basement::** Finished Tasks
  2918. You may also define it locally for a subtree by setting an ARCHIVE property
  2919. in the entry. If such a property is found in an entry, or anywhere up
  2920. the hierarchy, it will be used."
  2921. :group 'org-archive
  2922. :type 'string)
  2923. (defcustom org-archive-tag "ARCHIVE"
  2924. "The tag that marks a subtree as archived.
  2925. An archived subtree does not open during visibility cycling, and does
  2926. not contribute to the agenda listings.
  2927. After changing this, font-lock must be restarted in the relevant buffers to
  2928. get the proper fontification."
  2929. :group 'org-archive
  2930. :group 'org-keywords
  2931. :type 'string)
  2932. (defcustom org-agenda-skip-archived-trees t
  2933. "Non-nil means, the agenda will skip any items located in archived trees.
  2934. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2935. variable is no longer recommended, you should leave it at the value t.
  2936. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2937. :group 'org-archive
  2938. :group 'org-agenda-skip
  2939. :type 'boolean)
  2940. (defcustom org-columns-skip-arrchived-trees t
  2941. "Non-nil means, irgnore archived trees when creating column view."
  2942. :group 'org-archive
  2943. :group 'org-properties
  2944. :type 'boolean)
  2945. (defcustom org-cycle-open-archived-trees nil
  2946. "Non-nil means, `org-cycle' will open archived trees.
  2947. An archived tree is a tree marked with the tag ARCHIVE.
  2948. When nil, archived trees will stay folded. You can still open them with
  2949. normal outline commands like `show-all', but not with the cycling commands."
  2950. :group 'org-archive
  2951. :group 'org-cycle
  2952. :type 'boolean)
  2953. (defcustom org-sparse-tree-open-archived-trees nil
  2954. "Non-nil means sparse tree construction shows matches in archived trees.
  2955. When nil, matches in these trees are highlighted, but the trees are kept in
  2956. collapsed state."
  2957. :group 'org-archive
  2958. :group 'org-sparse-trees
  2959. :type 'boolean)
  2960. (defun org-cycle-hide-archived-subtrees (state)
  2961. "Re-hide all archived subtrees after a visibility state change."
  2962. (when (and (not org-cycle-open-archived-trees)
  2963. (not (memq state '(overview folded))))
  2964. (save-excursion
  2965. (let* ((globalp (memq state '(contents all)))
  2966. (beg (if globalp (point-min) (point)))
  2967. (end (if globalp (point-max) (org-end-of-subtree t))))
  2968. (org-hide-archived-subtrees beg end)
  2969. (goto-char beg)
  2970. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2971. (message "%s" (substitute-command-keys
  2972. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2973. (defun org-force-cycle-archived ()
  2974. "Cycle subtree even if it is archived."
  2975. (interactive)
  2976. (setq this-command 'org-cycle)
  2977. (let ((org-cycle-open-archived-trees t))
  2978. (call-interactively 'org-cycle)))
  2979. (defun org-hide-archived-subtrees (beg end)
  2980. "Re-hide all archived subtrees after a visibility state change."
  2981. (save-excursion
  2982. (let* ((re (concat ":" org-archive-tag ":")))
  2983. (goto-char beg)
  2984. (while (re-search-forward re end t)
  2985. (and (org-on-heading-p) (hide-subtree))
  2986. (org-end-of-subtree t)))))
  2987. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2988. (eval-and-compile
  2989. (org-autoload "org-archive"
  2990. '(org-add-archive-files org-archive-subtree
  2991. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2992. ;; Autoload Column View Code
  2993. (declare-function org-columns-number-to-string "org-colview")
  2994. (declare-function org-columns-get-format-and-top-level "org-colview")
  2995. (declare-function org-columns-compute "org-colview")
  2996. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2997. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2998. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2999. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3000. ;; Autoload ID code
  3001. (declare-function org-id-store-link "org-id")
  3002. (declare-function org-id-locations-load "org-id")
  3003. (declare-function org-id-locations-save "org-id")
  3004. (defvar org-id-track-globally)
  3005. (org-autoload "org-id"
  3006. '(org-id-get-create org-id-new org-id-copy org-id-get
  3007. org-id-get-with-outline-path-completion
  3008. org-id-get-with-outline-drilling
  3009. org-id-goto org-id-find org-id-store-link))
  3010. ;; Autoload Plotting Code
  3011. (org-autoload "org-plot"
  3012. '(org-plot/gnuplot))
  3013. ;;; Variables for pre-computed regular expressions, all buffer local
  3014. (defvar org-drawer-regexp nil
  3015. "Matches first line of a hidden block.")
  3016. (make-variable-buffer-local 'org-drawer-regexp)
  3017. (defvar org-todo-regexp nil
  3018. "Matches any of the TODO state keywords.")
  3019. (make-variable-buffer-local 'org-todo-regexp)
  3020. (defvar org-not-done-regexp nil
  3021. "Matches any of the TODO state keywords except the last one.")
  3022. (make-variable-buffer-local 'org-not-done-regexp)
  3023. (defvar org-not-done-heading-regexp nil
  3024. "Matches a TODO headline that is not done.")
  3025. (make-variable-buffer-local 'org-not-done-regexp)
  3026. (defvar org-todo-line-regexp nil
  3027. "Matches a headline and puts TODO state into group 2 if present.")
  3028. (make-variable-buffer-local 'org-todo-line-regexp)
  3029. (defvar org-complex-heading-regexp nil
  3030. "Matches a headline and puts everything into groups:
  3031. group 1: the stars
  3032. group 2: The todo keyword, maybe
  3033. group 3: Priority cookie
  3034. group 4: True headline
  3035. group 5: Tags")
  3036. (make-variable-buffer-local 'org-complex-heading-regexp)
  3037. (defvar org-complex-heading-regexp-format nil)
  3038. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3039. (defvar org-todo-line-tags-regexp nil
  3040. "Matches a headline and puts TODO state into group 2 if present.
  3041. Also put tags into group 4 if tags are present.")
  3042. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3043. (defvar org-nl-done-regexp nil
  3044. "Matches newline followed by a headline with the DONE keyword.")
  3045. (make-variable-buffer-local 'org-nl-done-regexp)
  3046. (defvar org-looking-at-done-regexp nil
  3047. "Matches the DONE keyword a point.")
  3048. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3049. (defvar org-ds-keyword-length 12
  3050. "Maximum length of the Deadline and SCHEDULED keywords.")
  3051. (make-variable-buffer-local 'org-ds-keyword-length)
  3052. (defvar org-deadline-regexp nil
  3053. "Matches the DEADLINE keyword.")
  3054. (make-variable-buffer-local 'org-deadline-regexp)
  3055. (defvar org-deadline-time-regexp nil
  3056. "Matches the DEADLINE keyword together with a time stamp.")
  3057. (make-variable-buffer-local 'org-deadline-time-regexp)
  3058. (defvar org-deadline-line-regexp nil
  3059. "Matches the DEADLINE keyword and the rest of the line.")
  3060. (make-variable-buffer-local 'org-deadline-line-regexp)
  3061. (defvar org-scheduled-regexp nil
  3062. "Matches the SCHEDULED keyword.")
  3063. (make-variable-buffer-local 'org-scheduled-regexp)
  3064. (defvar org-scheduled-time-regexp nil
  3065. "Matches the SCHEDULED keyword together with a time stamp.")
  3066. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3067. (defvar org-closed-time-regexp nil
  3068. "Matches the CLOSED keyword together with a time stamp.")
  3069. (make-variable-buffer-local 'org-closed-time-regexp)
  3070. (defvar org-keyword-time-regexp nil
  3071. "Matches any of the 4 keywords, together with the time stamp.")
  3072. (make-variable-buffer-local 'org-keyword-time-regexp)
  3073. (defvar org-keyword-time-not-clock-regexp nil
  3074. "Matches any of the 3 keywords, together with the time stamp.")
  3075. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3076. (defvar org-maybe-keyword-time-regexp nil
  3077. "Matches a timestamp, possibly preceeded by a keyword.")
  3078. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3079. (defvar org-planning-or-clock-line-re nil
  3080. "Matches a line with planning or clock info.")
  3081. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3082. (defconst org-plain-time-of-day-regexp
  3083. (concat
  3084. "\\(\\<[012]?[0-9]"
  3085. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3086. "\\(--?"
  3087. "\\(\\<[012]?[0-9]"
  3088. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3089. "\\)?")
  3090. "Regular expression to match a plain time or time range.
  3091. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3092. groups carry important information:
  3093. 0 the full match
  3094. 1 the first time, range or not
  3095. 8 the second time, if it is a range.")
  3096. (defconst org-plain-time-extension-regexp
  3097. (concat
  3098. "\\(\\<[012]?[0-9]"
  3099. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3100. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3101. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3102. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3103. groups carry important information:
  3104. 0 the full match
  3105. 7 hours of duration
  3106. 9 minutes of duration")
  3107. (defconst org-stamp-time-of-day-regexp
  3108. (concat
  3109. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3110. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3111. "\\(--?"
  3112. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3113. "Regular expression to match a timestamp time or time range.
  3114. After a match, the following groups carry important information:
  3115. 0 the full match
  3116. 1 date plus weekday, for backreferencing to make sure both times on same day
  3117. 2 the first time, range or not
  3118. 4 the second time, if it is a range.")
  3119. (defconst org-startup-options
  3120. '(("fold" org-startup-folded t)
  3121. ("overview" org-startup-folded t)
  3122. ("nofold" org-startup-folded nil)
  3123. ("showall" org-startup-folded nil)
  3124. ("showeverything" org-startup-folded showeverything)
  3125. ("content" org-startup-folded content)
  3126. ("indent" org-startup-indented t)
  3127. ("noindent" org-startup-indented nil)
  3128. ("hidestars" org-hide-leading-stars t)
  3129. ("showstars" org-hide-leading-stars nil)
  3130. ("odd" org-odd-levels-only t)
  3131. ("oddeven" org-odd-levels-only nil)
  3132. ("align" org-startup-align-all-tables t)
  3133. ("noalign" org-startup-align-all-tables nil)
  3134. ("customtime" org-display-custom-times t)
  3135. ("logdone" org-log-done time)
  3136. ("lognotedone" org-log-done note)
  3137. ("nologdone" org-log-done nil)
  3138. ("lognoteclock-out" org-log-note-clock-out t)
  3139. ("nolognoteclock-out" org-log-note-clock-out nil)
  3140. ("logrepeat" org-log-repeat state)
  3141. ("lognoterepeat" org-log-repeat note)
  3142. ("nologrepeat" org-log-repeat nil)
  3143. ("fninline" org-footnote-define-inline t)
  3144. ("nofninline" org-footnote-define-inline nil)
  3145. ("fnlocal" org-footnote-section nil)
  3146. ("fnauto" org-footnote-auto-label t)
  3147. ("fnprompt" org-footnote-auto-label nil)
  3148. ("fnconfirm" org-footnote-auto-label confirm)
  3149. ("fnplain" org-footnote-auto-label plain)
  3150. ("fnadjust" org-footnote-auto-adjust t)
  3151. ("nofnadjust" org-footnote-auto-adjust nil)
  3152. ("constcgs" constants-unit-system cgs)
  3153. ("constSI" constants-unit-system SI)
  3154. ("noptag" org-tag-persistent-alist nil)
  3155. ("hideblocks" org-hide-block-startup t)
  3156. ("nohideblocks" org-hide-block-startup nil))
  3157. "Variable associated with STARTUP options for org-mode.
  3158. Each element is a list of three items: The startup options as written
  3159. in the #+STARTUP line, the corresponding variable, and the value to
  3160. set this variable to if the option is found. An optional forth element PUSH
  3161. means to push this value onto the list in the variable.")
  3162. (defun org-set-regexps-and-options ()
  3163. "Precompute regular expressions for current buffer."
  3164. (when (org-mode-p)
  3165. (org-set-local 'org-todo-kwd-alist nil)
  3166. (org-set-local 'org-todo-key-alist nil)
  3167. (org-set-local 'org-todo-key-trigger nil)
  3168. (org-set-local 'org-todo-keywords-1 nil)
  3169. (org-set-local 'org-done-keywords nil)
  3170. (org-set-local 'org-todo-heads nil)
  3171. (org-set-local 'org-todo-sets nil)
  3172. (org-set-local 'org-todo-log-states nil)
  3173. (org-set-local 'org-file-properties nil)
  3174. (org-set-local 'org-file-tags nil)
  3175. (let ((re (org-make-options-regexp
  3176. '("CATEGORY" "TODO" "COLUMNS"
  3177. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3178. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3179. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3180. (splitre "[ \t]+")
  3181. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3182. tail sep kws1 prio props ftags drawers
  3183. ext-setup-or-nil setup-contents (start 0))
  3184. (save-excursion
  3185. (save-restriction
  3186. (widen)
  3187. (goto-char (point-min))
  3188. (while (or (and ext-setup-or-nil
  3189. (string-match re ext-setup-or-nil start)
  3190. (setq start (match-end 0)))
  3191. (and (setq ext-setup-or-nil nil start 0)
  3192. (re-search-forward re nil t)))
  3193. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3194. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3195. (cond
  3196. ((equal key "CATEGORY")
  3197. (if (string-match "[ \t]+$" value)
  3198. (setq value (replace-match "" t t value)))
  3199. (setq cat value))
  3200. ((member key '("SEQ_TODO" "TODO"))
  3201. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3202. ((equal key "TYP_TODO")
  3203. (push (cons 'type (org-split-string value splitre)) kwds))
  3204. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3205. ;; general TODO-like setup
  3206. (push (cons (intern (downcase (match-string 1 key)))
  3207. (org-split-string value splitre)) kwds))
  3208. ((equal key "TAGS")
  3209. (setq tags (append tags (if tags '("\\n") nil)
  3210. (org-split-string value splitre))))
  3211. ((equal key "COLUMNS")
  3212. (org-set-local 'org-columns-default-format value))
  3213. ((equal key "LINK")
  3214. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3215. (push (cons (match-string 1 value)
  3216. (org-trim (match-string 2 value)))
  3217. links)))
  3218. ((equal key "PRIORITIES")
  3219. (setq prio (org-split-string value " +")))
  3220. ((equal key "PROPERTY")
  3221. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3222. (push (cons (match-string 1 value) (match-string 2 value))
  3223. props)))
  3224. ((equal key "FILETAGS")
  3225. (when (string-match "\\S-" value)
  3226. (setq ftags
  3227. (append
  3228. ftags
  3229. (apply 'append
  3230. (mapcar (lambda (x) (org-split-string x ":"))
  3231. (org-split-string value)))))))
  3232. ((equal key "DRAWERS")
  3233. (setq drawers (org-split-string value splitre)))
  3234. ((equal key "CONSTANTS")
  3235. (setq const (append const (org-split-string value splitre))))
  3236. ((equal key "STARTUP")
  3237. (let ((opts (org-split-string value splitre))
  3238. l var val)
  3239. (while (setq l (pop opts))
  3240. (when (setq l (assoc l org-startup-options))
  3241. (setq var (nth 1 l) val (nth 2 l))
  3242. (if (not (nth 3 l))
  3243. (set (make-local-variable var) val)
  3244. (if (not (listp (symbol-value var)))
  3245. (set (make-local-variable var) nil))
  3246. (set (make-local-variable var) (symbol-value var))
  3247. (add-to-list var val))))))
  3248. ((equal key "ARCHIVE")
  3249. (string-match " *$" value)
  3250. (setq arch (replace-match "" t t value))
  3251. (remove-text-properties 0 (length arch)
  3252. '(face t fontified t) arch))
  3253. ((equal key "SETUPFILE")
  3254. (setq setup-contents (org-file-contents
  3255. (expand-file-name
  3256. (org-remove-double-quotes value))
  3257. 'noerror))
  3258. (if (not ext-setup-or-nil)
  3259. (setq ext-setup-or-nil setup-contents start 0)
  3260. (setq ext-setup-or-nil
  3261. (concat (substring ext-setup-or-nil 0 start)
  3262. "\n" setup-contents "\n"
  3263. (substring ext-setup-or-nil start)))))
  3264. ))))
  3265. (when cat
  3266. (org-set-local 'org-category (intern cat))
  3267. (push (cons "CATEGORY" cat) props))
  3268. (when prio
  3269. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3270. (setq prio (mapcar 'string-to-char prio))
  3271. (org-set-local 'org-highest-priority (nth 0 prio))
  3272. (org-set-local 'org-lowest-priority (nth 1 prio))
  3273. (org-set-local 'org-default-priority (nth 2 prio)))
  3274. (and props (org-set-local 'org-file-properties (nreverse props)))
  3275. (and ftags (org-set-local 'org-file-tags
  3276. (mapcar 'org-add-prop-inherited ftags)))
  3277. (and drawers (org-set-local 'org-drawers drawers))
  3278. (and arch (org-set-local 'org-archive-location arch))
  3279. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3280. ;; Process the TODO keywords
  3281. (unless kwds
  3282. ;; Use the global values as if they had been given locally.
  3283. (setq kwds (default-value 'org-todo-keywords))
  3284. (if (stringp (car kwds))
  3285. (setq kwds (list (cons org-todo-interpretation
  3286. (default-value 'org-todo-keywords)))))
  3287. (setq kwds (reverse kwds)))
  3288. (setq kwds (nreverse kwds))
  3289. (let (inter kws kw)
  3290. (while (setq kws (pop kwds))
  3291. (let ((kws (or
  3292. (run-hook-with-args-until-success
  3293. 'org-todo-setup-filter-hook kws)
  3294. kws)))
  3295. (setq inter (pop kws) sep (member "|" kws)
  3296. kws0 (delete "|" (copy-sequence kws))
  3297. kwsa nil
  3298. kws1 (mapcar
  3299. (lambda (x)
  3300. ;; 1 2
  3301. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3302. (progn
  3303. (setq kw (match-string 1 x)
  3304. key (and (match-end 2) (match-string 2 x))
  3305. log (org-extract-log-state-settings x))
  3306. (push (cons kw (and key (string-to-char key))) kwsa)
  3307. (and log (push log org-todo-log-states))
  3308. kw)
  3309. (error "Invalid TODO keyword %s" x)))
  3310. kws0)
  3311. kwsa (if kwsa (append '((:startgroup))
  3312. (nreverse kwsa)
  3313. '((:endgroup))))
  3314. hw (car kws1)
  3315. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3316. tail (list inter hw (car dws) (org-last dws))))
  3317. (add-to-list 'org-todo-heads hw 'append)
  3318. (push kws1 org-todo-sets)
  3319. (setq org-done-keywords (append org-done-keywords dws nil))
  3320. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3321. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3322. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3323. (setq org-todo-sets (nreverse org-todo-sets)
  3324. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3325. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3326. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3327. ;; Process the constants
  3328. (when const
  3329. (let (e cst)
  3330. (while (setq e (pop const))
  3331. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3332. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3333. (setq org-table-formula-constants-local cst)))
  3334. ;; Process the tags.
  3335. (when tags
  3336. (let (e tgs)
  3337. (while (setq e (pop tags))
  3338. (cond
  3339. ((equal e "{") (push '(:startgroup) tgs))
  3340. ((equal e "}") (push '(:endgroup) tgs))
  3341. ((equal e "\\n") (push '(:newline) tgs))
  3342. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3343. (push (cons (match-string 1 e)
  3344. (string-to-char (match-string 2 e)))
  3345. tgs))
  3346. (t (push (list e) tgs))))
  3347. (org-set-local 'org-tag-alist nil)
  3348. (while (setq e (pop tgs))
  3349. (or (and (stringp (car e))
  3350. (assoc (car e) org-tag-alist))
  3351. (push e org-tag-alist)))))
  3352. ;; Compute the regular expressions and other local variables
  3353. (if (not org-done-keywords)
  3354. (setq org-done-keywords (and org-todo-keywords-1
  3355. (list (org-last org-todo-keywords-1)))))
  3356. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3357. (length org-scheduled-string)
  3358. (length org-clock-string)
  3359. (length org-closed-string)))
  3360. org-drawer-regexp
  3361. (concat "^[ \t]*:\\("
  3362. (mapconcat 'regexp-quote org-drawers "\\|")
  3363. "\\):[ \t]*$")
  3364. org-not-done-keywords
  3365. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3366. org-todo-regexp
  3367. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3368. "\\|") "\\)\\>")
  3369. org-not-done-regexp
  3370. (concat "\\<\\("
  3371. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3372. "\\)\\>")
  3373. org-not-done-heading-regexp
  3374. (concat "^\\(\\*+\\)[ \t]+\\("
  3375. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3376. "\\)\\>")
  3377. org-todo-line-regexp
  3378. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3379. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3380. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3381. org-complex-heading-regexp
  3382. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3383. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3384. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3385. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3386. org-complex-heading-regexp-format
  3387. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3388. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3389. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3390. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3391. org-nl-done-regexp
  3392. (concat "\n\\*+[ \t]+"
  3393. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3394. "\\)" "\\>")
  3395. org-todo-line-tags-regexp
  3396. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3397. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3398. (org-re
  3399. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3400. org-looking-at-done-regexp
  3401. (concat "^" "\\(?:"
  3402. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3403. "\\>")
  3404. org-deadline-regexp (concat "\\<" org-deadline-string)
  3405. org-deadline-time-regexp
  3406. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3407. org-deadline-line-regexp
  3408. (concat "\\<\\(" org-deadline-string "\\).*")
  3409. org-scheduled-regexp
  3410. (concat "\\<" org-scheduled-string)
  3411. org-scheduled-time-regexp
  3412. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3413. org-closed-time-regexp
  3414. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3415. org-keyword-time-regexp
  3416. (concat "\\<\\(" org-scheduled-string
  3417. "\\|" org-deadline-string
  3418. "\\|" org-closed-string
  3419. "\\|" org-clock-string "\\)"
  3420. " *[[<]\\([^]>]+\\)[]>]")
  3421. org-keyword-time-not-clock-regexp
  3422. (concat "\\<\\(" org-scheduled-string
  3423. "\\|" org-deadline-string
  3424. "\\|" org-closed-string
  3425. "\\)"
  3426. " *[[<]\\([^]>]+\\)[]>]")
  3427. org-maybe-keyword-time-regexp
  3428. (concat "\\(\\<\\(" org-scheduled-string
  3429. "\\|" org-deadline-string
  3430. "\\|" org-closed-string
  3431. "\\|" org-clock-string "\\)\\)?"
  3432. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3433. org-planning-or-clock-line-re
  3434. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3435. "\\|" org-deadline-string
  3436. "\\|" org-closed-string "\\|" org-clock-string
  3437. "\\)\\>\\)")
  3438. )
  3439. (org-compute-latex-and-specials-regexp)
  3440. (org-set-font-lock-defaults))))
  3441. (defun org-file-contents (file &optional noerror)
  3442. "Return the contents of FILE, as a string."
  3443. (if (or (not file)
  3444. (not (file-readable-p file)))
  3445. (if noerror
  3446. (progn
  3447. (message "Cannot read file %s" file)
  3448. (ding) (sit-for 2)
  3449. "")
  3450. (error "Cannot read file %s" file))
  3451. (with-temp-buffer
  3452. (insert-file-contents file)
  3453. (buffer-string))))
  3454. (defun org-extract-log-state-settings (x)
  3455. "Extract the log state setting from a TODO keyword string.
  3456. This will extract info from a string like \"WAIT(w@/!)\"."
  3457. (let (kw key log1 log2)
  3458. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3459. (setq kw (match-string 1 x)
  3460. key (and (match-end 2) (match-string 2 x))
  3461. log1 (and (match-end 3) (match-string 3 x))
  3462. log2 (and (match-end 4) (match-string 4 x)))
  3463. (and (or log1 log2)
  3464. (list kw
  3465. (and log1 (if (equal log1 "!") 'time 'note))
  3466. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3467. (defun org-remove-keyword-keys (list)
  3468. "Remove a pair of parenthesis at the end of each string in LIST."
  3469. (mapcar (lambda (x)
  3470. (if (string-match "(.*)$" x)
  3471. (substring x 0 (match-beginning 0))
  3472. x))
  3473. list))
  3474. ;; FIXME: this could be done much better, using second characters etc.
  3475. (defun org-assign-fast-keys (alist)
  3476. "Assign fast keys to a keyword-key alist.
  3477. Respect keys that are already there."
  3478. (let (new e k c c1 c2 (char ?a))
  3479. (while (setq e (pop alist))
  3480. (cond
  3481. ((equal e '(:startgroup)) (push e new))
  3482. ((equal e '(:endgroup)) (push e new))
  3483. ((equal e '(:newline)) (push e new))
  3484. (t
  3485. (setq k (car e) c2 nil)
  3486. (if (cdr e)
  3487. (setq c (cdr e))
  3488. ;; automatically assign a character.
  3489. (setq c1 (string-to-char
  3490. (downcase (substring
  3491. k (if (= (string-to-char k) ?@) 1 0)))))
  3492. (if (or (rassoc c1 new) (rassoc c1 alist))
  3493. (while (or (rassoc char new) (rassoc char alist))
  3494. (setq char (1+ char)))
  3495. (setq c2 c1))
  3496. (setq c (or c2 char)))
  3497. (push (cons k c) new))))
  3498. (nreverse new)))
  3499. ;;; Some variables used in various places
  3500. (defvar org-window-configuration nil
  3501. "Used in various places to store a window configuration.")
  3502. (defvar org-selected-window nil
  3503. "Used in various places to store a window configuration.")
  3504. (defvar org-finish-function nil
  3505. "Function to be called when `C-c C-c' is used.
  3506. This is for getting out of special buffers like remember.")
  3507. ;; FIXME: Occasionally check by commenting these, to make sure
  3508. ;; no other functions uses these, forgetting to let-bind them.
  3509. (defvar entry)
  3510. (defvar last-state)
  3511. (defvar date)
  3512. ;; Defined somewhere in this file, but used before definition.
  3513. (defvar org-html-entities)
  3514. (defvar org-struct-menu)
  3515. (defvar org-org-menu)
  3516. (defvar org-tbl-menu)
  3517. (defvar org-agenda-keymap)
  3518. ;;;; Define the Org-mode
  3519. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3520. (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."))
  3521. ;; We use a before-change function to check if a table might need
  3522. ;; an update.
  3523. (defvar org-table-may-need-update t
  3524. "Indicates that a table might need an update.
  3525. This variable is set by `org-before-change-function'.
  3526. `org-table-align' sets it back to nil.")
  3527. (defun org-before-change-function (beg end)
  3528. "Every change indicates that a table might need an update."
  3529. (setq org-table-may-need-update t))
  3530. (defvar org-mode-map)
  3531. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3532. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3533. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3534. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3535. (defvar org-table-buffer-is-an nil)
  3536. (defconst org-outline-regexp "\\*+ ")
  3537. ;;;###autoload
  3538. (define-derived-mode org-mode outline-mode "Org"
  3539. "Outline-based notes management and organizer, alias
  3540. \"Carsten's outline-mode for keeping track of everything.\"
  3541. Org-mode develops organizational tasks around a NOTES file which
  3542. contains information about projects as plain text. Org-mode is
  3543. implemented on top of outline-mode, which is ideal to keep the content
  3544. of large files well structured. It supports ToDo items, deadlines and
  3545. time stamps, which magically appear in the diary listing of the Emacs
  3546. calendar. Tables are easily created with a built-in table editor.
  3547. Plain text URL-like links connect to websites, emails (VM), Usenet
  3548. messages (Gnus), BBDB entries, and any files related to the project.
  3549. For printing and sharing of notes, an Org-mode file (or a part of it)
  3550. can be exported as a structured ASCII or HTML file.
  3551. The following commands are available:
  3552. \\{org-mode-map}"
  3553. ;; Get rid of Outline menus, they are not needed
  3554. ;; Need to do this here because define-derived-mode sets up
  3555. ;; the keymap so late. Still, it is a waste to call this each time
  3556. ;; we switch another buffer into org-mode.
  3557. (if (featurep 'xemacs)
  3558. (when (boundp 'outline-mode-menu-heading)
  3559. ;; Assume this is Greg's port, it used easymenu
  3560. (easy-menu-remove outline-mode-menu-heading)
  3561. (easy-menu-remove outline-mode-menu-show)
  3562. (easy-menu-remove outline-mode-menu-hide))
  3563. (define-key org-mode-map [menu-bar headings] 'undefined)
  3564. (define-key org-mode-map [menu-bar hide] 'undefined)
  3565. (define-key org-mode-map [menu-bar show] 'undefined))
  3566. (org-load-modules-maybe)
  3567. (easy-menu-add org-org-menu)
  3568. (easy-menu-add org-tbl-menu)
  3569. (org-install-agenda-files-menu)
  3570. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3571. (org-add-to-invisibility-spec '(org-cwidth))
  3572. (org-add-to-invisibility-spec '(org-hide-block . t))
  3573. (when (featurep 'xemacs)
  3574. (org-set-local 'line-move-ignore-invisible t))
  3575. (org-set-local 'outline-regexp org-outline-regexp)
  3576. (org-set-local 'outline-level 'org-outline-level)
  3577. (when (and org-ellipsis
  3578. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3579. (fboundp 'make-glyph-code))
  3580. (unless org-display-table
  3581. (setq org-display-table (make-display-table)))
  3582. (set-display-table-slot
  3583. org-display-table 4
  3584. (vconcat (mapcar
  3585. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3586. org-ellipsis)))
  3587. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3588. (setq buffer-display-table org-display-table))
  3589. (org-set-regexps-and-options)
  3590. (when (and org-tag-faces (not org-tags-special-faces-re))
  3591. ;; tag faces set outside customize.... force initialization.
  3592. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3593. ;; Calc embedded
  3594. (org-set-local 'calc-embedded-open-mode "# ")
  3595. (modify-syntax-entry ?# "<")
  3596. (modify-syntax-entry ?@ "w")
  3597. (if org-startup-truncated (setq truncate-lines t))
  3598. (org-set-local 'font-lock-unfontify-region-function
  3599. 'org-unfontify-region)
  3600. ;; Activate before-change-function
  3601. (org-set-local 'org-table-may-need-update t)
  3602. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3603. 'local)
  3604. ;; Check for running clock before killing a buffer
  3605. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3606. ;; Paragraphs and auto-filling
  3607. (org-set-autofill-regexps)
  3608. (setq indent-line-function 'org-indent-line-function)
  3609. (org-update-radio-target-regexp)
  3610. ;; Make sure dependence stuff works reliably, even for users who set it
  3611. ;; too late :-(
  3612. (if org-enforce-todo-dependencies
  3613. (add-hook 'org-blocker-hook
  3614. 'org-block-todo-from-children-or-siblings-or-parent)
  3615. (remove-hook 'org-blocker-hook
  3616. 'org-block-todo-from-children-or-siblings-or-parent))
  3617. (if org-enforce-todo-checkbox-dependencies
  3618. (add-hook 'org-blocker-hook
  3619. 'org-block-todo-from-checkboxes)
  3620. (remove-hook 'org-blocker-hook
  3621. 'org-block-todo-from-checkboxes))
  3622. ;; Comment characters
  3623. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3624. (org-set-local 'comment-padding " ")
  3625. ;; Align options lines
  3626. (org-set-local
  3627. 'align-mode-rules-list
  3628. '((org-in-buffer-settings
  3629. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3630. (modes . '(org-mode)))))
  3631. ;; Imenu
  3632. (org-set-local 'imenu-create-index-function
  3633. 'org-imenu-get-tree)
  3634. ;; Make isearch reveal context
  3635. (if (or (featurep 'xemacs)
  3636. (not (boundp 'outline-isearch-open-invisible-function)))
  3637. ;; Emacs 21 and XEmacs make use of the hook
  3638. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3639. ;; Emacs 22 deals with this through a special variable
  3640. (org-set-local 'outline-isearch-open-invisible-function
  3641. (lambda (&rest ignore) (org-show-context 'isearch))))
  3642. ;; If empty file that did not turn on org-mode automatically, make it to.
  3643. (if (and org-insert-mode-line-in-empty-file
  3644. (interactive-p)
  3645. (= (point-min) (point-max)))
  3646. (insert "# -*- mode: org -*-\n\n"))
  3647. (unless org-inhibit-startup
  3648. (when org-startup-align-all-tables
  3649. (let ((bmp (buffer-modified-p)))
  3650. (org-table-map-tables 'org-table-align)
  3651. (set-buffer-modified-p bmp)))
  3652. (when org-startup-indented
  3653. (require 'org-indent)
  3654. (org-indent-mode 1))
  3655. (org-set-startup-visibility)))
  3656. (when (fboundp 'abbrev-table-put)
  3657. (abbrev-table-put org-mode-abbrev-table
  3658. :parents (list text-mode-abbrev-table)))
  3659. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3660. (defun org-current-time ()
  3661. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3662. (if (> (car org-time-stamp-rounding-minutes) 1)
  3663. (let ((r (car org-time-stamp-rounding-minutes))
  3664. (time (decode-time)))
  3665. (apply 'encode-time
  3666. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3667. (nthcdr 2 time))))
  3668. (current-time)))
  3669. ;;;; Font-Lock stuff, including the activators
  3670. (defvar org-mouse-map (make-sparse-keymap))
  3671. (org-defkey org-mouse-map
  3672. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3673. (org-defkey org-mouse-map
  3674. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3675. (when org-mouse-1-follows-link
  3676. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3677. (when org-tab-follows-link
  3678. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3679. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3680. (require 'font-lock)
  3681. (defconst org-non-link-chars "]\t\n\r<>")
  3682. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3683. "shell" "elisp"))
  3684. (defvar org-link-types-re nil
  3685. "Matches a link that has a url-like prefix like \"http:\"")
  3686. (defvar org-link-re-with-space nil
  3687. "Matches a link with spaces, optional angular brackets around it.")
  3688. (defvar org-link-re-with-space2 nil
  3689. "Matches a link with spaces, optional angular brackets around it.")
  3690. (defvar org-link-re-with-space3 nil
  3691. "Matches a link with spaces, only for internal part in bracket links.")
  3692. (defvar org-angle-link-re nil
  3693. "Matches link with angular brackets, spaces are allowed.")
  3694. (defvar org-plain-link-re nil
  3695. "Matches plain link, without spaces.")
  3696. (defvar org-bracket-link-regexp nil
  3697. "Matches a link in double brackets.")
  3698. (defvar org-bracket-link-analytic-regexp nil
  3699. "Regular expression used to analyze links.
  3700. Here is what the match groups contain after a match:
  3701. 1: http:
  3702. 2: http
  3703. 3: path
  3704. 4: [desc]
  3705. 5: desc")
  3706. (defvar org-bracket-link-analytic-regexp++ nil
  3707. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3708. (defvar org-any-link-re nil
  3709. "Regular expression matching any link.")
  3710. (defun org-make-link-regexps ()
  3711. "Update the link regular expressions.
  3712. This should be called after the variable `org-link-types' has changed."
  3713. (setq org-link-types-re
  3714. (concat
  3715. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3716. org-link-re-with-space
  3717. (concat
  3718. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3719. "\\([^" org-non-link-chars " ]"
  3720. "[^" org-non-link-chars "]*"
  3721. "[^" org-non-link-chars " ]\\)>?")
  3722. org-link-re-with-space2
  3723. (concat
  3724. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3725. "\\([^" org-non-link-chars " ]"
  3726. "[^\t\n\r]*"
  3727. "[^" org-non-link-chars " ]\\)>?")
  3728. org-link-re-with-space3
  3729. (concat
  3730. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3731. "\\([^" org-non-link-chars " ]"
  3732. "[^\t\n\r]*\\)")
  3733. org-angle-link-re
  3734. (concat
  3735. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3736. "\\([^" org-non-link-chars " ]"
  3737. "[^" org-non-link-chars "]*"
  3738. "\\)>")
  3739. org-plain-link-re
  3740. (concat
  3741. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3742. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3743. org-bracket-link-regexp
  3744. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3745. org-bracket-link-analytic-regexp
  3746. (concat
  3747. "\\[\\["
  3748. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3749. "\\([^]]+\\)"
  3750. "\\]"
  3751. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3752. "\\]")
  3753. org-bracket-link-analytic-regexp++
  3754. (concat
  3755. "\\[\\["
  3756. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3757. "\\([^]]+\\)"
  3758. "\\]"
  3759. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3760. "\\]")
  3761. org-any-link-re
  3762. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3763. org-angle-link-re "\\)\\|\\("
  3764. org-plain-link-re "\\)")))
  3765. (org-make-link-regexps)
  3766. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3767. "Regular expression for fast time stamp matching.")
  3768. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3769. "Regular expression for fast time stamp matching.")
  3770. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3771. "Regular expression matching time strings for analysis.
  3772. This one does not require the space after the date, so it can be used
  3773. on a string that terminates immediately after the date.")
  3774. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3775. "Regular expression matching time strings for analysis.")
  3776. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3777. "Regular expression matching time stamps, with groups.")
  3778. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3779. "Regular expression matching time stamps (also [..]), with groups.")
  3780. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3781. "Regular expression matching a time stamp range.")
  3782. (defconst org-tr-regexp-both
  3783. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3784. "Regular expression matching a time stamp range.")
  3785. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3786. org-ts-regexp "\\)?")
  3787. "Regular expression matching a time stamp or time stamp range.")
  3788. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3789. org-ts-regexp-both "\\)?")
  3790. "Regular expression matching a time stamp or time stamp range.
  3791. The time stamps may be either active or inactive.")
  3792. (defvar org-emph-face nil)
  3793. (defun org-do-emphasis-faces (limit)
  3794. "Run through the buffer and add overlays to links."
  3795. (let (rtn a)
  3796. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3797. (if (not (= (char-after (match-beginning 3))
  3798. (char-after (match-beginning 4))))
  3799. (progn
  3800. (setq rtn t)
  3801. (setq a (assoc (match-string 3) org-emphasis-alist))
  3802. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3803. 'face
  3804. (nth 1 a))
  3805. (and (nth 4 a)
  3806. (org-remove-flyspell-overlays-in
  3807. (match-beginning 0) (match-end 0)))
  3808. (add-text-properties (match-beginning 2) (match-end 2)
  3809. '(font-lock-multiline t))
  3810. (when org-hide-emphasis-markers
  3811. (add-text-properties (match-end 4) (match-beginning 5)
  3812. '(invisible org-link))
  3813. (add-text-properties (match-beginning 3) (match-end 3)
  3814. '(invisible org-link)))))
  3815. (backward-char 1))
  3816. rtn))
  3817. (defun org-emphasize (&optional char)
  3818. "Insert or change an emphasis, i.e. a font like bold or italic.
  3819. If there is an active region, change that region to a new emphasis.
  3820. If there is no region, just insert the marker characters and position
  3821. the cursor between them.
  3822. CHAR should be either the marker character, or the first character of the
  3823. HTML tag associated with that emphasis. If CHAR is a space, the means
  3824. to remove the emphasis of the selected region.
  3825. If char is not given (for example in an interactive call) it
  3826. will be prompted for."
  3827. (interactive)
  3828. (let ((eal org-emphasis-alist) e det
  3829. (erc org-emphasis-regexp-components)
  3830. (prompt "")
  3831. (string "") beg end move tag c s)
  3832. (if (org-region-active-p)
  3833. (setq beg (region-beginning) end (region-end)
  3834. string (buffer-substring beg end))
  3835. (setq move t))
  3836. (while (setq e (pop eal))
  3837. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3838. c (aref tag 0))
  3839. (push (cons c (string-to-char (car e))) det)
  3840. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3841. (substring tag 1)))))
  3842. (setq det (nreverse det))
  3843. (unless char
  3844. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3845. (setq char (read-char-exclusive)))
  3846. (setq char (or (cdr (assoc char det)) char))
  3847. (if (equal char ?\ )
  3848. (setq s "" move nil)
  3849. (unless (assoc (char-to-string char) org-emphasis-alist)
  3850. (error "No such emphasis marker: \"%c\"" char))
  3851. (setq s (char-to-string char)))
  3852. (while (and (> (length string) 1)
  3853. (equal (substring string 0 1) (substring string -1))
  3854. (assoc (substring string 0 1) org-emphasis-alist))
  3855. (setq string (substring string 1 -1)))
  3856. (setq string (concat s string s))
  3857. (if beg (delete-region beg end))
  3858. (unless (or (bolp)
  3859. (string-match (concat "[" (nth 0 erc) "\n]")
  3860. (char-to-string (char-before (point)))))
  3861. (insert " "))
  3862. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3863. (char-to-string (char-after (point))))
  3864. (insert " ") (backward-char 1))
  3865. (insert string)
  3866. (and move (backward-char 1))))
  3867. (defconst org-nonsticky-props
  3868. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3869. (defsubst org-rear-nonsticky-at (pos)
  3870. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3871. (defun org-activate-plain-links (limit)
  3872. "Run through the buffer and add overlays to links."
  3873. (catch 'exit
  3874. (let (f)
  3875. (if (re-search-forward org-plain-link-re limit t)
  3876. (progn
  3877. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3878. (setq f (get-text-property (match-beginning 0) 'face))
  3879. (if (or (eq f 'org-tag)
  3880. (and (listp f) (memq 'org-tag f)))
  3881. nil
  3882. (add-text-properties (match-beginning 0) (match-end 0)
  3883. (list 'mouse-face 'highlight
  3884. 'keymap org-mouse-map))
  3885. (org-rear-nonsticky-at (match-end 0)))
  3886. t)))))
  3887. (defun org-activate-code (limit)
  3888. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3889. (progn
  3890. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3891. (remove-text-properties (match-beginning 0) (match-end 0)
  3892. '(display t invisible t intangible t))
  3893. t)))
  3894. (defun org-fontify-meta-lines-and-blocks (limit)
  3895. "Fontify #+ lines and blocks, in the correct ways."
  3896. (let ((case-fold-search t))
  3897. (if (re-search-forward
  3898. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3899. limit t)
  3900. (let ((beg (match-beginning 0))
  3901. (beg1 (line-beginning-position 2))
  3902. (dc1 (downcase (match-string 2)))
  3903. (dc3 (downcase (match-string 3)))
  3904. end end1 quoting)
  3905. (cond
  3906. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3907. ;; a single line of backend-specific content
  3908. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3909. (remove-text-properties (match-beginning 0) (match-end 0)
  3910. '(display t invisible t intangible t))
  3911. (add-text-properties (match-beginning 1) (match-end 3)
  3912. '(font-lock-fontified t face org-meta-line))
  3913. (add-text-properties (match-beginning 6) (match-end 6)
  3914. '(font-lock-fontified t face org-block))
  3915. t)
  3916. ((and (match-end 4) (equal dc3 "begin"))
  3917. ;; Truely a block
  3918. (setq quoting (member (downcase (match-string 5))
  3919. org-protecting-blocks))
  3920. (when (re-search-forward
  3921. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3922. nil t) ;; on purpose, we look further than LIMIT
  3923. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3924. (when quoting
  3925. (remove-text-properties beg end
  3926. '(display t invisible t intangible t)))
  3927. (add-text-properties
  3928. beg end
  3929. '(font-lock-fontified t font-lock-multiline t))
  3930. (add-text-properties beg beg1 '(face org-meta-line))
  3931. (add-text-properties end1 end '(face org-meta-line))
  3932. (when quoting
  3933. (add-text-properties beg1 end1 '(face org-block)))
  3934. t))
  3935. ((not (member (char-after beg) '(?\ ?\t)))
  3936. ;; just any other in-buffer setting, but not indented
  3937. (add-text-properties
  3938. beg (match-end 0)
  3939. '(font-lock-fontified t face org-meta-line))
  3940. t)
  3941. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  3942. "orgtbl:" "tblfm:" "tblname:"))
  3943. (and (match-end 4) (equal dc3 "attr")))
  3944. (add-text-properties
  3945. beg (match-end 0)
  3946. '(font-lock-fontified t face org-meta-line))
  3947. t)
  3948. ((member dc3 '(" " ""))
  3949. (add-text-properties
  3950. beg (match-end 0)
  3951. '(font-lock-fontified t face font-lock-comment-face)))
  3952. (t nil))))))
  3953. (defun org-activate-angle-links (limit)
  3954. "Run through the buffer and add overlays to links."
  3955. (if (re-search-forward org-angle-link-re limit t)
  3956. (progn
  3957. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3958. (add-text-properties (match-beginning 0) (match-end 0)
  3959. (list 'mouse-face 'highlight
  3960. 'keymap org-mouse-map))
  3961. (org-rear-nonsticky-at (match-end 0))
  3962. t)))
  3963. (defun org-activate-footnote-links (limit)
  3964. "Run through the buffer and add overlays to links."
  3965. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3966. limit t)
  3967. (progn
  3968. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3969. (add-text-properties (match-beginning 2) (match-end 2)
  3970. (list 'mouse-face 'highlight
  3971. 'keymap org-mouse-map
  3972. 'help-echo
  3973. (if (= (point-at-bol) (match-beginning 2))
  3974. "Footnote definition"
  3975. "Footnote reference")
  3976. ))
  3977. (org-rear-nonsticky-at (match-end 2))
  3978. t)))
  3979. (defun org-activate-bracket-links (limit)
  3980. "Run through the buffer and add overlays to bracketed links."
  3981. (if (re-search-forward org-bracket-link-regexp limit t)
  3982. (let* ((help (concat "LINK: "
  3983. (org-match-string-no-properties 1)))
  3984. ;; FIXME: above we should remove the escapes.
  3985. ;; but that requires another match, protecting match data,
  3986. ;; a lot of overhead for font-lock.
  3987. (ip (org-maybe-intangible
  3988. (list 'invisible 'org-link
  3989. 'keymap org-mouse-map 'mouse-face 'highlight
  3990. 'font-lock-multiline t 'help-echo help)))
  3991. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3992. 'font-lock-multiline t 'help-echo help)))
  3993. ;; We need to remove the invisible property here. Table narrowing
  3994. ;; may have made some of this invisible.
  3995. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3996. (remove-text-properties (match-beginning 0) (match-end 0)
  3997. '(invisible nil))
  3998. (if (match-end 3)
  3999. (progn
  4000. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4001. (org-rear-nonsticky-at (match-beginning 3))
  4002. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4003. (org-rear-nonsticky-at (match-end 3))
  4004. (add-text-properties (match-end 3) (match-end 0) ip)
  4005. (org-rear-nonsticky-at (match-end 0)))
  4006. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4007. (org-rear-nonsticky-at (match-beginning 1))
  4008. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4009. (org-rear-nonsticky-at (match-end 1))
  4010. (add-text-properties (match-end 1) (match-end 0) ip)
  4011. (org-rear-nonsticky-at (match-end 0)))
  4012. t)))
  4013. (defun org-activate-dates (limit)
  4014. "Run through the buffer and add overlays to dates."
  4015. (if (re-search-forward org-tsr-regexp-both limit t)
  4016. (progn
  4017. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4018. (add-text-properties (match-beginning 0) (match-end 0)
  4019. (list 'mouse-face 'highlight
  4020. 'keymap org-mouse-map))
  4021. (org-rear-nonsticky-at (match-end 0))
  4022. (when org-display-custom-times
  4023. (if (match-end 3)
  4024. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4025. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4026. t)))
  4027. (defvar org-target-link-regexp nil
  4028. "Regular expression matching radio targets in plain text.")
  4029. (make-variable-buffer-local 'org-target-link-regexp)
  4030. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4031. "Regular expression matching a link target.")
  4032. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4033. "Regular expression matching a radio target.")
  4034. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4035. "Regular expression matching any target.")
  4036. (defun org-activate-target-links (limit)
  4037. "Run through the buffer and add overlays to target matches."
  4038. (when org-target-link-regexp
  4039. (let ((case-fold-search t))
  4040. (if (re-search-forward org-target-link-regexp limit t)
  4041. (progn
  4042. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4043. (add-text-properties (match-beginning 0) (match-end 0)
  4044. (list 'mouse-face 'highlight
  4045. 'keymap org-mouse-map
  4046. 'help-echo "Radio target link"
  4047. 'org-linked-text t))
  4048. (org-rear-nonsticky-at (match-end 0))
  4049. t)))))
  4050. (defun org-update-radio-target-regexp ()
  4051. "Find all radio targets in this file and update the regular expression."
  4052. (interactive)
  4053. (when (memq 'radio org-activate-links)
  4054. (setq org-target-link-regexp
  4055. (org-make-target-link-regexp (org-all-targets 'radio)))
  4056. (org-restart-font-lock)))
  4057. (defun org-hide-wide-columns (limit)
  4058. (let (s e)
  4059. (setq s (text-property-any (point) (or limit (point-max))
  4060. 'org-cwidth t))
  4061. (when s
  4062. (setq e (next-single-property-change s 'org-cwidth))
  4063. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4064. (goto-char e)
  4065. t)))
  4066. (defvar org-latex-and-specials-regexp nil
  4067. "Regular expression for highlighting export special stuff.")
  4068. (defvar org-match-substring-regexp)
  4069. (defvar org-match-substring-with-braces-regexp)
  4070. ;; This should be with the exporter code, but we also use if for font-locking
  4071. (defconst org-export-html-special-string-regexps
  4072. '(("\\\\-" . "&shy;")
  4073. ("---\\([^-]\\)" . "&mdash;\\1")
  4074. ("--\\([^-]\\)" . "&ndash;\\1")
  4075. ("\\.\\.\\." . "&hellip;"))
  4076. "Regular expressions for special string conversion.")
  4077. (defun org-compute-latex-and-specials-regexp ()
  4078. "Compute regular expression for stuff treated specially by exporters."
  4079. (if (not org-highlight-latex-fragments-and-specials)
  4080. (org-set-local 'org-latex-and-specials-regexp nil)
  4081. (require 'org-exp)
  4082. (let*
  4083. ((matchers (plist-get org-format-latex-options :matchers))
  4084. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4085. org-latex-regexps)))
  4086. (options (org-combine-plists (org-default-export-plist)
  4087. (org-infile-export-plist)))
  4088. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4089. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4090. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4091. (org-export-html-expand (plist-get options :expand-quoted-html))
  4092. (org-export-with-special-strings (plist-get options :special-strings))
  4093. (re-sub
  4094. (cond
  4095. ((equal org-export-with-sub-superscripts '{})
  4096. (list org-match-substring-with-braces-regexp))
  4097. (org-export-with-sub-superscripts
  4098. (list org-match-substring-regexp))
  4099. (t nil)))
  4100. (re-latex
  4101. (if org-export-with-LaTeX-fragments
  4102. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4103. (re-macros
  4104. (if org-export-with-TeX-macros
  4105. (list (concat "\\\\"
  4106. (regexp-opt
  4107. (append (mapcar 'car org-html-entities)
  4108. (if (boundp 'org-latex-entities)
  4109. (mapcar (lambda (x)
  4110. (or (car-safe x) x))
  4111. org-latex-entities)
  4112. nil))
  4113. 'words))) ; FIXME
  4114. ))
  4115. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4116. (re-special (if org-export-with-special-strings
  4117. (mapcar (lambda (x) (car x))
  4118. org-export-html-special-string-regexps)))
  4119. (re-rest
  4120. (delq nil
  4121. (list
  4122. (if org-export-html-expand "@<[^>\n]+>")
  4123. ))))
  4124. (org-set-local
  4125. 'org-latex-and-specials-regexp
  4126. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4127. re-rest) "\\|")))))
  4128. (defun org-do-latex-and-special-faces (limit)
  4129. "Run through the buffer and add overlays to links."
  4130. (when org-latex-and-specials-regexp
  4131. (let (rtn d)
  4132. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4133. limit t))
  4134. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4135. 'face))
  4136. '(org-code org-verbatim underline)))
  4137. (progn
  4138. (setq rtn t
  4139. d (cond ((member (char-after (1+ (match-beginning 0)))
  4140. '(?_ ?^)) 1)
  4141. (t 0)))
  4142. (font-lock-prepend-text-property
  4143. (+ d (match-beginning 0)) (match-end 0)
  4144. 'face 'org-latex-and-export-specials)
  4145. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4146. '(font-lock-multiline t)))))
  4147. rtn)))
  4148. (defun org-restart-font-lock ()
  4149. "Restart font-lock-mode, to force refontification."
  4150. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4151. (font-lock-mode -1)
  4152. (font-lock-mode 1)))
  4153. (defun org-all-targets (&optional radio)
  4154. "Return a list of all targets in this file.
  4155. With optional argument RADIO, only find radio targets."
  4156. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4157. rtn)
  4158. (save-excursion
  4159. (goto-char (point-min))
  4160. (while (re-search-forward re nil t)
  4161. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4162. rtn)))
  4163. (defun org-make-target-link-regexp (targets)
  4164. "Make regular expression matching all strings in TARGETS.
  4165. The regular expression finds the targets also if there is a line break
  4166. between words."
  4167. (and targets
  4168. (concat
  4169. "\\<\\("
  4170. (mapconcat
  4171. (lambda (x)
  4172. (while (string-match " +" x)
  4173. (setq x (replace-match "\\s-+" t t x)))
  4174. x)
  4175. targets
  4176. "\\|")
  4177. "\\)\\>")))
  4178. (defun org-activate-tags (limit)
  4179. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4180. (progn
  4181. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4182. (add-text-properties (match-beginning 1) (match-end 1)
  4183. (list 'mouse-face 'highlight
  4184. 'keymap org-mouse-map))
  4185. (org-rear-nonsticky-at (match-end 1))
  4186. t)))
  4187. (defun org-outline-level ()
  4188. "Compute the outline level of the heading at point.
  4189. This function assumes that the cursor is at the beginning of a line matched
  4190. by outline-regexp. Otherwise it returns garbage.
  4191. If this is called at a normal headline, the level is the number of stars.
  4192. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4193. For plain list items, if they are matched by `outline-regexp', this returns
  4194. 1000 plus the line indentation."
  4195. (save-excursion
  4196. (looking-at outline-regexp)
  4197. (if (match-beginning 1)
  4198. (+ (org-get-string-indentation (match-string 1)) 1000)
  4199. (1- (- (match-end 0) (match-beginning 0))))))
  4200. (defvar org-font-lock-keywords nil)
  4201. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4202. "Regular expression matching a property line.")
  4203. (defvar org-font-lock-hook nil
  4204. "Functions to be called for special font lock stuff.")
  4205. (defun org-font-lock-hook (limit)
  4206. (run-hook-with-args 'org-font-lock-hook limit))
  4207. (defun org-set-font-lock-defaults ()
  4208. (let* ((em org-fontify-emphasized-text)
  4209. (lk org-activate-links)
  4210. (org-font-lock-extra-keywords
  4211. (list
  4212. ;; Call the hook
  4213. '(org-font-lock-hook)
  4214. ;; Headlines
  4215. `(,(if org-fontify-whole-heading-line
  4216. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4217. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4218. (1 (org-get-level-face 1))
  4219. (2 (org-get-level-face 2))
  4220. (3 (org-get-level-face 3)))
  4221. ;; Table lines
  4222. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4223. (1 'org-table t))
  4224. ;; Table internals
  4225. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4226. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4227. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4228. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4229. ;; Drawers
  4230. (list org-drawer-regexp '(0 'org-special-keyword t))
  4231. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4232. ;; Properties
  4233. (list org-property-re
  4234. '(1 'org-special-keyword t)
  4235. '(3 'org-property-value t))
  4236. ;; Links
  4237. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4238. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4239. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4240. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4241. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4242. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4243. (if (memq 'footnote lk) '(org-activate-footnote-links
  4244. (2 'org-footnote t)))
  4245. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4246. '(org-hide-wide-columns (0 nil append))
  4247. ;; TODO lines
  4248. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4249. '(1 (org-get-todo-face 1) t))
  4250. ;; DONE
  4251. (if org-fontify-done-headline
  4252. (list (concat "^[*]+ +\\<\\("
  4253. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4254. "\\)\\(.*\\)")
  4255. '(2 'org-headline-done t))
  4256. nil)
  4257. ;; Priorities
  4258. '(org-font-lock-add-priority-faces)
  4259. ;; Tags
  4260. '(org-font-lock-add-tag-faces)
  4261. ;; Special keywords
  4262. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4263. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4264. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4265. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4266. ;; Emphasis
  4267. (if em
  4268. (if (featurep 'xemacs)
  4269. '(org-do-emphasis-faces (0 nil append))
  4270. '(org-do-emphasis-faces)))
  4271. ;; Checkboxes
  4272. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4273. 2 'org-checkbox prepend)
  4274. (if org-provide-checkbox-statistics
  4275. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4276. (0 (org-get-checkbox-statistics-face) t)))
  4277. ;; Description list items
  4278. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4279. 2 'bold prepend)
  4280. ;; ARCHIVEd headings
  4281. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4282. '(1 'org-archived prepend))
  4283. ;; Specials
  4284. '(org-do-latex-and-special-faces)
  4285. ;; Code
  4286. '(org-activate-code (1 'org-code t))
  4287. ;; COMMENT
  4288. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4289. "\\|" org-quote-string "\\)\\>")
  4290. '(1 'org-special-keyword t))
  4291. '("^#.*" (0 'font-lock-comment-face t))
  4292. ;; Blocks and meta lines
  4293. '(org-fontify-meta-lines-and-blocks)
  4294. )))
  4295. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4296. ;; Now set the full font-lock-keywords
  4297. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4298. (org-set-local 'font-lock-defaults
  4299. '(org-font-lock-keywords t nil nil backward-paragraph))
  4300. (kill-local-variable 'font-lock-keywords) nil))
  4301. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4302. "Fontify string S like in Org-mode"
  4303. (with-temp-buffer
  4304. (insert s)
  4305. (let ((org-odd-levels-only odd-levels))
  4306. (org-mode)
  4307. (font-lock-fontify-buffer)
  4308. (buffer-string))))
  4309. (defvar org-m nil)
  4310. (defvar org-l nil)
  4311. (defvar org-f nil)
  4312. (defun org-get-level-face (n)
  4313. "Get the right face for match N in font-lock matching of headlines."
  4314. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4315. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4316. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4317. (cond
  4318. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4319. ((eq n 2) org-f)
  4320. (t (if org-level-color-stars-only nil org-f))))
  4321. (defun org-get-todo-face (kwd)
  4322. "Get the right face for a TODO keyword KWD.
  4323. If KWD is a number, get the corresponding match group."
  4324. (if (numberp kwd) (setq kwd (match-string kwd)))
  4325. (or (cdr (assoc kwd org-todo-keyword-faces))
  4326. (and (member kwd org-done-keywords) 'org-done)
  4327. 'org-todo))
  4328. (defun org-font-lock-add-tag-faces (limit)
  4329. "Add the special tag faces."
  4330. (when (and org-tag-faces org-tags-special-faces-re)
  4331. (while (re-search-forward org-tags-special-faces-re limit t)
  4332. (add-text-properties (match-beginning 1) (match-end 1)
  4333. (list 'face (org-get-tag-face 1)
  4334. 'font-lock-fontified t))
  4335. (backward-char 1))))
  4336. (defun org-font-lock-add-priority-faces (limit)
  4337. "Add the special priority faces."
  4338. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4339. (add-text-properties
  4340. (match-beginning 0) (match-end 0)
  4341. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4342. org-priority-faces))
  4343. 'org-special-keyword)
  4344. 'font-lock-fontified t))))
  4345. (defun org-get-tag-face (kwd)
  4346. "Get the right face for a TODO keyword KWD.
  4347. If KWD is a number, get the corresponding match group."
  4348. (if (numberp kwd) (setq kwd (match-string kwd)))
  4349. (or (cdr (assoc kwd org-tag-faces))
  4350. 'org-tag))
  4351. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4352. "Remove fontification and activation overlays from links."
  4353. (font-lock-default-unfontify-region beg end)
  4354. (let* ((buffer-undo-list t)
  4355. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4356. (inhibit-modification-hooks t)
  4357. deactivate-mark buffer-file-name buffer-file-truename)
  4358. (remove-text-properties beg end
  4359. '(mouse-face t keymap t org-linked-text t
  4360. invisible t intangible t
  4361. line-prefix t wrap-prefix t
  4362. org-no-flyspell t))))
  4363. ;;;; Visibility cycling, including org-goto and indirect buffer
  4364. ;;; Cycling
  4365. (defvar org-cycle-global-status nil)
  4366. (make-variable-buffer-local 'org-cycle-global-status)
  4367. (defvar org-cycle-subtree-status nil)
  4368. (make-variable-buffer-local 'org-cycle-subtree-status)
  4369. ;;;###autoload
  4370. (defvar org-inlinetask-min-level)
  4371. (defun org-cycle (&optional arg)
  4372. "TAB-action and visibility cycling for Org-mode.
  4373. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4374. is outine visibility cycling, but it also invokes other actions
  4375. in special contexts.
  4376. - When this function is called with a prefix argument, rotate the entire
  4377. buffer through 3 states (global cycling)
  4378. 1. OVERVIEW: Show only top-level headlines.
  4379. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4380. 3. SHOW ALL: Show everything.
  4381. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4382. determined by the variable `org-startup-folded', and by any VISIBILITY
  4383. properties in the buffer.
  4384. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4385. including any drawers.
  4386. - When inside a table, re-align the table and move to the next field.
  4387. - When point is at the beginning of a headline, rotate the subtree started
  4388. by this line through 3 different states (local cycling)
  4389. 1. FOLDED: Only the main headline is shown.
  4390. 2. CHILDREN: The main headline and the direct children are shown.
  4391. From this state, you can move to one of the children
  4392. and zoom in further.
  4393. 3. SUBTREE: Show the entire subtree, including body text.
  4394. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4395. - When there is a numeric prefix, go up to a heading with level ARG, do
  4396. a `show-subtree' and return to the previous cursor position. If ARG
  4397. is negative, go up that many levels.
  4398. - When point is not at the beginning of a headline, execute the global
  4399. binding for TAB, which is re-indenting the line. See the option
  4400. `org-cycle-emulate-tab' for details.
  4401. - Special case: if point is at the beginning of the buffer and there is
  4402. no headline in line 1, this function will act as if called with prefix arg.
  4403. But only if also the variable `org-cycle-global-at-bob' is t."
  4404. (interactive "P")
  4405. (org-load-modules-maybe)
  4406. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4407. (let* ((limit-level
  4408. (or org-cycle-max-level
  4409. (and (boundp 'org-inlinetask-min-level)
  4410. org-inlinetask-min-level
  4411. (1- org-inlinetask-min-level))))
  4412. (nstars (and limit-level
  4413. (if org-odd-levels-only
  4414. (and limit-level (1- (* limit-level 2)))
  4415. limit-level)))
  4416. (outline-regexp
  4417. (cond
  4418. ((not (org-mode-p)) outline-regexp)
  4419. ((or (eq org-cycle-include-plain-lists 'integrate)
  4420. (and org-cycle-include-plain-lists (org-at-item-p)))
  4421. (concat "\\(?:\\*"
  4422. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4423. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4424. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4425. (bob-special (and org-cycle-global-at-bob (bobp)
  4426. (not (looking-at outline-regexp))))
  4427. (org-cycle-hook
  4428. (if bob-special
  4429. (delq 'org-optimize-window-after-visibility-change
  4430. (copy-sequence org-cycle-hook))
  4431. org-cycle-hook))
  4432. (pos (point)))
  4433. (if (or bob-special (equal arg '(4)))
  4434. ;; special case: use global cycling
  4435. (setq arg t))
  4436. (cond
  4437. ((equal arg '(16))
  4438. (org-set-startup-visibility)
  4439. (message "Startup visibility, plus VISIBILITY properties"))
  4440. ((equal arg '(64))
  4441. (show-all)
  4442. (message "Entire buffer visible, including drawers"))
  4443. ((org-at-table-p 'any)
  4444. ;; Enter the table or move to the next field in the table
  4445. (or (org-table-recognize-table.el)
  4446. (progn
  4447. (if arg (org-table-edit-field t)
  4448. (org-table-justify-field-maybe)
  4449. (call-interactively 'org-table-next-field)))))
  4450. ((run-hook-with-args-until-success
  4451. 'org-tab-after-check-for-table-hook))
  4452. ((eq arg t) ;; Global cycling
  4453. (org-cycle-internal-global))
  4454. ((and org-drawers org-drawer-regexp
  4455. (save-excursion
  4456. (beginning-of-line 1)
  4457. (looking-at org-drawer-regexp)))
  4458. ;; Toggle block visibility
  4459. (org-flag-drawer
  4460. (not (get-char-property (match-end 0) 'invisible))))
  4461. ((integerp arg)
  4462. ;; Show-subtree, ARG levels up from here.
  4463. (save-excursion
  4464. (org-back-to-heading)
  4465. (outline-up-heading (if (< arg 0) (- arg)
  4466. (- (funcall outline-level) arg)))
  4467. (org-show-subtree)))
  4468. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4469. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4470. (org-cycle-internal-local))
  4471. ;; TAB emulation and template completion
  4472. (buffer-read-only (org-back-to-heading))
  4473. ((run-hook-with-args-until-success
  4474. 'org-tab-after-check-for-cycling-hook))
  4475. ((org-try-structure-completion))
  4476. ((org-try-cdlatex-tab))
  4477. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4478. (or (not (bolp))
  4479. (not (looking-at outline-regexp))))
  4480. (call-interactively (global-key-binding "\t")))
  4481. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4482. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4483. (or (and (eq org-cycle-emulate-tab 'white)
  4484. (= (match-end 0) (point-at-eol)))
  4485. (and (eq org-cycle-emulate-tab 'whitestart)
  4486. (>= (match-end 0) pos))))
  4487. t
  4488. (eq org-cycle-emulate-tab t))
  4489. (call-interactively (global-key-binding "\t")))
  4490. (t (save-excursion
  4491. (org-back-to-heading)
  4492. (org-cycle)))))))
  4493. (defun org-cycle-internal-global ()
  4494. "Do the global cycling action."
  4495. (cond
  4496. ((and (eq last-command this-command)
  4497. (eq org-cycle-global-status 'overview))
  4498. ;; We just created the overview - now do table of contents
  4499. ;; This can be slow in very large buffers, so indicate action
  4500. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4501. (message "CONTENTS...")
  4502. (org-content)
  4503. (message "CONTENTS...done")
  4504. (setq org-cycle-global-status 'contents)
  4505. (run-hook-with-args 'org-cycle-hook 'contents))
  4506. ((and (eq last-command this-command)
  4507. (eq org-cycle-global-status 'contents))
  4508. ;; We just showed the table of contents - now show everything
  4509. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4510. (show-all)
  4511. (message "SHOW ALL")
  4512. (setq org-cycle-global-status 'all)
  4513. (run-hook-with-args 'org-cycle-hook 'all))
  4514. (t
  4515. ;; Default action: go to overview
  4516. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4517. (org-overview)
  4518. (message "OVERVIEW")
  4519. (setq org-cycle-global-status 'overview)
  4520. (run-hook-with-args 'org-cycle-hook 'overview))))
  4521. (defun org-cycle-internal-local ()
  4522. "Do the local cycling action."
  4523. (org-back-to-heading)
  4524. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4525. ;; First, some boundaries
  4526. (save-excursion
  4527. (org-back-to-heading)
  4528. (setq level (funcall outline-level))
  4529. (save-excursion
  4530. (beginning-of-line 2)
  4531. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4532. ; XEmacs does not have `next-single-char-property-change'
  4533. ; I'm not sure about Emacs 21.
  4534. (while (and (not (eobp)) ;; this is like `next-line'
  4535. (get-char-property (1- (point)) 'invisible))
  4536. (beginning-of-line 2))
  4537. (while (and (not (eobp)) ;; this is like `next-line'
  4538. (get-char-property (1- (point)) 'invisible))
  4539. (goto-char (next-single-char-property-change (point) 'invisible))
  4540. ;;;??? (or (bolp) (beginning-of-line 2))))
  4541. (and (eolp) (beginning-of-line 2))))
  4542. (setq eol (point)))
  4543. (outline-end-of-heading) (setq eoh (point))
  4544. (save-excursion
  4545. (outline-next-heading)
  4546. (setq has-children (and (org-at-heading-p t)
  4547. (> (funcall outline-level) level))))
  4548. (org-end-of-subtree t)
  4549. (unless (eobp)
  4550. (skip-chars-forward " \t\n")
  4551. (beginning-of-line 1) ; in case this is an item
  4552. )
  4553. (setq eos (if (eobp) (point) (1- (point)))))
  4554. ;; Find out what to do next and set `this-command'
  4555. (cond
  4556. ((= eos eoh)
  4557. ;; Nothing is hidden behind this heading
  4558. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4559. (message "EMPTY ENTRY")
  4560. (setq org-cycle-subtree-status nil)
  4561. (save-excursion
  4562. (goto-char eos)
  4563. (outline-next-heading)
  4564. (if (org-invisible-p) (org-flag-heading nil))))
  4565. ((and (or (>= eol eos)
  4566. (not (string-match "\\S-" (buffer-substring eol eos))))
  4567. (or has-children
  4568. (not (setq children-skipped
  4569. org-cycle-skip-children-state-if-no-children))))
  4570. ;; Entire subtree is hidden in one line: children view
  4571. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4572. (org-show-entry)
  4573. (show-children)
  4574. (message "CHILDREN")
  4575. (save-excursion
  4576. (goto-char eos)
  4577. (outline-next-heading)
  4578. (if (org-invisible-p) (org-flag-heading nil)))
  4579. (setq org-cycle-subtree-status 'children)
  4580. (run-hook-with-args 'org-cycle-hook 'children))
  4581. ((or children-skipped
  4582. (and (eq last-command this-command)
  4583. (eq org-cycle-subtree-status 'children)))
  4584. ;; We just showed the children, or no children are there,
  4585. ;; now show everything.
  4586. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4587. (org-show-subtree)
  4588. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4589. (setq org-cycle-subtree-status 'subtree)
  4590. (run-hook-with-args 'org-cycle-hook 'subtree))
  4591. (t
  4592. ;; Default action: hide the subtree.
  4593. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4594. (hide-subtree)
  4595. (message "FOLDED")
  4596. (setq org-cycle-subtree-status 'folded)
  4597. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4598. ;;;###autoload
  4599. (defun org-global-cycle (&optional arg)
  4600. "Cycle the global visibility. For details see `org-cycle'.
  4601. With C-u prefix arg, switch to startup visibility.
  4602. With a numeric prefix, show all headlines up to that level."
  4603. (interactive "P")
  4604. (let ((org-cycle-include-plain-lists
  4605. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4606. (cond
  4607. ((integerp arg)
  4608. (show-all)
  4609. (hide-sublevels arg)
  4610. (setq org-cycle-global-status 'contents))
  4611. ((equal arg '(4))
  4612. (org-set-startup-visibility)
  4613. (message "Startup visibility, plus VISIBILITY properties."))
  4614. (t
  4615. (org-cycle '(4))))))
  4616. (defun org-set-startup-visibility ()
  4617. "Set the visibility required by startup options and properties."
  4618. (cond
  4619. ((eq org-startup-folded t)
  4620. (org-cycle '(4)))
  4621. ((eq org-startup-folded 'content)
  4622. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4623. (org-cycle '(4)) (org-cycle '(4)))))
  4624. (unless (eq org-startup-folded 'showeverything)
  4625. (if org-hide-block-startup (org-hide-block-all))
  4626. (org-set-visibility-according-to-property 'no-cleanup)
  4627. (org-cycle-hide-archived-subtrees 'all)
  4628. (org-cycle-hide-drawers 'all)
  4629. (org-cycle-show-empty-lines 'all)))
  4630. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4631. "Switch subtree visibilities according to :VISIBILITY: property."
  4632. (interactive)
  4633. (let (org-show-entry-below state)
  4634. (save-excursion
  4635. (goto-char (point-min))
  4636. (while (re-search-forward
  4637. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4638. nil t)
  4639. (setq state (match-string 1))
  4640. (save-excursion
  4641. (org-back-to-heading t)
  4642. (hide-subtree)
  4643. (org-reveal)
  4644. (cond
  4645. ((equal state '("fold" "folded"))
  4646. (hide-subtree))
  4647. ((equal state "children")
  4648. (org-show-hidden-entry)
  4649. (show-children))
  4650. ((equal state "content")
  4651. (save-excursion
  4652. (save-restriction
  4653. (org-narrow-to-subtree)
  4654. (org-content))))
  4655. ((member state '("all" "showall"))
  4656. (show-subtree)))))
  4657. (unless no-cleanup
  4658. (org-cycle-hide-archived-subtrees 'all)
  4659. (org-cycle-hide-drawers 'all)
  4660. (org-cycle-show-empty-lines 'all)))))
  4661. (defun org-overview ()
  4662. "Switch to overview mode, showing only top-level headlines.
  4663. Really, this shows all headlines with level equal or greater than the level
  4664. of the first headline in the buffer. This is important, because if the
  4665. first headline is not level one, then (hide-sublevels 1) gives confusing
  4666. results."
  4667. (interactive)
  4668. (let ((level (save-excursion
  4669. (goto-char (point-min))
  4670. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4671. (progn
  4672. (goto-char (match-beginning 0))
  4673. (funcall outline-level))))))
  4674. (and level (hide-sublevels level))))
  4675. (defun org-content (&optional arg)
  4676. "Show all headlines in the buffer, like a table of contents.
  4677. With numerical argument N, show content up to level N."
  4678. (interactive "P")
  4679. (save-excursion
  4680. ;; Visit all headings and show their offspring
  4681. (and (integerp arg) (org-overview))
  4682. (goto-char (point-max))
  4683. (catch 'exit
  4684. (while (and (progn (condition-case nil
  4685. (outline-previous-visible-heading 1)
  4686. (error (goto-char (point-min))))
  4687. t)
  4688. (looking-at outline-regexp))
  4689. (if (integerp arg)
  4690. (show-children (1- arg))
  4691. (show-branches))
  4692. (if (bobp) (throw 'exit nil))))))
  4693. (defun org-optimize-window-after-visibility-change (state)
  4694. "Adjust the window after a change in outline visibility.
  4695. This function is the default value of the hook `org-cycle-hook'."
  4696. (when (get-buffer-window (current-buffer))
  4697. (cond
  4698. ((eq state 'content) nil)
  4699. ((eq state 'all) nil)
  4700. ((eq state 'folded) nil)
  4701. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4702. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4703. ;; FIXME: no longer in use
  4704. (defun org-compact-display-after-subtree-move ()
  4705. "Show a compacter version of the tree of the entry's parent."
  4706. (save-excursion
  4707. (if (org-up-heading-safe)
  4708. (progn
  4709. (hide-subtree)
  4710. (show-entry)
  4711. (show-children)
  4712. (org-cycle-show-empty-lines 'children)
  4713. (org-cycle-hide-drawers 'children))
  4714. (org-overview))))
  4715. (defun org-remove-empty-overlays-at (pos)
  4716. "Remove outline overlays that do not contain non-white stuff."
  4717. (mapc
  4718. (lambda (o)
  4719. (and (eq 'outline (org-overlay-get o 'invisible))
  4720. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4721. (org-overlay-end o))))
  4722. (org-delete-overlay o)))
  4723. (org-overlays-at pos)))
  4724. (defun org-clean-visibility-after-subtree-move ()
  4725. "Fix visibility issues after moving a subtree."
  4726. ;; First, find a reasonable region to look at:
  4727. ;; Start two siblings above, end three below
  4728. (let* ((beg (save-excursion
  4729. (and (org-get-last-sibling)
  4730. (org-get-last-sibling))
  4731. (point)))
  4732. (end (save-excursion
  4733. (and (org-get-next-sibling)
  4734. (org-get-next-sibling)
  4735. (org-get-next-sibling))
  4736. (if (org-at-heading-p)
  4737. (point-at-eol)
  4738. (point))))
  4739. (level (looking-at "\\*+"))
  4740. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4741. (save-excursion
  4742. (save-restriction
  4743. (narrow-to-region beg end)
  4744. (when re
  4745. ;; Properly fold already folded siblings
  4746. (goto-char (point-min))
  4747. (while (re-search-forward re nil t)
  4748. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4749. (hide-entry))))
  4750. (org-cycle-show-empty-lines 'overview)
  4751. (org-cycle-hide-drawers 'overview)))))
  4752. (defun org-cycle-show-empty-lines (state)
  4753. "Show empty lines above all visible headlines.
  4754. The region to be covered depends on STATE when called through
  4755. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4756. entire buffer covered. Note that an empty line is only shown if there
  4757. are at least `org-cycle-separator-lines' empty lines before the headline."
  4758. (when (not (= org-cycle-separator-lines 0))
  4759. (save-excursion
  4760. (let* ((n (abs org-cycle-separator-lines))
  4761. (re (cond
  4762. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4763. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4764. (t (let ((ns (number-to-string (- n 2))))
  4765. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4766. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4767. beg end b e)
  4768. (cond
  4769. ((memq state '(overview contents t))
  4770. (setq beg (point-min) end (point-max)))
  4771. ((memq state '(children folded))
  4772. (setq beg (point) end (progn (org-end-of-subtree t t)
  4773. (beginning-of-line 2)
  4774. (point)))))
  4775. (when beg
  4776. (goto-char beg)
  4777. (while (re-search-forward re end t)
  4778. (unless (get-char-property (match-end 1) 'invisible)
  4779. (setq e (match-end 1))
  4780. (if (< org-cycle-separator-lines 0)
  4781. (setq b (save-excursion
  4782. (goto-char (match-beginning 0))
  4783. (org-back-over-empty-lines)
  4784. (if (save-excursion
  4785. (goto-char (max (point-min) (1- (point))))
  4786. (org-on-heading-p))
  4787. (1- (point))
  4788. (point))))
  4789. (setq b (match-beginning 1)))
  4790. (outline-flag-region b e nil)))))))
  4791. ;; Never hide empty lines at the end of the file.
  4792. (save-excursion
  4793. (goto-char (point-max))
  4794. (outline-previous-heading)
  4795. (outline-end-of-heading)
  4796. (if (and (looking-at "[ \t\n]+")
  4797. (= (match-end 0) (point-max)))
  4798. (outline-flag-region (point) (match-end 0) nil))))
  4799. (defun org-show-empty-lines-in-parent ()
  4800. "Move to the parent and re-show empty lines before visible headlines."
  4801. (save-excursion
  4802. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4803. (org-cycle-show-empty-lines context))))
  4804. (defun org-files-list ()
  4805. "Return `org-agenda-files' list, plus all open org-mode files.
  4806. This is useful for operations that need to scan all of a user's
  4807. open and agenda-wise Org files."
  4808. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  4809. (dolist (buf (buffer-list))
  4810. (with-current-buffer buf
  4811. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  4812. (let ((file (expand-file-name (buffer-file-name))))
  4813. (unless (member file files)
  4814. (push file files))))))
  4815. files))
  4816. (defsubst org-entry-beginning-position ()
  4817. "Return the beginning position of the current entry."
  4818. (save-excursion (outline-back-to-heading t) (point)))
  4819. (defsubst org-entry-end-position ()
  4820. "Return the end position of the current entry."
  4821. (save-excursion (outline-next-heading) (point)))
  4822. (defun org-cycle-hide-drawers (state)
  4823. "Re-hide all drawers after a visibility state change."
  4824. (when (and (org-mode-p)
  4825. (not (memq state '(overview folded contents))))
  4826. (save-excursion
  4827. (let* ((globalp (memq state '(contents all)))
  4828. (beg (if globalp (point-min) (point)))
  4829. (end (if globalp (point-max)
  4830. (if (eq state 'children)
  4831. (save-excursion (outline-next-heading) (point))
  4832. (org-end-of-subtree t)))))
  4833. (goto-char beg)
  4834. (while (re-search-forward org-drawer-regexp end t)
  4835. (org-flag-drawer t))))))
  4836. (defun org-flag-drawer (flag)
  4837. (save-excursion
  4838. (beginning-of-line 1)
  4839. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4840. (let ((b (match-end 0))
  4841. (outline-regexp org-outline-regexp))
  4842. (if (re-search-forward
  4843. "^[ \t]*:END:"
  4844. (save-excursion (outline-next-heading) (point)) t)
  4845. (outline-flag-region b (point-at-eol) flag)
  4846. (error ":END: line missing at position %s" b))))))
  4847. (defun org-subtree-end-visible-p ()
  4848. "Is the end of the current subtree visible?"
  4849. (pos-visible-in-window-p
  4850. (save-excursion (org-end-of-subtree t) (point))))
  4851. (defun org-first-headline-recenter (&optional N)
  4852. "Move cursor to the first headline and recenter the headline.
  4853. Optional argument N means, put the headline into the Nth line of the window."
  4854. (goto-char (point-min))
  4855. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4856. (beginning-of-line)
  4857. (recenter (prefix-numeric-value N))))
  4858. ;;; Folding of blocks
  4859. (defconst org-block-regexp
  4860. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4861. "Regular expression for hiding blocks.")
  4862. (defvar org-hide-block-overlays nil
  4863. "Overays hiding blocks.")
  4864. (make-variable-buffer-local 'org-hide-block-overlays)
  4865. (defun org-block-map (function &optional start end)
  4866. "Call func at the head of all source blocks in the current
  4867. buffer. Optional arguments START and END can be used to limit
  4868. the range."
  4869. (let ((start (or start (point-min)))
  4870. (end (or end (point-max))))
  4871. (save-excursion
  4872. (goto-char start)
  4873. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4874. (save-excursion
  4875. (save-match-data
  4876. (goto-char (match-beginning 0))
  4877. (funcall function)))))))
  4878. (defun org-hide-block-toggle-all ()
  4879. "Toggle the visibility of all blocks in the current buffer."
  4880. (org-block-map #'org-hide-block-toggle))
  4881. (defun org-hide-block-all ()
  4882. "Fold all blocks in the current buffer."
  4883. (interactive)
  4884. (org-show-block-all)
  4885. (org-block-map #'org-hide-block-toggle-maybe))
  4886. (defun org-show-block-all ()
  4887. "Unfold all blocks in the current buffer."
  4888. (mapc 'org-delete-overlay org-hide-block-overlays)
  4889. (setq org-hide-block-overlays nil))
  4890. (defun org-hide-block-toggle-maybe ()
  4891. "Toggle visibility of block at point."
  4892. (interactive)
  4893. (let ((case-fold-search t))
  4894. (if (save-excursion
  4895. (beginning-of-line 1)
  4896. (looking-at org-block-regexp))
  4897. (progn (org-hide-block-toggle)
  4898. t) ;; to signal that we took action
  4899. nil))) ;; to signal that we did not
  4900. (defun org-hide-block-toggle (&optional force)
  4901. "Toggle the visibility of the current block."
  4902. (interactive)
  4903. (save-excursion
  4904. (beginning-of-line)
  4905. (if (re-search-forward org-block-regexp nil t)
  4906. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4907. (end (match-end 0)) ;; end of entire body
  4908. ov)
  4909. (if (memq t (mapcar (lambda (overlay)
  4910. (eq (org-overlay-get overlay 'invisible)
  4911. 'org-hide-block))
  4912. (org-overlays-at start)))
  4913. (if (or (not force) (eq force 'off))
  4914. (mapc (lambda (ov)
  4915. (when (member ov org-hide-block-overlays)
  4916. (setq org-hide-block-overlays
  4917. (delq ov org-hide-block-overlays)))
  4918. (when (eq (org-overlay-get ov 'invisible)
  4919. 'org-hide-block)
  4920. (org-delete-overlay ov)))
  4921. (org-overlays-at start)))
  4922. (setq ov (org-make-overlay start end))
  4923. (org-overlay-put ov 'invisible 'org-hide-block)
  4924. ;; make the block accessible to isearch
  4925. (org-overlay-put
  4926. ov 'isearch-open-invisible
  4927. (lambda (ov)
  4928. (when (member ov org-hide-block-overlays)
  4929. (setq org-hide-block-overlays
  4930. (delq ov org-hide-block-overlays)))
  4931. (when (eq (org-overlay-get ov 'invisible)
  4932. 'org-hide-block)
  4933. (org-delete-overlay ov))))
  4934. (push ov org-hide-block-overlays)))
  4935. (error "Not looking at a source block"))))
  4936. ;; org-tab-after-check-for-cycling-hook
  4937. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4938. ;; Remove overlays when changing major mode
  4939. (add-hook 'org-mode-hook
  4940. (lambda () (org-add-hook 'change-major-mode-hook
  4941. 'org-show-block-all 'append 'local)))
  4942. ;;; Org-goto
  4943. (defvar org-goto-window-configuration nil)
  4944. (defvar org-goto-marker nil)
  4945. (defvar org-goto-map
  4946. (let ((map (make-sparse-keymap)))
  4947. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4948. (while (setq cmd (pop cmds))
  4949. (substitute-key-definition cmd cmd map global-map)))
  4950. (suppress-keymap map)
  4951. (org-defkey map "\C-m" 'org-goto-ret)
  4952. (org-defkey map [(return)] 'org-goto-ret)
  4953. (org-defkey map [(left)] 'org-goto-left)
  4954. (org-defkey map [(right)] 'org-goto-right)
  4955. (org-defkey map [(control ?g)] 'org-goto-quit)
  4956. (org-defkey map "\C-i" 'org-cycle)
  4957. (org-defkey map [(tab)] 'org-cycle)
  4958. (org-defkey map [(down)] 'outline-next-visible-heading)
  4959. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4960. (if org-goto-auto-isearch
  4961. (if (fboundp 'define-key-after)
  4962. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4963. nil)
  4964. (org-defkey map "q" 'org-goto-quit)
  4965. (org-defkey map "n" 'outline-next-visible-heading)
  4966. (org-defkey map "p" 'outline-previous-visible-heading)
  4967. (org-defkey map "f" 'outline-forward-same-level)
  4968. (org-defkey map "b" 'outline-backward-same-level)
  4969. (org-defkey map "u" 'outline-up-heading))
  4970. (org-defkey map "/" 'org-occur)
  4971. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4972. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4973. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4974. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4975. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4976. map))
  4977. (defconst org-goto-help
  4978. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4979. RET=jump to location [Q]uit and return to previous location
  4980. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4981. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4982. ;; FIXME: Docstring doe not mention both interfaces
  4983. (defun org-goto (&optional alternative-interface)
  4984. "Look up a different location in the current file, keeping current visibility.
  4985. When you want look-up or go to a different location in a document, the
  4986. fastest way is often to fold the entire buffer and then dive into the tree.
  4987. This method has the disadvantage, that the previous location will be folded,
  4988. which may not be what you want.
  4989. This command works around this by showing a copy of the current buffer
  4990. in an indirect buffer, in overview mode. You can dive into the tree in
  4991. that copy, use org-occur and incremental search to find a location.
  4992. When pressing RET or `Q', the command returns to the original buffer in
  4993. which the visibility is still unchanged. After RET is will also jump to
  4994. the location selected in the indirect buffer and expose the
  4995. the headline hierarchy above."
  4996. (interactive "P")
  4997. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4998. (org-refile-use-outline-path t)
  4999. (org-refile-target-verify-function nil)
  5000. (interface
  5001. (if (not alternative-interface)
  5002. org-goto-interface
  5003. (if (eq org-goto-interface 'outline)
  5004. 'outline-path-completion
  5005. 'outline)))
  5006. (org-goto-start-pos (point))
  5007. (selected-point
  5008. (if (eq interface 'outline)
  5009. (car (org-get-location (current-buffer) org-goto-help))
  5010. (nth 3 (org-refile-get-location "Goto: ")))))
  5011. (if selected-point
  5012. (progn
  5013. (org-mark-ring-push org-goto-start-pos)
  5014. (goto-char selected-point)
  5015. (if (or (org-invisible-p) (org-invisible-p2))
  5016. (org-show-context 'org-goto)))
  5017. (message "Quit"))))
  5018. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5019. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5020. (defvar org-goto-local-auto-isearch-map) ; defined below
  5021. (defun org-get-location (buf help)
  5022. "Let the user select a location in the Org-mode buffer BUF.
  5023. This function uses a recursive edit. It returns the selected position
  5024. or nil."
  5025. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5026. (isearch-hide-immediately nil)
  5027. (isearch-search-fun-function
  5028. (lambda () 'org-goto-local-search-headings))
  5029. (org-goto-selected-point org-goto-exit-command))
  5030. (save-excursion
  5031. (save-window-excursion
  5032. (delete-other-windows)
  5033. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5034. (switch-to-buffer
  5035. (condition-case nil
  5036. (make-indirect-buffer (current-buffer) "*org-goto*")
  5037. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5038. (with-output-to-temp-buffer "*Help*"
  5039. (princ help))
  5040. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5041. (setq buffer-read-only nil)
  5042. (let ((org-startup-truncated t)
  5043. (org-startup-folded nil)
  5044. (org-startup-align-all-tables nil))
  5045. (org-mode)
  5046. (org-overview))
  5047. (setq buffer-read-only t)
  5048. (if (and (boundp 'org-goto-start-pos)
  5049. (integer-or-marker-p org-goto-start-pos))
  5050. (let ((org-show-hierarchy-above t)
  5051. (org-show-siblings t)
  5052. (org-show-following-heading t))
  5053. (goto-char org-goto-start-pos)
  5054. (and (org-invisible-p) (org-show-context)))
  5055. (goto-char (point-min)))
  5056. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5057. (message "Select location and press RET")
  5058. (use-local-map org-goto-map)
  5059. (recursive-edit)
  5060. ))
  5061. (kill-buffer "*org-goto*")
  5062. (cons org-goto-selected-point org-goto-exit-command)))
  5063. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5064. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5065. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5066. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5067. (defun org-goto-local-search-headings (string bound noerror)
  5068. "Search and make sure that any matches are in headlines."
  5069. (catch 'return
  5070. (while (if isearch-forward
  5071. (search-forward string bound noerror)
  5072. (search-backward string bound noerror))
  5073. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5074. (and (member :headline context)
  5075. (not (member :tags context))))
  5076. (throw 'return (point))))))
  5077. (defun org-goto-local-auto-isearch ()
  5078. "Start isearch."
  5079. (interactive)
  5080. (goto-char (point-min))
  5081. (let ((keys (this-command-keys)))
  5082. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5083. (isearch-mode t)
  5084. (isearch-process-search-char (string-to-char keys)))))
  5085. (defun org-goto-ret (&optional arg)
  5086. "Finish `org-goto' by going to the new location."
  5087. (interactive "P")
  5088. (setq org-goto-selected-point (point)
  5089. org-goto-exit-command 'return)
  5090. (throw 'exit nil))
  5091. (defun org-goto-left ()
  5092. "Finish `org-goto' by going to the new location."
  5093. (interactive)
  5094. (if (org-on-heading-p)
  5095. (progn
  5096. (beginning-of-line 1)
  5097. (setq org-goto-selected-point (point)
  5098. org-goto-exit-command 'left)
  5099. (throw 'exit nil))
  5100. (error "Not on a heading")))
  5101. (defun org-goto-right ()
  5102. "Finish `org-goto' by going to the new location."
  5103. (interactive)
  5104. (if (org-on-heading-p)
  5105. (progn
  5106. (setq org-goto-selected-point (point)
  5107. org-goto-exit-command 'right)
  5108. (throw 'exit nil))
  5109. (error "Not on a heading")))
  5110. (defun org-goto-quit ()
  5111. "Finish `org-goto' without cursor motion."
  5112. (interactive)
  5113. (setq org-goto-selected-point nil)
  5114. (setq org-goto-exit-command 'quit)
  5115. (throw 'exit nil))
  5116. ;;; Indirect buffer display of subtrees
  5117. (defvar org-indirect-dedicated-frame nil
  5118. "This is the frame being used for indirect tree display.")
  5119. (defvar org-last-indirect-buffer nil)
  5120. (defun org-tree-to-indirect-buffer (&optional arg)
  5121. "Create indirect buffer and narrow it to current subtree.
  5122. With numerical prefix ARG, go up to this level and then take that tree.
  5123. If ARG is negative, go up that many levels.
  5124. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5125. indirect buffer previously made with this command, to avoid proliferation of
  5126. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5127. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5128. is kept so that you can work with several indirect buffers at the same time.
  5129. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5130. requests that a new frame be made for the new buffer, so that the dedicated
  5131. frame is not changed."
  5132. (interactive "P")
  5133. (let ((cbuf (current-buffer))
  5134. (cwin (selected-window))
  5135. (pos (point))
  5136. beg end level heading ibuf)
  5137. (save-excursion
  5138. (org-back-to-heading t)
  5139. (when (numberp arg)
  5140. (setq level (org-outline-level))
  5141. (if (< arg 0) (setq arg (+ level arg)))
  5142. (while (> (setq level (org-outline-level)) arg)
  5143. (outline-up-heading 1 t)))
  5144. (setq beg (point)
  5145. heading (org-get-heading))
  5146. (org-end-of-subtree t t) (setq end (point)))
  5147. (if (and (buffer-live-p org-last-indirect-buffer)
  5148. (not (eq org-indirect-buffer-display 'new-frame))
  5149. (not arg))
  5150. (kill-buffer org-last-indirect-buffer))
  5151. (setq ibuf (org-get-indirect-buffer cbuf)
  5152. org-last-indirect-buffer ibuf)
  5153. (cond
  5154. ((or (eq org-indirect-buffer-display 'new-frame)
  5155. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5156. (select-frame (make-frame))
  5157. (delete-other-windows)
  5158. (switch-to-buffer ibuf)
  5159. (org-set-frame-title heading))
  5160. ((eq org-indirect-buffer-display 'dedicated-frame)
  5161. (raise-frame
  5162. (select-frame (or (and org-indirect-dedicated-frame
  5163. (frame-live-p org-indirect-dedicated-frame)
  5164. org-indirect-dedicated-frame)
  5165. (setq org-indirect-dedicated-frame (make-frame)))))
  5166. (delete-other-windows)
  5167. (switch-to-buffer ibuf)
  5168. (org-set-frame-title (concat "Indirect: " heading)))
  5169. ((eq org-indirect-buffer-display 'current-window)
  5170. (switch-to-buffer ibuf))
  5171. ((eq org-indirect-buffer-display 'other-window)
  5172. (pop-to-buffer ibuf))
  5173. (t (error "Invalid value")))
  5174. (if (featurep 'xemacs)
  5175. (save-excursion (org-mode) (turn-on-font-lock)))
  5176. (narrow-to-region beg end)
  5177. (show-all)
  5178. (goto-char pos)
  5179. (and (window-live-p cwin) (select-window cwin))))
  5180. (defun org-get-indirect-buffer (&optional buffer)
  5181. (setq buffer (or buffer (current-buffer)))
  5182. (let ((n 1) (base (buffer-name buffer)) bname)
  5183. (while (buffer-live-p
  5184. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5185. (setq n (1+ n)))
  5186. (condition-case nil
  5187. (make-indirect-buffer buffer bname 'clone)
  5188. (error (make-indirect-buffer buffer bname)))))
  5189. (defun org-set-frame-title (title)
  5190. "Set the title of the current frame to the string TITLE."
  5191. ;; FIXME: how to name a single frame in XEmacs???
  5192. (unless (featurep 'xemacs)
  5193. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5194. ;;;; Structure editing
  5195. ;;; Inserting headlines
  5196. (defun org-previous-line-empty-p ()
  5197. (save-excursion
  5198. (and (not (bobp))
  5199. (or (beginning-of-line 0) t)
  5200. (save-match-data
  5201. (looking-at "[ \t]*$")))))
  5202. (defun org-insert-heading (&optional force-heading)
  5203. "Insert a new heading or item with same depth at point.
  5204. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5205. If point is at the beginning of a headline, insert a sibling before the
  5206. current headline. If point is not at the beginning, do not split the line,
  5207. but create the new headline after the current line."
  5208. (interactive "P")
  5209. (if (= (buffer-size) 0)
  5210. (insert "\n* ")
  5211. (when (or force-heading (not (org-insert-item)))
  5212. (let* ((empty-line-p nil)
  5213. (head (save-excursion
  5214. (condition-case nil
  5215. (progn
  5216. (org-back-to-heading)
  5217. (setq empty-line-p (org-previous-line-empty-p))
  5218. (match-string 0))
  5219. (error "*"))))
  5220. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5221. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5222. pos hide-previous previous-pos)
  5223. (cond
  5224. ((and (org-on-heading-p) (bolp)
  5225. (or (bobp)
  5226. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5227. ;; insert before the current line
  5228. (open-line (if blank 2 1)))
  5229. ((and (bolp)
  5230. (not org-insert-heading-respect-content)
  5231. (or (bobp)
  5232. (save-excursion
  5233. (backward-char 1) (not (org-invisible-p)))))
  5234. ;; insert right here
  5235. nil)
  5236. (t
  5237. ;; somewhere in the line
  5238. (save-excursion
  5239. (setq previous-pos (point-at-bol))
  5240. (end-of-line)
  5241. (setq hide-previous (org-invisible-p)))
  5242. (and org-insert-heading-respect-content (org-show-subtree))
  5243. (let ((split
  5244. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5245. (save-excursion
  5246. (let ((p (point)))
  5247. (goto-char (point-at-bol))
  5248. (and (looking-at org-complex-heading-regexp)
  5249. (> p (match-beginning 4)))))))
  5250. tags pos)
  5251. (cond
  5252. (org-insert-heading-respect-content
  5253. (org-end-of-subtree nil t)
  5254. (or (bolp) (newline))
  5255. (or (org-previous-line-empty-p)
  5256. (and blank (newline)))
  5257. (open-line 1))
  5258. ((org-on-heading-p)
  5259. (when hide-previous
  5260. (show-children)
  5261. (org-show-entry))
  5262. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5263. (setq tags (and (match-end 2) (match-string 2)))
  5264. (and (match-end 1)
  5265. (delete-region (match-beginning 1) (match-end 1)))
  5266. (setq pos (point-at-bol))
  5267. (or split (end-of-line 1))
  5268. (delete-horizontal-space)
  5269. (newline (if blank 2 1))
  5270. (when tags
  5271. (save-excursion
  5272. (goto-char pos)
  5273. (end-of-line 1)
  5274. (insert " " tags)
  5275. (org-set-tags nil 'align))))
  5276. (t
  5277. (or split (end-of-line 1))
  5278. (newline (if blank 2 1)))))))
  5279. (insert head) (just-one-space)
  5280. (setq pos (point))
  5281. (end-of-line 1)
  5282. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5283. (when (and org-insert-heading-respect-content hide-previous)
  5284. (save-excursion
  5285. (goto-char previous-pos)
  5286. (hide-subtree)))
  5287. (run-hooks 'org-insert-heading-hook)))))
  5288. (defun org-get-heading (&optional no-tags)
  5289. "Return the heading of the current entry, without the stars."
  5290. (save-excursion
  5291. (org-back-to-heading t)
  5292. (if (looking-at
  5293. (if no-tags
  5294. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5295. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5296. (match-string 1) "")))
  5297. (defun org-heading-components ()
  5298. "Return the components of the current heading.
  5299. This is a list with the following elements:
  5300. - the level as an integer
  5301. - the reduced level, different if `org-odd-levels-only' is set.
  5302. - the TODO keyword, or nil
  5303. - the priority character, like ?A, or nil if no priority is given
  5304. - the headline text itself, or the tags string if no headline text
  5305. - the tags string, or nil."
  5306. (save-excursion
  5307. (org-back-to-heading t)
  5308. (if (looking-at org-complex-heading-regexp)
  5309. (list (length (match-string 1))
  5310. (org-reduced-level (length (match-string 1)))
  5311. (org-match-string-no-properties 2)
  5312. (and (match-end 3) (aref (match-string 3) 2))
  5313. (org-match-string-no-properties 4)
  5314. (org-match-string-no-properties 5)))))
  5315. (defun org-get-entry ()
  5316. "Get the entry text, after heading, entire subtree."
  5317. (save-excursion
  5318. (org-back-to-heading t)
  5319. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5320. (defun org-insert-heading-after-current ()
  5321. "Insert a new heading with same level as current, after current subtree."
  5322. (interactive)
  5323. (org-back-to-heading)
  5324. (org-insert-heading)
  5325. (org-move-subtree-down)
  5326. (end-of-line 1))
  5327. (defun org-insert-heading-respect-content ()
  5328. (interactive)
  5329. (let ((org-insert-heading-respect-content t))
  5330. (org-insert-heading t)))
  5331. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5332. (interactive "P")
  5333. (let ((org-insert-heading-respect-content t))
  5334. (org-insert-todo-heading force-state t)))
  5335. (defun org-insert-todo-heading (arg &optional force-heading)
  5336. "Insert a new heading with the same level and TODO state as current heading.
  5337. If the heading has no TODO state, or if the state is DONE, use the first
  5338. state (TODO by default). Also with prefix arg, force first state."
  5339. (interactive "P")
  5340. (when (or force-heading (not (org-insert-item 'checkbox)))
  5341. (org-insert-heading force-heading)
  5342. (save-excursion
  5343. (org-back-to-heading)
  5344. (outline-previous-heading)
  5345. (looking-at org-todo-line-regexp))
  5346. (let*
  5347. ((new-mark-x
  5348. (if (or arg
  5349. (not (match-beginning 2))
  5350. (member (match-string 2) org-done-keywords))
  5351. (car org-todo-keywords-1)
  5352. (match-string 2)))
  5353. (new-mark
  5354. (or
  5355. (run-hook-with-args-until-success
  5356. 'org-todo-get-default-hook new-mark-x nil)
  5357. new-mark-x)))
  5358. (beginning-of-line 1)
  5359. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5360. (if org-treat-insert-todo-heading-as-state-change
  5361. (org-todo new-mark)
  5362. (insert new-mark " "))))
  5363. (when org-provide-todo-statistics
  5364. (org-update-parent-todo-statistics))))
  5365. (defun org-insert-subheading (arg)
  5366. "Insert a new subheading and demote it.
  5367. Works for outline headings and for plain lists alike."
  5368. (interactive "P")
  5369. (org-insert-heading arg)
  5370. (cond
  5371. ((org-on-heading-p) (org-do-demote))
  5372. ((org-at-item-p) (org-indent-item 1))))
  5373. (defun org-insert-todo-subheading (arg)
  5374. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5375. Works for outline headings and for plain lists alike."
  5376. (interactive "P")
  5377. (org-insert-todo-heading arg)
  5378. (cond
  5379. ((org-on-heading-p) (org-do-demote))
  5380. ((org-at-item-p) (org-indent-item 1))))
  5381. ;;; Promotion and Demotion
  5382. (defvar org-after-demote-entry-hook nil
  5383. "Hook run after an entry has been demoted.
  5384. The cursor will be at the beginning of the entry.
  5385. When a subtree is being demoted, the hook will be called for each node.")
  5386. (defvar org-after-promote-entry-hook nil
  5387. "Hook run after an entry has been promoted.
  5388. The cursor will be at the beginning of the entry.
  5389. When a subtree is being promoted, the hook will be called for each node.")
  5390. (defun org-promote-subtree ()
  5391. "Promote the entire subtree.
  5392. See also `org-promote'."
  5393. (interactive)
  5394. (save-excursion
  5395. (org-map-tree 'org-promote))
  5396. (org-fix-position-after-promote))
  5397. (defun org-demote-subtree ()
  5398. "Demote the entire subtree. See `org-demote'.
  5399. See also `org-promote'."
  5400. (interactive)
  5401. (save-excursion
  5402. (org-map-tree 'org-demote))
  5403. (org-fix-position-after-promote))
  5404. (defun org-do-promote ()
  5405. "Promote the current heading higher up the tree.
  5406. If the region is active in `transient-mark-mode', promote all headings
  5407. in the region."
  5408. (interactive)
  5409. (save-excursion
  5410. (if (org-region-active-p)
  5411. (org-map-region 'org-promote (region-beginning) (region-end))
  5412. (org-promote)))
  5413. (org-fix-position-after-promote))
  5414. (defun org-do-demote ()
  5415. "Demote the current heading lower down the tree.
  5416. If the region is active in `transient-mark-mode', demote all headings
  5417. in the region."
  5418. (interactive)
  5419. (save-excursion
  5420. (if (org-region-active-p)
  5421. (org-map-region 'org-demote (region-beginning) (region-end))
  5422. (org-demote)))
  5423. (org-fix-position-after-promote))
  5424. (defun org-fix-position-after-promote ()
  5425. "Make sure that after pro/demotion cursor position is right."
  5426. (let ((pos (point)))
  5427. (when (save-excursion
  5428. (beginning-of-line 1)
  5429. (looking-at org-todo-line-regexp)
  5430. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5431. (cond ((eobp) (insert " "))
  5432. ((eolp) (insert " "))
  5433. ((equal (char-after) ?\ ) (forward-char 1))))))
  5434. (defun org-reduced-level (l)
  5435. "Compute the effective level of a heading.
  5436. This takes into account the setting of `org-odd-levels-only'."
  5437. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5438. (defun org-get-valid-level (level &optional change)
  5439. "Rectify a level change under the influence of `org-odd-levels-only'
  5440. LEVEL is a current level, CHANGE is by how much the level should be
  5441. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5442. even level numbers will become the next higher odd number."
  5443. (if org-odd-levels-only
  5444. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5445. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5446. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5447. (max 1 (+ level (or change 0)))))
  5448. (if (boundp 'define-obsolete-function-alias)
  5449. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5450. (define-obsolete-function-alias 'org-get-legal-level
  5451. 'org-get-valid-level)
  5452. (define-obsolete-function-alias 'org-get-legal-level
  5453. 'org-get-valid-level "23.1")))
  5454. (defun org-promote ()
  5455. "Promote the current heading higher up the tree.
  5456. If the region is active in `transient-mark-mode', promote all headings
  5457. in the region."
  5458. (org-back-to-heading t)
  5459. (let* ((level (save-match-data (funcall outline-level)))
  5460. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5461. (diff (abs (- level (length up-head) -1))))
  5462. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5463. (replace-match up-head nil t)
  5464. ;; Fixup tag positioning
  5465. (and org-auto-align-tags (org-set-tags nil t))
  5466. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5467. (run-hooks 'org-after-promote-entry-hook)))
  5468. (defun org-demote ()
  5469. "Demote the current heading lower down the tree.
  5470. If the region is active in `transient-mark-mode', demote all headings
  5471. in the region."
  5472. (org-back-to-heading t)
  5473. (let* ((level (save-match-data (funcall outline-level)))
  5474. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5475. (diff (abs (- level (length down-head) -1))))
  5476. (replace-match down-head nil t)
  5477. ;; Fixup tag positioning
  5478. (and org-auto-align-tags (org-set-tags nil t))
  5479. (if org-adapt-indentation (org-fixup-indentation diff))
  5480. (run-hooks 'org-after-demote-entry-hook)))
  5481. (defun org-map-tree (fun)
  5482. "Call FUN for every heading underneath the current one."
  5483. (org-back-to-heading)
  5484. (let ((level (funcall outline-level)))
  5485. (save-excursion
  5486. (funcall fun)
  5487. (while (and (progn
  5488. (outline-next-heading)
  5489. (> (funcall outline-level) level))
  5490. (not (eobp)))
  5491. (funcall fun)))))
  5492. (defun org-map-region (fun beg end)
  5493. "Call FUN for every heading between BEG and END."
  5494. (let ((org-ignore-region t))
  5495. (save-excursion
  5496. (setq end (copy-marker end))
  5497. (goto-char beg)
  5498. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5499. (< (point) end))
  5500. (funcall fun))
  5501. (while (and (progn
  5502. (outline-next-heading)
  5503. (< (point) end))
  5504. (not (eobp)))
  5505. (funcall fun)))))
  5506. (defun org-fixup-indentation (diff)
  5507. "Change the indentation in the current entry by DIFF
  5508. However, if any line in the current entry has no indentation, or if it
  5509. would end up with no indentation after the change, nothing at all is done."
  5510. (save-excursion
  5511. (let ((end (save-excursion (outline-next-heading)
  5512. (point-marker)))
  5513. (prohibit (if (> diff 0)
  5514. "^\\S-"
  5515. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5516. col)
  5517. (unless (save-excursion (end-of-line 1)
  5518. (re-search-forward prohibit end t))
  5519. (while (and (< (point) end)
  5520. (re-search-forward "^[ \t]+" end t))
  5521. (goto-char (match-end 0))
  5522. (setq col (current-column))
  5523. (if (< diff 0) (replace-match ""))
  5524. (org-indent-to-column (+ diff col))))
  5525. (move-marker end nil))))
  5526. (defun org-convert-to-odd-levels ()
  5527. "Convert an org-mode file with all levels allowed to one with odd levels.
  5528. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5529. level 5 etc."
  5530. (interactive)
  5531. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5532. (let ((outline-regexp org-outline-regexp)
  5533. (outline-level 'org-outline-level)
  5534. (org-odd-levels-only nil) n)
  5535. (save-excursion
  5536. (goto-char (point-min))
  5537. (while (re-search-forward "^\\*\\*+ " nil t)
  5538. (setq n (- (length (match-string 0)) 2))
  5539. (while (>= (setq n (1- n)) 0)
  5540. (org-demote))
  5541. (end-of-line 1))))))
  5542. (defun org-convert-to-oddeven-levels ()
  5543. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5544. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5545. section with an even level, conversion would destroy the structure of the file. An error
  5546. is signaled in this case."
  5547. (interactive)
  5548. (goto-char (point-min))
  5549. ;; First check if there are no even levels
  5550. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5551. (org-show-context t)
  5552. (error "Not all levels are odd in this file. Conversion not possible"))
  5553. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5554. (let ((outline-regexp org-outline-regexp)
  5555. (outline-level 'org-outline-level)
  5556. (org-odd-levels-only nil) n)
  5557. (save-excursion
  5558. (goto-char (point-min))
  5559. (while (re-search-forward "^\\*\\*+ " nil t)
  5560. (setq n (/ (1- (length (match-string 0))) 2))
  5561. (while (>= (setq n (1- n)) 0)
  5562. (org-promote))
  5563. (end-of-line 1))))))
  5564. (defun org-tr-level (n)
  5565. "Make N odd if required."
  5566. (if org-odd-levels-only (1+ (/ n 2)) n))
  5567. ;;; Vertical tree motion, cutting and pasting of subtrees
  5568. (defun org-move-subtree-up (&optional arg)
  5569. "Move the current subtree up past ARG headlines of the same level."
  5570. (interactive "p")
  5571. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5572. (defun org-move-subtree-down (&optional arg)
  5573. "Move the current subtree down past ARG headlines of the same level."
  5574. (interactive "p")
  5575. (setq arg (prefix-numeric-value arg))
  5576. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5577. 'org-get-last-sibling))
  5578. (ins-point (make-marker))
  5579. (cnt (abs arg))
  5580. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5581. ;; Select the tree
  5582. (org-back-to-heading)
  5583. (setq beg0 (point))
  5584. (save-excursion
  5585. (setq ne-beg (org-back-over-empty-lines))
  5586. (setq beg (point)))
  5587. (save-match-data
  5588. (save-excursion (outline-end-of-heading)
  5589. (setq folded (org-invisible-p)))
  5590. (outline-end-of-subtree))
  5591. (outline-next-heading)
  5592. (setq ne-end (org-back-over-empty-lines))
  5593. (setq end (point))
  5594. (goto-char beg0)
  5595. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5596. ;; include less whitespace
  5597. (save-excursion
  5598. (goto-char beg)
  5599. (forward-line (- ne-beg ne-end))
  5600. (setq beg (point))))
  5601. ;; Find insertion point, with error handling
  5602. (while (> cnt 0)
  5603. (or (and (funcall movfunc) (looking-at outline-regexp))
  5604. (progn (goto-char beg0)
  5605. (error "Cannot move past superior level or buffer limit")))
  5606. (setq cnt (1- cnt)))
  5607. (if (> arg 0)
  5608. ;; Moving forward - still need to move over subtree
  5609. (progn (org-end-of-subtree t t)
  5610. (save-excursion
  5611. (org-back-over-empty-lines)
  5612. (or (bolp) (newline)))))
  5613. (setq ne-ins (org-back-over-empty-lines))
  5614. (move-marker ins-point (point))
  5615. (setq txt (buffer-substring beg end))
  5616. (org-save-markers-in-region beg end)
  5617. (delete-region beg end)
  5618. (org-remove-empty-overlays-at beg)
  5619. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5620. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5621. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5622. (let ((bbb (point)))
  5623. (insert-before-markers txt)
  5624. (org-reinstall-markers-in-region bbb)
  5625. (move-marker ins-point bbb))
  5626. (or (bolp) (insert "\n"))
  5627. (setq ins-end (point))
  5628. (goto-char ins-point)
  5629. (org-skip-whitespace)
  5630. (when (and (< arg 0)
  5631. (org-first-sibling-p)
  5632. (> ne-ins ne-beg))
  5633. ;; Move whitespace back to beginning
  5634. (save-excursion
  5635. (goto-char ins-end)
  5636. (let ((kill-whole-line t))
  5637. (kill-line (- ne-ins ne-beg)) (point)))
  5638. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5639. (move-marker ins-point nil)
  5640. (if folded
  5641. (hide-subtree)
  5642. (org-show-entry)
  5643. (show-children)
  5644. (org-cycle-hide-drawers 'children))
  5645. (org-clean-visibility-after-subtree-move)))
  5646. (defvar org-subtree-clip ""
  5647. "Clipboard for cut and paste of subtrees.
  5648. This is actually only a copy of the kill, because we use the normal kill
  5649. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5650. (defvar org-subtree-clip-folded nil
  5651. "Was the last copied subtree folded?
  5652. This is used to fold the tree back after pasting.")
  5653. (defun org-cut-subtree (&optional n)
  5654. "Cut the current subtree into the clipboard.
  5655. With prefix arg N, cut this many sequential subtrees.
  5656. This is a short-hand for marking the subtree and then cutting it."
  5657. (interactive "p")
  5658. (org-copy-subtree n 'cut))
  5659. (defun org-copy-subtree (&optional n cut force-store-markers)
  5660. "Cut the current subtree into the clipboard.
  5661. With prefix arg N, cut this many sequential subtrees.
  5662. This is a short-hand for marking the subtree and then copying it.
  5663. If CUT is non-nil, actually cut the subtree.
  5664. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5665. of some markers in the region, even if CUT is non-nil. This is
  5666. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5667. (interactive "p")
  5668. (let (beg end folded (beg0 (point)))
  5669. (if (interactive-p)
  5670. (org-back-to-heading nil) ; take what looks like a subtree
  5671. (org-back-to-heading t)) ; take what is really there
  5672. (org-back-over-empty-lines)
  5673. (setq beg (point))
  5674. (skip-chars-forward " \t\r\n")
  5675. (save-match-data
  5676. (save-excursion (outline-end-of-heading)
  5677. (setq folded (org-invisible-p)))
  5678. (condition-case nil
  5679. (org-forward-same-level (1- n) t)
  5680. (error nil))
  5681. (org-end-of-subtree t t))
  5682. (org-back-over-empty-lines)
  5683. (setq end (point))
  5684. (goto-char beg0)
  5685. (when (> end beg)
  5686. (setq org-subtree-clip-folded folded)
  5687. (when (or cut force-store-markers)
  5688. (org-save-markers-in-region beg end))
  5689. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5690. (setq org-subtree-clip (current-kill 0))
  5691. (message "%s: Subtree(s) with %d characters"
  5692. (if cut "Cut" "Copied")
  5693. (length org-subtree-clip)))))
  5694. (defun org-paste-subtree (&optional level tree for-yank)
  5695. "Paste the clipboard as a subtree, with modification of headline level.
  5696. The entire subtree is promoted or demoted in order to match a new headline
  5697. level.
  5698. If the cursor is at the beginning of a headline, the same level as
  5699. that headline is used to paste the tree
  5700. If not, the new level is derived from the *visible* headings
  5701. before and after the insertion point, and taken to be the inferior headline
  5702. level of the two. So if the previous visible heading is level 3 and the
  5703. next is level 4 (or vice versa), level 4 will be used for insertion.
  5704. This makes sure that the subtree remains an independent subtree and does
  5705. not swallow low level entries.
  5706. You can also force a different level, either by using a numeric prefix
  5707. argument, or by inserting the heading marker by hand. For example, if the
  5708. cursor is after \"*****\", then the tree will be shifted to level 5.
  5709. If optional TREE is given, use this text instead of the kill ring.
  5710. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5711. move back over whitespace before inserting, and move point to the end of
  5712. the inserted text when done."
  5713. (interactive "P")
  5714. (setq tree (or tree (and kill-ring (current-kill 0))))
  5715. (unless (org-kill-is-subtree-p tree)
  5716. (error "%s"
  5717. (substitute-command-keys
  5718. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5719. (let* ((visp (not (org-invisible-p)))
  5720. (txt tree)
  5721. (^re (concat "^\\(" outline-regexp "\\)"))
  5722. (re (concat "\\(" outline-regexp "\\)"))
  5723. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5724. (old-level (if (string-match ^re txt)
  5725. (- (match-end 0) (match-beginning 0) 1)
  5726. -1))
  5727. (force-level (cond (level (prefix-numeric-value level))
  5728. ((and (looking-at "[ \t]*$")
  5729. (string-match
  5730. ^re_ (buffer-substring
  5731. (point-at-bol) (point))))
  5732. (- (match-end 1) (match-beginning 1)))
  5733. ((and (bolp)
  5734. (looking-at org-outline-regexp))
  5735. (- (match-end 0) (point) 1))
  5736. (t nil)))
  5737. (previous-level (save-excursion
  5738. (condition-case nil
  5739. (progn
  5740. (outline-previous-visible-heading 1)
  5741. (if (looking-at re)
  5742. (- (match-end 0) (match-beginning 0) 1)
  5743. 1))
  5744. (error 1))))
  5745. (next-level (save-excursion
  5746. (condition-case nil
  5747. (progn
  5748. (or (looking-at outline-regexp)
  5749. (outline-next-visible-heading 1))
  5750. (if (looking-at re)
  5751. (- (match-end 0) (match-beginning 0) 1)
  5752. 1))
  5753. (error 1))))
  5754. (new-level (or force-level (max previous-level next-level)))
  5755. (shift (if (or (= old-level -1)
  5756. (= new-level -1)
  5757. (= old-level new-level))
  5758. 0
  5759. (- new-level old-level)))
  5760. (delta (if (> shift 0) -1 1))
  5761. (func (if (> shift 0) 'org-demote 'org-promote))
  5762. (org-odd-levels-only nil)
  5763. beg end newend)
  5764. ;; Remove the forced level indicator
  5765. (if force-level
  5766. (delete-region (point-at-bol) (point)))
  5767. ;; Paste
  5768. (beginning-of-line 1)
  5769. (unless for-yank (org-back-over-empty-lines))
  5770. (setq beg (point))
  5771. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5772. (insert-before-markers txt)
  5773. (unless (string-match "\n\\'" txt) (insert "\n"))
  5774. (setq newend (point))
  5775. (org-reinstall-markers-in-region beg)
  5776. (setq end (point))
  5777. (goto-char beg)
  5778. (skip-chars-forward " \t\n\r")
  5779. (setq beg (point))
  5780. (if (and (org-invisible-p) visp)
  5781. (save-excursion (outline-show-heading)))
  5782. ;; Shift if necessary
  5783. (unless (= shift 0)
  5784. (save-restriction
  5785. (narrow-to-region beg end)
  5786. (while (not (= shift 0))
  5787. (org-map-region func (point-min) (point-max))
  5788. (setq shift (+ delta shift)))
  5789. (goto-char (point-min))
  5790. (setq newend (point-max))))
  5791. (when (or (interactive-p) for-yank)
  5792. (message "Clipboard pasted as level %d subtree" new-level))
  5793. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5794. kill-ring
  5795. (eq org-subtree-clip (current-kill 0))
  5796. org-subtree-clip-folded)
  5797. ;; The tree was folded before it was killed/copied
  5798. (hide-subtree))
  5799. (and for-yank (goto-char newend))))
  5800. (defun org-kill-is-subtree-p (&optional txt)
  5801. "Check if the current kill is an outline subtree, or a set of trees.
  5802. Returns nil if kill does not start with a headline, or if the first
  5803. headline level is not the largest headline level in the tree.
  5804. So this will actually accept several entries of equal levels as well,
  5805. which is OK for `org-paste-subtree'.
  5806. If optional TXT is given, check this string instead of the current kill."
  5807. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5808. (start-level (and kill
  5809. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5810. org-outline-regexp "\\)")
  5811. kill)
  5812. (- (match-end 2) (match-beginning 2) 1)))
  5813. (re (concat "^" org-outline-regexp))
  5814. (start (1+ (or (match-beginning 2) -1))))
  5815. (if (not start-level)
  5816. (progn
  5817. nil) ;; does not even start with a heading
  5818. (catch 'exit
  5819. (while (setq start (string-match re kill (1+ start)))
  5820. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5821. (throw 'exit nil)))
  5822. t))))
  5823. (defvar org-markers-to-move nil
  5824. "Markers that should be moved with a cut-and-paste operation.
  5825. Those markers are stored together with their positions relative to
  5826. the start of the region.")
  5827. (defun org-save-markers-in-region (beg end)
  5828. "Check markers in region.
  5829. If these markers are between BEG and END, record their position relative
  5830. to BEG, so that after moving the block of text, we can put the markers back
  5831. into place.
  5832. This function gets called just before an entry or tree gets cut from the
  5833. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5834. called immediately, to move the markers with the entries."
  5835. (setq org-markers-to-move nil)
  5836. (when (featurep 'org-clock)
  5837. (org-clock-save-markers-for-cut-and-paste beg end))
  5838. (when (featurep 'org-agenda)
  5839. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5840. (defun org-check-and-save-marker (marker beg end)
  5841. "Check if MARKER is between BEG and END.
  5842. If yes, remember the marker and the distance to BEG."
  5843. (when (and (marker-buffer marker)
  5844. (equal (marker-buffer marker) (current-buffer)))
  5845. (if (and (>= marker beg) (< marker end))
  5846. (push (cons marker (- marker beg)) org-markers-to-move))))
  5847. (defun org-reinstall-markers-in-region (beg)
  5848. "Move all remembered markers to their position relative to BEG."
  5849. (mapc (lambda (x)
  5850. (move-marker (car x) (+ beg (cdr x))))
  5851. org-markers-to-move)
  5852. (setq org-markers-to-move nil))
  5853. (defun org-narrow-to-subtree ()
  5854. "Narrow buffer to the current subtree."
  5855. (interactive)
  5856. (save-excursion
  5857. (save-match-data
  5858. (narrow-to-region
  5859. (progn (org-back-to-heading t) (point))
  5860. (progn (org-end-of-subtree t t) (point))))))
  5861. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5862. "Clone the task (subtree) at point N times.
  5863. The clones will be inserted as siblings.
  5864. In interactive use, the user will be prompted for the number of clones
  5865. to be produced, and for a time SHIFT, which may be a repeater as used
  5866. in time stamps, for example `+3d'.
  5867. When a valid repeater is given and the entry contains any time stamps,
  5868. the clones will become a sequence in time, with time stamps in the
  5869. subtree shifted for each clone produced. If SHIFT is nil or the
  5870. empty string, time stamps will be left alone.
  5871. If the original subtree did contain time stamps with a repeater,
  5872. the following will happen:
  5873. - the repeater will be removed in each clone
  5874. - an additional clone will be produced, with the current, unshifted
  5875. date(s) in the entry.
  5876. - the original entry will be placed *after* all the clones, with
  5877. repeater intact.
  5878. - the start days in the repeater in the original entry will be shifted
  5879. to past the last clone.
  5880. I this way you can spell out a number of instances of a repeating task,
  5881. and still retain the repeater to cover future instances of the task."
  5882. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5883. (let (beg end template task
  5884. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5885. (if (not (and (integerp n) (> n 0)))
  5886. (error "Invalid number of replications %s" n))
  5887. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5888. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5889. shift)))
  5890. (error "Invalid shift specification %s" shift))
  5891. (when doshift
  5892. (setq shift-n (string-to-number (match-string 1 shift))
  5893. shift-what (cdr (assoc (match-string 2 shift)
  5894. '(("d" . day) ("w" . week)
  5895. ("m" . month) ("y" . year))))))
  5896. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5897. (setq nmin 1 nmax n)
  5898. (org-back-to-heading t)
  5899. (setq beg (point))
  5900. (org-end-of-subtree t t)
  5901. (setq end (point))
  5902. (setq template (buffer-substring beg end))
  5903. (when (and doshift
  5904. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5905. (delete-region beg end)
  5906. (setq end beg)
  5907. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5908. (goto-char end)
  5909. (loop for n from nmin to nmax do
  5910. (if (not doshift)
  5911. (setq task template)
  5912. (with-temp-buffer
  5913. (insert template)
  5914. (org-mode)
  5915. (goto-char (point-min))
  5916. (while (re-search-forward org-ts-regexp-both nil t)
  5917. (org-timestamp-change (* n shift-n) shift-what))
  5918. (unless (= n n-no-remove)
  5919. (goto-char (point-min))
  5920. (while (re-search-forward org-ts-regexp nil t)
  5921. (save-excursion
  5922. (goto-char (match-beginning 0))
  5923. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5924. (delete-region (match-beginning 1) (match-end 1))))))
  5925. (setq task (buffer-string))))
  5926. (insert task))
  5927. (goto-char beg)))
  5928. ;;; Outline Sorting
  5929. (defun org-sort (with-case)
  5930. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5931. Optional argument WITH-CASE means sort case-sensitively.
  5932. With a double prefix argument, also remove duplicate entries."
  5933. (interactive "P")
  5934. (if (org-at-table-p)
  5935. (org-call-with-arg 'org-table-sort-lines with-case)
  5936. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5937. (defun org-sort-remove-invisible (s)
  5938. (remove-text-properties 0 (length s) org-rm-props s)
  5939. (while (string-match org-bracket-link-regexp s)
  5940. (setq s (replace-match (if (match-end 2)
  5941. (match-string 3 s)
  5942. (match-string 1 s)) t t s)))
  5943. s)
  5944. (defvar org-priority-regexp) ; defined later in the file
  5945. (defvar org-after-sorting-entries-or-items-hook nil
  5946. "Hook that is run after a bunch of entries or items have been sorted.
  5947. When children are sorted, the cursor is in the parent line when this
  5948. hook gets called. When a region or a plain list is sorted, the cursor
  5949. will be in the first entry of the sorted region/list.")
  5950. (defun org-sort-entries-or-items
  5951. (&optional with-case sorting-type getkey-func compare-func property)
  5952. "Sort entries on a certain level of an outline tree, or plain list items.
  5953. If there is an active region, the entries in the region are sorted.
  5954. Else, if the cursor is before the first entry, sort the top-level items.
  5955. Else, the children of the entry at point are sorted.
  5956. If the cursor is at the first item in a plain list, the list items will be
  5957. sorted.
  5958. Sorting can be alphabetically, numerically, by date/time as given by
  5959. a time stamp, by a property or by priority.
  5960. The command prompts for the sorting type unless it has been given to the
  5961. function through the SORTING-TYPE argument, which needs to a character,
  5962. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5963. precise meaning of each character:
  5964. n Numerically, by converting the beginning of the entry/item to a number.
  5965. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5966. t By date/time, either the first active time stamp in the entry, or, if
  5967. none exist, by the first inactive one.
  5968. In items, only the first line will be chekced.
  5969. s By the scheduled date/time.
  5970. d By deadline date/time.
  5971. c By creation time, which is assumed to be the first inactive time stamp
  5972. at the beginning of a line.
  5973. p By priority according to the cookie.
  5974. r By the value of a property.
  5975. Capital letters will reverse the sort order.
  5976. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5977. called with point at the beginning of the record. It must return either
  5978. a string or a number that should serve as the sorting key for that record.
  5979. Comparing entries ignores case by default. However, with an optional argument
  5980. WITH-CASE, the sorting considers case as well."
  5981. (interactive "P")
  5982. (let ((case-func (if with-case 'identity 'downcase))
  5983. start beg end stars re re2
  5984. txt what tmp plain-list-p)
  5985. ;; Find beginning and end of region to sort
  5986. (cond
  5987. ((org-region-active-p)
  5988. ;; we will sort the region
  5989. (setq end (region-end)
  5990. what "region")
  5991. (goto-char (region-beginning))
  5992. (if (not (org-on-heading-p)) (outline-next-heading))
  5993. (setq start (point)))
  5994. ((org-at-item-p)
  5995. ;; we will sort this plain list
  5996. (org-beginning-of-item-list) (setq start (point))
  5997. (org-end-of-item-list) (setq end (point))
  5998. (goto-char start)
  5999. (setq plain-list-p t
  6000. what "plain list"))
  6001. ((or (org-on-heading-p)
  6002. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6003. ;; we will sort the children of the current headline
  6004. (org-back-to-heading)
  6005. (setq start (point)
  6006. end (progn (org-end-of-subtree t t)
  6007. (org-back-over-empty-lines)
  6008. (point))
  6009. what "children")
  6010. (goto-char start)
  6011. (show-subtree)
  6012. (outline-next-heading))
  6013. (t
  6014. ;; we will sort the top-level entries in this file
  6015. (goto-char (point-min))
  6016. (or (org-on-heading-p) (outline-next-heading))
  6017. (setq start (point) end (point-max) what "top-level")
  6018. (goto-char start)
  6019. (show-all)))
  6020. (setq beg (point))
  6021. (if (>= beg end) (error "Nothing to sort"))
  6022. (unless plain-list-p
  6023. (looking-at "\\(\\*+\\)")
  6024. (setq stars (match-string 1)
  6025. re (concat "^" (regexp-quote stars) " +")
  6026. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6027. txt (buffer-substring beg end))
  6028. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6029. (if (and (not (equal stars "*")) (string-match re2 txt))
  6030. (error "Region to sort contains a level above the first entry")))
  6031. (unless sorting-type
  6032. (message
  6033. (if plain-list-p
  6034. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6035. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6036. [t]ime [s]cheduled [d]eadline [c]reated
  6037. A/N/T/S/D/C/P/O/F means reversed:")
  6038. what)
  6039. (setq sorting-type (read-char-exclusive))
  6040. (and (= (downcase sorting-type) ?f)
  6041. (setq getkey-func
  6042. (org-icompleting-read "Sort using function: "
  6043. obarray 'fboundp t nil nil))
  6044. (setq getkey-func (intern getkey-func)))
  6045. (and (= (downcase sorting-type) ?r)
  6046. (setq property
  6047. (org-icompleting-read "Property: "
  6048. (mapcar 'list (org-buffer-property-keys t))
  6049. nil t))))
  6050. (message "Sorting entries...")
  6051. (save-restriction
  6052. (narrow-to-region start end)
  6053. (let ((dcst (downcase sorting-type))
  6054. (case-fold-search nil)
  6055. (now (current-time)))
  6056. (sort-subr
  6057. (/= dcst sorting-type)
  6058. ;; This function moves to the beginning character of the "record" to
  6059. ;; be sorted.
  6060. (if plain-list-p
  6061. (lambda nil
  6062. (if (org-at-item-p) t (goto-char (point-max))))
  6063. (lambda nil
  6064. (if (re-search-forward re nil t)
  6065. (goto-char (match-beginning 0))
  6066. (goto-char (point-max)))))
  6067. ;; This function moves to the last character of the "record" being
  6068. ;; sorted.
  6069. (if plain-list-p
  6070. 'org-end-of-item
  6071. (lambda nil
  6072. (save-match-data
  6073. (condition-case nil
  6074. (outline-forward-same-level 1)
  6075. (error
  6076. (goto-char (point-max)))))))
  6077. ;; This function returns the value that gets sorted against.
  6078. (if plain-list-p
  6079. (lambda nil
  6080. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6081. (cond
  6082. ((= dcst ?n)
  6083. (string-to-number (buffer-substring (match-end 0)
  6084. (point-at-eol))))
  6085. ((= dcst ?a)
  6086. (buffer-substring (match-end 0) (point-at-eol)))
  6087. ((= dcst ?t)
  6088. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6089. (re-search-forward org-ts-regexp-both
  6090. (point-at-eol) t))
  6091. (org-time-string-to-seconds (match-string 0))
  6092. (org-float-time now)))
  6093. ((= dcst ?f)
  6094. (if getkey-func
  6095. (progn
  6096. (setq tmp (funcall getkey-func))
  6097. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6098. tmp)
  6099. (error "Invalid key function `%s'" getkey-func)))
  6100. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6101. (lambda nil
  6102. (cond
  6103. ((= dcst ?n)
  6104. (if (looking-at org-complex-heading-regexp)
  6105. (string-to-number (match-string 4))
  6106. nil))
  6107. ((= dcst ?a)
  6108. (if (looking-at org-complex-heading-regexp)
  6109. (funcall case-func (match-string 4))
  6110. nil))
  6111. ((= dcst ?t)
  6112. (let ((end (save-excursion (outline-next-heading) (point))))
  6113. (if (or (re-search-forward org-ts-regexp end t)
  6114. (re-search-forward org-ts-regexp-both end t))
  6115. (org-time-string-to-seconds (match-string 0))
  6116. (org-float-time now))))
  6117. ((= dcst ?c)
  6118. (let ((end (save-excursion (outline-next-heading) (point))))
  6119. (if (re-search-forward
  6120. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6121. end t)
  6122. (org-time-string-to-seconds (match-string 0))
  6123. (org-float-time now))))
  6124. ((= dcst ?s)
  6125. (let ((end (save-excursion (outline-next-heading) (point))))
  6126. (if (re-search-forward org-scheduled-time-regexp end t)
  6127. (org-time-string-to-seconds (match-string 1))
  6128. (org-float-time now))))
  6129. ((= dcst ?d)
  6130. (let ((end (save-excursion (outline-next-heading) (point))))
  6131. (if (re-search-forward org-deadline-time-regexp end t)
  6132. (org-time-string-to-seconds (match-string 1))
  6133. (org-float-time now))))
  6134. ((= dcst ?p)
  6135. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6136. (string-to-char (match-string 2))
  6137. org-default-priority))
  6138. ((= dcst ?r)
  6139. (or (org-entry-get nil property) ""))
  6140. ((= dcst ?o)
  6141. (if (looking-at org-complex-heading-regexp)
  6142. (- 9999 (length (member (match-string 2)
  6143. org-todo-keywords-1)))))
  6144. ((= dcst ?f)
  6145. (if getkey-func
  6146. (progn
  6147. (setq tmp (funcall getkey-func))
  6148. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6149. tmp)
  6150. (error "Invalid key function `%s'" getkey-func)))
  6151. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6152. nil
  6153. (cond
  6154. ((= dcst ?a) 'string<)
  6155. ((= dcst ?f) compare-func)
  6156. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6157. (t nil)))))
  6158. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6159. (message "Sorting entries...done")))
  6160. (defun org-do-sort (table what &optional with-case sorting-type)
  6161. "Sort TABLE of WHAT according to SORTING-TYPE.
  6162. The user will be prompted for the SORTING-TYPE if the call to this
  6163. function does not specify it. WHAT is only for the prompt, to indicate
  6164. what is being sorted. The sorting key will be extracted from
  6165. the car of the elements of the table.
  6166. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6167. (unless sorting-type
  6168. (message
  6169. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6170. what)
  6171. (setq sorting-type (read-char-exclusive)))
  6172. (let ((dcst (downcase sorting-type))
  6173. extractfun comparefun)
  6174. ;; Define the appropriate functions
  6175. (cond
  6176. ((= dcst ?n)
  6177. (setq extractfun 'string-to-number
  6178. comparefun (if (= dcst sorting-type) '< '>)))
  6179. ((= dcst ?a)
  6180. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6181. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6182. comparefun (if (= dcst sorting-type)
  6183. 'string<
  6184. (lambda (a b) (and (not (string< a b))
  6185. (not (string= a b)))))))
  6186. ((= dcst ?t)
  6187. (setq extractfun
  6188. (lambda (x)
  6189. (if (or (string-match org-ts-regexp x)
  6190. (string-match org-ts-regexp-both x))
  6191. (org-float-time
  6192. (org-time-string-to-time (match-string 0 x)))
  6193. 0))
  6194. comparefun (if (= dcst sorting-type) '< '>)))
  6195. (t (error "Invalid sorting type `%c'" sorting-type)))
  6196. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6197. table)
  6198. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6199. ;;; The orgstruct minor mode
  6200. ;; Define a minor mode which can be used in other modes in order to
  6201. ;; integrate the org-mode structure editing commands.
  6202. ;; This is really a hack, because the org-mode structure commands use
  6203. ;; keys which normally belong to the major mode. Here is how it
  6204. ;; works: The minor mode defines all the keys necessary to operate the
  6205. ;; structure commands, but wraps the commands into a function which
  6206. ;; tests if the cursor is currently at a headline or a plain list
  6207. ;; item. If that is the case, the structure command is used,
  6208. ;; temporarily setting many Org-mode variables like regular
  6209. ;; expressions for filling etc. However, when any of those keys is
  6210. ;; used at a different location, function uses `key-binding' to look
  6211. ;; up if the key has an associated command in another currently active
  6212. ;; keymap (minor modes, major mode, global), and executes that
  6213. ;; command. There might be problems if any of the keys is otherwise
  6214. ;; used as a prefix key.
  6215. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6216. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6217. ;; addresses this by checking explicitly for both bindings.
  6218. (defvar orgstruct-mode-map (make-sparse-keymap)
  6219. "Keymap for the minor `orgstruct-mode'.")
  6220. (defvar org-local-vars nil
  6221. "List of local variables, for use by `orgstruct-mode'")
  6222. ;;;###autoload
  6223. (define-minor-mode orgstruct-mode
  6224. "Toggle the minor more `orgstruct-mode'.
  6225. This mode is for using Org-mode structure commands in other modes.
  6226. The following key behave as if Org-mode was active, if the cursor
  6227. is on a headline, or on a plain list item (both in the definition
  6228. of Org-mode).
  6229. M-up Move entry/item up
  6230. M-down Move entry/item down
  6231. M-left Promote
  6232. M-right Demote
  6233. M-S-up Move entry/item up
  6234. M-S-down Move entry/item down
  6235. M-S-left Promote subtree
  6236. M-S-right Demote subtree
  6237. M-q Fill paragraph and items like in Org-mode
  6238. C-c ^ Sort entries
  6239. C-c - Cycle list bullet
  6240. TAB Cycle item visibility
  6241. M-RET Insert new heading/item
  6242. S-M-RET Insert new TODO heading / Checkbox item
  6243. C-c C-c Set tags / toggle checkbox"
  6244. nil " OrgStruct" nil
  6245. (org-load-modules-maybe)
  6246. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6247. ;;;###autoload
  6248. (defun turn-on-orgstruct ()
  6249. "Unconditionally turn on `orgstruct-mode'."
  6250. (orgstruct-mode 1))
  6251. (defun orgstruct++-mode (&optional arg)
  6252. "Toggle `orgstruct-mode', the enhanced version of it.
  6253. In addition to setting orgstruct-mode, this also exports all indentation
  6254. and autofilling variables from org-mode into the buffer. It will also
  6255. recognize item context in multiline items.
  6256. Note that turning off orgstruct-mode will *not* remove the
  6257. indentation/paragraph settings. This can only be done by refreshing the
  6258. major mode, for example with \\[normal-mode]."
  6259. (interactive "P")
  6260. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6261. (if (< arg 1)
  6262. (orgstruct-mode -1)
  6263. (orgstruct-mode 1)
  6264. (let (var val)
  6265. (mapc
  6266. (lambda (x)
  6267. (when (string-match
  6268. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6269. (symbol-name (car x)))
  6270. (setq var (car x) val (nth 1 x))
  6271. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6272. org-local-vars)
  6273. (org-set-local 'orgstruct-is-++ t))))
  6274. (defvar orgstruct-is-++ nil
  6275. "Is orgstruct-mode in ++ version in the current-buffer?")
  6276. (make-variable-buffer-local 'orgstruct-is-++)
  6277. ;;;###autoload
  6278. (defun turn-on-orgstruct++ ()
  6279. "Unconditionally turn on `orgstruct++-mode'."
  6280. (orgstruct++-mode 1))
  6281. (defun orgstruct-error ()
  6282. "Error when there is no default binding for a structure key."
  6283. (interactive)
  6284. (error "This key has no function outside structure elements"))
  6285. (defun orgstruct-setup ()
  6286. "Setup orgstruct keymaps."
  6287. (let ((nfunc 0)
  6288. (bindings
  6289. (list
  6290. '([(meta up)] org-metaup)
  6291. '([(meta down)] org-metadown)
  6292. '([(meta left)] org-metaleft)
  6293. '([(meta right)] org-metaright)
  6294. '([(meta shift up)] org-shiftmetaup)
  6295. '([(meta shift down)] org-shiftmetadown)
  6296. '([(meta shift left)] org-shiftmetaleft)
  6297. '([(meta shift right)] org-shiftmetaright)
  6298. '([?\e (up)] org-metaup)
  6299. '([?\e (down)] org-metadown)
  6300. '([?\e (left)] org-metaleft)
  6301. '([?\e (right)] org-metaright)
  6302. '([?\e (shift up)] org-shiftmetaup)
  6303. '([?\e (shift down)] org-shiftmetadown)
  6304. '([?\e (shift left)] org-shiftmetaleft)
  6305. '([?\e (shift right)] org-shiftmetaright)
  6306. '([(shift up)] org-shiftup)
  6307. '([(shift down)] org-shiftdown)
  6308. '([(shift left)] org-shiftleft)
  6309. '([(shift right)] org-shiftright)
  6310. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6311. '("\M-q" fill-paragraph)
  6312. '("\C-c^" org-sort)
  6313. '("\C-c-" org-cycle-list-bullet)))
  6314. elt key fun cmd)
  6315. (while (setq elt (pop bindings))
  6316. (setq nfunc (1+ nfunc))
  6317. (setq key (org-key (car elt))
  6318. fun (nth 1 elt)
  6319. cmd (orgstruct-make-binding fun nfunc key))
  6320. (org-defkey orgstruct-mode-map key cmd))
  6321. ;; Special treatment needed for TAB and RET
  6322. (org-defkey orgstruct-mode-map [(tab)]
  6323. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6324. (org-defkey orgstruct-mode-map "\C-i"
  6325. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6326. (org-defkey orgstruct-mode-map "\M-\C-m"
  6327. (orgstruct-make-binding 'org-insert-heading 105
  6328. "\M-\C-m" [(meta return)]))
  6329. (org-defkey orgstruct-mode-map [(meta return)]
  6330. (orgstruct-make-binding 'org-insert-heading 106
  6331. [(meta return)] "\M-\C-m"))
  6332. (org-defkey orgstruct-mode-map [(shift meta return)]
  6333. (orgstruct-make-binding 'org-insert-todo-heading 107
  6334. [(meta return)] "\M-\C-m"))
  6335. (org-defkey orgstruct-mode-map "\e\C-m"
  6336. (orgstruct-make-binding 'org-insert-heading 108
  6337. "\e\C-m" [?\e (return)]))
  6338. (org-defkey orgstruct-mode-map [?\e (return)]
  6339. (orgstruct-make-binding 'org-insert-heading 109
  6340. [?\e (return)] "\e\C-m"))
  6341. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6342. (orgstruct-make-binding 'org-insert-todo-heading 110
  6343. [?\e (return)] "\e\C-m"))
  6344. (unless org-local-vars
  6345. (setq org-local-vars (org-get-local-variables)))
  6346. t))
  6347. (defun orgstruct-make-binding (fun n &rest keys)
  6348. "Create a function for binding in the structure minor mode.
  6349. FUN is the command to call inside a table. N is used to create a unique
  6350. command name. KEYS are keys that should be checked in for a command
  6351. to execute outside of tables."
  6352. (eval
  6353. (list 'defun
  6354. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6355. '(arg)
  6356. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6357. "Outside of structure, run the binding of `"
  6358. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6359. "'.")
  6360. '(interactive "p")
  6361. (list 'if
  6362. `(org-context-p 'headline 'item
  6363. (and orgstruct-is-++
  6364. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6365. 'item-body))
  6366. (list 'org-run-like-in-org-mode (list 'quote fun))
  6367. (list 'let '(orgstruct-mode)
  6368. (list 'call-interactively
  6369. (append '(or)
  6370. (mapcar (lambda (k)
  6371. (list 'key-binding k))
  6372. keys)
  6373. '('orgstruct-error))))))))
  6374. (defun org-context-p (&rest contexts)
  6375. "Check if local context is any of CONTEXTS.
  6376. Possible values in the list of contexts are `table', `headline', and `item'."
  6377. (let ((pos (point)))
  6378. (goto-char (point-at-bol))
  6379. (prog1 (or (and (memq 'table contexts)
  6380. (looking-at "[ \t]*|"))
  6381. (and (memq 'headline contexts)
  6382. ;;????????? (looking-at "\\*+"))
  6383. (looking-at outline-regexp))
  6384. (and (memq 'item contexts)
  6385. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6386. (and (memq 'item-body contexts)
  6387. (org-in-item-p)))
  6388. (goto-char pos))))
  6389. (defun org-get-local-variables ()
  6390. "Return a list of all local variables in an org-mode buffer."
  6391. (let (varlist)
  6392. (with-current-buffer (get-buffer-create "*Org tmp*")
  6393. (erase-buffer)
  6394. (org-mode)
  6395. (setq varlist (buffer-local-variables)))
  6396. (kill-buffer "*Org tmp*")
  6397. (delq nil
  6398. (mapcar
  6399. (lambda (x)
  6400. (setq x
  6401. (if (symbolp x)
  6402. (list x)
  6403. (list (car x) (list 'quote (cdr x)))))
  6404. (if (string-match
  6405. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6406. (symbol-name (car x)))
  6407. x nil))
  6408. varlist))))
  6409. ;;;###autoload
  6410. (defun org-run-like-in-org-mode (cmd)
  6411. "Run a command, pretending that the current buffer is in Org-mode.
  6412. This will temporarily bind local variables that are typically bound in
  6413. Org-mode to the values they have in Org-mode, and then interactively
  6414. call CMD."
  6415. (org-load-modules-maybe)
  6416. (unless org-local-vars
  6417. (setq org-local-vars (org-get-local-variables)))
  6418. (eval (list 'let org-local-vars
  6419. (list 'call-interactively (list 'quote cmd)))))
  6420. ;;;; Archiving
  6421. (defun org-get-category (&optional pos)
  6422. "Get the category applying to position POS."
  6423. (get-text-property (or pos (point)) 'org-category))
  6424. (defun org-refresh-category-properties ()
  6425. "Refresh category text properties in the buffer."
  6426. (let ((def-cat (cond
  6427. ((null org-category)
  6428. (if buffer-file-name
  6429. (file-name-sans-extension
  6430. (file-name-nondirectory buffer-file-name))
  6431. "???"))
  6432. ((symbolp org-category) (symbol-name org-category))
  6433. (t org-category)))
  6434. beg end cat pos optionp)
  6435. (org-unmodified
  6436. (save-excursion
  6437. (save-restriction
  6438. (widen)
  6439. (goto-char (point-min))
  6440. (put-text-property (point) (point-max) 'org-category def-cat)
  6441. (while (re-search-forward
  6442. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6443. (setq pos (match-end 0)
  6444. optionp (equal (char-after (match-beginning 0)) ?#)
  6445. cat (org-trim (match-string 2)))
  6446. (if optionp
  6447. (setq beg (point-at-bol) end (point-max))
  6448. (org-back-to-heading t)
  6449. (setq beg (point) end (org-end-of-subtree t t)))
  6450. (put-text-property beg end 'org-category cat)
  6451. (goto-char pos)))))))
  6452. ;;;; Link Stuff
  6453. ;;; Link abbreviations
  6454. (defun org-link-expand-abbrev (link)
  6455. "Apply replacements as defined in `org-link-abbrev-alist."
  6456. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6457. (let* ((key (match-string 1 link))
  6458. (as (or (assoc key org-link-abbrev-alist-local)
  6459. (assoc key org-link-abbrev-alist)))
  6460. (tag (and (match-end 2) (match-string 3 link)))
  6461. rpl)
  6462. (if (not as)
  6463. link
  6464. (setq rpl (cdr as))
  6465. (cond
  6466. ((symbolp rpl) (funcall rpl tag))
  6467. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6468. ((string-match "%h" rpl)
  6469. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6470. (t (concat rpl tag)))))
  6471. link))
  6472. ;;; Storing and inserting links
  6473. (defvar org-insert-link-history nil
  6474. "Minibuffer history for links inserted with `org-insert-link'.")
  6475. (defvar org-stored-links nil
  6476. "Contains the links stored with `org-store-link'.")
  6477. (defvar org-store-link-plist nil
  6478. "Plist with info about the most recently link created with `org-store-link'.")
  6479. (defvar org-link-protocols nil
  6480. "Link protocols added to Org-mode using `org-add-link-type'.")
  6481. (defvar org-store-link-functions nil
  6482. "List of functions that are called to create and store a link.
  6483. Each function will be called in turn until one returns a non-nil
  6484. value. Each function should check if it is responsible for creating
  6485. this link (for example by looking at the major mode).
  6486. If not, it must exit and return nil.
  6487. If yes, it should return a non-nil value after a calling
  6488. `org-store-link-props' with a list of properties and values.
  6489. Special properties are:
  6490. :type The link prefix. like \"http\". This must be given.
  6491. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6492. This is obligatory as well.
  6493. :description Optional default description for the second pair
  6494. of brackets in an Org-mode link. The user can still change
  6495. this when inserting this link into an Org-mode buffer.
  6496. In addition to these, any additional properties can be specified
  6497. and then used in remember templates.")
  6498. (defun org-add-link-type (type &optional follow export)
  6499. "Add TYPE to the list of `org-link-types'.
  6500. Re-compute all regular expressions depending on `org-link-types'
  6501. FOLLOW and EXPORT are two functions.
  6502. FOLLOW should take the link path as the single argument and do whatever
  6503. is necessary to follow the link, for example find a file or display
  6504. a mail message.
  6505. EXPORT should format the link path for export to one of the export formats.
  6506. It should be a function accepting three arguments:
  6507. path the path of the link, the text after the prefix (like \"http:\")
  6508. desc the description of the link, if any, nil if there was no description
  6509. format the export format, a symbol like `html' or `latex'.
  6510. The function may use the FORMAT information to return different values
  6511. depending on the format. The return value will be put literally into
  6512. the exported file.
  6513. Org-mode has a built-in default for exporting links. If you are happy with
  6514. this default, there is no need to define an export function for the link
  6515. type. For a simple example of an export function, see `org-bbdb.el'."
  6516. (add-to-list 'org-link-types type t)
  6517. (org-make-link-regexps)
  6518. (if (assoc type org-link-protocols)
  6519. (setcdr (assoc type org-link-protocols) (list follow export))
  6520. (push (list type follow export) org-link-protocols)))
  6521. (defvar org-agenda-buffer-name)
  6522. ;;;###autoload
  6523. (defun org-store-link (arg)
  6524. "\\<org-mode-map>Store an org-link to the current location.
  6525. This link is added to `org-stored-links' and can later be inserted
  6526. into an org-buffer with \\[org-insert-link].
  6527. For some link types, a prefix arg is interpreted:
  6528. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6529. For file links, arg negates `org-context-in-file-links'."
  6530. (interactive "P")
  6531. (org-load-modules-maybe)
  6532. (setq org-store-link-plist nil) ; reset
  6533. (let ((outline-regexp (org-get-limited-outline-regexp))
  6534. link cpltxt desc description search txt custom-id)
  6535. (cond
  6536. ((run-hook-with-args-until-success 'org-store-link-functions)
  6537. (setq link (plist-get org-store-link-plist :link)
  6538. desc (or (plist-get org-store-link-plist :description) link)))
  6539. ((equal (buffer-name) "*Org Edit Src Example*")
  6540. (let (label gc)
  6541. (while (or (not label)
  6542. (save-excursion
  6543. (save-restriction
  6544. (widen)
  6545. (goto-char (point-min))
  6546. (re-search-forward
  6547. (regexp-quote (format org-coderef-label-format label))
  6548. nil t))))
  6549. (when label (message "Label exists already") (sit-for 2))
  6550. (setq label (read-string "Code line label: " label)))
  6551. (end-of-line 1)
  6552. (setq link (format org-coderef-label-format label))
  6553. (setq gc (- 79 (length link)))
  6554. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6555. (insert link)
  6556. (setq link (concat "(" label ")") desc nil)))
  6557. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6558. ;; We are in the agenda, link to referenced location
  6559. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6560. (get-text-property (point) 'org-marker))))
  6561. (when m
  6562. (org-with-point-at m
  6563. (call-interactively 'org-store-link)))))
  6564. ((eq major-mode 'calendar-mode)
  6565. (let ((cd (calendar-cursor-to-date)))
  6566. (setq link
  6567. (format-time-string
  6568. (car org-time-stamp-formats)
  6569. (apply 'encode-time
  6570. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6571. nil nil nil))))
  6572. (org-store-link-props :type "calendar" :date cd)))
  6573. ((eq major-mode 'w3-mode)
  6574. (setq cpltxt (if (and (buffer-name)
  6575. (not (string-match "Untitled" (buffer-name))))
  6576. (buffer-name)
  6577. (url-view-url t))
  6578. link (org-make-link (url-view-url t)))
  6579. (org-store-link-props :type "w3" :url (url-view-url t)))
  6580. ((eq major-mode 'w3m-mode)
  6581. (setq cpltxt (or w3m-current-title w3m-current-url)
  6582. link (org-make-link w3m-current-url))
  6583. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6584. ((setq search (run-hook-with-args-until-success
  6585. 'org-create-file-search-functions))
  6586. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6587. "::" search))
  6588. (setq cpltxt (or description link)))
  6589. ((eq major-mode 'image-mode)
  6590. (setq cpltxt (concat "file:"
  6591. (abbreviate-file-name buffer-file-name))
  6592. link (org-make-link cpltxt))
  6593. (org-store-link-props :type "image" :file buffer-file-name))
  6594. ((eq major-mode 'dired-mode)
  6595. ;; link to the file in the current line
  6596. (setq cpltxt (concat "file:"
  6597. (abbreviate-file-name
  6598. (expand-file-name
  6599. (dired-get-filename nil t))))
  6600. link (org-make-link cpltxt)))
  6601. ((and buffer-file-name (org-mode-p))
  6602. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6603. (cond
  6604. ((org-in-regexp "<<\\(.*?\\)>>")
  6605. (setq cpltxt
  6606. (concat "file:"
  6607. (abbreviate-file-name buffer-file-name)
  6608. "::" (match-string 1))
  6609. link (org-make-link cpltxt)))
  6610. ((and (featurep 'org-id)
  6611. (or (eq org-link-to-org-use-id t)
  6612. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6613. (interactive-p))
  6614. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6615. (interactive-p)
  6616. (not custom-id))
  6617. (and org-link-to-org-use-id
  6618. (condition-case nil
  6619. (org-entry-get nil "ID")
  6620. (error nil)))))
  6621. ;; We can make a link using the ID.
  6622. (setq link (condition-case nil
  6623. (prog1 (org-id-store-link)
  6624. (setq desc (plist-get org-store-link-plist
  6625. :description)))
  6626. (error
  6627. ;; probably before first headline, link to file only
  6628. (concat "file:"
  6629. (abbreviate-file-name buffer-file-name))))))
  6630. (t
  6631. ;; Just link to current headline
  6632. (setq cpltxt (concat "file:"
  6633. (abbreviate-file-name buffer-file-name)))
  6634. ;; Add a context search string
  6635. (when (org-xor org-context-in-file-links arg)
  6636. (setq txt (cond
  6637. ((org-on-heading-p) nil)
  6638. ((org-region-active-p)
  6639. (buffer-substring (region-beginning) (region-end)))
  6640. (t nil)))
  6641. (when (or (null txt) (string-match "\\S-" txt))
  6642. (setq cpltxt
  6643. (concat cpltxt "::"
  6644. (condition-case nil
  6645. (org-make-org-heading-search-string txt)
  6646. (error "")))
  6647. desc (or (nth 4 (ignore-errors
  6648. (org-heading-components))) "NONE"))))
  6649. (if (string-match "::\\'" cpltxt)
  6650. (setq cpltxt (substring cpltxt 0 -2)))
  6651. (setq link (org-make-link cpltxt)))))
  6652. ((buffer-file-name (buffer-base-buffer))
  6653. ;; Just link to this file here.
  6654. (setq cpltxt (concat "file:"
  6655. (abbreviate-file-name
  6656. (buffer-file-name (buffer-base-buffer)))))
  6657. ;; Add a context string
  6658. (when (org-xor org-context-in-file-links arg)
  6659. (setq txt (if (org-region-active-p)
  6660. (buffer-substring (region-beginning) (region-end))
  6661. (buffer-substring (point-at-bol) (point-at-eol))))
  6662. ;; Only use search option if there is some text.
  6663. (when (string-match "\\S-" txt)
  6664. (setq cpltxt
  6665. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6666. desc "NONE")))
  6667. (setq link (org-make-link cpltxt)))
  6668. ((interactive-p)
  6669. (error "Cannot link to a buffer which is not visiting a file"))
  6670. (t (setq link nil)))
  6671. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6672. (setq link (or link cpltxt)
  6673. desc (or desc cpltxt))
  6674. (if (equal desc "NONE") (setq desc nil))
  6675. (if (and (or (interactive-p) executing-kbd-macro) link)
  6676. (progn
  6677. (setq org-stored-links
  6678. (cons (list link desc) org-stored-links))
  6679. (message "Stored: %s" (or desc link))
  6680. (when custom-id
  6681. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6682. "::#" custom-id))
  6683. (setq org-stored-links
  6684. (cons (list link desc) org-stored-links))))
  6685. (and link (org-make-link-string link desc)))))
  6686. (defun org-store-link-props (&rest plist)
  6687. "Store link properties, extract names and addresses."
  6688. (let (x adr)
  6689. (when (setq x (plist-get plist :from))
  6690. (setq adr (mail-extract-address-components x))
  6691. (setq plist (plist-put plist :fromname (car adr)))
  6692. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6693. (when (setq x (plist-get plist :to))
  6694. (setq adr (mail-extract-address-components x))
  6695. (setq plist (plist-put plist :toname (car adr)))
  6696. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6697. (let ((from (plist-get plist :from))
  6698. (to (plist-get plist :to)))
  6699. (when (and from to org-from-is-user-regexp)
  6700. (setq plist
  6701. (plist-put plist :fromto
  6702. (if (string-match org-from-is-user-regexp from)
  6703. (concat "to %t")
  6704. (concat "from %f"))))))
  6705. (setq org-store-link-plist plist))
  6706. (defun org-add-link-props (&rest plist)
  6707. "Add these properties to the link property list."
  6708. (let (key value)
  6709. (while plist
  6710. (setq key (pop plist) value (pop plist))
  6711. (setq org-store-link-plist
  6712. (plist-put org-store-link-plist key value)))))
  6713. (defun org-email-link-description (&optional fmt)
  6714. "Return the description part of an email link.
  6715. This takes information from `org-store-link-plist' and formats it
  6716. according to FMT (default from `org-email-link-description-format')."
  6717. (setq fmt (or fmt org-email-link-description-format))
  6718. (let* ((p org-store-link-plist)
  6719. (to (plist-get p :toaddress))
  6720. (from (plist-get p :fromaddress))
  6721. (table
  6722. (list
  6723. (cons "%c" (plist-get p :fromto))
  6724. (cons "%F" (plist-get p :from))
  6725. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6726. (cons "%T" (plist-get p :to))
  6727. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6728. (cons "%s" (plist-get p :subject))
  6729. (cons "%m" (plist-get p :message-id)))))
  6730. (when (string-match "%c" fmt)
  6731. ;; Check if the user wrote this message
  6732. (if (and org-from-is-user-regexp from to
  6733. (save-match-data (string-match org-from-is-user-regexp from)))
  6734. (setq fmt (replace-match "to %t" t t fmt))
  6735. (setq fmt (replace-match "from %f" t t fmt))))
  6736. (org-replace-escapes fmt table)))
  6737. (defun org-make-org-heading-search-string (&optional string heading)
  6738. "Make search string for STRING or current headline."
  6739. (interactive)
  6740. (let ((s (or string (org-get-heading))))
  6741. (unless (and string (not heading))
  6742. ;; We are using a headline, clean up garbage in there.
  6743. (if (string-match org-todo-regexp s)
  6744. (setq s (replace-match "" t t s)))
  6745. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6746. (setq s (replace-match "" t t s)))
  6747. (setq s (org-trim s))
  6748. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6749. org-comment-string "\\)") s)
  6750. (setq s (replace-match "" t t s)))
  6751. (while (string-match org-ts-regexp s)
  6752. (setq s (replace-match "" t t s))))
  6753. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6754. (setq s (replace-match " " t t s)))
  6755. (or string (setq s (concat "*" s))) ; Add * for headlines
  6756. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6757. (defun org-make-link (&rest strings)
  6758. "Concatenate STRINGS."
  6759. (apply 'concat strings))
  6760. (defun org-make-link-string (link &optional description)
  6761. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6762. (unless (string-match "\\S-" link)
  6763. (error "Empty link"))
  6764. (when (stringp description)
  6765. ;; Remove brackets from the description, they are fatal.
  6766. (while (string-match "\\[" description)
  6767. (setq description (replace-match "{" t t description)))
  6768. (while (string-match "\\]" description)
  6769. (setq description (replace-match "}" t t description))))
  6770. (when (equal (org-link-escape link) description)
  6771. ;; No description needed, it is identical
  6772. (setq description nil))
  6773. (when (and (not description)
  6774. (not (equal link (org-link-escape link))))
  6775. (setq description (org-extract-attributes link)))
  6776. (concat "[[" (org-link-escape link) "]"
  6777. (if description (concat "[" description "]") "")
  6778. "]"))
  6779. (defconst org-link-escape-chars
  6780. '((?\ . "%20")
  6781. (?\[ . "%5B")
  6782. (?\] . "%5D")
  6783. (?\340 . "%E0") ; `a
  6784. (?\342 . "%E2") ; ^a
  6785. (?\347 . "%E7") ; ,c
  6786. (?\350 . "%E8") ; `e
  6787. (?\351 . "%E9") ; 'e
  6788. (?\352 . "%EA") ; ^e
  6789. (?\356 . "%EE") ; ^i
  6790. (?\364 . "%F4") ; ^o
  6791. (?\371 . "%F9") ; `u
  6792. (?\373 . "%FB") ; ^u
  6793. (?\; . "%3B")
  6794. (?? . "%3F")
  6795. (?= . "%3D")
  6796. (?+ . "%2B")
  6797. )
  6798. "Association list of escapes for some characters problematic in links.
  6799. This is the list that is used for internal purposes.")
  6800. (defvar org-url-encoding-use-url-hexify nil)
  6801. (defconst org-link-escape-chars-browser
  6802. '((?\ . "%20")) ; 32 for the SPC char
  6803. "Association list of escapes for some characters problematic in links.
  6804. This is the list that is used before handing over to the browser.")
  6805. (defun org-link-escape (text &optional table)
  6806. "Escape characters in TEXT that are problematic for links."
  6807. (if org-url-encoding-use-url-hexify
  6808. (url-hexify-string text)
  6809. (setq table (or table org-link-escape-chars))
  6810. (when text
  6811. (let ((re (mapconcat (lambda (x) (regexp-quote
  6812. (char-to-string (car x))))
  6813. table "\\|")))
  6814. (while (string-match re text)
  6815. (setq text
  6816. (replace-match
  6817. (cdr (assoc (string-to-char (match-string 0 text))
  6818. table))
  6819. t t text)))
  6820. text))))
  6821. (defun org-link-unescape (text &optional table)
  6822. "Reverse the action of `org-link-escape'."
  6823. (if org-url-encoding-use-url-hexify
  6824. (url-unhex-string text)
  6825. (setq table (or table org-link-escape-chars))
  6826. (when text
  6827. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6828. table "\\|")))
  6829. (while (string-match re text)
  6830. (setq text
  6831. (replace-match
  6832. (char-to-string (car (rassoc (match-string 0 text) table)))
  6833. t t text)))
  6834. text))))
  6835. (defun org-xor (a b)
  6836. "Exclusive or."
  6837. (if a (not b) b))
  6838. (defun org-fixup-message-id-for-http (s)
  6839. "Replace special characters in a message id, so it can be used in an http query."
  6840. (while (string-match "<" s)
  6841. (setq s (replace-match "%3C" t t s)))
  6842. (while (string-match ">" s)
  6843. (setq s (replace-match "%3E" t t s)))
  6844. (while (string-match "@" s)
  6845. (setq s (replace-match "%40" t t s)))
  6846. s)
  6847. ;;;###autoload
  6848. (defun org-insert-link-global ()
  6849. "Insert a link like Org-mode does.
  6850. This command can be called in any mode to insert a link in Org-mode syntax."
  6851. (interactive)
  6852. (org-load-modules-maybe)
  6853. (org-run-like-in-org-mode 'org-insert-link))
  6854. (defun org-insert-link (&optional complete-file link-location)
  6855. "Insert a link. At the prompt, enter the link.
  6856. Completion can be used to insert any of the link protocol prefixes like
  6857. http or ftp in use.
  6858. The history can be used to select a link previously stored with
  6859. `org-store-link'. When the empty string is entered (i.e. if you just
  6860. press RET at the prompt), the link defaults to the most recently
  6861. stored link. As SPC triggers completion in the minibuffer, you need to
  6862. use M-SPC or C-q SPC to force the insertion of a space character.
  6863. You will also be prompted for a description, and if one is given, it will
  6864. be displayed in the buffer instead of the link.
  6865. If there is already a link at point, this command will allow you to edit link
  6866. and description parts.
  6867. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6868. be selected using completion. The path to the file will be relative to the
  6869. current directory if the file is in the current directory or a subdirectory.
  6870. Otherwise, the link will be the absolute path as completed in the minibuffer
  6871. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6872. option `org-link-file-path-type'.
  6873. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6874. the current directory or below.
  6875. With three \\[universal-argument] prefixes, negate the meaning of
  6876. `org-keep-stored-link-after-insertion'.
  6877. If `org-make-link-description-function' is non-nil, this function will be
  6878. called with the link target, and the result will be the default
  6879. link description.
  6880. If the LINK-LOCATION parameter is non-nil, this value will be
  6881. used as the link location instead of reading one interactively."
  6882. (interactive "P")
  6883. (let* ((wcf (current-window-configuration))
  6884. (region (if (org-region-active-p)
  6885. (buffer-substring (region-beginning) (region-end))))
  6886. (remove (and region (list (region-beginning) (region-end))))
  6887. (desc region)
  6888. tmphist ; byte-compile incorrectly complains about this
  6889. (link link-location)
  6890. entry file all-prefixes)
  6891. (cond
  6892. (link-location) ; specified by arg, just use it.
  6893. ((org-in-regexp org-bracket-link-regexp 1)
  6894. ;; We do have a link at point, and we are going to edit it.
  6895. (setq remove (list (match-beginning 0) (match-end 0)))
  6896. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6897. (setq link (read-string "Link: "
  6898. (org-link-unescape
  6899. (org-match-string-no-properties 1)))))
  6900. ((or (org-in-regexp org-angle-link-re)
  6901. (org-in-regexp org-plain-link-re))
  6902. ;; Convert to bracket link
  6903. (setq remove (list (match-beginning 0) (match-end 0))
  6904. link (read-string "Link: "
  6905. (org-remove-angle-brackets (match-string 0)))))
  6906. ((member complete-file '((4) (16)))
  6907. ;; Completing read for file names.
  6908. (setq link (org-file-complete-link complete-file)))
  6909. (t
  6910. ;; Read link, with completion for stored links.
  6911. (with-output-to-temp-buffer "*Org Links*"
  6912. (princ "Insert a link.
  6913. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6914. (when org-stored-links
  6915. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6916. (princ (mapconcat
  6917. (lambda (x)
  6918. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6919. (reverse org-stored-links) "\n"))))
  6920. (let ((cw (selected-window)))
  6921. (select-window (get-buffer-window "*Org Links*"))
  6922. (setq truncate-lines t)
  6923. (unless (pos-visible-in-window-p (point-max))
  6924. (org-fit-window-to-buffer))
  6925. (and (window-live-p cw) (select-window cw)))
  6926. ;; Fake a link history, containing the stored links.
  6927. (setq tmphist (append (mapcar 'car org-stored-links)
  6928. org-insert-link-history))
  6929. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6930. (mapcar 'car org-link-abbrev-alist)
  6931. org-link-types))
  6932. (unwind-protect
  6933. (progn
  6934. (setq link
  6935. (let ((org-completion-use-ido nil)
  6936. (org-completion-use-iswitchb nil))
  6937. (org-completing-read
  6938. "Link: "
  6939. (append
  6940. (mapcar (lambda (x) (list (concat x ":")))
  6941. all-prefixes)
  6942. (mapcar 'car org-stored-links))
  6943. nil nil nil
  6944. 'tmphist
  6945. (car (car org-stored-links)))))
  6946. (if (or (member link all-prefixes)
  6947. (and (equal ":" (substring link -1))
  6948. (member (substring link 0 -1) all-prefixes)
  6949. (setq link (substring link 0 -1))))
  6950. (setq link (org-link-try-special-completion link))))
  6951. (set-window-configuration wcf)
  6952. (kill-buffer "*Org Links*"))
  6953. (setq entry (assoc link org-stored-links))
  6954. (or entry (push link org-insert-link-history))
  6955. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6956. (not org-keep-stored-link-after-insertion))
  6957. (setq org-stored-links (delq (assoc link org-stored-links)
  6958. org-stored-links)))
  6959. (setq desc (or desc (nth 1 entry)))))
  6960. (if (string-match org-plain-link-re link)
  6961. ;; URL-like link, normalize the use of angular brackets.
  6962. (setq link (org-make-link (org-remove-angle-brackets link))))
  6963. ;; Check if we are linking to the current file with a search option
  6964. ;; If yes, simplify the link by using only the search option.
  6965. (when (and buffer-file-name
  6966. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6967. (let* ((path (match-string 1 link))
  6968. (case-fold-search nil)
  6969. (search (match-string 2 link)))
  6970. (save-match-data
  6971. (if (equal (file-truename buffer-file-name) (file-truename path))
  6972. ;; We are linking to this same file, with a search option
  6973. (setq link search)))))
  6974. ;; Check if we can/should use a relative path. If yes, simplify the link
  6975. (when (string-match "^file:\\(.*\\)" link)
  6976. (let* ((path (match-string 1 link))
  6977. (origpath path)
  6978. (case-fold-search nil))
  6979. (cond
  6980. ((or (eq org-link-file-path-type 'absolute)
  6981. (equal complete-file '(16)))
  6982. (setq path (abbreviate-file-name (expand-file-name path))))
  6983. ((eq org-link-file-path-type 'noabbrev)
  6984. (setq path (expand-file-name path)))
  6985. ((eq org-link-file-path-type 'relative)
  6986. (setq path (file-relative-name path)))
  6987. (t
  6988. (save-match-data
  6989. (if (string-match (concat "^" (regexp-quote
  6990. (file-name-as-directory
  6991. (expand-file-name "."))))
  6992. (expand-file-name path))
  6993. ;; We are linking a file with relative path name.
  6994. (setq path (substring (expand-file-name path)
  6995. (match-end 0)))
  6996. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6997. (setq link (concat "file:" path))
  6998. (if (equal desc origpath)
  6999. (setq desc path))))
  7000. (if org-make-link-description-function
  7001. (setq desc (funcall org-make-link-description-function link desc)))
  7002. (setq desc (read-string "Description: " desc))
  7003. (unless (string-match "\\S-" desc) (setq desc nil))
  7004. (if remove (apply 'delete-region remove))
  7005. (insert (org-make-link-string link desc))))
  7006. (defun org-link-try-special-completion (type)
  7007. "If there is completion support for link type TYPE, offer it."
  7008. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7009. (if (functionp fun)
  7010. (funcall fun)
  7011. (read-string "Link (no completion support): " (concat type ":")))))
  7012. (defun org-file-complete-link (&optional arg)
  7013. "Create a file link using completion."
  7014. (let (file link)
  7015. (setq file (read-file-name "File: "))
  7016. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7017. (pwd1 (file-name-as-directory (abbreviate-file-name
  7018. (expand-file-name ".")))))
  7019. (cond
  7020. ((equal arg '(16))
  7021. (setq link (org-make-link
  7022. "file:"
  7023. (abbreviate-file-name (expand-file-name file)))))
  7024. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7025. (setq link (org-make-link "file:" (match-string 1 file))))
  7026. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7027. (expand-file-name file))
  7028. (setq link (org-make-link
  7029. "file:" (match-string 1 (expand-file-name file)))))
  7030. (t (setq link (org-make-link "file:" file)))))
  7031. link))
  7032. (defun org-completing-read (&rest args)
  7033. "Completing-read with SPACE being a normal character."
  7034. (let ((minibuffer-local-completion-map
  7035. (copy-keymap minibuffer-local-completion-map)))
  7036. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7037. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7038. (apply 'org-icompleting-read args)))
  7039. (defun org-completing-read-no-i (&rest args)
  7040. (let (org-completion-use-ido org-completion-use-iswitchb)
  7041. (apply 'org-completing-read args)))
  7042. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7043. "Use iswitch as a completing-read replacement to choose from choices.
  7044. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7045. from."
  7046. (let* ((iswitchb-use-virtual-buffers nil)
  7047. (iswitchb-make-buflist-hook
  7048. (lambda ()
  7049. (setq iswitchb-temp-buflist choices))))
  7050. (iswitchb-read-buffer prompt)))
  7051. (defun org-icompleting-read (&rest args)
  7052. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7053. (org-without-partial-completion
  7054. (if (and org-completion-use-ido
  7055. (fboundp 'ido-completing-read)
  7056. (boundp 'ido-mode) ido-mode
  7057. (listp (second args)))
  7058. (let ((ido-enter-matching-directory nil))
  7059. (apply 'ido-completing-read (concat (car args))
  7060. (if (consp (car (nth 1 args)))
  7061. (mapcar (lambda (x) (car x)) (nth 1 args))
  7062. (nth 1 args))
  7063. (cddr args)))
  7064. (if (and org-completion-use-iswitchb
  7065. (boundp 'iswitchb-mode) iswitchb-mode
  7066. (listp (second args)))
  7067. (apply 'org-iswitchb-completing-read (concat (car args))
  7068. (if (consp (car (nth 1 args)))
  7069. (mapcar (lambda (x) (car x)) (nth 1 args))
  7070. (nth 1 args))
  7071. (cddr args))
  7072. (apply 'completing-read args)))))
  7073. (defun org-extract-attributes (s)
  7074. "Extract the attributes cookie from a string and set as text property."
  7075. (let (a attr (start 0) key value)
  7076. (save-match-data
  7077. (when (string-match "{{\\([^}]+\\)}}$" s)
  7078. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7079. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7080. (setq key (match-string 1 a) value (match-string 2 a)
  7081. start (match-end 0)
  7082. attr (plist-put attr (intern key) value))))
  7083. (org-add-props s nil 'org-attr attr))
  7084. s))
  7085. (defun org-extract-attributes-from-string (tag)
  7086. (let (key value attr)
  7087. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7088. (setq key (match-string 1 tag) value (match-string 2 tag)
  7089. tag (replace-match "" t t tag)
  7090. attr (plist-put attr (intern key) value)))
  7091. (cons tag attr)))
  7092. (defun org-attributes-to-string (plist)
  7093. "Format a property list into an HTML attribute list."
  7094. (let ((s "") key value)
  7095. (while plist
  7096. (setq key (pop plist) value (pop plist))
  7097. (and value
  7098. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7099. s))
  7100. ;;; Opening/following a link
  7101. (defvar org-link-search-failed nil)
  7102. (defun org-next-link ()
  7103. "Move forward to the next link.
  7104. If the link is in hidden text, expose it."
  7105. (interactive)
  7106. (when (and org-link-search-failed (eq this-command last-command))
  7107. (goto-char (point-min))
  7108. (message "Link search wrapped back to beginning of buffer"))
  7109. (setq org-link-search-failed nil)
  7110. (let* ((pos (point))
  7111. (ct (org-context))
  7112. (a (assoc :link ct)))
  7113. (if a (goto-char (nth 2 a)))
  7114. (if (re-search-forward org-any-link-re nil t)
  7115. (progn
  7116. (goto-char (match-beginning 0))
  7117. (if (org-invisible-p) (org-show-context)))
  7118. (goto-char pos)
  7119. (setq org-link-search-failed t)
  7120. (error "No further link found"))))
  7121. (defun org-previous-link ()
  7122. "Move backward to the previous link.
  7123. If the link is in hidden text, expose it."
  7124. (interactive)
  7125. (when (and org-link-search-failed (eq this-command last-command))
  7126. (goto-char (point-max))
  7127. (message "Link search wrapped back to end of buffer"))
  7128. (setq org-link-search-failed nil)
  7129. (let* ((pos (point))
  7130. (ct (org-context))
  7131. (a (assoc :link ct)))
  7132. (if a (goto-char (nth 1 a)))
  7133. (if (re-search-backward org-any-link-re nil t)
  7134. (progn
  7135. (goto-char (match-beginning 0))
  7136. (if (org-invisible-p) (org-show-context)))
  7137. (goto-char pos)
  7138. (setq org-link-search-failed t)
  7139. (error "No further link found"))))
  7140. (defun org-translate-link (s)
  7141. "Translate a link string if a translation function has been defined."
  7142. (if (and org-link-translation-function
  7143. (fboundp org-link-translation-function)
  7144. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7145. (progn
  7146. (setq s (funcall org-link-translation-function
  7147. (match-string 1) (match-string 2)))
  7148. (concat (car s) ":" (cdr s)))
  7149. s))
  7150. (defun org-translate-link-from-planner (type path)
  7151. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7152. This is still an experimental function, your mileage may vary."
  7153. (cond
  7154. ((member type '("http" "https" "news" "ftp"))
  7155. ;; standard Internet links are the same.
  7156. nil)
  7157. ((and (equal type "irc") (string-match "^//" path))
  7158. ;; Planner has two / at the beginning of an irc link, we have 1.
  7159. ;; We should have zero, actually....
  7160. (setq path (substring path 1)))
  7161. ((and (equal type "lisp") (string-match "^/" path))
  7162. ;; Planner has a slash, we do not.
  7163. (setq type "elisp" path (substring path 1)))
  7164. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7165. ;; A typical message link. Planner has the id after the fina slash,
  7166. ;; we separate it with a hash mark
  7167. (setq path (concat (match-string 1 path) "#"
  7168. (org-remove-angle-brackets (match-string 2 path)))))
  7169. )
  7170. (cons type path))
  7171. (defun org-find-file-at-mouse (ev)
  7172. "Open file link or URL at mouse."
  7173. (interactive "e")
  7174. (mouse-set-point ev)
  7175. (org-open-at-point 'in-emacs))
  7176. (defun org-open-at-mouse (ev)
  7177. "Open file link or URL at mouse."
  7178. (interactive "e")
  7179. (mouse-set-point ev)
  7180. (if (eq major-mode 'org-agenda-mode)
  7181. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7182. (org-open-at-point))
  7183. (defvar org-window-config-before-follow-link nil
  7184. "The window configuration before following a link.
  7185. This is saved in case the need arises to restore it.")
  7186. (defvar org-open-link-marker (make-marker)
  7187. "Marker pointing to the location where `org-open-at-point; was called.")
  7188. ;;;###autoload
  7189. (defun org-open-at-point-global ()
  7190. "Follow a link like Org-mode does.
  7191. This command can be called in any mode to follow a link that has
  7192. Org-mode syntax."
  7193. (interactive)
  7194. (org-run-like-in-org-mode 'org-open-at-point))
  7195. ;;;###autoload
  7196. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7197. "Open a link in the string S, as if it was in Org-mode."
  7198. (interactive "sLink: \nP")
  7199. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7200. (with-temp-buffer
  7201. (let ((org-inhibit-startup t))
  7202. (org-mode)
  7203. (insert s)
  7204. (goto-char (point-min))
  7205. (org-open-at-point arg reference-buffer)))))
  7206. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7207. "Open link at or after point.
  7208. If there is no link at point, this function will search forward up to
  7209. the end of the current line.
  7210. Normally, files will be opened by an appropriate application. If the
  7211. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7212. With a double prefix argument, try to open outside of Emacs, in the
  7213. application the system uses for this file type."
  7214. (interactive "P")
  7215. (org-load-modules-maybe)
  7216. (move-marker org-open-link-marker (point))
  7217. (setq org-window-config-before-follow-link (current-window-configuration))
  7218. (org-remove-occur-highlights nil nil t)
  7219. (cond
  7220. ((and (org-on-heading-p)
  7221. (not (org-in-regexp
  7222. (concat org-plain-link-re "\\|"
  7223. org-bracket-link-regexp "\\|"
  7224. org-angle-link-re "\\|"
  7225. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7226. (or (org-offer-links-in-entry in-emacs)
  7227. (org-attach-reveal 'if-exists)))
  7228. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7229. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7230. (org-footnote-action))
  7231. (t
  7232. (let (type path link line search (pos (point)))
  7233. (catch 'match
  7234. (save-excursion
  7235. (skip-chars-forward "^]\n\r")
  7236. (when (org-in-regexp org-bracket-link-regexp)
  7237. (setq link (org-extract-attributes
  7238. (org-link-unescape (org-match-string-no-properties 1))))
  7239. (while (string-match " *\n *" link)
  7240. (setq link (replace-match " " t t link)))
  7241. (setq link (org-link-expand-abbrev link))
  7242. (cond
  7243. ((or (file-name-absolute-p link)
  7244. (string-match "^\\.\\.?/" link))
  7245. (setq type "file" path link))
  7246. ((string-match org-link-re-with-space3 link)
  7247. (setq type (match-string 1 link) path (match-string 2 link)))
  7248. (t (setq type "thisfile" path link)))
  7249. (throw 'match t)))
  7250. (when (get-text-property (point) 'org-linked-text)
  7251. (setq type "thisfile"
  7252. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7253. (1+ (point)) (point))
  7254. path (buffer-substring
  7255. (previous-single-property-change pos 'org-linked-text)
  7256. (next-single-property-change pos 'org-linked-text)))
  7257. (throw 'match t))
  7258. (save-excursion
  7259. (when (or (org-in-regexp org-angle-link-re)
  7260. (org-in-regexp org-plain-link-re))
  7261. (setq type (match-string 1) path (match-string 2))
  7262. (throw 'match t)))
  7263. (save-excursion
  7264. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7265. (setq type "tags"
  7266. path (match-string 1))
  7267. (while (string-match ":" path)
  7268. (setq path (replace-match "+" t t path)))
  7269. (throw 'match t)))
  7270. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7271. (setq type "tree-match"
  7272. path (match-string 1))
  7273. (throw 'match t)))
  7274. (unless path
  7275. (error "No link found"))
  7276. ;; switch back to reference buffer
  7277. ;; needed when if called in a temporary buffer through
  7278. ;; org-open-link-from-string
  7279. (with-current-buffer (or reference-buffer (current-buffer))
  7280. ;; Remove any trailing spaces in path
  7281. (if (string-match " +\\'" path)
  7282. (setq path (replace-match "" t t path)))
  7283. (if (and org-link-translation-function
  7284. (fboundp org-link-translation-function))
  7285. ;; Check if we need to translate the link
  7286. (let ((tmp (funcall org-link-translation-function type path)))
  7287. (setq type (car tmp) path (cdr tmp))))
  7288. (cond
  7289. ((assoc type org-link-protocols)
  7290. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7291. ((equal type "mailto")
  7292. (let ((cmd (car org-link-mailto-program))
  7293. (args (cdr org-link-mailto-program)) args1
  7294. (address path) (subject "") a)
  7295. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7296. (setq address (match-string 1 path)
  7297. subject (org-link-escape (match-string 2 path))))
  7298. (while args
  7299. (cond
  7300. ((not (stringp (car args))) (push (pop args) args1))
  7301. (t (setq a (pop args))
  7302. (if (string-match "%a" a)
  7303. (setq a (replace-match address t t a)))
  7304. (if (string-match "%s" a)
  7305. (setq a (replace-match subject t t a)))
  7306. (push a args1))))
  7307. (apply cmd (nreverse args1))))
  7308. ((member type '("http" "https" "ftp" "news"))
  7309. (browse-url (concat type ":" (org-link-escape
  7310. path org-link-escape-chars-browser))))
  7311. ((member type '("message"))
  7312. (browse-url (concat type ":" path)))
  7313. ((string= type "tags")
  7314. (org-tags-view in-emacs path))
  7315. ((string= type "thisfile")
  7316. (if in-emacs
  7317. (switch-to-buffer-other-window
  7318. (org-get-buffer-for-internal-link (current-buffer)))
  7319. (org-mark-ring-push))
  7320. (let ((cmd `(org-link-search
  7321. ,path
  7322. ,(cond ((equal in-emacs '(4)) 'occur)
  7323. ((equal in-emacs '(16)) 'org-occur)
  7324. (t nil))
  7325. ,pos)))
  7326. (condition-case nil (eval cmd)
  7327. (error (progn (widen) (eval cmd))))))
  7328. ((string= type "tree-match")
  7329. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7330. ((string= type "file")
  7331. (if (string-match "::\\([0-9]+\\)\\'" path)
  7332. (setq line (string-to-number (match-string 1 path))
  7333. path (substring path 0 (match-beginning 0)))
  7334. (if (string-match "::\\(.+\\)\\'" path)
  7335. (setq search (match-string 1 path)
  7336. path (substring path 0 (match-beginning 0)))))
  7337. (if (string-match "[*?{]" (file-name-nondirectory path))
  7338. (dired path)
  7339. (org-open-file path in-emacs line search)))
  7340. ((string= type "news")
  7341. (require 'org-gnus)
  7342. (org-gnus-follow-link path))
  7343. ((string= type "shell")
  7344. (let ((cmd path))
  7345. (if (or (not org-confirm-shell-link-function)
  7346. (funcall org-confirm-shell-link-function
  7347. (format "Execute \"%s\" in shell? "
  7348. (org-add-props cmd nil
  7349. 'face 'org-warning))))
  7350. (progn
  7351. (message "Executing %s" cmd)
  7352. (shell-command cmd))
  7353. (error "Abort"))))
  7354. ((string= type "elisp")
  7355. (let ((cmd path))
  7356. (if (or (not org-confirm-elisp-link-function)
  7357. (funcall org-confirm-elisp-link-function
  7358. (format "Execute \"%s\" as elisp? "
  7359. (org-add-props cmd nil
  7360. 'face 'org-warning))))
  7361. (message "%s => %s" cmd
  7362. (if (equal (string-to-char cmd) ?\()
  7363. (eval (read cmd))
  7364. (call-interactively (read cmd))))
  7365. (error "Abort"))))
  7366. (t
  7367. (browse-url-at-point)))))))
  7368. (move-marker org-open-link-marker nil)
  7369. (run-hook-with-args 'org-follow-link-hook))
  7370. (defun org-offer-links-in-entry (&optional nth zero)
  7371. "Offer links in the curren entry and follow the selected link.
  7372. If there is only one link, follow it immediately as well.
  7373. If NTH is an integer, immediately pick the NTH link found.
  7374. If ZERO is a string, check also this string for a link, and if
  7375. there is one, offer it as link number zero."
  7376. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7377. "\\(" org-angle-link-re "\\)\\|"
  7378. "\\(" org-plain-link-re "\\)"))
  7379. (cnt ?0)
  7380. (in-emacs (if (integerp nth) nil nth))
  7381. have-zero end links link c)
  7382. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7383. (push (match-string 0 zero) links)
  7384. (setq cnt (1- cnt) have-zero t))
  7385. (save-excursion
  7386. (org-back-to-heading t)
  7387. (setq end (save-excursion (outline-next-heading) (point)))
  7388. (while (re-search-forward re end t)
  7389. (push (match-string 0) links))
  7390. (setq links (org-uniquify (reverse links))))
  7391. (cond
  7392. ((null links)
  7393. (message "No links"))
  7394. ((equal (length links) 1)
  7395. (setq link (car links)))
  7396. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7397. (setq link (nth (if have-zero nth (1- nth)) links)))
  7398. (t ; we have to select a link
  7399. (save-excursion
  7400. (save-window-excursion
  7401. (delete-other-windows)
  7402. (with-output-to-temp-buffer "*Select Link*"
  7403. (mapc (lambda (l)
  7404. (if (not (string-match org-bracket-link-regexp l))
  7405. (princ (format "[%c] %s\n" (incf cnt)
  7406. (org-remove-angle-brackets l)))
  7407. (if (match-end 3)
  7408. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7409. (match-string 3 l) (match-string 1 l)))
  7410. (princ (format "[%c] %s\n" (incf cnt)
  7411. (match-string 1 l))))))
  7412. links))
  7413. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7414. (message "Select link to open:")
  7415. (setq c (read-char-exclusive))
  7416. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7417. (when (equal c ?q) (error "Abort"))
  7418. (setq nth (- c ?0))
  7419. (if have-zero (setq nth (1+ nth)))
  7420. (unless (and (integerp nth) (>= (length links) nth))
  7421. (error "Invalid link selection"))
  7422. (setq link (nth (1- nth) links))))
  7423. (if link
  7424. (progn (org-open-link-from-string link in-emacs (current-buffer)) t)
  7425. nil)))
  7426. ;;;; Time estimates
  7427. (defun org-get-effort (&optional pom)
  7428. "Get the effort estimate for the current entry."
  7429. (org-entry-get pom org-effort-property))
  7430. ;;; File search
  7431. (defvar org-create-file-search-functions nil
  7432. "List of functions to construct the right search string for a file link.
  7433. These functions are called in turn with point at the location to
  7434. which the link should point.
  7435. A function in the hook should first test if it would like to
  7436. handle this file type, for example by checking the major-mode or
  7437. the file extension. If it decides not to handle this file, it
  7438. should just return nil to give other functions a chance. If it
  7439. does handle the file, it must return the search string to be used
  7440. when following the link. The search string will be part of the
  7441. file link, given after a double colon, and `org-open-at-point'
  7442. will automatically search for it. If special measures must be
  7443. taken to make the search successful, another function should be
  7444. added to the companion hook `org-execute-file-search-functions',
  7445. which see.
  7446. A function in this hook may also use `setq' to set the variable
  7447. `description' to provide a suggestion for the descriptive text to
  7448. be used for this link when it gets inserted into an Org-mode
  7449. buffer with \\[org-insert-link].")
  7450. (defvar org-execute-file-search-functions nil
  7451. "List of functions to execute a file search triggered by a link.
  7452. Functions added to this hook must accept a single argument, the
  7453. search string that was part of the file link, the part after the
  7454. double colon. The function must first check if it would like to
  7455. handle this search, for example by checking the major-mode or the
  7456. file extension. If it decides not to handle this search, it
  7457. should just return nil to give other functions a chance. If it
  7458. does handle the search, it must return a non-nil value to keep
  7459. other functions from trying.
  7460. Each function can access the current prefix argument through the
  7461. variable `current-prefix-argument'. Note that a single prefix is
  7462. used to force opening a link in Emacs, so it may be good to only
  7463. use a numeric or double prefix to guide the search function.
  7464. In case this is needed, a function in this hook can also restore
  7465. the window configuration before `org-open-at-point' was called using:
  7466. (set-window-configuration org-window-config-before-follow-link)")
  7467. (defun org-link-search (s &optional type avoid-pos)
  7468. "Search for a link search option.
  7469. If S is surrounded by forward slashes, it is interpreted as a
  7470. regular expression. In org-mode files, this will create an `org-occur'
  7471. sparse tree. In ordinary files, `occur' will be used to list matches.
  7472. If the current buffer is in `dired-mode', grep will be used to search
  7473. in all files. If AVOID-POS is given, ignore matches near that position."
  7474. (let ((case-fold-search t)
  7475. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7476. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7477. (append '(("") (" ") ("\t") ("\n"))
  7478. org-emphasis-alist)
  7479. "\\|") "\\)"))
  7480. (pos (point))
  7481. (pre nil) (post nil)
  7482. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7483. (cond
  7484. ;; First check if there are any special
  7485. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7486. ;; Now try the builtin stuff
  7487. ((and (equal (string-to-char s0) ?#)
  7488. (> (length s0) 1)
  7489. (save-excursion
  7490. (goto-char (point-min))
  7491. (and
  7492. (re-search-forward
  7493. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7494. (setq type 'dedicated
  7495. pos (match-beginning 0))))
  7496. ;; There is an exact target for this
  7497. (goto-char pos)
  7498. (org-back-to-heading t)))
  7499. ((save-excursion
  7500. (goto-char (point-min))
  7501. (and
  7502. (re-search-forward
  7503. (concat "<<" (regexp-quote s0) ">>") nil t)
  7504. (setq type 'dedicated
  7505. pos (match-beginning 0))))
  7506. ;; There is an exact target for this
  7507. (goto-char pos))
  7508. ((and (string-match "^(\\(.*\\))$" s0)
  7509. (save-excursion
  7510. (goto-char (point-min))
  7511. (and
  7512. (re-search-forward
  7513. (concat "[^[]" (regexp-quote
  7514. (format org-coderef-label-format
  7515. (match-string 1 s0))))
  7516. nil t)
  7517. (setq type 'dedicated
  7518. pos (1+ (match-beginning 0))))))
  7519. ;; There is a coderef target for this
  7520. (goto-char pos))
  7521. ((string-match "^/\\(.*\\)/$" s)
  7522. ;; A regular expression
  7523. (cond
  7524. ((org-mode-p)
  7525. (org-occur (match-string 1 s)))
  7526. ;;((eq major-mode 'dired-mode)
  7527. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7528. (t (org-do-occur (match-string 1 s)))))
  7529. (t
  7530. ;; A normal search strings
  7531. (when (equal (string-to-char s) ?*)
  7532. ;; Anchor on headlines, post may include tags.
  7533. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7534. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7535. s (substring s 1)))
  7536. (remove-text-properties
  7537. 0 (length s)
  7538. '(face nil mouse-face nil keymap nil fontified nil) s)
  7539. ;; Make a series of regular expressions to find a match
  7540. (setq words (org-split-string s "[ \n\r\t]+")
  7541. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7542. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7543. "\\)" markers)
  7544. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7545. re2a (concat "[ \t\r\n]" re2a_)
  7546. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7547. re4 (concat "[^a-zA-Z_]" re4_)
  7548. re1 (concat pre re2 post)
  7549. re3 (concat pre (if pre re4_ re4) post)
  7550. re5 (concat pre ".*" re4)
  7551. re2 (concat pre re2)
  7552. re2a (concat pre (if pre re2a_ re2a))
  7553. re4 (concat pre (if pre re4_ re4))
  7554. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7555. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7556. re5 "\\)"
  7557. ))
  7558. (cond
  7559. ((eq type 'org-occur) (org-occur reall))
  7560. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7561. (t (goto-char (point-min))
  7562. (setq type 'fuzzy)
  7563. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7564. (org-search-not-self 1 re1 nil t)
  7565. (org-search-not-self 1 re2 nil t)
  7566. (org-search-not-self 1 re2a nil t)
  7567. (org-search-not-self 1 re3 nil t)
  7568. (org-search-not-self 1 re4 nil t)
  7569. (org-search-not-self 1 re5 nil t)
  7570. )
  7571. (goto-char (match-beginning 1))
  7572. (goto-char pos)
  7573. (error "No match")))))
  7574. (t
  7575. ;; Normal string-search
  7576. (goto-char (point-min))
  7577. (if (search-forward s nil t)
  7578. (goto-char (match-beginning 0))
  7579. (error "No match"))))
  7580. (and (org-mode-p) (org-show-context 'link-search))
  7581. type))
  7582. (defun org-search-not-self (group &rest args)
  7583. "Execute `re-search-forward', but only accept matches that do not
  7584. enclose the position of `org-open-link-marker'."
  7585. (let ((m org-open-link-marker))
  7586. (catch 'exit
  7587. (while (apply 're-search-forward args)
  7588. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7589. (goto-char (match-end group))
  7590. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7591. (> (match-beginning 0) (marker-position m))
  7592. (< (match-end 0) (marker-position m)))
  7593. (save-match-data
  7594. (or (not (org-in-regexp
  7595. org-bracket-link-analytic-regexp 1))
  7596. (not (match-end 4)) ; no description
  7597. (and (<= (match-beginning 4) (point))
  7598. (>= (match-end 4) (point))))))
  7599. (throw 'exit (point))))))))
  7600. (defun org-get-buffer-for-internal-link (buffer)
  7601. "Return a buffer to be used for displaying the link target of internal links."
  7602. (cond
  7603. ((not org-display-internal-link-with-indirect-buffer)
  7604. buffer)
  7605. ((string-match "(Clone)$" (buffer-name buffer))
  7606. (message "Buffer is already a clone, not making another one")
  7607. ;; we also do not modify visibility in this case
  7608. buffer)
  7609. (t ; make a new indirect buffer for displaying the link
  7610. (let* ((bn (buffer-name buffer))
  7611. (ibn (concat bn "(Clone)"))
  7612. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7613. (with-current-buffer ib (org-overview))
  7614. ib))))
  7615. (defun org-do-occur (regexp &optional cleanup)
  7616. "Call the Emacs command `occur'.
  7617. If CLEANUP is non-nil, remove the printout of the regular expression
  7618. in the *Occur* buffer. This is useful if the regex is long and not useful
  7619. to read."
  7620. (occur regexp)
  7621. (when cleanup
  7622. (let ((cwin (selected-window)) win beg end)
  7623. (when (setq win (get-buffer-window "*Occur*"))
  7624. (select-window win))
  7625. (goto-char (point-min))
  7626. (when (re-search-forward "match[a-z]+" nil t)
  7627. (setq beg (match-end 0))
  7628. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7629. (setq end (1- (match-beginning 0)))))
  7630. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7631. (goto-char (point-min))
  7632. (select-window cwin))))
  7633. ;;; The mark ring for links jumps
  7634. (defvar org-mark-ring nil
  7635. "Mark ring for positions before jumps in Org-mode.")
  7636. (defvar org-mark-ring-last-goto nil
  7637. "Last position in the mark ring used to go back.")
  7638. ;; Fill and close the ring
  7639. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7640. (loop for i from 1 to org-mark-ring-length do
  7641. (push (make-marker) org-mark-ring))
  7642. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7643. org-mark-ring)
  7644. (defun org-mark-ring-push (&optional pos buffer)
  7645. "Put the current position or POS into the mark ring and rotate it."
  7646. (interactive)
  7647. (setq pos (or pos (point)))
  7648. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7649. (move-marker (car org-mark-ring)
  7650. (or pos (point))
  7651. (or buffer (current-buffer)))
  7652. (message "%s"
  7653. (substitute-command-keys
  7654. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7655. (defun org-mark-ring-goto (&optional n)
  7656. "Jump to the previous position in the mark ring.
  7657. With prefix arg N, jump back that many stored positions. When
  7658. called several times in succession, walk through the entire ring.
  7659. Org-mode commands jumping to a different position in the current file,
  7660. or to another Org-mode file, automatically push the old position
  7661. onto the ring."
  7662. (interactive "p")
  7663. (let (p m)
  7664. (if (eq last-command this-command)
  7665. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7666. (setq p org-mark-ring))
  7667. (setq org-mark-ring-last-goto p)
  7668. (setq m (car p))
  7669. (switch-to-buffer (marker-buffer m))
  7670. (goto-char m)
  7671. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7672. (defun org-remove-angle-brackets (s)
  7673. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7674. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7675. s)
  7676. (defun org-add-angle-brackets (s)
  7677. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7678. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7679. s)
  7680. (defun org-remove-double-quotes (s)
  7681. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7682. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7683. s)
  7684. ;;; Following specific links
  7685. (defun org-follow-timestamp-link ()
  7686. (cond
  7687. ((org-at-date-range-p t)
  7688. (let ((org-agenda-start-on-weekday)
  7689. (t1 (match-string 1))
  7690. (t2 (match-string 2)))
  7691. (setq t1 (time-to-days (org-time-string-to-time t1))
  7692. t2 (time-to-days (org-time-string-to-time t2)))
  7693. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7694. ((org-at-timestamp-p t)
  7695. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7696. (substring (match-string 1) 0 10)))
  7697. 1))
  7698. (t (error "This should not happen"))))
  7699. ;;; Following file links
  7700. (defvar org-wait nil)
  7701. (defun org-open-file (path &optional in-emacs line search)
  7702. "Open the file at PATH.
  7703. First, this expands any special file name abbreviations. Then the
  7704. configuration variable `org-file-apps' is checked if it contains an
  7705. entry for this file type, and if yes, the corresponding command is launched.
  7706. If no application is found, Emacs simply visits the file.
  7707. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7708. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7709. and o use an external application to visit the file.
  7710. Optional LINE specifies a line to go to, optional SEARCH a string to
  7711. search for. If LINE or SEARCH is given, the file will always be
  7712. opened in Emacs.
  7713. If the file does not exist, an error is thrown."
  7714. (setq in-emacs (or in-emacs line search))
  7715. (let* ((file (if (equal path "")
  7716. buffer-file-name
  7717. (substitute-in-file-name (expand-file-name path))))
  7718. (apps (append org-file-apps (org-default-apps)))
  7719. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7720. (dirp (if remp nil (file-directory-p file)))
  7721. (file (if (and dirp org-open-directory-means-index-dot-org)
  7722. (concat (file-name-as-directory file) "index.org")
  7723. file))
  7724. (a-m-a-p (assq 'auto-mode apps))
  7725. (dfile (downcase file))
  7726. (old-buffer (current-buffer))
  7727. (old-pos (point))
  7728. (old-mode major-mode)
  7729. ext cmd)
  7730. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7731. (setq ext (match-string 1 dfile))
  7732. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7733. (setq ext (match-string 1 dfile))))
  7734. (cond
  7735. ((equal in-emacs '(16))
  7736. (setq cmd (cdr (assoc 'system apps))))
  7737. (in-emacs (setq cmd 'emacs))
  7738. (t
  7739. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7740. (and dirp (cdr (assoc 'directory apps)))
  7741. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7742. 'string-match)
  7743. (cdr (assoc ext apps))
  7744. (cdr (assoc t apps))))))
  7745. (when (eq cmd 'system)
  7746. (setq cmd (cdr (assoc 'system apps))))
  7747. (when (eq cmd 'default)
  7748. (setq cmd (cdr (assoc t apps))))
  7749. (when (eq cmd 'mailcap)
  7750. (require 'mailcap)
  7751. (mailcap-parse-mailcaps)
  7752. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7753. (command (mailcap-mime-info mime-type)))
  7754. (if (stringp command)
  7755. (setq cmd command)
  7756. (setq cmd 'emacs))))
  7757. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7758. (not (file-exists-p file))
  7759. (not org-open-non-existing-files))
  7760. (error "No such file: %s" file))
  7761. (cond
  7762. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7763. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7764. (while (string-match "['\"]%s['\"]" cmd)
  7765. (setq cmd (replace-match "%s" t t cmd)))
  7766. (while (string-match "%s" cmd)
  7767. (setq cmd (replace-match
  7768. (save-match-data
  7769. (shell-quote-argument
  7770. (convert-standard-filename file)))
  7771. t t cmd)))
  7772. (save-window-excursion
  7773. (start-process-shell-command cmd nil cmd)
  7774. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7775. ))
  7776. ((or (stringp cmd)
  7777. (eq cmd 'emacs))
  7778. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7779. (widen)
  7780. (if line (org-goto-line line)
  7781. (if search (org-link-search search))))
  7782. ((consp cmd)
  7783. (let ((file (convert-standard-filename file)))
  7784. (eval cmd)))
  7785. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7786. (and (org-mode-p) (eq old-mode 'org-mode)
  7787. (or (not (equal old-buffer (current-buffer)))
  7788. (not (equal old-pos (point))))
  7789. (org-mark-ring-push old-pos old-buffer))))
  7790. (defun org-default-apps ()
  7791. "Return the default applications for this operating system."
  7792. (cond
  7793. ((eq system-type 'darwin)
  7794. org-file-apps-defaults-macosx)
  7795. ((eq system-type 'windows-nt)
  7796. org-file-apps-defaults-windowsnt)
  7797. (t org-file-apps-defaults-gnu)))
  7798. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7799. "Convert extensions to regular expressions in the cars of LIST.
  7800. Also, weed out any non-string entries, because the return value is used
  7801. only for regexp matching.
  7802. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7803. point to the symbol `emacs', indicating that the file should
  7804. be opened in Emacs."
  7805. (append
  7806. (delq nil
  7807. (mapcar (lambda (x)
  7808. (if (not (stringp (car x)))
  7809. nil
  7810. (if (string-match "\\W" (car x))
  7811. x
  7812. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7813. list))
  7814. (if add-auto-mode
  7815. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7816. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7817. (defun org-file-remote-p (file)
  7818. "Test whether FILE specifies a location on a remote system.
  7819. Return non-nil if the location is indeed remote.
  7820. For example, the filename \"/user@host:/foo\" specifies a location
  7821. on the system \"/user@host:\"."
  7822. (cond ((fboundp 'file-remote-p)
  7823. (file-remote-p file))
  7824. ((fboundp 'tramp-handle-file-remote-p)
  7825. (tramp-handle-file-remote-p file))
  7826. ((and (boundp 'ange-ftp-name-format)
  7827. (string-match (car ange-ftp-name-format) file))
  7828. t)
  7829. (t nil)))
  7830. ;;;; Refiling
  7831. (defun org-get-org-file ()
  7832. "Read a filename, with default directory `org-directory'."
  7833. (let ((default (or org-default-notes-file remember-data-file)))
  7834. (read-file-name (format "File name [%s]: " default)
  7835. (file-name-as-directory org-directory)
  7836. default)))
  7837. (defun org-notes-order-reversed-p ()
  7838. "Check if the current file should receive notes in reversed order."
  7839. (cond
  7840. ((not org-reverse-note-order) nil)
  7841. ((eq t org-reverse-note-order) t)
  7842. ((not (listp org-reverse-note-order)) nil)
  7843. (t (catch 'exit
  7844. (let ((all org-reverse-note-order)
  7845. entry)
  7846. (while (setq entry (pop all))
  7847. (if (string-match (car entry) buffer-file-name)
  7848. (throw 'exit (cdr entry))))
  7849. nil)))))
  7850. (defvar org-refile-target-table nil
  7851. "The list of refile targets, created by `org-refile'.")
  7852. (defvar org-agenda-new-buffers nil
  7853. "Buffers created to visit agenda files.")
  7854. (defun org-get-refile-targets (&optional default-buffer)
  7855. "Produce a table with refile targets."
  7856. (let ((case-fold-search nil)
  7857. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7858. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7859. targets txt re files f desc descre fast-path-p level pos0)
  7860. (message "Getting targets...")
  7861. (with-current-buffer (or default-buffer (current-buffer))
  7862. (while (setq entry (pop entries))
  7863. (setq files (car entry) desc (cdr entry))
  7864. (setq fast-path-p nil)
  7865. (cond
  7866. ((null files) (setq files (list (current-buffer))))
  7867. ((eq files 'org-agenda-files)
  7868. (setq files (org-agenda-files 'unrestricted)))
  7869. ((and (symbolp files) (fboundp files))
  7870. (setq files (funcall files)))
  7871. ((and (symbolp files) (boundp files))
  7872. (setq files (symbol-value files))))
  7873. (if (stringp files) (setq files (list files)))
  7874. (cond
  7875. ((eq (car desc) :tag)
  7876. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7877. ((eq (car desc) :todo)
  7878. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7879. ((eq (car desc) :regexp)
  7880. (setq descre (cdr desc)))
  7881. ((eq (car desc) :level)
  7882. (setq descre (concat "^\\*\\{" (number-to-string
  7883. (if org-odd-levels-only
  7884. (1- (* 2 (cdr desc)))
  7885. (cdr desc)))
  7886. "\\}[ \t]")))
  7887. ((eq (car desc) :maxlevel)
  7888. (setq fast-path-p t)
  7889. (setq descre (concat "^\\*\\{1," (number-to-string
  7890. (if org-odd-levels-only
  7891. (1- (* 2 (cdr desc)))
  7892. (cdr desc)))
  7893. "\\}[ \t]")))
  7894. (t (error "Bad refiling target description %s" desc)))
  7895. (while (setq f (pop files))
  7896. (save-excursion
  7897. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7898. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7899. (setq f (expand-file-name f))
  7900. (if (eq org-refile-use-outline-path 'file)
  7901. (push (list (file-name-nondirectory f) f nil nil) targets))
  7902. (save-excursion
  7903. (save-restriction
  7904. (widen)
  7905. (goto-char (point-min))
  7906. (while (re-search-forward descre nil t)
  7907. (goto-char (setq pos0 (point-at-bol)))
  7908. (catch 'next
  7909. (when org-refile-target-verify-function
  7910. (save-match-data
  7911. (or (funcall org-refile-target-verify-function)
  7912. (throw 'next t))))
  7913. (when (looking-at org-complex-heading-regexp)
  7914. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7915. txt (org-link-display-format (match-string 4))
  7916. re (concat "^" (regexp-quote
  7917. (buffer-substring (match-beginning 1)
  7918. (match-end 4)))))
  7919. (if (match-end 5) (setq re (concat re "[ \t]+"
  7920. (regexp-quote
  7921. (match-string 5)))))
  7922. (setq re (concat re "[ \t]*$"))
  7923. (when org-refile-use-outline-path
  7924. (setq txt (mapconcat 'org-protect-slash
  7925. (append
  7926. (if (eq org-refile-use-outline-path 'file)
  7927. (list (file-name-nondirectory
  7928. (buffer-file-name (buffer-base-buffer))))
  7929. (if (eq org-refile-use-outline-path 'full-file-path)
  7930. (list (buffer-file-name (buffer-base-buffer)))))
  7931. (org-get-outline-path fast-path-p level txt)
  7932. (list txt))
  7933. "/")))
  7934. (push (list txt f re (point)) targets)))
  7935. (when (= (point) pos0)
  7936. ;; verification function has not moved point
  7937. (goto-char (point-at-eol))))))))))
  7938. (message "Getting targets...done")
  7939. (nreverse targets)))
  7940. (defun org-protect-slash (s)
  7941. (while (string-match "/" s)
  7942. (setq s (replace-match "\\" t t s)))
  7943. s)
  7944. (defvar org-olpa (make-vector 20 nil))
  7945. (defun org-get-outline-path (&optional fastp level heading)
  7946. "Return the outline path to the current entry, as a list."
  7947. (if fastp
  7948. (progn
  7949. (if (> level 19)
  7950. (error "Outline path failure, more than 19 levels."))
  7951. (loop for i from level upto 19 do
  7952. (aset org-olpa i nil))
  7953. (prog1
  7954. (delq nil (append org-olpa nil))
  7955. (aset org-olpa level heading)))
  7956. (let (rtn)
  7957. (save-excursion
  7958. (while (org-up-heading-safe)
  7959. (when (looking-at org-complex-heading-regexp)
  7960. (push (org-match-string-no-properties 4) rtn)))
  7961. rtn))))
  7962. (defvar org-refile-history nil
  7963. "History for refiling operations.")
  7964. (defvar org-after-refile-insert-hook nil
  7965. "Hook run after `org-refile' has inserted its stuff at the new location.
  7966. Note that this is still *before* the stuff will be removed from
  7967. the *old* location.")
  7968. (defun org-refile (&optional goto default-buffer rfloc)
  7969. "Move the entry at point to another heading.
  7970. The list of target headings is compiled using the information in
  7971. `org-refile-targets', which see. This list is created before each use
  7972. and will therefore always be up-to-date.
  7973. At the target location, the entry is filed as a subitem of the target heading.
  7974. Depending on `org-reverse-note-order', the new subitem will either be the
  7975. first or the last subitem.
  7976. If there is an active region, all entries in that region will be moved.
  7977. However, the region must fulfil the requirement that the first heading
  7978. is the first one sets the top-level of the moved text - at most siblings
  7979. below it are allowed.
  7980. With prefix arg GOTO, the command will only visit the target location,
  7981. not actually move anything.
  7982. With a double prefix `C-u C-u', go to the location where the last refiling
  7983. operation has put the subtree.
  7984. RFLOC can be a refile location obtained in a different way.
  7985. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7986. (interactive "P")
  7987. (let* ((cbuf (current-buffer))
  7988. (regionp (org-region-active-p))
  7989. (region-start (and regionp (region-beginning)))
  7990. (region-end (and regionp (region-end)))
  7991. (region-length (and regionp (- region-end region-start)))
  7992. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7993. pos it nbuf file re level reversed)
  7994. (when regionp
  7995. (goto-char region-start)
  7996. (or (bolp) (goto-char (point-at-bol)))
  7997. (setq region-start (point))
  7998. (unless (org-kill-is-subtree-p
  7999. (buffer-substring region-start region-end))
  8000. (error "The region is not a (sequence of) subtree(s)")))
  8001. (if (equal goto '(16))
  8002. (org-refile-goto-last-stored)
  8003. (when (setq it (or rfloc
  8004. (save-excursion
  8005. (org-refile-get-location
  8006. (if goto "Goto: " "Refile to: ") default-buffer
  8007. org-refile-allow-creating-parent-nodes))))
  8008. (setq file (nth 1 it)
  8009. re (nth 2 it)
  8010. pos (nth 3 it))
  8011. (if (and (not goto)
  8012. pos
  8013. (equal (buffer-file-name) file)
  8014. (if regionp
  8015. (and (>= pos region-start)
  8016. (<= pos region-end))
  8017. (and (>= pos (point))
  8018. (< pos (save-excursion
  8019. (org-end-of-subtree t t))))))
  8020. (error "Cannot refile to position inside the tree or region"))
  8021. (setq nbuf (or (find-buffer-visiting file)
  8022. (find-file-noselect file)))
  8023. (if goto
  8024. (progn
  8025. (switch-to-buffer nbuf)
  8026. (goto-char pos)
  8027. (org-show-context 'org-goto))
  8028. (if regionp
  8029. (progn
  8030. (org-kill-new (buffer-substring region-start region-end))
  8031. (org-save-markers-in-region region-start region-end))
  8032. (org-copy-subtree 1 nil t))
  8033. (save-excursion
  8034. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  8035. (find-file-noselect file))))
  8036. (setq reversed (org-notes-order-reversed-p))
  8037. (save-excursion
  8038. (save-restriction
  8039. (widen)
  8040. (if pos
  8041. (progn
  8042. (goto-char pos)
  8043. (looking-at outline-regexp)
  8044. (setq level (org-get-valid-level (funcall outline-level) 1))
  8045. (goto-char
  8046. (if reversed
  8047. (or (outline-next-heading) (point-max))
  8048. (or (save-excursion (org-get-next-sibling))
  8049. (org-end-of-subtree t t)
  8050. (point-max)))))
  8051. (setq level 1)
  8052. (if (not reversed)
  8053. (goto-char (point-max))
  8054. (goto-char (point-min))
  8055. (or (outline-next-heading) (goto-char (point-max)))))
  8056. (if (not (bolp)) (newline))
  8057. (bookmark-set "org-refile-last-stored")
  8058. (org-paste-subtree level)
  8059. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8060. (run-hooks 'org-after-refile-insert-hook))))
  8061. (if regionp
  8062. (delete-region (point) (+ (point) region-length))
  8063. (org-cut-subtree))
  8064. (when (featurep 'org-inlinetask)
  8065. (org-inlinetask-remove-END-maybe))
  8066. (setq org-markers-to-move nil)
  8067. (message "Refiled to \"%s\"" (car it))))))
  8068. (org-reveal))
  8069. (defun org-refile-goto-last-stored ()
  8070. "Go to the location where the last refile was stored."
  8071. (interactive)
  8072. (bookmark-jump "org-refile-last-stored")
  8073. (message "This is the location of the last refile"))
  8074. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8075. "Prompt the user for a refile location, using PROMPT."
  8076. (let ((org-refile-targets org-refile-targets)
  8077. (org-refile-use-outline-path org-refile-use-outline-path))
  8078. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8079. (unless org-refile-target-table
  8080. (error "No refile targets"))
  8081. (let* ((cbuf (current-buffer))
  8082. (partial-completion-mode nil)
  8083. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8084. (cfunc (if (and org-refile-use-outline-path
  8085. org-outline-path-complete-in-steps)
  8086. 'org-olpath-completing-read
  8087. 'org-icompleting-read))
  8088. (extra (if org-refile-use-outline-path "/" ""))
  8089. (filename (and cfn (expand-file-name cfn)))
  8090. (tbl (mapcar
  8091. (lambda (x)
  8092. (if (and (not (member org-refile-use-outline-path
  8093. '(file full-file-path)))
  8094. (not (equal filename (nth 1 x))))
  8095. (cons (concat (car x) extra " ("
  8096. (file-name-nondirectory (nth 1 x)) ")")
  8097. (cdr x))
  8098. (cons (concat (car x) extra) (cdr x))))
  8099. org-refile-target-table))
  8100. (completion-ignore-case t)
  8101. pa answ parent-target child parent old-hist)
  8102. (setq old-hist org-refile-history)
  8103. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8104. nil 'org-refile-history))
  8105. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8106. (if pa
  8107. (progn
  8108. (when (or (not org-refile-history)
  8109. (not (eq old-hist org-refile-history))
  8110. (not (equal (car pa) (car org-refile-history))))
  8111. (setq org-refile-history
  8112. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8113. org-refile-history
  8114. (cdr org-refile-history))))
  8115. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8116. (pop org-refile-history)))
  8117. pa)
  8118. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8119. (setq parent (match-string 1 answ)
  8120. child (match-string 2 answ))
  8121. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8122. (when (and parent-target
  8123. (or (eq new-nodes t)
  8124. (and (eq new-nodes 'confirm)
  8125. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8126. (org-refile-new-child parent-target child))))))
  8127. (defun org-refile-new-child (parent-target child)
  8128. "Use refile target PARENT-TARGET to add new CHILD below it."
  8129. (unless parent-target
  8130. (error "Cannot find parent for new node"))
  8131. (let ((file (nth 1 parent-target))
  8132. (pos (nth 3 parent-target))
  8133. level)
  8134. (with-current-buffer (or (find-buffer-visiting file)
  8135. (find-file-noselect file))
  8136. (save-excursion
  8137. (save-restriction
  8138. (widen)
  8139. (if pos
  8140. (goto-char pos)
  8141. (goto-char (point-max))
  8142. (if (not (bolp)) (newline)))
  8143. (when (looking-at outline-regexp)
  8144. (setq level (funcall outline-level))
  8145. (org-end-of-subtree t t))
  8146. (org-back-over-empty-lines)
  8147. (insert "\n" (make-string
  8148. (if pos (org-get-valid-level level 1) 1) ?*)
  8149. " " child "\n")
  8150. (beginning-of-line 0)
  8151. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8152. (defun org-olpath-completing-read (prompt collection &rest args)
  8153. "Read an outline path like a file name."
  8154. (let ((thetable collection)
  8155. (org-completion-use-ido nil) ; does not work with ido.
  8156. (org-completion-use-iswitchb nil)) ; or iswitchb
  8157. (apply
  8158. 'org-icompleting-read prompt
  8159. (lambda (string predicate &optional flag)
  8160. (let (rtn r f (l (length string)))
  8161. (cond
  8162. ((eq flag nil)
  8163. ;; try completion
  8164. (try-completion string thetable))
  8165. ((eq flag t)
  8166. ;; all-completions
  8167. (setq rtn (all-completions string thetable predicate))
  8168. (mapcar
  8169. (lambda (x)
  8170. (setq r (substring x l))
  8171. (if (string-match " ([^)]*)$" x)
  8172. (setq f (match-string 0 x))
  8173. (setq f ""))
  8174. (if (string-match "/" r)
  8175. (concat string (substring r 0 (match-end 0)) f)
  8176. x))
  8177. rtn))
  8178. ((eq flag 'lambda)
  8179. ;; exact match?
  8180. (assoc string thetable)))
  8181. ))
  8182. args)))
  8183. ;;;; Dynamic blocks
  8184. (defun org-find-dblock (name)
  8185. "Find the first dynamic block with name NAME in the buffer.
  8186. If not found, stay at current position and return nil."
  8187. (let (pos)
  8188. (save-excursion
  8189. (goto-char (point-min))
  8190. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8191. nil t)
  8192. (match-beginning 0))))
  8193. (if pos (goto-char pos))
  8194. pos))
  8195. (defconst org-dblock-start-re
  8196. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8197. "Matches the startline of a dynamic block, with parameters.")
  8198. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8199. "Matches the end of a dynamic block.")
  8200. (defun org-create-dblock (plist)
  8201. "Create a dynamic block section, with parameters taken from PLIST.
  8202. PLIST must contain a :name entry which is used as name of the block."
  8203. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8204. (end-of-line 1)
  8205. (newline))
  8206. (let ((col (current-column))
  8207. (name (plist-get plist :name)))
  8208. (insert "#+BEGIN: " name)
  8209. (while plist
  8210. (if (eq (car plist) :name)
  8211. (setq plist (cddr plist))
  8212. (insert " " (prin1-to-string (pop plist)))))
  8213. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8214. (beginning-of-line -2)))
  8215. (defun org-prepare-dblock ()
  8216. "Prepare dynamic block for refresh.
  8217. This empties the block, puts the cursor at the insert position and returns
  8218. the property list including an extra property :name with the block name."
  8219. (unless (looking-at org-dblock-start-re)
  8220. (error "Not at a dynamic block"))
  8221. (let* ((begdel (1+ (match-end 0)))
  8222. (name (org-no-properties (match-string 1)))
  8223. (params (append (list :name name)
  8224. (read (concat "(" (match-string 3) ")")))))
  8225. (save-excursion
  8226. (beginning-of-line 1)
  8227. (skip-chars-forward " \t")
  8228. (setq params (plist-put params :indentation-column (current-column))))
  8229. (unless (re-search-forward org-dblock-end-re nil t)
  8230. (error "Dynamic block not terminated"))
  8231. (setq params
  8232. (append params
  8233. (list :content (buffer-substring
  8234. begdel (match-beginning 0)))))
  8235. (delete-region begdel (match-beginning 0))
  8236. (goto-char begdel)
  8237. (open-line 1)
  8238. params))
  8239. (defun org-map-dblocks (&optional command)
  8240. "Apply COMMAND to all dynamic blocks in the current buffer.
  8241. If COMMAND is not given, use `org-update-dblock'."
  8242. (let ((cmd (or command 'org-update-dblock))
  8243. pos)
  8244. (save-excursion
  8245. (goto-char (point-min))
  8246. (while (re-search-forward org-dblock-start-re nil t)
  8247. (goto-char (setq pos (match-beginning 0)))
  8248. (condition-case nil
  8249. (funcall cmd)
  8250. (error (message "Error during update of dynamic block")))
  8251. (goto-char pos)
  8252. (unless (re-search-forward org-dblock-end-re nil t)
  8253. (error "Dynamic block not terminated"))))))
  8254. (defun org-dblock-update (&optional arg)
  8255. "User command for updating dynamic blocks.
  8256. Update the dynamic block at point. With prefix ARG, update all dynamic
  8257. blocks in the buffer."
  8258. (interactive "P")
  8259. (if arg
  8260. (org-update-all-dblocks)
  8261. (or (looking-at org-dblock-start-re)
  8262. (org-beginning-of-dblock))
  8263. (org-update-dblock)))
  8264. (defun org-update-dblock ()
  8265. "Update the dynamic block at point
  8266. This means to empty the block, parse for parameters and then call
  8267. the correct writing function."
  8268. (save-window-excursion
  8269. (let* ((pos (point))
  8270. (line (org-current-line))
  8271. (params (org-prepare-dblock))
  8272. (name (plist-get params :name))
  8273. (indent (plist-get params :indentation-column))
  8274. (cmd (intern (concat "org-dblock-write:" name))))
  8275. (message "Updating dynamic block `%s' at line %d..." name line)
  8276. (funcall cmd params)
  8277. (message "Updating dynamic block `%s' at line %d...done" name line)
  8278. (goto-char pos)
  8279. (when (and indent (> indent 0))
  8280. (setq indent (make-string indent ?\ ))
  8281. (save-excursion
  8282. (org-beginning-of-dblock)
  8283. (forward-line 1)
  8284. (while (not (looking-at org-dblock-end-re))
  8285. (insert indent)
  8286. (beginning-of-line 2))
  8287. (when (looking-at org-dblock-end-re)
  8288. (and (looking-at "[ \t]+")
  8289. (replace-match ""))
  8290. (insert indent)))))))
  8291. (defun org-beginning-of-dblock ()
  8292. "Find the beginning of the dynamic block at point.
  8293. Error if there is no such block at point."
  8294. (let ((pos (point))
  8295. beg)
  8296. (end-of-line 1)
  8297. (if (and (re-search-backward org-dblock-start-re nil t)
  8298. (setq beg (match-beginning 0))
  8299. (re-search-forward org-dblock-end-re nil t)
  8300. (> (match-end 0) pos))
  8301. (goto-char beg)
  8302. (goto-char pos)
  8303. (error "Not in a dynamic block"))))
  8304. (defun org-update-all-dblocks ()
  8305. "Update all dynamic blocks in the buffer.
  8306. This function can be used in a hook."
  8307. (when (org-mode-p)
  8308. (org-map-dblocks 'org-update-dblock)))
  8309. ;;;; Completion
  8310. (defconst org-additional-option-like-keywords
  8311. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8312. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8313. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
  8314. "BEGIN:" "END:"
  8315. "ORGTBL" "TBLFM:" "TBLNAME:"
  8316. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8317. "BEGIN_QUOTE" "END_QUOTE"
  8318. "BEGIN_VERSE" "END_VERSE"
  8319. "BEGIN_CENTER" "END_CENTER"
  8320. "BEGIN_SRC" "END_SRC"
  8321. "CATEGORY" "COLUMNS"
  8322. "CAPTION" "LABEL"
  8323. "SETUPFILE"
  8324. "BIND"
  8325. "MACRO"))
  8326. (defcustom org-structure-template-alist
  8327. '(
  8328. ("s" "#+begin_src ?\n\n#+end_src"
  8329. "<src lang=\"?\">\n\n</src>")
  8330. ("e" "#+begin_example\n?\n#+end_example"
  8331. "<example>\n?\n</example>")
  8332. ("q" "#+begin_quote\n?\n#+end_quote"
  8333. "<quote>\n?\n</quote>")
  8334. ("v" "#+begin_verse\n?\n#+end_verse"
  8335. "<verse>\n?\n/verse>")
  8336. ("c" "#+begin_center\n?\n#+end_center"
  8337. "<center>\n?\n/center>")
  8338. ("l" "#+begin_latex\n?\n#+end_latex"
  8339. "<literal style=\"latex\">\n?\n</literal>")
  8340. ("L" "#+latex: "
  8341. "<literal style=\"latex\">?</literal>")
  8342. ("h" "#+begin_html\n?\n#+end_html"
  8343. "<literal style=\"html\">\n?\n</literal>")
  8344. ("H" "#+html: "
  8345. "<literal style=\"html\">?</literal>")
  8346. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8347. ("A" "#+ascii: ")
  8348. ("i" "#+include %file ?"
  8349. "<include file=%file markup=\"?\">")
  8350. )
  8351. "Structure completion elements.
  8352. This is a list of abbreviation keys and values. The value gets inserted
  8353. it you type @samp{.} followed by the key and then the completion key,
  8354. usually `M-TAB'. %file will be replaced by a file name after prompting
  8355. for the file using completion.
  8356. There are two templates for each key, the first uses the original Org syntax,
  8357. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8358. the default when the /org-mtags.el/ module has been loaded. See also the
  8359. variable `org-mtags-prefer-muse-templates'.
  8360. This is an experimental feature, it is undecided if it is going to stay in."
  8361. :group 'org-completion
  8362. :type '(repeat
  8363. (string :tag "Key")
  8364. (string :tag "Template")
  8365. (string :tag "Muse Template")))
  8366. (defun org-try-structure-completion ()
  8367. "Try to complete a structure template before point.
  8368. This looks for strings like \"<e\" on an otherwise empty line and
  8369. expands them."
  8370. (let ((l (buffer-substring (point-at-bol) (point)))
  8371. a)
  8372. (when (and (looking-at "[ \t]*$")
  8373. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8374. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8375. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8376. (match-beginning 1)) a)
  8377. t)))
  8378. (defun org-complete-expand-structure-template (start cell)
  8379. "Expand a structure template."
  8380. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8381. (rpl (nth (if musep 2 1) cell))
  8382. (ind ""))
  8383. (delete-region start (point))
  8384. (when (string-match "\\`#\\+" rpl)
  8385. (cond
  8386. ((bolp))
  8387. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8388. (setq ind (buffer-substring (point-at-bol) (point))))
  8389. (t (newline))))
  8390. (setq start (point))
  8391. (if (string-match "%file" rpl)
  8392. (setq rpl (replace-match
  8393. (concat
  8394. "\""
  8395. (save-match-data
  8396. (abbreviate-file-name (read-file-name "Include file: ")))
  8397. "\"")
  8398. t t rpl)))
  8399. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8400. (concat "\n" ind)))
  8401. (insert rpl)
  8402. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8403. (defun org-complete (&optional arg)
  8404. "Perform completion on word at point.
  8405. At the beginning of a headline, this completes TODO keywords as given in
  8406. `org-todo-keywords'.
  8407. If the current word is preceded by a backslash, completes the TeX symbols
  8408. that are supported for HTML support.
  8409. If the current word is preceded by \"#+\", completes special words for
  8410. setting file options.
  8411. In the line after \"#+STARTUP:, complete valid keywords.\"
  8412. At all other locations, this simply calls the value of
  8413. `org-completion-fallback-command'."
  8414. (interactive "P")
  8415. (org-without-partial-completion
  8416. (catch 'exit
  8417. (let* ((a nil)
  8418. (end (point))
  8419. (beg1 (save-excursion
  8420. (skip-chars-backward (org-re "[:alnum:]_@"))
  8421. (point)))
  8422. (beg (save-excursion
  8423. (skip-chars-backward "a-zA-Z0-9_:$")
  8424. (point)))
  8425. (confirm (lambda (x) (stringp (car x))))
  8426. (searchhead (equal (char-before beg) ?*))
  8427. (struct
  8428. (when (and (member (char-before beg1) '(?. ?<))
  8429. (setq a (assoc (buffer-substring beg1 (point))
  8430. org-structure-template-alist)))
  8431. (org-complete-expand-structure-template (1- beg1) a)
  8432. (throw 'exit t)))
  8433. (tag (and (equal (char-before beg1) ?:)
  8434. (equal (char-after (point-at-bol)) ?*)))
  8435. (prop (and (equal (char-before beg1) ?:)
  8436. (not (equal (char-after (point-at-bol)) ?*))))
  8437. (texp (equal (char-before beg) ?\\))
  8438. (link (equal (char-before beg) ?\[))
  8439. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8440. beg)
  8441. "#+"))
  8442. (startup (string-match "^#\\+STARTUP:.*"
  8443. (buffer-substring (point-at-bol) (point))))
  8444. (completion-ignore-case opt)
  8445. (type nil)
  8446. (tbl nil)
  8447. (table (cond
  8448. (opt
  8449. (setq type :opt)
  8450. (require 'org-exp)
  8451. (append
  8452. (delq nil
  8453. (mapcar
  8454. (lambda (x)
  8455. (if (string-match
  8456. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8457. (cons (match-string 2 x)
  8458. (match-string 1 x))))
  8459. (org-split-string (org-get-current-options) "\n")))
  8460. (mapcar 'list org-additional-option-like-keywords)))
  8461. (startup
  8462. (setq type :startup)
  8463. org-startup-options)
  8464. (link (append org-link-abbrev-alist-local
  8465. org-link-abbrev-alist))
  8466. (texp
  8467. (setq type :tex)
  8468. org-html-entities)
  8469. ((string-match "\\`\\*+[ \t]+\\'"
  8470. (buffer-substring (point-at-bol) beg))
  8471. (setq type :todo)
  8472. (mapcar 'list org-todo-keywords-1))
  8473. (searchhead
  8474. (setq type :searchhead)
  8475. (save-excursion
  8476. (goto-char (point-min))
  8477. (while (re-search-forward org-todo-line-regexp nil t)
  8478. (push (list
  8479. (org-make-org-heading-search-string
  8480. (match-string 3) t))
  8481. tbl)))
  8482. tbl)
  8483. (tag (setq type :tag beg beg1)
  8484. (or org-tag-alist (org-get-buffer-tags)))
  8485. (prop (setq type :prop beg beg1)
  8486. (mapcar 'list (org-buffer-property-keys nil t t)))
  8487. (t (progn
  8488. (call-interactively org-completion-fallback-command)
  8489. (throw 'exit nil)))))
  8490. (pattern (buffer-substring-no-properties beg end))
  8491. (completion (try-completion pattern table confirm)))
  8492. (cond ((eq completion t)
  8493. (if (not (assoc (upcase pattern) table))
  8494. (message "Already complete")
  8495. (if (and (equal type :opt)
  8496. (not (member (car (assoc (upcase pattern) table))
  8497. org-additional-option-like-keywords)))
  8498. (insert (substring (cdr (assoc (upcase pattern) table))
  8499. (length pattern)))
  8500. (if (memq type '(:tag :prop)) (insert ":")))))
  8501. ((null completion)
  8502. (message "Can't find completion for \"%s\"" pattern)
  8503. (ding))
  8504. ((not (string= pattern completion))
  8505. (delete-region beg end)
  8506. (if (string-match " +$" completion)
  8507. (setq completion (replace-match "" t t completion)))
  8508. (insert completion)
  8509. (if (get-buffer-window "*Completions*")
  8510. (delete-window (get-buffer-window "*Completions*")))
  8511. (if (assoc completion table)
  8512. (if (eq type :todo) (insert " ")
  8513. (if (memq type '(:tag :prop)) (insert ":"))))
  8514. (if (and (equal type :opt) (assoc completion table))
  8515. (message "%s" (substitute-command-keys
  8516. "Press \\[org-complete] again to insert example settings"))))
  8517. (t
  8518. (message "Making completion list...")
  8519. (let ((list (sort (all-completions pattern table confirm)
  8520. 'string<)))
  8521. (with-output-to-temp-buffer "*Completions*"
  8522. (condition-case nil
  8523. ;; Protection needed for XEmacs and emacs 21
  8524. (display-completion-list list pattern)
  8525. (error (display-completion-list list)))))
  8526. (message "Making completion list...%s" "done")))))))
  8527. ;;;; TODO, DEADLINE, Comments
  8528. (defun org-toggle-comment ()
  8529. "Change the COMMENT state of an entry."
  8530. (interactive)
  8531. (save-excursion
  8532. (org-back-to-heading)
  8533. (let (case-fold-search)
  8534. (if (looking-at (concat outline-regexp
  8535. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8536. (replace-match "" t t nil 1)
  8537. (if (looking-at outline-regexp)
  8538. (progn
  8539. (goto-char (match-end 0))
  8540. (insert org-comment-string " ")))))))
  8541. (defvar org-last-todo-state-is-todo nil
  8542. "This is non-nil when the last TODO state change led to a TODO state.
  8543. If the last change removed the TODO tag or switched to DONE, then
  8544. this is nil.")
  8545. (defvar org-setting-tags nil) ; dynamically skipped
  8546. (defun org-parse-local-options (string var)
  8547. "Parse STRING for startup setting relevant for variable VAR."
  8548. (let ((rtn (symbol-value var))
  8549. e opts)
  8550. (save-match-data
  8551. (if (or (not string) (not (string-match "\\S-" string)))
  8552. rtn
  8553. (setq opts (delq nil (mapcar (lambda (x)
  8554. (setq e (assoc x org-startup-options))
  8555. (if (eq (nth 1 e) var) e nil))
  8556. (org-split-string string "[ \t]+"))))
  8557. (if (not opts)
  8558. rtn
  8559. (setq rtn nil)
  8560. (while (setq e (pop opts))
  8561. (if (not (nth 3 e))
  8562. (setq rtn (nth 2 e))
  8563. (if (not (listp rtn)) (setq rtn nil))
  8564. (push (nth 2 e) rtn)))
  8565. rtn)))))
  8566. (defvar org-todo-setup-filter-hook nil
  8567. "Hook for functions that pre-filter todo specs.
  8568. Each function takes a todo spec and returns either `nil' or the spec
  8569. transformed into canonical form." )
  8570. (defvar org-todo-get-default-hook nil
  8571. "Hook for functions that get a default item for todo.
  8572. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8573. `nil' or a string to be used for the todo mark." )
  8574. (defvar org-agenda-headline-snapshot-before-repeat)
  8575. (defun org-todo (&optional arg)
  8576. "Change the TODO state of an item.
  8577. The state of an item is given by a keyword at the start of the heading,
  8578. like
  8579. *** TODO Write paper
  8580. *** DONE Call mom
  8581. The different keywords are specified in the variable `org-todo-keywords'.
  8582. By default the available states are \"TODO\" and \"DONE\".
  8583. So for this example: when the item starts with TODO, it is changed to DONE.
  8584. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8585. DONE are present, add TODO at the beginning of the heading.
  8586. With C-u prefix arg, use completion to determine the new state.
  8587. With numeric prefix arg, switch to that state.
  8588. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8589. With a tripple C-u prefix, circumvent any state blocking.
  8590. For calling through lisp, arg is also interpreted in the following way:
  8591. 'none -> empty state
  8592. \"\"(empty string) -> switch to empty state
  8593. 'done -> switch to DONE
  8594. 'nextset -> switch to the next set of keywords
  8595. 'previousset -> switch to the previous set of keywords
  8596. \"WAITING\" -> switch to the specified keyword, but only if it
  8597. really is a member of `org-todo-keywords'."
  8598. (interactive "P")
  8599. (if (equal arg '(16)) (setq arg 'nextset))
  8600. (let ((org-blocker-hook org-blocker-hook)
  8601. (case-fold-search nil))
  8602. (when (equal arg '(64))
  8603. (setq arg nil org-blocker-hook nil))
  8604. (when (and org-blocker-hook
  8605. (or org-inhibit-blocking
  8606. (org-entry-get nil "NOBLOCKING")))
  8607. (setq org-blocker-hook nil))
  8608. (save-excursion
  8609. (catch 'exit
  8610. (org-back-to-heading t)
  8611. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8612. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8613. (looking-at " *"))
  8614. (let* ((match-data (match-data))
  8615. (startpos (point-at-bol))
  8616. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8617. (org-log-done org-log-done)
  8618. (org-log-repeat org-log-repeat)
  8619. (org-todo-log-states org-todo-log-states)
  8620. (this (match-string 1))
  8621. (hl-pos (match-beginning 0))
  8622. (head (org-get-todo-sequence-head this))
  8623. (ass (assoc head org-todo-kwd-alist))
  8624. (interpret (nth 1 ass))
  8625. (done-word (nth 3 ass))
  8626. (final-done-word (nth 4 ass))
  8627. (last-state (or this ""))
  8628. (completion-ignore-case t)
  8629. (member (member this org-todo-keywords-1))
  8630. (tail (cdr member))
  8631. (state (cond
  8632. ((and org-todo-key-trigger
  8633. (or (and (equal arg '(4))
  8634. (eq org-use-fast-todo-selection 'prefix))
  8635. (and (not arg) org-use-fast-todo-selection
  8636. (not (eq org-use-fast-todo-selection
  8637. 'prefix)))))
  8638. ;; Use fast selection
  8639. (org-fast-todo-selection))
  8640. ((and (equal arg '(4))
  8641. (or (not org-use-fast-todo-selection)
  8642. (not org-todo-key-trigger)))
  8643. ;; Read a state with completion
  8644. (org-icompleting-read
  8645. "State: " (mapcar (lambda(x) (list x))
  8646. org-todo-keywords-1)
  8647. nil t))
  8648. ((eq arg 'right)
  8649. (if this
  8650. (if tail (car tail) nil)
  8651. (car org-todo-keywords-1)))
  8652. ((eq arg 'left)
  8653. (if (equal member org-todo-keywords-1)
  8654. nil
  8655. (if this
  8656. (nth (- (length org-todo-keywords-1)
  8657. (length tail) 2)
  8658. org-todo-keywords-1)
  8659. (org-last org-todo-keywords-1))))
  8660. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8661. (setq arg nil))) ; hack to fall back to cycling
  8662. (arg
  8663. ;; user or caller requests a specific state
  8664. (cond
  8665. ((equal arg "") nil)
  8666. ((eq arg 'none) nil)
  8667. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8668. ((eq arg 'nextset)
  8669. (or (car (cdr (member head org-todo-heads)))
  8670. (car org-todo-heads)))
  8671. ((eq arg 'previousset)
  8672. (let ((org-todo-heads (reverse org-todo-heads)))
  8673. (or (car (cdr (member head org-todo-heads)))
  8674. (car org-todo-heads))))
  8675. ((car (member arg org-todo-keywords-1)))
  8676. ((stringp arg)
  8677. (error "State `%s' not valid in this file" arg))
  8678. ((nth (1- (prefix-numeric-value arg))
  8679. org-todo-keywords-1))))
  8680. ((null member) (or head (car org-todo-keywords-1)))
  8681. ((equal this final-done-word) nil) ;; -> make empty
  8682. ((null tail) nil) ;; -> first entry
  8683. ((memq interpret '(type priority))
  8684. (if (eq this-command last-command)
  8685. (car tail)
  8686. (if (> (length tail) 0)
  8687. (or done-word (car org-done-keywords))
  8688. nil)))
  8689. (t
  8690. (car tail))))
  8691. (state (or
  8692. (run-hook-with-args-until-success
  8693. 'org-todo-get-default-hook state last-state)
  8694. state))
  8695. (next (if state (concat " " state " ") " "))
  8696. (change-plist (list :type 'todo-state-change :from this :to state
  8697. :position startpos))
  8698. dolog now-done-p)
  8699. (when org-blocker-hook
  8700. (setq org-last-todo-state-is-todo
  8701. (not (member this org-done-keywords)))
  8702. (unless (save-excursion
  8703. (save-match-data
  8704. (run-hook-with-args-until-failure
  8705. 'org-blocker-hook change-plist)))
  8706. (if (interactive-p)
  8707. (error "TODO state change from %s to %s blocked" this state)
  8708. ;; fail silently
  8709. (message "TODO state change from %s to %s blocked" this state)
  8710. (throw 'exit nil))))
  8711. (store-match-data match-data)
  8712. (replace-match next t t)
  8713. (unless (pos-visible-in-window-p hl-pos)
  8714. (message "TODO state changed to %s" (org-trim next)))
  8715. (unless head
  8716. (setq head (org-get-todo-sequence-head state)
  8717. ass (assoc head org-todo-kwd-alist)
  8718. interpret (nth 1 ass)
  8719. done-word (nth 3 ass)
  8720. final-done-word (nth 4 ass)))
  8721. (when (memq arg '(nextset previousset))
  8722. (message "Keyword-Set %d/%d: %s"
  8723. (- (length org-todo-sets) -1
  8724. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8725. (length org-todo-sets)
  8726. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8727. (setq org-last-todo-state-is-todo
  8728. (not (member state org-done-keywords)))
  8729. (setq now-done-p (and (member state org-done-keywords)
  8730. (not (member this org-done-keywords))))
  8731. (and logging (org-local-logging logging))
  8732. (when (and (or org-todo-log-states org-log-done)
  8733. (not (eq org-inhibit-logging t))
  8734. (not (memq arg '(nextset previousset))))
  8735. ;; we need to look at recording a time and note
  8736. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8737. (nth 2 (assoc this org-todo-log-states))))
  8738. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8739. (setq dolog 'time))
  8740. (when (and state
  8741. (member state org-not-done-keywords)
  8742. (not (member this org-not-done-keywords)))
  8743. ;; This is now a todo state and was not one before
  8744. ;; If there was a CLOSED time stamp, get rid of it.
  8745. (org-add-planning-info nil nil 'closed))
  8746. (when (and now-done-p org-log-done)
  8747. ;; It is now done, and it was not done before
  8748. (org-add-planning-info 'closed (org-current-time))
  8749. (if (and (not dolog) (eq 'note org-log-done))
  8750. (org-add-log-setup 'done state this 'findpos 'note)))
  8751. (when (and state dolog)
  8752. ;; This is a non-nil state, and we need to log it
  8753. (org-add-log-setup 'state state this 'findpos dolog)))
  8754. ;; Fixup tag positioning
  8755. (org-todo-trigger-tag-changes state)
  8756. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8757. (when org-provide-todo-statistics
  8758. (org-update-parent-todo-statistics))
  8759. (run-hooks 'org-after-todo-state-change-hook)
  8760. (if (and arg (not (member state org-done-keywords)))
  8761. (setq head (org-get-todo-sequence-head state)))
  8762. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8763. ;; Do we need to trigger a repeat?
  8764. (when now-done-p
  8765. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8766. ;; This is for the agenda, take a snapshot of the headline.
  8767. (save-match-data
  8768. (setq org-agenda-headline-snapshot-before-repeat
  8769. (org-get-heading))))
  8770. (org-auto-repeat-maybe state))
  8771. ;; Fixup cursor location if close to the keyword
  8772. (if (and (outline-on-heading-p)
  8773. (not (bolp))
  8774. (save-excursion (beginning-of-line 1)
  8775. (looking-at org-todo-line-regexp))
  8776. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8777. (progn
  8778. (goto-char (or (match-end 2) (match-end 1)))
  8779. (and (looking-at " ") (just-one-space))))
  8780. (when org-trigger-hook
  8781. (save-excursion
  8782. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8783. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8784. "Block turning an entry into a TODO, using the hierarchy.
  8785. This checks whether the current task should be blocked from state
  8786. changes. Such blocking occurs when:
  8787. 1. The task has children which are not all in a completed state.
  8788. 2. A task has a parent with the property :ORDERED:, and there
  8789. are siblings prior to the current task with incomplete
  8790. status.
  8791. 3. The parent of the task is blocked because it has siblings that should
  8792. be done first, or is child of a block grandparent TODO entry."
  8793. (catch 'dont-block
  8794. ;; If this is not a todo state change, or if this entry is already DONE,
  8795. ;; do not block
  8796. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8797. (member (plist-get change-plist :from)
  8798. (cons 'done org-done-keywords))
  8799. (member (plist-get change-plist :to)
  8800. (cons 'todo org-not-done-keywords)))
  8801. (throw 'dont-block t))
  8802. ;; If this task has children, and any are undone, it's blocked
  8803. (save-excursion
  8804. (org-back-to-heading t)
  8805. (let ((this-level (funcall outline-level)))
  8806. (outline-next-heading)
  8807. (let ((child-level (funcall outline-level)))
  8808. (while (and (not (eobp))
  8809. (> child-level this-level))
  8810. ;; this todo has children, check whether they are all
  8811. ;; completed
  8812. (if (and (not (org-entry-is-done-p))
  8813. (org-entry-is-todo-p))
  8814. (throw 'dont-block nil))
  8815. (outline-next-heading)
  8816. (setq child-level (funcall outline-level))))))
  8817. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8818. ;; any previous siblings are undone, it's blocked
  8819. (save-excursion
  8820. (org-back-to-heading t)
  8821. (let* ((pos (point))
  8822. (parent-pos (and (org-up-heading-safe) (point))))
  8823. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8824. (when (and (org-entry-get (point) "ORDERED")
  8825. (forward-line 1)
  8826. (re-search-forward org-not-done-heading-regexp pos t))
  8827. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8828. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8829. (while t
  8830. (goto-char parent-pos)
  8831. (if (not (looking-at org-not-done-heading-regexp))
  8832. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8833. (setq pos (point))
  8834. (setq parent-pos (and (org-up-heading-safe) (point)))
  8835. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8836. (when (and (org-entry-get (point) "ORDERED")
  8837. (forward-line 1)
  8838. (re-search-forward org-not-done-heading-regexp pos t))
  8839. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8840. (defcustom org-track-ordered-property-with-tag nil
  8841. "Should the ORDERED property also be shown as a tag?
  8842. The ORDERED property decides if an entry should require subtasks to be
  8843. completed in sequence. Since a property is not very visible, setting
  8844. this option means that toggling the ORDERED property with the command
  8845. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8846. not relevant for the behavior, but it makes things more visible.
  8847. Note that toggling the tag with tags commands will not change the property
  8848. and therefore not influence behavior!
  8849. This can be t, meaning the tag ORDERED should be used, It can also be a
  8850. string to select a different tag for this task."
  8851. :group 'org-todo
  8852. :type '(choice
  8853. (const :tag "No tracking" nil)
  8854. (const :tag "Track with ORDERED tag" t)
  8855. (string :tag "Use other tag")))
  8856. (defun org-toggle-ordered-property ()
  8857. "Toggle the ORDERED property of the current entry.
  8858. For better visibility, you can track the value of this property with a tag.
  8859. See variable `org-track-ordered-property-with-tag'."
  8860. (interactive)
  8861. (let* ((t1 org-track-ordered-property-with-tag)
  8862. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8863. (save-excursion
  8864. (org-back-to-heading)
  8865. (if (org-entry-get nil "ORDERED")
  8866. (progn
  8867. (org-delete-property "ORDERED")
  8868. (and tag (org-toggle-tag tag 'off))
  8869. (message "Subtasks can be completed in arbitrary order"))
  8870. (org-entry-put nil "ORDERED" "t")
  8871. (and tag (org-toggle-tag tag 'on))
  8872. (message "Subtasks must be completed in sequence")))))
  8873. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8874. (defun org-block-todo-from-checkboxes (change-plist)
  8875. "Block turning an entry into a TODO, using checkboxes.
  8876. This checks whether the current task should be blocked from state
  8877. changes because there are uncheckd boxes in this entry."
  8878. (catch 'dont-block
  8879. ;; If this is not a todo state change, or if this entry is already DONE,
  8880. ;; do not block
  8881. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8882. (member (plist-get change-plist :from)
  8883. (cons 'done org-done-keywords))
  8884. (member (plist-get change-plist :to)
  8885. (cons 'todo org-not-done-keywords)))
  8886. (throw 'dont-block t))
  8887. ;; If this task has checkboxes that are not checked, it's blocked
  8888. (save-excursion
  8889. (org-back-to-heading t)
  8890. (let ((beg (point)) end)
  8891. (outline-next-heading)
  8892. (setq end (point))
  8893. (goto-char beg)
  8894. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8895. end t)
  8896. (progn
  8897. (if (boundp 'org-blocked-by-checkboxes)
  8898. (setq org-blocked-by-checkboxes t))
  8899. (throw 'dont-block nil)))))
  8900. t)) ; do not block
  8901. (defun org-update-statistics-cookies (all)
  8902. "Update the statistics cookie, either from TODO or from checkboxes.
  8903. This should be called with the cursor in a line with a statistics cookie."
  8904. (interactive "P")
  8905. (if all
  8906. (progn
  8907. (org-update-checkbox-count 'all)
  8908. (org-map-entries 'org-update-parent-todo-statistics))
  8909. (if (not (org-on-heading-p))
  8910. (org-update-checkbox-count)
  8911. (let ((pos (move-marker (make-marker) (point)))
  8912. end l1 l2)
  8913. (ignore-errors (org-back-to-heading t))
  8914. (if (not (org-on-heading-p))
  8915. (org-update-checkbox-count)
  8916. (setq l1 (org-outline-level))
  8917. (setq end (save-excursion
  8918. (outline-next-heading)
  8919. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  8920. (point)))
  8921. (if (and (save-excursion (re-search-forward
  8922. "^[ \t]*[-+*] \\[[- X]\\]" end t))
  8923. (not (save-excursion (re-search-forward
  8924. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  8925. (org-update-checkbox-count)
  8926. (if (and l2 (> l2 l1))
  8927. (progn
  8928. (goto-char end)
  8929. (org-update-parent-todo-statistics))
  8930. (error "No data for statistics cookie"))))
  8931. (goto-char pos)
  8932. (move-marker pos nil)))))
  8933. (defvar org-entry-property-inherited-from) ;; defined below
  8934. (defun org-update-parent-todo-statistics ()
  8935. "Update any statistics cookie in the parent of the current headline.
  8936. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8937. the entire subtree and this will travel up the hierarchy and update
  8938. statistics everywhere."
  8939. (interactive)
  8940. (let* ((lim 0) prop
  8941. (recursive (or (not org-hierarchical-todo-statistics)
  8942. (string-match
  8943. "\\<recursive\\>"
  8944. (or (setq prop (org-entry-get
  8945. nil "COOKIE_DATA" 'inherit)) ""))))
  8946. (lim (or (and prop (marker-position
  8947. org-entry-property-inherited-from))
  8948. lim))
  8949. (first t)
  8950. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8951. level ltoggle l1 new ndel
  8952. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8953. (catch 'exit
  8954. (save-excursion
  8955. (beginning-of-line 1)
  8956. (if (org-at-heading-p)
  8957. (setq ltoggle (funcall outline-level))
  8958. (error "This should not happen"))
  8959. (while (and (setq level (org-up-heading-safe))
  8960. (or recursive first)
  8961. (>= (point) lim))
  8962. (setq first nil)
  8963. (unless (and level
  8964. (not (string-match
  8965. "\\<checkbox\\>"
  8966. (downcase
  8967. (or (org-entry-get
  8968. nil "COOKIE_DATA")
  8969. "")))))
  8970. (throw 'exit nil))
  8971. (while (re-search-forward box-re (point-at-eol) t)
  8972. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8973. (setq is-percent (match-end 2))
  8974. (save-match-data
  8975. (unless (outline-next-heading) (throw 'exit nil))
  8976. (while (and (looking-at org-complex-heading-regexp)
  8977. (> (setq l1 (length (match-string 1))) level))
  8978. (setq kwd (and (or recursive (= l1 ltoggle))
  8979. (match-string 2)))
  8980. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8981. (and (listp org-provide-todo-statistics)
  8982. (or (member kwd org-provide-todo-statistics)
  8983. (member kwd org-done-keywords))))
  8984. (setq cnt-all (1+ cnt-all))
  8985. (if (eq org-provide-todo-statistics t)
  8986. (and kwd (setq cnt-all (1+ cnt-all)))))
  8987. (and (member kwd org-done-keywords)
  8988. (setq cnt-done (1+ cnt-done)))
  8989. (outline-next-heading)))
  8990. (setq new
  8991. (if is-percent
  8992. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8993. (format "[%d/%d]" cnt-done cnt-all))
  8994. ndel (- (match-end 0) (match-beginning 0)))
  8995. (goto-char (match-beginning 0))
  8996. (insert new)
  8997. (delete-region (point) (+ (point) ndel))))
  8998. (when cookie-present
  8999. (run-hook-with-args 'org-after-todo-statistics-hook
  9000. cnt-done (- cnt-all cnt-done)))))
  9001. (run-hooks 'org-todo-statistics-hook)))
  9002. (defvar org-after-todo-statistics-hook nil
  9003. "Hook that is called after a TODO statistics cookie has been updated.
  9004. Each function is called with two arguments: the number of not-done entries
  9005. and the number of done entries.
  9006. For example, the following function, when added to this hook, will switch
  9007. an entry to DONE when all children are done, and back to TODO when new
  9008. entries are set to a TODO status. Note that this hook is only called
  9009. when there is a statistics cookie in the headline!
  9010. (defun org-summary-todo (n-done n-not-done)
  9011. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9012. (let (org-log-done org-log-states) ; turn off logging
  9013. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9014. ")
  9015. (defvar org-todo-statistics-hook nil
  9016. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9017. This hook runs even if there is no statisics cookie present, in which case
  9018. `org-after-todo-statistics-hook' would not run.")
  9019. (defun org-todo-trigger-tag-changes (state)
  9020. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9021. (let ((l org-todo-state-tags-triggers)
  9022. changes)
  9023. (when (or (not state) (equal state ""))
  9024. (setq changes (append changes (cdr (assoc "" l)))))
  9025. (when (and (stringp state) (> (length state) 0))
  9026. (setq changes (append changes (cdr (assoc state l)))))
  9027. (when (member state org-not-done-keywords)
  9028. (setq changes (append changes (cdr (assoc 'todo l)))))
  9029. (when (member state org-done-keywords)
  9030. (setq changes (append changes (cdr (assoc 'done l)))))
  9031. (dolist (c changes)
  9032. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9033. (defun org-local-logging (value)
  9034. "Get logging settings from a property VALUE."
  9035. (let* (words w a)
  9036. ;; directly set the variables, they are already local.
  9037. (setq org-log-done nil
  9038. org-log-repeat nil
  9039. org-todo-log-states nil)
  9040. (setq words (org-split-string value))
  9041. (while (setq w (pop words))
  9042. (cond
  9043. ((setq a (assoc w org-startup-options))
  9044. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9045. (set (nth 1 a) (nth 2 a))))
  9046. ((setq a (org-extract-log-state-settings w))
  9047. (and (member (car a) org-todo-keywords-1)
  9048. (push a org-todo-log-states)))))))
  9049. (defun org-get-todo-sequence-head (kwd)
  9050. "Return the head of the TODO sequence to which KWD belongs.
  9051. If KWD is not set, check if there is a text property remembering the
  9052. right sequence."
  9053. (let (p)
  9054. (cond
  9055. ((not kwd)
  9056. (or (get-text-property (point-at-bol) 'org-todo-head)
  9057. (progn
  9058. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9059. nil (point-at-eol)))
  9060. (get-text-property p 'org-todo-head))))
  9061. ((not (member kwd org-todo-keywords-1))
  9062. (car org-todo-keywords-1))
  9063. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9064. (defun org-fast-todo-selection ()
  9065. "Fast TODO keyword selection with single keys.
  9066. Returns the new TODO keyword, or nil if no state change should occur."
  9067. (let* ((fulltable org-todo-key-alist)
  9068. (done-keywords org-done-keywords) ;; needed for the faces.
  9069. (maxlen (apply 'max (mapcar
  9070. (lambda (x)
  9071. (if (stringp (car x)) (string-width (car x)) 0))
  9072. fulltable)))
  9073. (expert nil)
  9074. (fwidth (+ maxlen 3 1 3))
  9075. (ncol (/ (- (window-width) 4) fwidth))
  9076. tg cnt e c tbl
  9077. groups ingroup)
  9078. (save-excursion
  9079. (save-window-excursion
  9080. (if expert
  9081. (set-buffer (get-buffer-create " *Org todo*"))
  9082. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9083. (erase-buffer)
  9084. (org-set-local 'org-done-keywords done-keywords)
  9085. (setq tbl fulltable cnt 0)
  9086. (while (setq e (pop tbl))
  9087. (cond
  9088. ((equal e '(:startgroup))
  9089. (push '() groups) (setq ingroup t)
  9090. (when (not (= cnt 0))
  9091. (setq cnt 0)
  9092. (insert "\n"))
  9093. (insert "{ "))
  9094. ((equal e '(:endgroup))
  9095. (setq ingroup nil cnt 0)
  9096. (insert "}\n"))
  9097. ((equal e '(:newline))
  9098. (when (not (= cnt 0))
  9099. (setq cnt 0)
  9100. (insert "\n")
  9101. (setq e (car tbl))
  9102. (while (equal (car tbl) '(:newline))
  9103. (insert "\n")
  9104. (setq tbl (cdr tbl)))))
  9105. (t
  9106. (setq tg (car e) c (cdr e))
  9107. (if ingroup (push tg (car groups)))
  9108. (setq tg (org-add-props tg nil 'face
  9109. (org-get-todo-face tg)))
  9110. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9111. (insert "[" c "] " tg (make-string
  9112. (- fwidth 4 (length tg)) ?\ ))
  9113. (when (= (setq cnt (1+ cnt)) ncol)
  9114. (insert "\n")
  9115. (if ingroup (insert " "))
  9116. (setq cnt 0)))))
  9117. (insert "\n")
  9118. (goto-char (point-min))
  9119. (if (not expert) (org-fit-window-to-buffer))
  9120. (message "[a-z..]:Set [SPC]:clear")
  9121. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9122. (cond
  9123. ((or (= c ?\C-g)
  9124. (and (= c ?q) (not (rassoc c fulltable))))
  9125. (setq quit-flag t))
  9126. ((= c ?\ ) nil)
  9127. ((setq e (rassoc c fulltable) tg (car e))
  9128. tg)
  9129. (t (setq quit-flag t)))))))
  9130. (defun org-entry-is-todo-p ()
  9131. (member (org-get-todo-state) org-not-done-keywords))
  9132. (defun org-entry-is-done-p ()
  9133. (member (org-get-todo-state) org-done-keywords))
  9134. (defun org-get-todo-state ()
  9135. (save-excursion
  9136. (org-back-to-heading t)
  9137. (and (looking-at org-todo-line-regexp)
  9138. (match-end 2)
  9139. (match-string 2))))
  9140. (defun org-at-date-range-p (&optional inactive-ok)
  9141. "Is the cursor inside a date range?"
  9142. (interactive)
  9143. (save-excursion
  9144. (catch 'exit
  9145. (let ((pos (point)))
  9146. (skip-chars-backward "^[<\r\n")
  9147. (skip-chars-backward "<[")
  9148. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9149. (>= (match-end 0) pos)
  9150. (throw 'exit t))
  9151. (skip-chars-backward "^<[\r\n")
  9152. (skip-chars-backward "<[")
  9153. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9154. (>= (match-end 0) pos)
  9155. (throw 'exit t)))
  9156. nil)))
  9157. (defun org-get-repeat (&optional tagline)
  9158. "Check if there is a deadline/schedule with repeater in this entry."
  9159. (save-match-data
  9160. (save-excursion
  9161. (org-back-to-heading t)
  9162. (and (re-search-forward (if tagline
  9163. (concat tagline "\\s-*" org-repeat-re)
  9164. org-repeat-re)
  9165. (org-entry-end-position) t)
  9166. (match-string-no-properties 1)))))
  9167. (defvar org-last-changed-timestamp)
  9168. (defvar org-last-inserted-timestamp)
  9169. (defvar org-log-post-message)
  9170. (defvar org-log-note-purpose)
  9171. (defvar org-log-note-how)
  9172. (defvar org-log-note-extra)
  9173. (defun org-auto-repeat-maybe (done-word)
  9174. "Check if the current headline contains a repeated deadline/schedule.
  9175. If yes, set TODO state back to what it was and change the base date
  9176. of repeating deadline/scheduled time stamps to new date.
  9177. This function is run automatically after each state change to a DONE state."
  9178. ;; last-state is dynamically scoped into this function
  9179. (let* ((repeat (org-get-repeat))
  9180. (aa (assoc last-state org-todo-kwd-alist))
  9181. (interpret (nth 1 aa))
  9182. (head (nth 2 aa))
  9183. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9184. (msg "Entry repeats: ")
  9185. (org-log-done nil)
  9186. (org-todo-log-states nil)
  9187. (nshiftmax 10) (nshift 0)
  9188. re type n what ts time)
  9189. (when repeat
  9190. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9191. (org-todo (if (eq interpret 'type) last-state head))
  9192. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9193. (org-time-stamp-format t t)))
  9194. (when org-log-repeat
  9195. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9196. (memq 'org-add-log-note post-command-hook))
  9197. ;; OK, we are already setup for some record
  9198. (if (eq org-log-repeat 'note)
  9199. ;; make sure we take a note, not only a time stamp
  9200. (setq org-log-note-how 'note))
  9201. ;; Set up for taking a record
  9202. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9203. last-state
  9204. 'findpos org-log-repeat)))
  9205. (org-back-to-heading t)
  9206. (org-add-planning-info nil nil 'closed)
  9207. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9208. org-deadline-time-regexp "\\)\\|\\("
  9209. org-ts-regexp "\\)"))
  9210. (while (re-search-forward
  9211. re (save-excursion (outline-next-heading) (point)) t)
  9212. (setq type (if (match-end 1) org-scheduled-string
  9213. (if (match-end 3) org-deadline-string "Plain:"))
  9214. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9215. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9216. (setq n (string-to-number (match-string 2 ts))
  9217. what (match-string 3 ts))
  9218. (if (equal what "w") (setq n (* n 7) what "d"))
  9219. ;; Preparation, see if we need to modify the start date for the change
  9220. (when (match-end 1)
  9221. (setq time (save-match-data (org-time-string-to-time ts)))
  9222. (cond
  9223. ((equal (match-string 1 ts) ".")
  9224. ;; Shift starting date to today
  9225. (org-timestamp-change
  9226. (- (time-to-days (current-time)) (time-to-days time))
  9227. 'day))
  9228. ((equal (match-string 1 ts) "+")
  9229. (while (or (= nshift 0)
  9230. (<= (time-to-days time) (time-to-days (current-time))))
  9231. (when (= (incf nshift) nshiftmax)
  9232. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9233. (error "Abort")))
  9234. (org-timestamp-change n (cdr (assoc what whata)))
  9235. (org-at-timestamp-p t)
  9236. (setq ts (match-string 1))
  9237. (setq time (save-match-data (org-time-string-to-time ts))))
  9238. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9239. ;; rematch, so that we have everything in place for the real shift
  9240. (org-at-timestamp-p t)
  9241. (setq ts (match-string 1))
  9242. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9243. (org-timestamp-change n (cdr (assoc what whata)))
  9244. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9245. (setq org-log-post-message msg)
  9246. (message "%s" msg))))
  9247. (defun org-show-todo-tree (arg)
  9248. "Make a compact tree which shows all headlines marked with TODO.
  9249. The tree will show the lines where the regexp matches, and all higher
  9250. headlines above the match.
  9251. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9252. With a numeric prefix N, construct a sparse tree for the Nth element
  9253. of `org-todo-keywords-1'."
  9254. (interactive "P")
  9255. (let ((case-fold-search nil)
  9256. (kwd-re
  9257. (cond ((null arg) org-not-done-regexp)
  9258. ((equal arg '(4))
  9259. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9260. (mapcar 'list org-todo-keywords-1))))
  9261. (concat "\\("
  9262. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9263. "\\)\\>")))
  9264. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9265. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9266. org-todo-keywords-1)))
  9267. (t (error "Invalid prefix argument: %s" arg)))))
  9268. (message "%d TODO entries found"
  9269. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9270. (defun org-deadline (&optional remove time)
  9271. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9272. With argument REMOVE, remove any deadline from the item.
  9273. When TIME is set, it should be an internal time specification, and the
  9274. scheduling will use the corresponding date."
  9275. (interactive "P")
  9276. (if remove
  9277. (progn
  9278. (org-remove-timestamp-with-keyword org-deadline-string)
  9279. (message "Item no longer has a deadline."))
  9280. (if (org-get-repeat)
  9281. (error "Cannot change deadline on task with repeater, please do that by hand")
  9282. (org-add-planning-info 'deadline time 'closed)
  9283. (message "Deadline on %s" org-last-inserted-timestamp))))
  9284. (defun org-schedule (&optional remove time)
  9285. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9286. With argument REMOVE, remove any scheduling date from the item.
  9287. When TIME is set, it should be an internal time specification, and the
  9288. scheduling will use the corresponding date."
  9289. (interactive "P")
  9290. (if remove
  9291. (progn
  9292. (org-remove-timestamp-with-keyword org-scheduled-string)
  9293. (message "Item is no longer scheduled."))
  9294. (if (org-get-repeat)
  9295. (error "Cannot reschedule task with repeater, please do that by hand")
  9296. (org-add-planning-info 'scheduled time 'closed)
  9297. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9298. (defun org-get-scheduled-time (pom &optional inherit)
  9299. "Get the scheduled time as a time tuple, of a format suitable
  9300. for calling org-schedule with, or if there is no scheduling,
  9301. returns nil."
  9302. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9303. (when time
  9304. (apply 'encode-time (org-parse-time-string time)))))
  9305. (defun org-get-deadline-time (pom &optional inherit)
  9306. "Get the deadine as a time tuple, of a format suitable for
  9307. calling org-deadlin with, or if there is no scheduling, returns
  9308. nil."
  9309. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9310. (when time
  9311. (apply 'encode-time (org-parse-time-string time)))))
  9312. (defun org-remove-timestamp-with-keyword (keyword)
  9313. "Remove all time stamps with KEYWORD in the current entry."
  9314. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9315. beg)
  9316. (save-excursion
  9317. (org-back-to-heading t)
  9318. (setq beg (point))
  9319. (outline-next-heading)
  9320. (while (re-search-backward re beg t)
  9321. (replace-match "")
  9322. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9323. (equal (char-before) ?\ ))
  9324. (backward-delete-char 1)
  9325. (if (string-match "^[ \t]*$" (buffer-substring
  9326. (point-at-bol) (point-at-eol)))
  9327. (delete-region (point-at-bol)
  9328. (min (point-max) (1+ (point-at-eol))))))))))
  9329. (defun org-add-planning-info (what &optional time &rest remove)
  9330. "Insert new timestamp with keyword in the line directly after the headline.
  9331. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9332. If non is given, the user is prompted for a date.
  9333. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9334. be removed."
  9335. (interactive)
  9336. (let (org-time-was-given org-end-time-was-given ts
  9337. end default-time default-input)
  9338. (catch 'exit
  9339. (when (and (not time) (memq what '(scheduled deadline)))
  9340. ;; Try to get a default date/time from existing timestamp
  9341. (save-excursion
  9342. (org-back-to-heading t)
  9343. (setq end (save-excursion (outline-next-heading) (point)))
  9344. (when (re-search-forward (if (eq what 'scheduled)
  9345. org-scheduled-time-regexp
  9346. org-deadline-time-regexp)
  9347. end t)
  9348. (setq ts (match-string 1)
  9349. default-time
  9350. (apply 'encode-time (org-parse-time-string ts))
  9351. default-input (and ts (org-get-compact-tod ts))))))
  9352. (when what
  9353. ;; If necessary, get the time from the user
  9354. (setq time (or time (org-read-date nil 'to-time nil nil
  9355. default-time default-input))))
  9356. (when (and org-insert-labeled-timestamps-at-point
  9357. (member what '(scheduled deadline)))
  9358. (insert
  9359. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9360. (org-insert-time-stamp time org-time-was-given
  9361. nil nil nil (list org-end-time-was-given))
  9362. (setq what nil))
  9363. (save-excursion
  9364. (save-restriction
  9365. (let (col list elt ts buffer-invisibility-spec)
  9366. (org-back-to-heading t)
  9367. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9368. (goto-char (match-end 1))
  9369. (setq col (current-column))
  9370. (goto-char (match-end 0))
  9371. (if (eobp) (insert "\n") (forward-char 1))
  9372. (when (and (not what)
  9373. (not (looking-at
  9374. (concat "[ \t]*"
  9375. org-keyword-time-not-clock-regexp))))
  9376. ;; Nothing to add, nothing to remove...... :-)
  9377. (throw 'exit nil))
  9378. (if (and (not (looking-at outline-regexp))
  9379. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9380. "[^\r\n]*"))
  9381. (not (equal (match-string 1) org-clock-string)))
  9382. (narrow-to-region (match-beginning 0) (match-end 0))
  9383. (insert-before-markers "\n")
  9384. (backward-char 1)
  9385. (narrow-to-region (point) (point))
  9386. (and org-adapt-indentation (org-indent-to-column col)))
  9387. ;; Check if we have to remove something.
  9388. (setq list (cons what remove))
  9389. (while list
  9390. (setq elt (pop list))
  9391. (goto-char (point-min))
  9392. (when (or (and (eq elt 'scheduled)
  9393. (re-search-forward org-scheduled-time-regexp nil t))
  9394. (and (eq elt 'deadline)
  9395. (re-search-forward org-deadline-time-regexp nil t))
  9396. (and (eq elt 'closed)
  9397. (re-search-forward org-closed-time-regexp nil t)))
  9398. (replace-match "")
  9399. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9400. (skip-chars-backward " ")
  9401. (if (looking-at " +") (replace-match ""))))
  9402. (goto-char (point-max))
  9403. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  9404. (when what
  9405. (insert
  9406. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9407. (cond ((eq what 'scheduled) org-scheduled-string)
  9408. ((eq what 'deadline) org-deadline-string)
  9409. ((eq what 'closed) org-closed-string))
  9410. " ")
  9411. (setq ts (org-insert-time-stamp
  9412. time
  9413. (or org-time-was-given
  9414. (and (eq what 'closed) org-log-done-with-time))
  9415. (eq what 'closed)
  9416. nil nil (list org-end-time-was-given)))
  9417. (end-of-line 1))
  9418. (goto-char (point-min))
  9419. (widen)
  9420. (if (and (looking-at "[ \t]+\n")
  9421. (equal (char-before) ?\n))
  9422. (delete-region (1- (point)) (point-at-eol)))
  9423. ts))))))
  9424. (defvar org-log-note-marker (make-marker))
  9425. (defvar org-log-note-purpose nil)
  9426. (defvar org-log-note-state nil)
  9427. (defvar org-log-note-previous-state nil)
  9428. (defvar org-log-note-how nil)
  9429. (defvar org-log-note-extra nil)
  9430. (defvar org-log-note-window-configuration nil)
  9431. (defvar org-log-note-return-to (make-marker))
  9432. (defvar org-log-post-message nil
  9433. "Message to be displayed after a log note has been stored.
  9434. The auto-repeater uses this.")
  9435. (defun org-add-note ()
  9436. "Add a note to the current entry.
  9437. This is done in the same way as adding a state change note."
  9438. (interactive)
  9439. (org-add-log-setup 'note nil nil 'findpos nil))
  9440. (defvar org-property-end-re)
  9441. (defun org-add-log-setup (&optional purpose state prev-state
  9442. findpos how &optional extra)
  9443. "Set up the post command hook to take a note.
  9444. If this is about to TODO state change, the new state is expected in STATE.
  9445. When FINDPOS is non-nil, find the correct position for the note in
  9446. the current entry. If not, assume that it can be inserted at point.
  9447. HOW is an indicator what kind of note should be created.
  9448. EXTRA is additional text that will be inserted into the notes buffer."
  9449. (let* ((org-log-into-drawer (org-log-into-drawer))
  9450. (drawer (cond ((stringp org-log-into-drawer)
  9451. org-log-into-drawer)
  9452. (org-log-into-drawer "LOGBOOK")
  9453. (t nil))))
  9454. (save-restriction
  9455. (save-excursion
  9456. (when findpos
  9457. (org-back-to-heading t)
  9458. (narrow-to-region (point) (save-excursion
  9459. (outline-next-heading) (point)))
  9460. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9461. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9462. "[^\r\n]*\\)?"))
  9463. (goto-char (match-end 0))
  9464. (cond
  9465. (drawer
  9466. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9467. nil t)
  9468. (progn
  9469. (goto-char (match-end 0))
  9470. (or org-log-states-order-reversed
  9471. (and (re-search-forward org-property-end-re nil t)
  9472. (goto-char (1- (match-beginning 0))))))
  9473. (insert "\n:" drawer ":\n:END:")
  9474. (beginning-of-line 0)
  9475. (org-indent-line-function)
  9476. (beginning-of-line 2)
  9477. (org-indent-line-function)
  9478. (end-of-line 0)))
  9479. ((and org-log-state-notes-insert-after-drawers
  9480. (save-excursion
  9481. (forward-line) (looking-at org-drawer-regexp)))
  9482. (forward-line)
  9483. (while (looking-at org-drawer-regexp)
  9484. (goto-char (match-end 0))
  9485. (re-search-forward org-property-end-re (point-max) t)
  9486. (forward-line))
  9487. (forward-line -1)))
  9488. (unless org-log-states-order-reversed
  9489. (and (= (char-after) ?\n) (forward-char 1))
  9490. (org-skip-over-state-notes)
  9491. (skip-chars-backward " \t\n\r")))
  9492. (move-marker org-log-note-marker (point))
  9493. (setq org-log-note-purpose purpose
  9494. org-log-note-state state
  9495. org-log-note-previous-state prev-state
  9496. org-log-note-how how
  9497. org-log-note-extra extra)
  9498. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9499. (defun org-skip-over-state-notes ()
  9500. "Skip past the list of State notes in an entry."
  9501. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9502. (while (looking-at "[ \t]*- State")
  9503. (condition-case nil
  9504. (org-next-item)
  9505. (error (org-end-of-item)))))
  9506. (defun org-add-log-note (&optional purpose)
  9507. "Pop up a window for taking a note, and add this note later at point."
  9508. (remove-hook 'post-command-hook 'org-add-log-note)
  9509. (setq org-log-note-window-configuration (current-window-configuration))
  9510. (delete-other-windows)
  9511. (move-marker org-log-note-return-to (point))
  9512. (switch-to-buffer (marker-buffer org-log-note-marker))
  9513. (goto-char org-log-note-marker)
  9514. (org-switch-to-buffer-other-window "*Org Note*")
  9515. (erase-buffer)
  9516. (if (memq org-log-note-how '(time state))
  9517. (let (current-prefix-arg) (org-store-log-note))
  9518. (let ((org-inhibit-startup t)) (org-mode))
  9519. (insert (format "# Insert note for %s.
  9520. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9521. (cond
  9522. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9523. ((eq org-log-note-purpose 'done) "closed todo item")
  9524. ((eq org-log-note-purpose 'state)
  9525. (format "state change from \"%s\" to \"%s\""
  9526. (or org-log-note-previous-state "")
  9527. (or org-log-note-state "")))
  9528. ((eq org-log-note-purpose 'note)
  9529. "this entry")
  9530. (t (error "This should not happen")))))
  9531. (if org-log-note-extra (insert org-log-note-extra))
  9532. (org-set-local 'org-finish-function 'org-store-log-note)))
  9533. (defvar org-note-abort nil) ; dynamically scoped
  9534. (defun org-store-log-note ()
  9535. "Finish taking a log note, and insert it to where it belongs."
  9536. (let ((txt (buffer-string))
  9537. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9538. lines ind)
  9539. (kill-buffer (current-buffer))
  9540. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9541. (setq txt (replace-match "" t t txt)))
  9542. (if (string-match "\\s-+\\'" txt)
  9543. (setq txt (replace-match "" t t txt)))
  9544. (setq lines (org-split-string txt "\n"))
  9545. (when (and note (string-match "\\S-" note))
  9546. (setq note
  9547. (org-replace-escapes
  9548. note
  9549. (list (cons "%u" (user-login-name))
  9550. (cons "%U" user-full-name)
  9551. (cons "%t" (format-time-string
  9552. (org-time-stamp-format 'long 'inactive)
  9553. (current-time)))
  9554. (cons "%s" (if org-log-note-state
  9555. (concat "\"" org-log-note-state "\"")
  9556. ""))
  9557. (cons "%S" (if org-log-note-previous-state
  9558. (concat "\"" org-log-note-previous-state "\"")
  9559. "\"\"")))))
  9560. (if lines (setq note (concat note " \\\\")))
  9561. (push note lines))
  9562. (when (or current-prefix-arg org-note-abort)
  9563. (when org-log-into-drawer
  9564. (org-remove-empty-drawer-at
  9565. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9566. org-log-note-marker))
  9567. (setq lines nil))
  9568. (when lines
  9569. (save-excursion
  9570. (set-buffer (marker-buffer org-log-note-marker))
  9571. (save-excursion
  9572. (goto-char org-log-note-marker)
  9573. (move-marker org-log-note-marker nil)
  9574. (end-of-line 1)
  9575. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9576. (insert "- " (pop lines))
  9577. (org-indent-line-function)
  9578. (beginning-of-line 1)
  9579. (looking-at "[ \t]*")
  9580. (setq ind (concat (match-string 0) " "))
  9581. (end-of-line 1)
  9582. (while lines (insert "\n" ind (pop lines)))
  9583. (message "Note stored")
  9584. (org-back-to-heading t)
  9585. (org-cycle-hide-drawers 'children)))))
  9586. (set-window-configuration org-log-note-window-configuration)
  9587. (with-current-buffer (marker-buffer org-log-note-return-to)
  9588. (goto-char org-log-note-return-to))
  9589. (move-marker org-log-note-return-to nil)
  9590. (and org-log-post-message (message "%s" org-log-post-message)))
  9591. (defun org-remove-empty-drawer-at (drawer pos)
  9592. "Remove an emptyr DARWER drawer at position POS.
  9593. POS may also be a marker."
  9594. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9595. (save-excursion
  9596. (save-restriction
  9597. (widen)
  9598. (goto-char pos)
  9599. (if (org-in-regexp
  9600. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9601. (replace-match ""))))))
  9602. (defun org-sparse-tree (&optional arg)
  9603. "Create a sparse tree, prompt for the details.
  9604. This command can create sparse trees. You first need to select the type
  9605. of match used to create the tree:
  9606. t Show entries with a specific TODO keyword.
  9607. m Show entries selected by a tags/property match.
  9608. p Enter a property name and its value (both with completion on existing
  9609. names/values) and show entries with that property.
  9610. r Show entries matching a regular expression.
  9611. d Show deadlines due within `org-deadline-warning-days'.
  9612. b Show deadlines and scheduled items before a date.
  9613. a Show deadlines and scheduled items after a date."
  9614. (interactive "P")
  9615. (let (ans kwd value)
  9616. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9617. (setq ans (read-char-exclusive))
  9618. (cond
  9619. ((equal ans ?d)
  9620. (call-interactively 'org-check-deadlines))
  9621. ((equal ans ?b)
  9622. (call-interactively 'org-check-before-date))
  9623. ((equal ans ?a)
  9624. (call-interactively 'org-check-after-date))
  9625. ((equal ans ?t)
  9626. (org-show-todo-tree '(4)))
  9627. ((member ans '(?T ?m))
  9628. (call-interactively 'org-match-sparse-tree))
  9629. ((member ans '(?p ?P))
  9630. (setq kwd (org-icompleting-read "Property: "
  9631. (mapcar 'list (org-buffer-property-keys))))
  9632. (setq value (org-icompleting-read "Value: "
  9633. (mapcar 'list (org-property-values kwd))))
  9634. (unless (string-match "\\`{.*}\\'" value)
  9635. (setq value (concat "\"" value "\"")))
  9636. (org-match-sparse-tree arg (concat kwd "=" value)))
  9637. ((member ans '(?r ?R ?/))
  9638. (call-interactively 'org-occur))
  9639. (t (error "No such sparse tree command \"%c\"" ans)))))
  9640. (defvar org-occur-highlights nil
  9641. "List of overlays used for occur matches.")
  9642. (make-variable-buffer-local 'org-occur-highlights)
  9643. (defvar org-occur-parameters nil
  9644. "Parameters of the active org-occur calls.
  9645. This is a list, each call to org-occur pushes as cons cell,
  9646. containing the regular expression and the callback, onto the list.
  9647. The list can contain several entries if `org-occur' has been called
  9648. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9649. will only contain one set of parameters. When the highlights are
  9650. removed (for example with `C-c C-c', or with the next edit (depending
  9651. on `org-remove-highlights-with-change'), this variable is emptied
  9652. as well.")
  9653. (make-variable-buffer-local 'org-occur-parameters)
  9654. (defun org-occur (regexp &optional keep-previous callback)
  9655. "Make a compact tree which shows all matches of REGEXP.
  9656. The tree will show the lines where the regexp matches, and all higher
  9657. headlines above the match. It will also show the heading after the match,
  9658. to make sure editing the matching entry is easy.
  9659. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9660. call to `org-occur' will be kept, to allow stacking of calls to this
  9661. command.
  9662. If CALLBACK is non-nil, it is a function which is called to confirm
  9663. that the match should indeed be shown."
  9664. (interactive "sRegexp: \nP")
  9665. (when (equal regexp "")
  9666. (error "Regexp cannot be empty"))
  9667. (unless keep-previous
  9668. (org-remove-occur-highlights nil nil t))
  9669. (push (cons regexp callback) org-occur-parameters)
  9670. (let ((cnt 0))
  9671. (save-excursion
  9672. (goto-char (point-min))
  9673. (if (or (not keep-previous) ; do not want to keep
  9674. (not org-occur-highlights)) ; no previous matches
  9675. ;; hide everything
  9676. (org-overview))
  9677. (while (re-search-forward regexp nil t)
  9678. (when (or (not callback)
  9679. (save-match-data (funcall callback)))
  9680. (setq cnt (1+ cnt))
  9681. (when org-highlight-sparse-tree-matches
  9682. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9683. (org-show-context 'occur-tree))))
  9684. (when org-remove-highlights-with-change
  9685. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9686. nil 'local))
  9687. (unless org-sparse-tree-open-archived-trees
  9688. (org-hide-archived-subtrees (point-min) (point-max)))
  9689. (run-hooks 'org-occur-hook)
  9690. (if (interactive-p)
  9691. (message "%d match(es) for regexp %s" cnt regexp))
  9692. cnt))
  9693. (defun org-show-context (&optional key)
  9694. "Make sure point and context and visible.
  9695. How much context is shown depends upon the variables
  9696. `org-show-hierarchy-above', `org-show-following-heading'. and
  9697. `org-show-siblings'."
  9698. (let ((heading-p (org-on-heading-p t))
  9699. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9700. (following-p (org-get-alist-option org-show-following-heading key))
  9701. (entry-p (org-get-alist-option org-show-entry-below key))
  9702. (siblings-p (org-get-alist-option org-show-siblings key)))
  9703. (catch 'exit
  9704. ;; Show heading or entry text
  9705. (if (and heading-p (not entry-p))
  9706. (org-flag-heading nil) ; only show the heading
  9707. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9708. (org-show-hidden-entry))) ; show entire entry
  9709. (when following-p
  9710. ;; Show next sibling, or heading below text
  9711. (save-excursion
  9712. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9713. (org-flag-heading nil))))
  9714. (when siblings-p (org-show-siblings))
  9715. (when hierarchy-p
  9716. ;; show all higher headings, possibly with siblings
  9717. (save-excursion
  9718. (while (and (condition-case nil
  9719. (progn (org-up-heading-all 1) t)
  9720. (error nil))
  9721. (not (bobp)))
  9722. (org-flag-heading nil)
  9723. (when siblings-p (org-show-siblings))))))))
  9724. (defun org-reveal (&optional siblings)
  9725. "Show current entry, hierarchy above it, and the following headline.
  9726. This can be used to show a consistent set of context around locations
  9727. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9728. not t for the search context.
  9729. With optional argument SIBLINGS, on each level of the hierarchy all
  9730. siblings are shown. This repairs the tree structure to what it would
  9731. look like when opened with hierarchical calls to `org-cycle'."
  9732. (interactive "P")
  9733. (let ((org-show-hierarchy-above t)
  9734. (org-show-following-heading t)
  9735. (org-show-siblings (if siblings t org-show-siblings)))
  9736. (org-show-context nil)))
  9737. (defun org-highlight-new-match (beg end)
  9738. "Highlight from BEG to END and mark the highlight is an occur headline."
  9739. (let ((ov (org-make-overlay beg end)))
  9740. (org-overlay-put ov 'face 'secondary-selection)
  9741. (push ov org-occur-highlights)))
  9742. (defun org-remove-occur-highlights (&optional beg end noremove)
  9743. "Remove the occur highlights from the buffer.
  9744. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9745. from the `before-change-functions' in the current buffer."
  9746. (interactive)
  9747. (unless org-inhibit-highlight-removal
  9748. (mapc 'org-delete-overlay org-occur-highlights)
  9749. (setq org-occur-highlights nil)
  9750. (setq org-occur-parameters nil)
  9751. (unless noremove
  9752. (remove-hook 'before-change-functions
  9753. 'org-remove-occur-highlights 'local))))
  9754. ;;;; Priorities
  9755. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9756. "Regular expression matching the priority indicator.")
  9757. (defvar org-remove-priority-next-time nil)
  9758. (defun org-priority-up ()
  9759. "Increase the priority of the current item."
  9760. (interactive)
  9761. (org-priority 'up))
  9762. (defun org-priority-down ()
  9763. "Decrease the priority of the current item."
  9764. (interactive)
  9765. (org-priority 'down))
  9766. (defun org-priority (&optional action)
  9767. "Change the priority of an item by ARG.
  9768. ACTION can be `set', `up', `down', or a character."
  9769. (interactive)
  9770. (unless org-enable-priority-commands
  9771. (error "Priority commands are disabled"))
  9772. (setq action (or action 'set))
  9773. (let (current new news have remove)
  9774. (save-excursion
  9775. (org-back-to-heading t)
  9776. (if (looking-at org-priority-regexp)
  9777. (setq current (string-to-char (match-string 2))
  9778. have t)
  9779. (setq current org-default-priority))
  9780. (cond
  9781. ((or (eq action 'set)
  9782. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9783. (if (not (eq action 'set))
  9784. (setq new action)
  9785. (message "Priority %c-%c, SPC to remove: "
  9786. org-highest-priority org-lowest-priority)
  9787. (setq new (read-char-exclusive)))
  9788. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9789. (= (upcase org-lowest-priority) org-lowest-priority))
  9790. (setq new (upcase new)))
  9791. (cond ((equal new ?\ ) (setq remove t))
  9792. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9793. (error "Priority must be between `%c' and `%c'"
  9794. org-highest-priority org-lowest-priority))))
  9795. ((eq action 'up)
  9796. (if (and (not have) (eq last-command this-command))
  9797. (setq new org-lowest-priority)
  9798. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9799. org-default-priority (1- current)))))
  9800. ((eq action 'down)
  9801. (if (and (not have) (eq last-command this-command))
  9802. (setq new org-highest-priority)
  9803. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9804. org-default-priority (1+ current)))))
  9805. (t (error "Invalid action")))
  9806. (if (or (< (upcase new) org-highest-priority)
  9807. (> (upcase new) org-lowest-priority))
  9808. (setq remove t))
  9809. (setq news (format "%c" new))
  9810. (if have
  9811. (if remove
  9812. (replace-match "" t t nil 1)
  9813. (replace-match news t t nil 2))
  9814. (if remove
  9815. (error "No priority cookie found in line")
  9816. (let ((case-fold-search nil))
  9817. (looking-at org-todo-line-regexp))
  9818. (if (match-end 2)
  9819. (progn
  9820. (goto-char (match-end 2))
  9821. (insert " [#" news "]"))
  9822. (goto-char (match-beginning 3))
  9823. (insert "[#" news "] "))))
  9824. (org-preserve-lc (org-set-tags nil 'align)))
  9825. (if remove
  9826. (message "Priority removed")
  9827. (message "Priority of current item set to %s" news))))
  9828. (defun org-get-priority (s)
  9829. "Find priority cookie and return priority."
  9830. (save-match-data
  9831. (if (not (string-match org-priority-regexp s))
  9832. (* 1000 (- org-lowest-priority org-default-priority))
  9833. (* 1000 (- org-lowest-priority
  9834. (string-to-char (match-string 2 s)))))))
  9835. ;;;; Tags
  9836. (defvar org-agenda-archives-mode)
  9837. (defvar org-map-continue-from nil
  9838. "Position from where mapping should continue.
  9839. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9840. (defvar org-scanner-tags nil
  9841. "The current tag list while the tags scanner is running.")
  9842. (defvar org-trust-scanner-tags nil
  9843. "Should `org-get-tags-at' use the tags fro the scanner.
  9844. This is for internal dynamical scoping only.
  9845. When this is non-nil, the function `org-get-tags-at' will return the value
  9846. of `org-scanner-tags' instead of building the list by itself. This
  9847. can lead to large speed-ups when the tags scanner is used in a file with
  9848. many entries, and when the list of tags is retrieved, for example to
  9849. obtain a list of properties. Building the tags list for each entry in such
  9850. a file becomes an N^2 operation - but with this variable set, it scales
  9851. as N.")
  9852. (defun org-scan-tags (action matcher &optional todo-only)
  9853. "Scan headline tags with inheritance and produce output ACTION.
  9854. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9855. or `agenda' to produce an entry list for an agenda view. It can also be
  9856. a Lisp form or a function that should be called at each matched headline, in
  9857. this case the return value is a list of all return values from these calls.
  9858. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9859. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9860. only lines with a TODO keyword are included in the output."
  9861. (require 'org-agenda)
  9862. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9863. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9864. (org-re
  9865. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9866. (props (list 'face 'default
  9867. 'done-face 'org-agenda-done
  9868. 'undone-face 'default
  9869. 'mouse-face 'highlight
  9870. 'org-not-done-regexp org-not-done-regexp
  9871. 'org-todo-regexp org-todo-regexp
  9872. 'keymap org-agenda-keymap
  9873. 'help-echo
  9874. (format "mouse-2 or RET jump to org file %s"
  9875. (abbreviate-file-name
  9876. (or (buffer-file-name (buffer-base-buffer))
  9877. (buffer-name (buffer-base-buffer)))))))
  9878. (case-fold-search nil)
  9879. (org-map-continue-from nil)
  9880. lspos tags tags-list
  9881. (tags-alist (list (cons 0 org-file-tags)))
  9882. (llast 0) rtn rtn1 level category i txt
  9883. todo marker entry priority)
  9884. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9885. (setq action (list 'lambda nil action)))
  9886. (save-excursion
  9887. (goto-char (point-min))
  9888. (when (eq action 'sparse-tree)
  9889. (org-overview)
  9890. (org-remove-occur-highlights))
  9891. (while (re-search-forward re nil t)
  9892. (catch :skip
  9893. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9894. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9895. (goto-char (setq lspos (match-beginning 0)))
  9896. (setq level (org-reduced-level (funcall outline-level))
  9897. category (org-get-category))
  9898. (setq i llast llast level)
  9899. ;; remove tag lists from same and sublevels
  9900. (while (>= i level)
  9901. (when (setq entry (assoc i tags-alist))
  9902. (setq tags-alist (delete entry tags-alist)))
  9903. (setq i (1- i)))
  9904. ;; add the next tags
  9905. (when tags
  9906. (setq tags (org-split-string tags ":")
  9907. tags-alist
  9908. (cons (cons level tags) tags-alist)))
  9909. ;; compile tags for current headline
  9910. (setq tags-list
  9911. (if org-use-tag-inheritance
  9912. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9913. tags)
  9914. org-scanner-tags tags-list)
  9915. (when org-use-tag-inheritance
  9916. (setcdr (car tags-alist)
  9917. (mapcar (lambda (x)
  9918. (setq x (copy-sequence x))
  9919. (org-add-prop-inherited x))
  9920. (cdar tags-alist))))
  9921. (when (and tags org-use-tag-inheritance
  9922. (or (not (eq t org-use-tag-inheritance))
  9923. org-tags-exclude-from-inheritance))
  9924. ;; selective inheritance, remove uninherited ones
  9925. (setcdr (car tags-alist)
  9926. (org-remove-uniherited-tags (cdar tags-alist))))
  9927. (when (and (or (not todo-only)
  9928. (and (member todo org-not-done-keywords)
  9929. (or (not org-agenda-tags-todo-honor-ignore-options)
  9930. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9931. (let ((case-fold-search t)) (eval matcher))
  9932. (or
  9933. (not (member org-archive-tag tags-list))
  9934. ;; we have an archive tag, should we use this anyway?
  9935. (or (not org-agenda-skip-archived-trees)
  9936. (and (eq action 'agenda) org-agenda-archives-mode))))
  9937. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9938. ;; select this headline
  9939. (cond
  9940. ((eq action 'sparse-tree)
  9941. (and org-highlight-sparse-tree-matches
  9942. (org-get-heading) (match-end 0)
  9943. (org-highlight-new-match
  9944. (match-beginning 0) (match-beginning 1)))
  9945. (org-show-context 'tags-tree))
  9946. ((eq action 'agenda)
  9947. (setq txt (org-format-agenda-item
  9948. ""
  9949. (concat
  9950. (if (eq org-tags-match-list-sublevels 'indented)
  9951. (make-string (1- level) ?.) "")
  9952. (org-get-heading))
  9953. category
  9954. tags-list
  9955. )
  9956. priority (org-get-priority txt))
  9957. (goto-char lspos)
  9958. (setq marker (org-agenda-new-marker))
  9959. (org-add-props txt props
  9960. 'org-marker marker 'org-hd-marker marker 'org-category category
  9961. 'todo-state todo
  9962. 'priority priority 'type "tagsmatch")
  9963. (push txt rtn))
  9964. ((functionp action)
  9965. (setq org-map-continue-from nil)
  9966. (save-excursion
  9967. (setq rtn1 (funcall action))
  9968. (push rtn1 rtn)))
  9969. (t (error "Invalid action")))
  9970. ;; if we are to skip sublevels, jump to end of subtree
  9971. (unless org-tags-match-list-sublevels
  9972. (org-end-of-subtree t)
  9973. (backward-char 1))))
  9974. ;; Get the correct position from where to continue
  9975. (if org-map-continue-from
  9976. (goto-char org-map-continue-from)
  9977. (and (= (point) lspos) (end-of-line 1)))))
  9978. (when (and (eq action 'sparse-tree)
  9979. (not org-sparse-tree-open-archived-trees))
  9980. (org-hide-archived-subtrees (point-min) (point-max)))
  9981. (nreverse rtn)))
  9982. (defun org-remove-uniherited-tags (tags)
  9983. "Remove all tags that are not inherited from the list TAGS."
  9984. (cond
  9985. ((eq org-use-tag-inheritance t)
  9986. (if org-tags-exclude-from-inheritance
  9987. (org-delete-all org-tags-exclude-from-inheritance tags)
  9988. tags))
  9989. ((not org-use-tag-inheritance) nil)
  9990. ((stringp org-use-tag-inheritance)
  9991. (delq nil (mapcar
  9992. (lambda (x)
  9993. (if (and (string-match org-use-tag-inheritance x)
  9994. (not (member x org-tags-exclude-from-inheritance)))
  9995. x nil))
  9996. tags)))
  9997. ((listp org-use-tag-inheritance)
  9998. (delq nil (mapcar
  9999. (lambda (x)
  10000. (if (member x org-use-tag-inheritance) x nil))
  10001. tags)))))
  10002. (defvar todo-only) ;; dynamically scoped
  10003. (defun org-match-sparse-tree (&optional todo-only match)
  10004. "Create a sparse tree according to tags string MATCH.
  10005. MATCH can contain positive and negative selection of tags, like
  10006. \"+WORK+URGENT-WITHBOSS\".
  10007. If optional argument TODO-ONLY is non-nil, only select lines that are
  10008. also TODO lines."
  10009. (interactive "P")
  10010. (org-prepare-agenda-buffers (list (current-buffer)))
  10011. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10012. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10013. (defvar org-cached-props nil)
  10014. (defun org-cached-entry-get (pom property)
  10015. (if (or (eq t org-use-property-inheritance)
  10016. (and (stringp org-use-property-inheritance)
  10017. (string-match org-use-property-inheritance property))
  10018. (and (listp org-use-property-inheritance)
  10019. (member property org-use-property-inheritance)))
  10020. ;; Caching is not possible, check it directly
  10021. (org-entry-get pom property 'inherit)
  10022. ;; Get all properties, so that we can do complicated checks easily
  10023. (cdr (assoc property (or org-cached-props
  10024. (setq org-cached-props
  10025. (org-entry-properties pom)))))))
  10026. (defun org-global-tags-completion-table (&optional files)
  10027. "Return the list of all tags in all agenda buffer/files."
  10028. (save-excursion
  10029. (org-uniquify
  10030. (delq nil
  10031. (apply 'append
  10032. (mapcar
  10033. (lambda (file)
  10034. (set-buffer (find-file-noselect file))
  10035. (append (org-get-buffer-tags)
  10036. (mapcar (lambda (x) (if (stringp (car-safe x))
  10037. (list (car-safe x)) nil))
  10038. org-tag-alist)))
  10039. (if (and files (car files))
  10040. files
  10041. (org-agenda-files))))))))
  10042. (defun org-make-tags-matcher (match)
  10043. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10044. ;; todo-only is scoped dynamically into this function, and the function
  10045. ;; may change it if the matcher asks for it.
  10046. (unless match
  10047. ;; Get a new match request, with completion
  10048. (let ((org-last-tags-completion-table
  10049. (org-global-tags-completion-table)))
  10050. (setq match (org-completing-read-no-i
  10051. "Match: " 'org-tags-completion-function nil nil nil
  10052. 'org-tags-history))))
  10053. ;; Parse the string and create a lisp form
  10054. (let ((match0 match)
  10055. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10056. minus tag mm
  10057. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10058. orterms term orlist re-p str-p level-p level-op time-p
  10059. prop-p pn pv po cat-p gv rest)
  10060. (if (string-match "/+" match)
  10061. ;; match contains also a todo-matching request
  10062. (progn
  10063. (setq tagsmatch (substring match 0 (match-beginning 0))
  10064. todomatch (substring match (match-end 0)))
  10065. (if (string-match "^!" todomatch)
  10066. (setq todo-only t todomatch (substring todomatch 1)))
  10067. (if (string-match "^\\s-*$" todomatch)
  10068. (setq todomatch nil)))
  10069. ;; only matching tags
  10070. (setq tagsmatch match todomatch nil))
  10071. ;; Make the tags matcher
  10072. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10073. (setq tagsmatcher t)
  10074. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10075. (while (setq term (pop orterms))
  10076. (while (and (equal (substring term -1) "\\") orterms)
  10077. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10078. (while (string-match re term)
  10079. (setq rest (substring term (match-end 0))
  10080. minus (and (match-end 1)
  10081. (equal (match-string 1 term) "-"))
  10082. tag (match-string 2 term)
  10083. re-p (equal (string-to-char tag) ?{)
  10084. level-p (match-end 4)
  10085. prop-p (match-end 5)
  10086. mm (cond
  10087. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10088. (level-p
  10089. (setq level-op (org-op-to-function (match-string 3 term)))
  10090. `(,level-op level ,(string-to-number
  10091. (match-string 4 term))))
  10092. (prop-p
  10093. (setq pn (match-string 5 term)
  10094. po (match-string 6 term)
  10095. pv (match-string 7 term)
  10096. cat-p (equal pn "CATEGORY")
  10097. re-p (equal (string-to-char pv) ?{)
  10098. str-p (equal (string-to-char pv) ?\")
  10099. time-p (save-match-data
  10100. (string-match "^\"[[<].*[]>]\"$" pv))
  10101. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10102. (if time-p (setq pv (org-matcher-time pv)))
  10103. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10104. (cond
  10105. ((equal pn "CATEGORY")
  10106. (setq gv '(get-text-property (point) 'org-category)))
  10107. ((equal pn "TODO")
  10108. (setq gv 'todo))
  10109. (t
  10110. (setq gv `(org-cached-entry-get nil ,pn))))
  10111. (if re-p
  10112. (if (eq po 'org<>)
  10113. `(not (string-match ,pv (or ,gv "")))
  10114. `(string-match ,pv (or ,gv "")))
  10115. (if str-p
  10116. `(,po (or ,gv "") ,pv)
  10117. `(,po (string-to-number (or ,gv ""))
  10118. ,(string-to-number pv) ))))
  10119. (t `(member ,tag tags-list)))
  10120. mm (if minus (list 'not mm) mm)
  10121. term rest)
  10122. (push mm tagsmatcher))
  10123. (push (if (> (length tagsmatcher) 1)
  10124. (cons 'and tagsmatcher)
  10125. (car tagsmatcher))
  10126. orlist)
  10127. (setq tagsmatcher nil))
  10128. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10129. (setq tagsmatcher
  10130. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10131. ;; Make the todo matcher
  10132. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10133. (setq todomatcher t)
  10134. (setq orterms (org-split-string todomatch "|") orlist nil)
  10135. (while (setq term (pop orterms))
  10136. (while (string-match re term)
  10137. (setq minus (and (match-end 1)
  10138. (equal (match-string 1 term) "-"))
  10139. kwd (match-string 2 term)
  10140. re-p (equal (string-to-char kwd) ?{)
  10141. term (substring term (match-end 0))
  10142. mm (if re-p
  10143. `(string-match ,(substring kwd 1 -1) todo)
  10144. (list 'equal 'todo kwd))
  10145. mm (if minus (list 'not mm) mm))
  10146. (push mm todomatcher))
  10147. (push (if (> (length todomatcher) 1)
  10148. (cons 'and todomatcher)
  10149. (car todomatcher))
  10150. orlist)
  10151. (setq todomatcher nil))
  10152. (setq todomatcher (if (> (length orlist) 1)
  10153. (cons 'or orlist) (car orlist))))
  10154. ;; Return the string and lisp forms of the matcher
  10155. (setq matcher (if todomatcher
  10156. (list 'and tagsmatcher todomatcher)
  10157. tagsmatcher))
  10158. (cons match0 matcher)))
  10159. (defun org-op-to-function (op &optional stringp)
  10160. "Turn an operator into the appropriate function."
  10161. (setq op
  10162. (cond
  10163. ((equal op "<" ) '(< string< org-time<))
  10164. ((equal op ">" ) '(> org-string> org-time>))
  10165. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10166. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10167. ((member op '("=" "==")) '(= string= org-time=))
  10168. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10169. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10170. (defun org<> (a b) (not (= a b)))
  10171. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10172. (defun org-string>= (a b) (not (string< a b)))
  10173. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10174. (defun org-string<> (a b) (not (string= a b)))
  10175. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= 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) (org<> a b)))
  10181. (defun org-2ft (s)
  10182. "Convert S to a floating point time.
  10183. If S is already a number, just return it. If it is a string, parse
  10184. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10185. (cond
  10186. ((numberp s) s)
  10187. ((stringp s)
  10188. (condition-case nil
  10189. (float-time (apply 'encode-time (org-parse-time-string s)))
  10190. (error 0.)))
  10191. (t 0.)))
  10192. (defun org-time-today ()
  10193. "Time in seconds today at 0:00.
  10194. Returns the float number of seconds since the beginning of the
  10195. epoch to the beginning of today (00:00)."
  10196. (float-time (apply 'encode-time
  10197. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10198. (defun org-matcher-time (s)
  10199. "Interpret a time comparison value."
  10200. (save-match-data
  10201. (cond
  10202. ((string= s "<now>") (float-time))
  10203. ((string= s "<today>") (org-time-today))
  10204. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10205. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10206. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10207. (+ (org-time-today)
  10208. (* (string-to-number (match-string 1 s))
  10209. (cdr (assoc (match-string 2 s)
  10210. '(("d" . 86400.0) ("w" . 604800.0)
  10211. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10212. (t (org-2ft s)))))
  10213. (defun org-match-any-p (re list)
  10214. "Does re match any element of list?"
  10215. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10216. (delq nil list))
  10217. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10218. (defvar org-tags-overlay (org-make-overlay 1 1))
  10219. (org-detach-overlay org-tags-overlay)
  10220. (defun org-get-local-tags-at (&optional pos)
  10221. "Get a list of tags defined in the current headline."
  10222. (org-get-tags-at pos 'local))
  10223. (defun org-get-local-tags ()
  10224. "Get a list of tags defined in the current headline."
  10225. (org-get-tags-at nil 'local))
  10226. (defun org-get-tags-at (&optional pos local)
  10227. "Get a list of all headline tags applicable at POS.
  10228. POS defaults to point. If tags are inherited, the list contains
  10229. the targets in the same sequence as the headlines appear, i.e.
  10230. the tags of the current headline come last.
  10231. When LOCAL is non-nil, only return tags from the current headline,
  10232. ignore inherited ones."
  10233. (interactive)
  10234. (if (and org-trust-scanner-tags
  10235. (or (not pos) (equal pos (point)))
  10236. (not local))
  10237. org-scanner-tags
  10238. (let (tags ltags lastpos parent)
  10239. (save-excursion
  10240. (save-restriction
  10241. (widen)
  10242. (goto-char (or pos (point)))
  10243. (save-match-data
  10244. (catch 'done
  10245. (condition-case nil
  10246. (progn
  10247. (org-back-to-heading t)
  10248. (while (not (equal lastpos (point)))
  10249. (setq lastpos (point))
  10250. (when (looking-at
  10251. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10252. (setq ltags (org-split-string
  10253. (org-match-string-no-properties 1) ":"))
  10254. (when parent
  10255. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10256. (setq tags (append
  10257. (if parent
  10258. (org-remove-uniherited-tags ltags)
  10259. ltags)
  10260. tags)))
  10261. (or org-use-tag-inheritance (throw 'done t))
  10262. (if local (throw 'done t))
  10263. (or (org-up-heading-safe) (error nil))
  10264. (setq parent t)))
  10265. (error nil)))))
  10266. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10267. (defun org-add-prop-inherited (s)
  10268. (add-text-properties 0 (length s) '(inherited t) s)
  10269. s)
  10270. (defun org-toggle-tag (tag &optional onoff)
  10271. "Toggle the tag TAG for the current line.
  10272. If ONOFF is `on' or `off', don't toggle but set to this state."
  10273. (let (res current)
  10274. (save-excursion
  10275. (org-back-to-heading t)
  10276. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10277. (point-at-eol) t)
  10278. (progn
  10279. (setq current (match-string 1))
  10280. (replace-match ""))
  10281. (setq current ""))
  10282. (setq current (nreverse (org-split-string current ":")))
  10283. (cond
  10284. ((eq onoff 'on)
  10285. (setq res t)
  10286. (or (member tag current) (push tag current)))
  10287. ((eq onoff 'off)
  10288. (or (not (member tag current)) (setq current (delete tag current))))
  10289. (t (if (member tag current)
  10290. (setq current (delete tag current))
  10291. (setq res t)
  10292. (push tag current))))
  10293. (end-of-line 1)
  10294. (if current
  10295. (progn
  10296. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10297. (org-set-tags nil t))
  10298. (delete-horizontal-space))
  10299. (run-hooks 'org-after-tags-change-hook))
  10300. res))
  10301. (defun org-align-tags-here (to-col)
  10302. ;; Assumes that this is a headline
  10303. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10304. (beginning-of-line 1)
  10305. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10306. (< pos (match-beginning 2)))
  10307. (progn
  10308. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10309. (goto-char (match-beginning 1))
  10310. (insert " ")
  10311. (delete-region (point) (1+ (match-beginning 2)))
  10312. (setq ncol (max (1+ (current-column))
  10313. (1+ col)
  10314. (if (> to-col 0)
  10315. to-col
  10316. (- (abs to-col) tags-l))))
  10317. (setq p (point))
  10318. (insert (make-string (- ncol (current-column)) ?\ ))
  10319. (setq ncol (current-column))
  10320. (when indent-tabs-mode (tabify p (point-at-eol)))
  10321. (org-move-to-column (min ncol col) t))
  10322. (goto-char pos))))
  10323. (defun org-set-tags-command (&optional arg just-align)
  10324. "Call the set-tags command for the current entry."
  10325. (interactive "P")
  10326. (if (org-on-heading-p)
  10327. (org-set-tags arg just-align)
  10328. (save-excursion
  10329. (org-back-to-heading t)
  10330. (org-set-tags arg just-align))))
  10331. (defun org-set-tags-to (data)
  10332. "Set the tags of the current entry to DATA, replacing the current tags.
  10333. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10334. If DATA is nil or the empty string, any tags will be removed."
  10335. (interactive "sTags: ")
  10336. (setq data
  10337. (cond
  10338. ((eq data nil) "")
  10339. ((equal data "") "")
  10340. ((stringp data)
  10341. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10342. ":"))
  10343. ((listp data)
  10344. (concat ":" (mapconcat 'identity data ":") ":"))
  10345. (t nil)))
  10346. (when data
  10347. (save-excursion
  10348. (org-back-to-heading t)
  10349. (when (looking-at org-complex-heading-regexp)
  10350. (if (match-end 5)
  10351. (progn
  10352. (goto-char (match-beginning 5))
  10353. (insert data)
  10354. (delete-region (point) (point-at-eol))
  10355. (org-set-tags nil 'align))
  10356. (goto-char (point-at-eol))
  10357. (insert " " data)
  10358. (org-set-tags nil 'align)))
  10359. (beginning-of-line 1)
  10360. (if (looking-at ".*?\\([ \t]+\\)$")
  10361. (delete-region (match-beginning 1) (match-end 1))))))
  10362. (defun org-set-tags (&optional arg just-align)
  10363. "Set the tags for the current headline.
  10364. With prefix ARG, realign all tags in headings in the current buffer."
  10365. (interactive "P")
  10366. (let* ((re (concat "^" outline-regexp))
  10367. (current (org-get-tags-string))
  10368. (col (current-column))
  10369. (org-setting-tags t)
  10370. table current-tags inherited-tags ; computed below when needed
  10371. tags p0 c0 c1 rpl)
  10372. (if arg
  10373. (save-excursion
  10374. (goto-char (point-min))
  10375. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10376. (while (re-search-forward re nil t)
  10377. (org-set-tags nil t)
  10378. (end-of-line 1)))
  10379. (message "All tags realigned to column %d" org-tags-column))
  10380. (if just-align
  10381. (setq tags current)
  10382. ;; Get a new set of tags from the user
  10383. (save-excursion
  10384. (setq table (append org-tag-persistent-alist
  10385. (or org-tag-alist (org-get-buffer-tags)))
  10386. org-last-tags-completion-table table
  10387. current-tags (org-split-string current ":")
  10388. inherited-tags (nreverse
  10389. (nthcdr (length current-tags)
  10390. (nreverse (org-get-tags-at))))
  10391. tags
  10392. (if (or (eq t org-use-fast-tag-selection)
  10393. (and org-use-fast-tag-selection
  10394. (delq nil (mapcar 'cdr table))))
  10395. (org-fast-tag-selection
  10396. current-tags inherited-tags table
  10397. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10398. (let ((org-add-colon-after-tag-completion t))
  10399. (org-trim
  10400. (org-without-partial-completion
  10401. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10402. nil nil current 'org-tags-history)))))))
  10403. (while (string-match "[-+&]+" tags)
  10404. ;; No boolean logic, just a list
  10405. (setq tags (replace-match ":" t t tags))))
  10406. (if org-tags-sort-function
  10407. (setq tags (mapconcat 'identity
  10408. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10409. org-tags-sort-function) ":")))
  10410. (if (string-match "\\`[\t ]*\\'" tags)
  10411. (setq tags "")
  10412. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10413. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10414. ;; Insert new tags at the correct column
  10415. (beginning-of-line 1)
  10416. (cond
  10417. ((and (equal current "") (equal tags "")))
  10418. ((re-search-forward
  10419. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10420. (point-at-eol) t)
  10421. (if (equal tags "")
  10422. (setq rpl "")
  10423. (goto-char (match-beginning 0))
  10424. (setq c0 (current-column) p0 (point)
  10425. c1 (max (1+ c0) (if (> org-tags-column 0)
  10426. org-tags-column
  10427. (- (- org-tags-column) (length tags))))
  10428. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10429. (replace-match rpl t t)
  10430. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10431. tags)
  10432. (t (error "Tags alignment failed")))
  10433. (org-move-to-column col)
  10434. (unless just-align
  10435. (run-hooks 'org-after-tags-change-hook)))))
  10436. (defun org-change-tag-in-region (beg end tag off)
  10437. "Add or remove TAG for each entry in the region.
  10438. This works in the agenda, and also in an org-mode buffer."
  10439. (interactive
  10440. (list (region-beginning) (region-end)
  10441. (let ((org-last-tags-completion-table
  10442. (if (org-mode-p)
  10443. (org-get-buffer-tags)
  10444. (org-global-tags-completion-table))))
  10445. (org-icompleting-read
  10446. "Tag: " 'org-tags-completion-function nil nil nil
  10447. 'org-tags-history))
  10448. (progn
  10449. (message "[s]et or [r]emove? ")
  10450. (equal (read-char-exclusive) ?r))))
  10451. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10452. (let ((agendap (equal major-mode 'org-agenda-mode))
  10453. l1 l2 m buf pos newhead (cnt 0))
  10454. (goto-char end)
  10455. (setq l2 (1- (org-current-line)))
  10456. (goto-char beg)
  10457. (setq l1 (org-current-line))
  10458. (loop for l from l1 to l2 do
  10459. (org-goto-line l)
  10460. (setq m (get-text-property (point) 'org-hd-marker))
  10461. (when (or (and (org-mode-p) (org-on-heading-p))
  10462. (and agendap m))
  10463. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10464. pos (if agendap m (point)))
  10465. (with-current-buffer buf
  10466. (save-excursion
  10467. (save-restriction
  10468. (goto-char pos)
  10469. (setq cnt (1+ cnt))
  10470. (org-toggle-tag tag (if off 'off 'on))
  10471. (setq newhead (org-get-heading)))))
  10472. (and agendap (org-agenda-change-all-lines newhead m))))
  10473. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10474. (defun org-tags-completion-function (string predicate &optional flag)
  10475. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10476. (confirm (lambda (x) (stringp (car x)))))
  10477. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10478. (setq s1 (match-string 1 string)
  10479. s2 (match-string 2 string))
  10480. (setq s1 "" s2 string))
  10481. (cond
  10482. ((eq flag nil)
  10483. ;; try completion
  10484. (setq rtn (try-completion s2 ctable confirm))
  10485. (if (stringp rtn)
  10486. (setq rtn
  10487. (concat s1 s2 (substring rtn (length s2))
  10488. (if (and org-add-colon-after-tag-completion
  10489. (assoc rtn ctable))
  10490. ":" ""))))
  10491. rtn)
  10492. ((eq flag t)
  10493. ;; all-completions
  10494. (all-completions s2 ctable confirm)
  10495. )
  10496. ((eq flag 'lambda)
  10497. ;; exact match?
  10498. (assoc s2 ctable)))
  10499. ))
  10500. (defun org-fast-tag-insert (kwd tags face &optional end)
  10501. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10502. (insert (format "%-12s" (concat kwd ":"))
  10503. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10504. (or end "")))
  10505. (defun org-fast-tag-show-exit (flag)
  10506. (save-excursion
  10507. (org-goto-line 3)
  10508. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10509. (replace-match ""))
  10510. (when flag
  10511. (end-of-line 1)
  10512. (org-move-to-column (- (window-width) 19) t)
  10513. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10514. (defun org-set-current-tags-overlay (current prefix)
  10515. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10516. (if (featurep 'xemacs)
  10517. (org-overlay-display org-tags-overlay (concat prefix s)
  10518. 'secondary-selection)
  10519. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10520. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10521. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10522. "Fast tag selection with single keys.
  10523. CURRENT is the current list of tags in the headline, INHERITED is the
  10524. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10525. possibly with grouping information. TODO-TABLE is a similar table with
  10526. TODO keywords, should these have keys assigned to them.
  10527. If the keys are nil, a-z are automatically assigned.
  10528. Returns the new tags string, or nil to not change the current settings."
  10529. (let* ((fulltable (append table todo-table))
  10530. (maxlen (apply 'max (mapcar
  10531. (lambda (x)
  10532. (if (stringp (car x)) (string-width (car x)) 0))
  10533. fulltable)))
  10534. (buf (current-buffer))
  10535. (expert (eq org-fast-tag-selection-single-key 'expert))
  10536. (buffer-tags nil)
  10537. (fwidth (+ maxlen 3 1 3))
  10538. (ncol (/ (- (window-width) 4) fwidth))
  10539. (i-face 'org-done)
  10540. (c-face 'org-todo)
  10541. tg cnt e c char c1 c2 ntable tbl rtn
  10542. ov-start ov-end ov-prefix
  10543. (exit-after-next org-fast-tag-selection-single-key)
  10544. (done-keywords org-done-keywords)
  10545. groups ingroup)
  10546. (save-excursion
  10547. (beginning-of-line 1)
  10548. (if (looking-at
  10549. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10550. (setq ov-start (match-beginning 1)
  10551. ov-end (match-end 1)
  10552. ov-prefix "")
  10553. (setq ov-start (1- (point-at-eol))
  10554. ov-end (1+ ov-start))
  10555. (skip-chars-forward "^\n\r")
  10556. (setq ov-prefix
  10557. (concat
  10558. (buffer-substring (1- (point)) (point))
  10559. (if (> (current-column) org-tags-column)
  10560. " "
  10561. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10562. (org-move-overlay org-tags-overlay ov-start ov-end)
  10563. (save-window-excursion
  10564. (if expert
  10565. (set-buffer (get-buffer-create " *Org tags*"))
  10566. (delete-other-windows)
  10567. (split-window-vertically)
  10568. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10569. (erase-buffer)
  10570. (org-set-local 'org-done-keywords done-keywords)
  10571. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10572. (org-fast-tag-insert "Current" current c-face "\n\n")
  10573. (org-fast-tag-show-exit exit-after-next)
  10574. (org-set-current-tags-overlay current ov-prefix)
  10575. (setq tbl fulltable char ?a cnt 0)
  10576. (while (setq e (pop tbl))
  10577. (cond
  10578. ((equal (car e) :startgroup)
  10579. (push '() groups) (setq ingroup t)
  10580. (when (not (= cnt 0))
  10581. (setq cnt 0)
  10582. (insert "\n"))
  10583. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  10584. ((equal (car e) :endgroup)
  10585. (setq ingroup nil cnt 0)
  10586. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  10587. ((equal e '(:newline))
  10588. (when (not (= cnt 0))
  10589. (setq cnt 0)
  10590. (insert "\n")
  10591. (setq e (car tbl))
  10592. (while (equal (car tbl) '(:newline))
  10593. (insert "\n")
  10594. (setq tbl (cdr tbl)))))
  10595. (t
  10596. (setq tg (copy-sequence (car e)) c2 nil)
  10597. (if (cdr e)
  10598. (setq c (cdr e))
  10599. ;; automatically assign a character.
  10600. (setq c1 (string-to-char
  10601. (downcase (substring
  10602. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10603. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10604. (while (or (rassoc char ntable) (rassoc char table))
  10605. (setq char (1+ char)))
  10606. (setq c2 c1))
  10607. (setq c (or c2 char)))
  10608. (if ingroup (push tg (car groups)))
  10609. (setq tg (org-add-props tg nil 'face
  10610. (cond
  10611. ((not (assoc tg table))
  10612. (org-get-todo-face tg))
  10613. ((member tg current) c-face)
  10614. ((member tg inherited) i-face)
  10615. (t nil))))
  10616. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10617. (insert "[" c "] " tg (make-string
  10618. (- fwidth 4 (length tg)) ?\ ))
  10619. (push (cons tg c) ntable)
  10620. (when (= (setq cnt (1+ cnt)) ncol)
  10621. (insert "\n")
  10622. (if ingroup (insert " "))
  10623. (setq cnt 0)))))
  10624. (setq ntable (nreverse ntable))
  10625. (insert "\n")
  10626. (goto-char (point-min))
  10627. (if (not expert) (org-fit-window-to-buffer))
  10628. (setq rtn
  10629. (catch 'exit
  10630. (while t
  10631. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  10632. (if (not groups) "no " "")
  10633. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10634. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10635. (cond
  10636. ((= c ?\r) (throw 'exit t))
  10637. ((= c ?!)
  10638. (setq groups (not groups))
  10639. (goto-char (point-min))
  10640. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10641. ((= c ?\C-c)
  10642. (if (not expert)
  10643. (org-fast-tag-show-exit
  10644. (setq exit-after-next (not exit-after-next)))
  10645. (setq expert nil)
  10646. (delete-other-windows)
  10647. (split-window-vertically)
  10648. (org-switch-to-buffer-other-window " *Org tags*")
  10649. (org-fit-window-to-buffer)))
  10650. ((or (= c ?\C-g)
  10651. (and (= c ?q) (not (rassoc c ntable))))
  10652. (org-detach-overlay org-tags-overlay)
  10653. (setq quit-flag t))
  10654. ((= c ?\ )
  10655. (setq current nil)
  10656. (if exit-after-next (setq exit-after-next 'now)))
  10657. ((= c ?\t)
  10658. (condition-case nil
  10659. (setq tg (org-icompleting-read
  10660. "Tag: "
  10661. (or buffer-tags
  10662. (with-current-buffer buf
  10663. (org-get-buffer-tags)))))
  10664. (quit (setq tg "")))
  10665. (when (string-match "\\S-" tg)
  10666. (add-to-list 'buffer-tags (list tg))
  10667. (if (member tg current)
  10668. (setq current (delete tg current))
  10669. (push tg current)))
  10670. (if exit-after-next (setq exit-after-next 'now)))
  10671. ((setq e (rassoc c todo-table) tg (car e))
  10672. (with-current-buffer buf
  10673. (save-excursion (org-todo tg)))
  10674. (if exit-after-next (setq exit-after-next 'now)))
  10675. ((setq e (rassoc c ntable) tg (car e))
  10676. (if (member tg current)
  10677. (setq current (delete tg current))
  10678. (loop for g in groups do
  10679. (if (member tg g)
  10680. (mapc (lambda (x)
  10681. (setq current (delete x current)))
  10682. g)))
  10683. (push tg current))
  10684. (if exit-after-next (setq exit-after-next 'now))))
  10685. ;; Create a sorted list
  10686. (setq current
  10687. (sort current
  10688. (lambda (a b)
  10689. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10690. (if (eq exit-after-next 'now) (throw 'exit t))
  10691. (goto-char (point-min))
  10692. (beginning-of-line 2)
  10693. (delete-region (point) (point-at-eol))
  10694. (org-fast-tag-insert "Current" current c-face)
  10695. (org-set-current-tags-overlay current ov-prefix)
  10696. (while (re-search-forward
  10697. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10698. (setq tg (match-string 1))
  10699. (add-text-properties
  10700. (match-beginning 1) (match-end 1)
  10701. (list 'face
  10702. (cond
  10703. ((member tg current) c-face)
  10704. ((member tg inherited) i-face)
  10705. (t (get-text-property (match-beginning 1) 'face))))))
  10706. (goto-char (point-min)))))
  10707. (org-detach-overlay org-tags-overlay)
  10708. (if rtn
  10709. (mapconcat 'identity current ":")
  10710. nil))))
  10711. (defun org-get-tags-string ()
  10712. "Get the TAGS string in the current headline."
  10713. (unless (org-on-heading-p t)
  10714. (error "Not on a heading"))
  10715. (save-excursion
  10716. (beginning-of-line 1)
  10717. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10718. (org-match-string-no-properties 1)
  10719. "")))
  10720. (defun org-get-tags ()
  10721. "Get the list of tags specified in the current headline."
  10722. (org-split-string (org-get-tags-string) ":"))
  10723. (defun org-get-buffer-tags ()
  10724. "Get a table of all tags used in the buffer, for completion."
  10725. (let (tags)
  10726. (save-excursion
  10727. (goto-char (point-min))
  10728. (while (re-search-forward
  10729. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10730. (when (equal (char-after (point-at-bol 0)) ?*)
  10731. (mapc (lambda (x) (add-to-list 'tags x))
  10732. (org-split-string (org-match-string-no-properties 1) ":")))))
  10733. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  10734. (mapcar 'list tags)))
  10735. ;;;; The mapping API
  10736. ;;;###autoload
  10737. (defun org-map-entries (func &optional match scope &rest skip)
  10738. "Call FUNC at each headline selected by MATCH in SCOPE.
  10739. FUNC is a function or a lisp form. The function will be called without
  10740. arguments, with the cursor positioned at the beginning of the headline.
  10741. The return values of all calls to the function will be collected and
  10742. returned as a list.
  10743. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10744. does not need to preserve point. After evaluation, the cursor will be
  10745. moved to the end of the line (presumably of the headline of the
  10746. processed entry) and search continues from there. Under some
  10747. circumstances, this may not produce the wanted results. For example,
  10748. if you have removed (e.g. archived) the current (sub)tree it could
  10749. mean that the next entry will be skipped entirely. In such cases, you
  10750. can specify the position from where search should continue by making
  10751. FUNC set the variable `org-map-continue-from' to the desired buffer
  10752. position.
  10753. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10754. Only headlines that are matched by this query will be considered during
  10755. the iteration. When MATCH is nil or t, all headlines will be
  10756. visited by the iteration.
  10757. SCOPE determines the scope of this command. It can be any of:
  10758. nil The current buffer, respecting the restriction if any
  10759. tree The subtree started with the entry at point
  10760. file The current buffer, without restriction
  10761. file-with-archives
  10762. The current buffer, and any archives associated with it
  10763. agenda All agenda files
  10764. agenda-with-archives
  10765. All agenda files with any archive files associated with them
  10766. \(file1 file2 ...)
  10767. If this is a list, all files in the list will be scanned
  10768. The remaining args are treated as settings for the skipping facilities of
  10769. the scanner. The following items can be given here:
  10770. archive skip trees with the archive tag.
  10771. comment skip trees with the COMMENT keyword
  10772. function or Emacs Lisp form:
  10773. will be used as value for `org-agenda-skip-function', so whenever
  10774. the function returns t, FUNC will not be called for that
  10775. entry and search will continue from the point where the
  10776. function leaves it.
  10777. If your function needs to retrieve the tags including inherited tags
  10778. at the *current* entry, you can use the value of the variable
  10779. `org-scanner-tags' which will be much faster than getting the value
  10780. with `org-get-tags-at'. If your function gets properties with
  10781. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10782. to t around the call to `org-entry-properties' to get the same speedup.
  10783. Note that if your function moves around to retrieve tags and properties at
  10784. a *different* entry, you cannot use these techniques."
  10785. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10786. (org-agenda-skip-archived-trees (memq 'archive skip))
  10787. (org-agenda-skip-comment-trees (memq 'comment skip))
  10788. (org-agenda-skip-function
  10789. (car (org-delete-all '(comment archive) skip)))
  10790. (org-tags-match-list-sublevels t)
  10791. matcher file res
  10792. org-todo-keywords-for-agenda
  10793. org-done-keywords-for-agenda
  10794. org-todo-keyword-alist-for-agenda
  10795. org-drawers-for-agenda
  10796. org-tag-alist-for-agenda)
  10797. (cond
  10798. ((eq match t) (setq matcher t))
  10799. ((eq match nil) (setq matcher t))
  10800. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10801. (save-excursion
  10802. (save-restriction
  10803. (when (eq scope 'tree)
  10804. (org-back-to-heading t)
  10805. (org-narrow-to-subtree)
  10806. (setq scope nil))
  10807. (if (not scope)
  10808. (progn
  10809. (org-prepare-agenda-buffers
  10810. (list (buffer-file-name (current-buffer))))
  10811. (setq res (org-scan-tags func matcher)))
  10812. ;; Get the right scope
  10813. (cond
  10814. ((and scope (listp scope) (symbolp (car scope)))
  10815. (setq scope (eval scope)))
  10816. ((eq scope 'agenda)
  10817. (setq scope (org-agenda-files t)))
  10818. ((eq scope 'agenda-with-archives)
  10819. (setq scope (org-agenda-files t))
  10820. (setq scope (org-add-archive-files scope)))
  10821. ((eq scope 'file)
  10822. (setq scope (list (buffer-file-name))))
  10823. ((eq scope 'file-with-archives)
  10824. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10825. (org-prepare-agenda-buffers scope)
  10826. (while (setq file (pop scope))
  10827. (with-current-buffer (org-find-base-buffer-visiting file)
  10828. (save-excursion
  10829. (save-restriction
  10830. (widen)
  10831. (goto-char (point-min))
  10832. (setq res (append res (org-scan-tags func matcher))))))))))
  10833. res))
  10834. ;;;; Properties
  10835. ;;; Setting and retrieving properties
  10836. (defconst org-special-properties
  10837. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10838. "TIMESTAMP" "TIMESTAMP_IA")
  10839. "The special properties valid in Org-mode.
  10840. These are properties that are not defined in the property drawer,
  10841. but in some other way.")
  10842. (defconst org-default-properties
  10843. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10844. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10845. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10846. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10847. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10848. "CLOCK_MODELINE_TOTAL" "STYLE")
  10849. "Some properties that are used by Org-mode for various purposes.
  10850. Being in this list makes sure that they are offered for completion.")
  10851. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10852. "Regular expression matching the first line of a property drawer.")
  10853. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10854. "Regular expression matching the first line of a property drawer.")
  10855. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10856. "Regular expression matching the first line of a property drawer.")
  10857. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10858. "Regular expression matching the first line of a property drawer.")
  10859. (defconst org-property-drawer-re
  10860. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10861. org-property-end-re "\\)\n?")
  10862. "Matches an entire property drawer.")
  10863. (defconst org-clock-drawer-re
  10864. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10865. org-property-end-re "\\)\n?")
  10866. "Matches an entire clock drawer.")
  10867. (defun org-property-action ()
  10868. "Do an action on properties."
  10869. (interactive)
  10870. (let (c)
  10871. (org-at-property-p)
  10872. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10873. (setq c (read-char-exclusive))
  10874. (cond
  10875. ((equal c ?s)
  10876. (call-interactively 'org-set-property))
  10877. ((equal c ?d)
  10878. (call-interactively 'org-delete-property))
  10879. ((equal c ?D)
  10880. (call-interactively 'org-delete-property-globally))
  10881. ((equal c ?c)
  10882. (call-interactively 'org-compute-property-at-point))
  10883. (t (error "No such property action %c" c)))))
  10884. (defun org-set-effort (&optional value)
  10885. "Set the effort property of the current entry.
  10886. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  10887. allowed value."
  10888. (interactive "P")
  10889. (if (equal value 0) (setq value 10))
  10890. (let* ((completion-ignore-case t)
  10891. (prop org-effort-property)
  10892. (cur (org-entry-get nil prop))
  10893. (allowed (org-property-get-allowed-values nil prop 'table))
  10894. (existing (mapcar 'list (org-property-values prop)))
  10895. (val (cond
  10896. ((stringp value) value)
  10897. ((and allowed (integerp value))
  10898. (or (car (nth (1- value) allowed))
  10899. (car (org-last allowed))))
  10900. (allowed
  10901. (org-completing-read "Value: " allowed nil 'req-match))
  10902. (t
  10903. (let (org-completion-use-ido org-completion-use-iswitchb)
  10904. (org-completing-read
  10905. (concat "Value " (if (and cur (string-match "\\S-" cur))
  10906. (concat "[" cur "]") "")
  10907. ": ")
  10908. existing nil nil "" nil cur))))))
  10909. (unless (equal (org-entry-get nil prop) val)
  10910. (org-entry-put nil prop val))
  10911. (message "%s is now %s" prop val)))
  10912. (defun org-at-property-p ()
  10913. "Is the cursor in a property line?"
  10914. ;; FIXME: Does not check if we are actually in the drawer.
  10915. ;; FIXME: also returns true on any drawers.....
  10916. ;; This is used by C-c C-c for property action.
  10917. (save-excursion
  10918. (beginning-of-line 1)
  10919. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10920. (defun org-get-property-block (&optional beg end force)
  10921. "Return the (beg . end) range of the body of the property drawer.
  10922. BEG and END can be beginning and end of subtree, if not given
  10923. they will be found.
  10924. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10925. (catch 'exit
  10926. (save-excursion
  10927. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10928. (end (or end (progn (outline-next-heading) (point)))))
  10929. (goto-char beg)
  10930. (if (re-search-forward org-property-start-re end t)
  10931. (setq beg (1+ (match-end 0)))
  10932. (if force
  10933. (save-excursion
  10934. (org-insert-property-drawer)
  10935. (setq end (progn (outline-next-heading) (point))))
  10936. (throw 'exit nil))
  10937. (goto-char beg)
  10938. (if (re-search-forward org-property-start-re end t)
  10939. (setq beg (1+ (match-end 0)))))
  10940. (if (re-search-forward org-property-end-re end t)
  10941. (setq end (match-beginning 0))
  10942. (or force (throw 'exit nil))
  10943. (goto-char beg)
  10944. (setq end beg)
  10945. (org-indent-line-function)
  10946. (insert ":END:\n"))
  10947. (cons beg end)))))
  10948. (defun org-entry-properties (&optional pom which)
  10949. "Get all properties of the entry at point-or-marker POM.
  10950. This includes the TODO keyword, the tags, time strings for deadline,
  10951. scheduled, and clocking, and any additional properties defined in the
  10952. entry. The return value is an alist, keys may occur multiple times
  10953. if the property key was used several times.
  10954. POM may also be nil, in which case the current entry is used.
  10955. If WHICH is nil or `all', get all properties. If WHICH is
  10956. `special' or `standard', only get that subclass."
  10957. (setq which (or which 'all))
  10958. (org-with-point-at pom
  10959. (let ((clockstr (substring org-clock-string 0 -1))
  10960. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10961. beg end range props sum-props key value string clocksum)
  10962. (save-excursion
  10963. (when (condition-case nil
  10964. (and (org-mode-p) (org-back-to-heading t))
  10965. (error nil))
  10966. (setq beg (point))
  10967. (setq sum-props (get-text-property (point) 'org-summaries))
  10968. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10969. (outline-next-heading)
  10970. (setq end (point))
  10971. (when (memq which '(all special))
  10972. ;; Get the special properties, like TODO and tags
  10973. (goto-char beg)
  10974. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10975. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10976. (when (looking-at org-priority-regexp)
  10977. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10978. (when (and (setq value (org-get-tags-string))
  10979. (string-match "\\S-" value))
  10980. (push (cons "TAGS" value) props))
  10981. (when (setq value (org-get-tags-at))
  10982. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10983. props))
  10984. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10985. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10986. string (if (equal key clockstr)
  10987. (org-no-properties
  10988. (org-trim
  10989. (buffer-substring
  10990. (match-beginning 3) (goto-char (point-at-eol)))))
  10991. (substring (org-match-string-no-properties 3) 1 -1)))
  10992. (unless key
  10993. (if (= (char-after (match-beginning 3)) ?\[)
  10994. (setq key "TIMESTAMP_IA")
  10995. (setq key "TIMESTAMP")))
  10996. (when (or (equal key clockstr) (not (assoc key props)))
  10997. (push (cons key string) props)))
  10998. )
  10999. (when (memq which '(all standard))
  11000. ;; Get the standard properties, like :PROP: ...
  11001. (setq range (org-get-property-block beg end))
  11002. (when range
  11003. (goto-char (car range))
  11004. (while (re-search-forward
  11005. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11006. (cdr range) t)
  11007. (setq key (org-match-string-no-properties 1)
  11008. value (org-trim (or (org-match-string-no-properties 2) "")))
  11009. (unless (member key excluded)
  11010. (push (cons key (or value "")) props)))))
  11011. (if clocksum
  11012. (push (cons "CLOCKSUM"
  11013. (org-columns-number-to-string (/ (float clocksum) 60.)
  11014. 'add_times))
  11015. props))
  11016. (unless (assoc "CATEGORY" props)
  11017. (setq value (or (org-get-category)
  11018. (progn (org-refresh-category-properties)
  11019. (org-get-category))))
  11020. (push (cons "CATEGORY" value) props))
  11021. (append sum-props (nreverse props)))))))
  11022. (defun org-entry-get (pom property &optional inherit)
  11023. "Get value of PROPERTY for entry at point-or-marker POM.
  11024. If INHERIT is non-nil and the entry does not have the property,
  11025. then also check higher levels of the hierarchy.
  11026. If INHERIT is the symbol `selective', use inheritance only if the setting
  11027. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11028. If the property is present but empty, the return value is the empty string.
  11029. If the property is not present at all, nil is returned."
  11030. (org-with-point-at pom
  11031. (if (and inherit (if (eq inherit 'selective)
  11032. (org-property-inherit-p property)
  11033. t))
  11034. (org-entry-get-with-inheritance property)
  11035. (if (member property org-special-properties)
  11036. ;; We need a special property. Use brute force, get all properties.
  11037. (cdr (assoc property (org-entry-properties nil 'special)))
  11038. (let ((range (org-get-property-block)))
  11039. (if (and range
  11040. (goto-char (car range))
  11041. (re-search-forward
  11042. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11043. (cdr range) t))
  11044. ;; Found the property, return it.
  11045. (if (match-end 1)
  11046. (org-match-string-no-properties 1)
  11047. "")))))))
  11048. (defun org-property-or-variable-value (var &optional inherit)
  11049. "Check if there is a property fixing the value of VAR.
  11050. If yes, return this value. If not, return the current value of the variable."
  11051. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11052. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11053. (read prop)
  11054. (symbol-value var))))
  11055. (defun org-entry-delete (pom property)
  11056. "Delete the property PROPERTY from entry at point-or-marker POM."
  11057. (org-with-point-at pom
  11058. (if (member property org-special-properties)
  11059. nil ; cannot delete these properties.
  11060. (let ((range (org-get-property-block)))
  11061. (if (and range
  11062. (goto-char (car range))
  11063. (re-search-forward
  11064. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11065. (cdr range) t))
  11066. (progn
  11067. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11068. t)
  11069. nil)))))
  11070. ;; Multi-values properties are properties that contain multiple values
  11071. ;; These values are assumed to be single words, separated by whitespace.
  11072. (defun org-entry-add-to-multivalued-property (pom property value)
  11073. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11074. (let* ((old (org-entry-get pom property))
  11075. (values (and old (org-split-string old "[ \t]"))))
  11076. (setq value (org-entry-protect-space value))
  11077. (unless (member value values)
  11078. (setq values (cons value values))
  11079. (org-entry-put pom property
  11080. (mapconcat 'identity values " ")))))
  11081. (defun org-entry-remove-from-multivalued-property (pom property value)
  11082. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11083. (let* ((old (org-entry-get pom property))
  11084. (values (and old (org-split-string old "[ \t]"))))
  11085. (setq value (org-entry-protect-space value))
  11086. (when (member value values)
  11087. (setq values (delete value values))
  11088. (org-entry-put pom property
  11089. (mapconcat 'identity values " ")))))
  11090. (defun org-entry-member-in-multivalued-property (pom property value)
  11091. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11092. (let* ((old (org-entry-get pom property))
  11093. (values (and old (org-split-string old "[ \t]"))))
  11094. (setq value (org-entry-protect-space value))
  11095. (member value values)))
  11096. (defun org-entry-get-multivalued-property (pom property)
  11097. "Return a list of values in a multivalued property."
  11098. (let* ((value (org-entry-get pom property))
  11099. (values (and value (org-split-string value "[ \t]"))))
  11100. (mapcar 'org-entry-restore-space values)))
  11101. (defun org-entry-put-multivalued-property (pom property &rest values)
  11102. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11103. VALUES should be a list of strings. Spaces will be protected."
  11104. (org-entry-put pom property
  11105. (mapconcat 'org-entry-protect-space values " "))
  11106. (let* ((value (org-entry-get pom property))
  11107. (values (and value (org-split-string value "[ \t]"))))
  11108. (mapcar 'org-entry-restore-space values)))
  11109. (defun org-entry-protect-space (s)
  11110. "Protect spaces and newline in string S."
  11111. (while (string-match " " s)
  11112. (setq s (replace-match "%20" t t s)))
  11113. (while (string-match "\n" s)
  11114. (setq s (replace-match "%0A" t t s)))
  11115. s)
  11116. (defun org-entry-restore-space (s)
  11117. "Restore spaces and newline in string S."
  11118. (while (string-match "%20" s)
  11119. (setq s (replace-match " " t t s)))
  11120. (while (string-match "%0A" s)
  11121. (setq s (replace-match "\n" t t s)))
  11122. s)
  11123. (defvar org-entry-property-inherited-from (make-marker)
  11124. "Marker pointing to the entry from where a property was inherited.
  11125. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11126. location of the entry where the inheritance search matched. If there was
  11127. no match, the marker will point nowhere.
  11128. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11129. is set.")
  11130. (defun org-entry-get-with-inheritance (property)
  11131. "Get entry property, and search higher levels if not present."
  11132. (move-marker org-entry-property-inherited-from nil)
  11133. (let (tmp)
  11134. (save-excursion
  11135. (save-restriction
  11136. (widen)
  11137. (catch 'ex
  11138. (while t
  11139. (when (setq tmp (org-entry-get nil property))
  11140. (org-back-to-heading t)
  11141. (move-marker org-entry-property-inherited-from (point))
  11142. (throw 'ex tmp))
  11143. (or (org-up-heading-safe) (throw 'ex nil)))))
  11144. (or tmp
  11145. (cdr (assoc property org-file-properties))
  11146. (cdr (assoc property org-global-properties))
  11147. (cdr (assoc property org-global-properties-fixed))))))
  11148. (defun org-entry-put (pom property value)
  11149. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11150. (org-with-point-at pom
  11151. (org-back-to-heading t)
  11152. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11153. range)
  11154. (cond
  11155. ((equal property "TODO")
  11156. (when (and (stringp value) (string-match "\\S-" value)
  11157. (not (member value org-todo-keywords-1)))
  11158. (error "\"%s\" is not a valid TODO state" value))
  11159. (if (or (not value)
  11160. (not (string-match "\\S-" value)))
  11161. (setq value 'none))
  11162. (org-todo value)
  11163. (org-set-tags nil 'align))
  11164. ((equal property "PRIORITY")
  11165. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11166. (string-to-char value) ?\ ))
  11167. (org-set-tags nil 'align))
  11168. ((equal property "SCHEDULED")
  11169. (if (re-search-forward org-scheduled-time-regexp end t)
  11170. (cond
  11171. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11172. ((eq value 'later) (org-timestamp-change 1 'day))
  11173. (t (call-interactively 'org-schedule)))
  11174. (call-interactively 'org-schedule)))
  11175. ((equal property "DEADLINE")
  11176. (if (re-search-forward org-deadline-time-regexp end t)
  11177. (cond
  11178. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11179. ((eq value 'later) (org-timestamp-change 1 'day))
  11180. (t (call-interactively 'org-deadline)))
  11181. (call-interactively 'org-deadline)))
  11182. ((member property org-special-properties)
  11183. (error "The %s property can not yet be set with `org-entry-put'"
  11184. property))
  11185. (t ; a non-special property
  11186. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11187. (setq range (org-get-property-block beg end 'force))
  11188. (goto-char (car range))
  11189. (if (re-search-forward
  11190. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11191. (progn
  11192. (delete-region (match-beginning 1) (match-end 1))
  11193. (goto-char (match-beginning 1)))
  11194. (goto-char (cdr range))
  11195. (insert "\n")
  11196. (backward-char 1)
  11197. (org-indent-line-function)
  11198. (insert ":" property ":"))
  11199. (and value (insert " " value))
  11200. (org-indent-line-function)))))))
  11201. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11202. "Get all property keys in the current buffer.
  11203. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11204. like tags and TODO state.
  11205. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11206. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11207. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11208. formats in the current buffer."
  11209. (let (rtn range cfmt s p)
  11210. (save-excursion
  11211. (save-restriction
  11212. (widen)
  11213. (goto-char (point-min))
  11214. (while (re-search-forward org-property-start-re nil t)
  11215. (setq range (org-get-property-block))
  11216. (goto-char (car range))
  11217. (while (re-search-forward
  11218. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11219. (cdr range) t)
  11220. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11221. (outline-next-heading))))
  11222. (when include-specials
  11223. (setq rtn (append org-special-properties rtn)))
  11224. (when include-defaults
  11225. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11226. (add-to-list 'rtn org-effort-property))
  11227. (when include-columns
  11228. (save-excursion
  11229. (save-restriction
  11230. (widen)
  11231. (goto-char (point-min))
  11232. (while (re-search-forward
  11233. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11234. nil t)
  11235. (setq cfmt (match-string 2) s 0)
  11236. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11237. cfmt s)
  11238. (setq s (match-end 0)
  11239. p (match-string 1 cfmt))
  11240. (unless (or (equal p "ITEM")
  11241. (member p org-special-properties))
  11242. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11243. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11244. (defun org-property-values (key)
  11245. "Return a list of all values of property KEY."
  11246. (save-excursion
  11247. (save-restriction
  11248. (widen)
  11249. (goto-char (point-min))
  11250. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11251. values)
  11252. (while (re-search-forward re nil t)
  11253. (add-to-list 'values (org-trim (match-string 1))))
  11254. (delete "" values)))))
  11255. (defun org-insert-property-drawer ()
  11256. "Insert a property drawer into the current entry."
  11257. (interactive)
  11258. (org-back-to-heading t)
  11259. (looking-at outline-regexp)
  11260. (let ((indent (if org-adapt-indentation
  11261. (- (match-end 0)(match-beginning 0))
  11262. 0))
  11263. (beg (point))
  11264. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11265. end hiddenp)
  11266. (outline-next-heading)
  11267. (setq end (point))
  11268. (goto-char beg)
  11269. (while (re-search-forward re end t))
  11270. (setq hiddenp (org-invisible-p))
  11271. (end-of-line 1)
  11272. (and (equal (char-after) ?\n) (forward-char 1))
  11273. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11274. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11275. ;; just skip this line
  11276. (beginning-of-line 2)
  11277. ;; Drawer start, find the end
  11278. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11279. (beginning-of-line 1)))
  11280. (org-skip-over-state-notes)
  11281. (skip-chars-backward " \t\n\r")
  11282. (if (eq (char-before) ?*) (forward-char 1))
  11283. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11284. (beginning-of-line 0)
  11285. (org-indent-to-column indent)
  11286. (beginning-of-line 2)
  11287. (org-indent-to-column indent)
  11288. (beginning-of-line 0)
  11289. (if hiddenp
  11290. (save-excursion
  11291. (org-back-to-heading t)
  11292. (hide-entry))
  11293. (org-flag-drawer t))))
  11294. (defun org-set-property (property value)
  11295. "In the current entry, set PROPERTY to VALUE.
  11296. When called interactively, this will prompt for a property name, offering
  11297. completion on existing and default properties. And then it will prompt
  11298. for a value, offering completion either on allowed values (via an inherited
  11299. xxx_ALL property) or on existing values in other instances of this property
  11300. in the current file."
  11301. (interactive
  11302. (let* ((completion-ignore-case t)
  11303. (keys (org-buffer-property-keys nil t t))
  11304. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11305. (prop (if (member prop0 keys)
  11306. prop0
  11307. (or (cdr (assoc (downcase prop0)
  11308. (mapcar (lambda (x) (cons (downcase x) x))
  11309. keys)))
  11310. prop0)))
  11311. (cur (org-entry-get nil prop))
  11312. (allowed (org-property-get-allowed-values nil prop 'table))
  11313. (existing (mapcar 'list (org-property-values prop)))
  11314. (val (if allowed
  11315. (org-completing-read "Value: " allowed nil 'req-match)
  11316. (let (org-completion-use-ido org-completion-use-iswitchb)
  11317. (org-completing-read
  11318. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11319. (concat "[" cur "]") "")
  11320. ": ")
  11321. existing nil nil "" nil cur)))))
  11322. (list prop (if (equal val "") cur val))))
  11323. (unless (equal (org-entry-get nil property) value)
  11324. (org-entry-put nil property value)))
  11325. (defun org-delete-property (property)
  11326. "In the current entry, delete PROPERTY."
  11327. (interactive
  11328. (let* ((completion-ignore-case t)
  11329. (prop (org-icompleting-read
  11330. "Property: " (org-entry-properties nil 'standard))))
  11331. (list prop)))
  11332. (message "Property %s %s" property
  11333. (if (org-entry-delete nil property)
  11334. "deleted"
  11335. "was not present in the entry")))
  11336. (defun org-delete-property-globally (property)
  11337. "Remove PROPERTY globally, from all entries."
  11338. (interactive
  11339. (let* ((completion-ignore-case t)
  11340. (prop (org-icompleting-read
  11341. "Globally remove property: "
  11342. (mapcar 'list (org-buffer-property-keys)))))
  11343. (list prop)))
  11344. (save-excursion
  11345. (save-restriction
  11346. (widen)
  11347. (goto-char (point-min))
  11348. (let ((cnt 0))
  11349. (while (re-search-forward
  11350. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11351. nil t)
  11352. (setq cnt (1+ cnt))
  11353. (replace-match ""))
  11354. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11355. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11356. (defun org-compute-property-at-point ()
  11357. "Compute the property at point.
  11358. This looks for an enclosing column format, extracts the operator and
  11359. then applies it to the property in the column format's scope."
  11360. (interactive)
  11361. (unless (org-at-property-p)
  11362. (error "Not at a property"))
  11363. (let ((prop (org-match-string-no-properties 2)))
  11364. (org-columns-get-format-and-top-level)
  11365. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11366. (error "No operator defined for property %s" prop))
  11367. (org-columns-compute prop)))
  11368. (defun org-property-get-allowed-values (pom property &optional table)
  11369. "Get allowed values for the property PROPERTY.
  11370. When TABLE is non-nil, return an alist that can directly be used for
  11371. completion."
  11372. (let (vals)
  11373. (cond
  11374. ((equal property "TODO")
  11375. (setq vals (org-with-point-at pom
  11376. (append org-todo-keywords-1 '("")))))
  11377. ((equal property "PRIORITY")
  11378. (let ((n org-lowest-priority))
  11379. (while (>= n org-highest-priority)
  11380. (push (char-to-string n) vals)
  11381. (setq n (1- n)))))
  11382. ((member property org-special-properties))
  11383. (t
  11384. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11385. (when (and vals (string-match "\\S-" vals))
  11386. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11387. (setq vals (mapcar (lambda (x)
  11388. (cond ((stringp x) x)
  11389. ((numberp x) (number-to-string x))
  11390. ((symbolp x) (symbol-name x))
  11391. (t "???")))
  11392. vals)))))
  11393. (if table (mapcar 'list vals) vals)))
  11394. (defun org-property-previous-allowed-value (&optional previous)
  11395. "Switch to the next allowed value for this property."
  11396. (interactive)
  11397. (org-property-next-allowed-value t))
  11398. (defun org-property-next-allowed-value (&optional previous)
  11399. "Switch to the next allowed value for this property."
  11400. (interactive)
  11401. (unless (org-at-property-p)
  11402. (error "Not at a property"))
  11403. (let* ((key (match-string 2))
  11404. (value (match-string 3))
  11405. (allowed (or (org-property-get-allowed-values (point) key)
  11406. (and (member value '("[ ]" "[-]" "[X]"))
  11407. '("[ ]" "[X]"))))
  11408. nval)
  11409. (unless allowed
  11410. (error "Allowed values for this property have not been defined"))
  11411. (if previous (setq allowed (reverse allowed)))
  11412. (if (member value allowed)
  11413. (setq nval (car (cdr (member value allowed)))))
  11414. (setq nval (or nval (car allowed)))
  11415. (if (equal nval value)
  11416. (error "Only one allowed value for this property"))
  11417. (org-at-property-p)
  11418. (replace-match (concat " :" key ": " nval) t t)
  11419. (org-indent-line-function)
  11420. (beginning-of-line 1)
  11421. (skip-chars-forward " \t")))
  11422. (defun org-find-entry-with-id (ident)
  11423. "Locate the entry that contains the ID property with exact value IDENT.
  11424. IDENT can be a string, a symbol or a number, this function will search for
  11425. the string representation of it.
  11426. Return the position where this entry starts, or nil if there is no such entry."
  11427. (interactive "sID: ")
  11428. (let ((id (cond
  11429. ((stringp ident) ident)
  11430. ((symbol-name ident) (symbol-name ident))
  11431. ((numberp ident) (number-to-string ident))
  11432. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11433. (case-fold-search nil))
  11434. (save-excursion
  11435. (save-restriction
  11436. (widen)
  11437. (goto-char (point-min))
  11438. (when (re-search-forward
  11439. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11440. nil t)
  11441. (org-back-to-heading t)
  11442. (point))))))
  11443. ;;;; Timestamps
  11444. (defvar org-last-changed-timestamp nil)
  11445. (defvar org-last-inserted-timestamp nil
  11446. "The last time stamp inserted with `org-insert-time-stamp'.")
  11447. (defvar org-time-was-given) ; dynamically scoped parameter
  11448. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11449. (defvar org-ts-what) ; dynamically scoped parameter
  11450. (defun org-time-stamp (arg &optional inactive)
  11451. "Prompt for a date/time and insert a time stamp.
  11452. If the user specifies a time like HH:MM, or if this command is called
  11453. with a prefix argument, the time stamp will contain date and time.
  11454. Otherwise, only the date will be included. All parts of a date not
  11455. specified by the user will be filled in from the current date/time.
  11456. So if you press just return without typing anything, the time stamp
  11457. will represent the current date/time. If there is already a timestamp
  11458. at the cursor, it will be modified."
  11459. (interactive "P")
  11460. (let* ((ts nil)
  11461. (default-time
  11462. ;; Default time is either today, or, when entering a range,
  11463. ;; the range start.
  11464. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11465. (save-excursion
  11466. (re-search-backward
  11467. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11468. (- (point) 20) t)))
  11469. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11470. (current-time)))
  11471. (default-input (and ts (org-get-compact-tod ts)))
  11472. org-time-was-given org-end-time-was-given time)
  11473. (cond
  11474. ((and (org-at-timestamp-p t)
  11475. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11476. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11477. (insert "--")
  11478. (setq time (let ((this-command this-command))
  11479. (org-read-date arg 'totime nil nil
  11480. default-time default-input)))
  11481. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11482. ((org-at-timestamp-p t)
  11483. (setq time (let ((this-command this-command))
  11484. (org-read-date arg 'totime nil nil default-time default-input)))
  11485. (when (org-at-timestamp-p t) ; just to get the match data
  11486. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11487. (replace-match "")
  11488. (setq org-last-changed-timestamp
  11489. (org-insert-time-stamp
  11490. time (or org-time-was-given arg)
  11491. inactive nil nil (list org-end-time-was-given))))
  11492. (message "Timestamp updated"))
  11493. (t
  11494. (setq time (let ((this-command this-command))
  11495. (org-read-date arg 'totime nil nil default-time default-input)))
  11496. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11497. nil nil (list org-end-time-was-given))))))
  11498. ;; FIXME: can we use this for something else, like computing time differences?
  11499. (defun org-get-compact-tod (s)
  11500. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11501. (let* ((t1 (match-string 1 s))
  11502. (h1 (string-to-number (match-string 2 s)))
  11503. (m1 (string-to-number (match-string 3 s)))
  11504. (t2 (and (match-end 4) (match-string 5 s)))
  11505. (h2 (and t2 (string-to-number (match-string 6 s))))
  11506. (m2 (and t2 (string-to-number (match-string 7 s))))
  11507. dh dm)
  11508. (if (not t2)
  11509. t1
  11510. (setq dh (- h2 h1) dm (- m2 m1))
  11511. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11512. (concat t1 "+" (number-to-string dh)
  11513. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11514. (defun org-time-stamp-inactive (&optional arg)
  11515. "Insert an inactive time stamp.
  11516. An inactive time stamp is enclosed in square brackets instead of angle
  11517. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11518. does not link to the calendar and cannot be changed with the S-cursor keys.
  11519. So these are more for recording a certain time/date."
  11520. (interactive "P")
  11521. (org-time-stamp arg 'inactive))
  11522. (defvar org-date-ovl (org-make-overlay 1 1))
  11523. (org-overlay-put org-date-ovl 'face 'org-warning)
  11524. (org-detach-overlay org-date-ovl)
  11525. (defvar org-ans1) ; dynamically scoped parameter
  11526. (defvar org-ans2) ; dynamically scoped parameter
  11527. (defvar org-plain-time-of-day-regexp) ; defined below
  11528. (defvar org-overriding-default-time nil) ; dynamically scoped
  11529. (defvar org-read-date-overlay nil)
  11530. (defvar org-dcst nil) ; dynamically scoped
  11531. (defvar org-read-date-history nil)
  11532. (defvar org-read-date-final-answer nil)
  11533. (defun org-read-date (&optional with-time to-time from-string prompt
  11534. default-time default-input)
  11535. "Read a date, possibly a time, and make things smooth for the user.
  11536. The prompt will suggest to enter an ISO date, but you can also enter anything
  11537. which will at least partially be understood by `parse-time-string'.
  11538. Unrecognized parts of the date will default to the current day, month, year,
  11539. hour and minute. If this command is called to replace a timestamp at point,
  11540. of to enter the second timestamp of a range, the default time is taken from the
  11541. existing stamp. For example,
  11542. 3-2-5 --> 2003-02-05
  11543. feb 15 --> currentyear-02-15
  11544. sep 12 9 --> 2009-09-12
  11545. 12:45 --> today 12:45
  11546. 22 sept 0:34 --> currentyear-09-22 0:34
  11547. 12 --> currentyear-currentmonth-12
  11548. Fri --> nearest Friday (today or later)
  11549. etc.
  11550. Furthermore you can specify a relative date by giving, as the *first* thing
  11551. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11552. change in days weeks, months, years.
  11553. With a single plus or minus, the date is relative to today. With a double
  11554. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11555. +4d --> four days from today
  11556. +4 --> same as above
  11557. +2w --> two weeks from today
  11558. ++5 --> five days from default date
  11559. The function understands only English month and weekday abbreviations,
  11560. but this can be configured with the variables `parse-time-months' and
  11561. `parse-time-weekdays'.
  11562. While prompting, a calendar is popped up - you can also select the
  11563. date with the mouse (button 1). The calendar shows a period of three
  11564. months. To scroll it to other months, use the keys `>' and `<'.
  11565. If you don't like the calendar, turn it off with
  11566. \(setq org-read-date-popup-calendar nil)
  11567. With optional argument TO-TIME, the date will immediately be converted
  11568. to an internal time.
  11569. With an optional argument WITH-TIME, the prompt will suggest to also
  11570. insert a time. Note that when WITH-TIME is not set, you can still
  11571. enter a time, and this function will inform the calling routine about
  11572. this change. The calling routine may then choose to change the format
  11573. used to insert the time stamp into the buffer to include the time.
  11574. With optional argument FROM-STRING, read from this string instead from
  11575. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11576. the time/date that is used for everything that is not specified by the
  11577. user."
  11578. (require 'parse-time)
  11579. (let* ((org-time-stamp-rounding-minutes
  11580. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11581. (org-dcst org-display-custom-times)
  11582. (ct (org-current-time))
  11583. (def (or org-overriding-default-time default-time ct))
  11584. (defdecode (decode-time def))
  11585. (dummy (progn
  11586. (when (< (nth 2 defdecode) org-extend-today-until)
  11587. (setcar (nthcdr 2 defdecode) -1)
  11588. (setcar (nthcdr 1 defdecode) 59)
  11589. (setq def (apply 'encode-time defdecode)
  11590. defdecode (decode-time def)))))
  11591. (calendar-frame-setup nil)
  11592. (calendar-move-hook nil)
  11593. (calendar-view-diary-initially-flag nil)
  11594. (view-diary-entries-initially nil)
  11595. (calendar-view-holidays-initially-flag nil)
  11596. (view-calendar-holidays-initially nil)
  11597. (timestr (format-time-string
  11598. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11599. (prompt (concat (if prompt (concat prompt " ") "")
  11600. (format "Date+time [%s]: " timestr)))
  11601. ans (org-ans0 "") org-ans1 org-ans2 final)
  11602. (cond
  11603. (from-string (setq ans from-string))
  11604. (org-read-date-popup-calendar
  11605. (save-excursion
  11606. (save-window-excursion
  11607. (calendar)
  11608. (calendar-forward-day (- (time-to-days def)
  11609. (calendar-absolute-from-gregorian
  11610. (calendar-current-date))))
  11611. (org-eval-in-calendar nil t)
  11612. (let* ((old-map (current-local-map))
  11613. (map (copy-keymap calendar-mode-map))
  11614. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11615. (org-defkey map (kbd "RET") 'org-calendar-select)
  11616. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11617. 'org-calendar-select-mouse)
  11618. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11619. 'org-calendar-select-mouse)
  11620. (org-defkey minibuffer-local-map [(meta shift left)]
  11621. (lambda () (interactive)
  11622. (org-eval-in-calendar '(calendar-backward-month 1))))
  11623. (org-defkey minibuffer-local-map [(meta shift right)]
  11624. (lambda () (interactive)
  11625. (org-eval-in-calendar '(calendar-forward-month 1))))
  11626. (org-defkey minibuffer-local-map [(meta shift up)]
  11627. (lambda () (interactive)
  11628. (org-eval-in-calendar '(calendar-backward-year 1))))
  11629. (org-defkey minibuffer-local-map [(meta shift down)]
  11630. (lambda () (interactive)
  11631. (org-eval-in-calendar '(calendar-forward-year 1))))
  11632. (org-defkey minibuffer-local-map [?\e (shift left)]
  11633. (lambda () (interactive)
  11634. (org-eval-in-calendar '(calendar-backward-month 1))))
  11635. (org-defkey minibuffer-local-map [?\e (shift right)]
  11636. (lambda () (interactive)
  11637. (org-eval-in-calendar '(calendar-forward-month 1))))
  11638. (org-defkey minibuffer-local-map [?\e (shift up)]
  11639. (lambda () (interactive)
  11640. (org-eval-in-calendar '(calendar-backward-year 1))))
  11641. (org-defkey minibuffer-local-map [?\e (shift down)]
  11642. (lambda () (interactive)
  11643. (org-eval-in-calendar '(calendar-forward-year 1))))
  11644. (org-defkey minibuffer-local-map [(shift up)]
  11645. (lambda () (interactive)
  11646. (org-eval-in-calendar '(calendar-backward-week 1))))
  11647. (org-defkey minibuffer-local-map [(shift down)]
  11648. (lambda () (interactive)
  11649. (org-eval-in-calendar '(calendar-forward-week 1))))
  11650. (org-defkey minibuffer-local-map [(shift left)]
  11651. (lambda () (interactive)
  11652. (org-eval-in-calendar '(calendar-backward-day 1))))
  11653. (org-defkey minibuffer-local-map [(shift right)]
  11654. (lambda () (interactive)
  11655. (org-eval-in-calendar '(calendar-forward-day 1))))
  11656. (org-defkey minibuffer-local-map ">"
  11657. (lambda () (interactive)
  11658. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11659. (org-defkey minibuffer-local-map "<"
  11660. (lambda () (interactive)
  11661. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11662. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11663. (unwind-protect
  11664. (progn
  11665. (use-local-map map)
  11666. (add-hook 'post-command-hook 'org-read-date-display)
  11667. (setq org-ans0 (read-string prompt default-input
  11668. 'org-read-date-history nil))
  11669. ;; org-ans0: from prompt
  11670. ;; org-ans1: from mouse click
  11671. ;; org-ans2: from calendar motion
  11672. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11673. (remove-hook 'post-command-hook 'org-read-date-display)
  11674. (use-local-map old-map)
  11675. (when org-read-date-overlay
  11676. (org-delete-overlay org-read-date-overlay)
  11677. (setq org-read-date-overlay nil)))))))
  11678. (t ; Naked prompt only
  11679. (unwind-protect
  11680. (setq ans (read-string prompt default-input
  11681. 'org-read-date-history timestr))
  11682. (when org-read-date-overlay
  11683. (org-delete-overlay org-read-date-overlay)
  11684. (setq org-read-date-overlay nil)))))
  11685. (setq final (org-read-date-analyze ans def defdecode))
  11686. (setq org-read-date-final-answer ans)
  11687. (if to-time
  11688. (apply 'encode-time final)
  11689. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11690. (format "%04d-%02d-%02d %02d:%02d"
  11691. (nth 5 final) (nth 4 final) (nth 3 final)
  11692. (nth 2 final) (nth 1 final))
  11693. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11694. (defvar def)
  11695. (defvar defdecode)
  11696. (defvar with-time)
  11697. (defvar org-read-date-analyze-futurep nil)
  11698. (defun org-read-date-display ()
  11699. "Display the current date prompt interpretation in the minibuffer."
  11700. (when org-read-date-display-live
  11701. (when org-read-date-overlay
  11702. (org-delete-overlay org-read-date-overlay))
  11703. (let ((p (point)))
  11704. (end-of-line 1)
  11705. (while (not (equal (buffer-substring
  11706. (max (point-min) (- (point) 4)) (point))
  11707. " "))
  11708. (insert " "))
  11709. (goto-char p))
  11710. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11711. " " (or org-ans1 org-ans2)))
  11712. (org-end-time-was-given nil)
  11713. (f (org-read-date-analyze ans def defdecode))
  11714. (fmts (if org-dcst
  11715. org-time-stamp-custom-formats
  11716. org-time-stamp-formats))
  11717. (fmt (if (or with-time
  11718. (and (boundp 'org-time-was-given) org-time-was-given))
  11719. (cdr fmts)
  11720. (car fmts)))
  11721. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11722. (when (and org-end-time-was-given
  11723. (string-match org-plain-time-of-day-regexp txt))
  11724. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11725. org-end-time-was-given
  11726. (substring txt (match-end 0)))))
  11727. (when org-read-date-analyze-futurep
  11728. (setq txt (concat txt " (=>F)")))
  11729. (setq org-read-date-overlay
  11730. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11731. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11732. (defun org-read-date-analyze (ans def defdecode)
  11733. "Analyse the combined answer of the date prompt."
  11734. ;; FIXME: cleanup and comment
  11735. (let (delta deltan deltaw deltadef year month day
  11736. hour minute second wday pm h2 m2 tl wday1
  11737. iso-year iso-weekday iso-week iso-year iso-date futurep)
  11738. (setq org-read-date-analyze-futurep nil)
  11739. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11740. (setq ans "+0"))
  11741. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11742. (setq ans (replace-match "" t t ans)
  11743. deltan (car delta)
  11744. deltaw (nth 1 delta)
  11745. deltadef (nth 2 delta)))
  11746. ;; Check if there is an iso week date in there
  11747. ;; If yes, sore the info and postpone interpreting it until the rest
  11748. ;; of the parsing is done
  11749. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11750. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11751. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11752. iso-week (string-to-number (match-string 2 ans)))
  11753. (setq ans (replace-match "" t t ans)))
  11754. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11755. (when (string-match
  11756. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11757. (setq year (if (match-end 2)
  11758. (string-to-number (match-string 2 ans))
  11759. (string-to-number (format-time-string "%Y")))
  11760. month (string-to-number (match-string 3 ans))
  11761. day (string-to-number (match-string 4 ans)))
  11762. (if (< year 100) (setq year (+ 2000 year)))
  11763. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11764. t nil ans)))
  11765. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11766. ;; If there is a time with am/pm, and *no* time without it, we convert
  11767. ;; so that matching will be successful.
  11768. (loop for i from 1 to 2 do ; twice, for end time as well
  11769. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11770. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11771. (setq hour (string-to-number (match-string 1 ans))
  11772. minute (if (match-end 3)
  11773. (string-to-number (match-string 3 ans))
  11774. 0)
  11775. pm (equal ?p
  11776. (string-to-char (downcase (match-string 4 ans)))))
  11777. (if (and (= hour 12) (not pm))
  11778. (setq hour 0)
  11779. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11780. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11781. t t ans))))
  11782. ;; Check if a time range is given as a duration
  11783. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11784. (setq hour (string-to-number (match-string 1 ans))
  11785. h2 (+ hour (string-to-number (match-string 3 ans)))
  11786. minute (string-to-number (match-string 2 ans))
  11787. m2 (+ minute (if (match-end 5) (string-to-number
  11788. (match-string 5 ans))0)))
  11789. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11790. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11791. t t ans)))
  11792. ;; Check if there is a time range
  11793. (when (boundp 'org-end-time-was-given)
  11794. (setq org-time-was-given nil)
  11795. (when (and (string-match org-plain-time-of-day-regexp ans)
  11796. (match-end 8))
  11797. (setq org-end-time-was-given (match-string 8 ans))
  11798. (setq ans (concat (substring ans 0 (match-beginning 7))
  11799. (substring ans (match-end 7))))))
  11800. (setq tl (parse-time-string ans)
  11801. day (or (nth 3 tl) (nth 3 defdecode))
  11802. month (or (nth 4 tl)
  11803. (if (and org-read-date-prefer-future
  11804. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11805. (prog1 (1+ (nth 4 defdecode)) (setq futurep t))
  11806. (nth 4 defdecode)))
  11807. year (or (nth 5 tl)
  11808. (if (and org-read-date-prefer-future
  11809. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11810. (prog1 (1+ (nth 5 defdecode)) (setq futurep t))
  11811. (nth 5 defdecode)))
  11812. hour (or (nth 2 tl) (nth 2 defdecode))
  11813. minute (or (nth 1 tl) (nth 1 defdecode))
  11814. second (or (nth 0 tl) 0)
  11815. wday (nth 6 tl))
  11816. (when (and (eq org-read-date-prefer-future 'time)
  11817. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  11818. (equal day (nth 3 defdecode))
  11819. (equal month (nth 4 defdecode))
  11820. (equal year (nth 5 defdecode))
  11821. (nth 2 tl)
  11822. (or (< (nth 2 tl) (nth 2 defdecode))
  11823. (and (= (nth 2 tl) (nth 2 defdecode))
  11824. (nth 1 tl)
  11825. (< (nth 1 tl) (nth 1 defdecode)))))
  11826. (setq day (1+ day)
  11827. futurep t))
  11828. ;; Special date definitions below
  11829. (cond
  11830. (iso-week
  11831. ;; There was an iso week
  11832. (setq futurep nil)
  11833. (setq year (or iso-year year)
  11834. day (or iso-weekday wday 1)
  11835. wday nil ; to make sure that the trigger below does not match
  11836. iso-date (calendar-gregorian-from-absolute
  11837. (calendar-absolute-from-iso
  11838. (list iso-week day year))))
  11839. ; FIXME: Should we also push ISO weeks into the future?
  11840. ; (when (and org-read-date-prefer-future
  11841. ; (not iso-year)
  11842. ; (< (calendar-absolute-from-gregorian iso-date)
  11843. ; (time-to-days (current-time))))
  11844. ; (setq year (1+ year)
  11845. ; iso-date (calendar-gregorian-from-absolute
  11846. ; (calendar-absolute-from-iso
  11847. ; (list iso-week day year)))))
  11848. (setq month (car iso-date)
  11849. year (nth 2 iso-date)
  11850. day (nth 1 iso-date)))
  11851. (deltan
  11852. (setq futurep nil)
  11853. (unless deltadef
  11854. (let ((now (decode-time (current-time))))
  11855. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11856. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11857. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11858. ((equal deltaw "m") (setq month (+ month deltan)))
  11859. ((equal deltaw "y") (setq year (+ year deltan)))))
  11860. ((and wday (not (nth 3 tl)))
  11861. (setq futurep nil)
  11862. ;; Weekday was given, but no day, so pick that day in the week
  11863. ;; on or after the derived date.
  11864. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11865. (unless (equal wday wday1)
  11866. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11867. (if (and (boundp 'org-time-was-given)
  11868. (nth 2 tl))
  11869. (setq org-time-was-given t))
  11870. (if (< year 100) (setq year (+ 2000 year)))
  11871. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11872. (setq org-read-date-analyze-futurep futurep)
  11873. (list second minute hour day month year)))
  11874. (defvar parse-time-weekdays)
  11875. (defun org-read-date-get-relative (s today default)
  11876. "Check string S for special relative date string.
  11877. TODAY and DEFAULT are internal times, for today and for a default.
  11878. Return shift list (N what def-flag)
  11879. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11880. N is the number of WHATs to shift.
  11881. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11882. the DEFAULT date rather than TODAY."
  11883. (when (and
  11884. (string-match
  11885. (concat
  11886. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11887. "\\([0-9]+\\)?"
  11888. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11889. "\\([ \t]\\|$\\)") s)
  11890. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11891. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11892. (string-to-char (substring (match-string 1 s) -1))
  11893. ?+))
  11894. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11895. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11896. (what (if (match-end 3) (match-string 3 s) "d"))
  11897. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11898. (date (if rel default today))
  11899. (wday (nth 6 (decode-time date)))
  11900. delta)
  11901. (if wday1
  11902. (progn
  11903. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11904. (if (= dir ?-) (setq delta (- delta 7)))
  11905. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11906. (list delta "d" rel))
  11907. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11908. (defun org-eval-in-calendar (form &optional keepdate)
  11909. "Eval FORM in the calendar window and return to current window.
  11910. Also, store the cursor date in variable org-ans2."
  11911. (let ((sf (selected-frame))
  11912. (sw (selected-window)))
  11913. (select-window (get-buffer-window "*Calendar*" t))
  11914. (eval form)
  11915. (when (and (not keepdate) (calendar-cursor-to-date))
  11916. (let* ((date (calendar-cursor-to-date))
  11917. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11918. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11919. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11920. (select-window sw)
  11921. (org-select-frame-set-input-focus sf)))
  11922. (defun org-calendar-select ()
  11923. "Return to `org-read-date' with the date currently selected.
  11924. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11925. (interactive)
  11926. (when (calendar-cursor-to-date)
  11927. (let* ((date (calendar-cursor-to-date))
  11928. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11929. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11930. (if (active-minibuffer-window) (exit-minibuffer))))
  11931. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11932. "Insert a date stamp for the date given by the internal TIME.
  11933. WITH-HM means, use the stamp format that includes the time of the day.
  11934. INACTIVE means use square brackets instead of angular ones, so that the
  11935. stamp will not contribute to the agenda.
  11936. PRE and POST are optional strings to be inserted before and after the
  11937. stamp.
  11938. The command returns the inserted time stamp."
  11939. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11940. stamp)
  11941. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11942. (insert-before-markers (or pre ""))
  11943. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11944. (when (listp extra)
  11945. (setq extra (car extra))
  11946. (if (and (stringp extra)
  11947. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11948. (setq extra (format "-%02d:%02d"
  11949. (string-to-number (match-string 1 extra))
  11950. (string-to-number (match-string 2 extra))))
  11951. (setq extra nil)))
  11952. (when extra
  11953. (backward-char 1)
  11954. (insert-before-markers extra)
  11955. (forward-char 1))
  11956. (insert-before-markers (or post ""))
  11957. (setq org-last-inserted-timestamp stamp)))
  11958. (defun org-toggle-time-stamp-overlays ()
  11959. "Toggle the use of custom time stamp formats."
  11960. (interactive)
  11961. (setq org-display-custom-times (not org-display-custom-times))
  11962. (unless org-display-custom-times
  11963. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11964. (while (setq p (next-single-property-change p 'display))
  11965. (if (and (get-text-property p 'display)
  11966. (eq (get-text-property p 'face) 'org-date))
  11967. (remove-text-properties
  11968. p (setq p (next-single-property-change p 'display))
  11969. '(display t))))
  11970. (set-buffer-modified-p bmp)))
  11971. (if (featurep 'xemacs)
  11972. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11973. (org-restart-font-lock)
  11974. (setq org-table-may-need-update t)
  11975. (if org-display-custom-times
  11976. (message "Time stamps are overlayed with custom format")
  11977. (message "Time stamp overlays removed")))
  11978. (defun org-display-custom-time (beg end)
  11979. "Overlay modified time stamp format over timestamp between BEG and END."
  11980. (let* ((ts (buffer-substring beg end))
  11981. t1 w1 with-hm tf time str w2 (off 0))
  11982. (save-match-data
  11983. (setq t1 (org-parse-time-string ts t))
  11984. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  11985. (setq off (- (match-end 0) (match-beginning 0)))))
  11986. (setq end (- end off))
  11987. (setq w1 (- end beg)
  11988. with-hm (and (nth 1 t1) (nth 2 t1))
  11989. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11990. time (org-fix-decoded-time t1)
  11991. str (org-add-props
  11992. (format-time-string
  11993. (substring tf 1 -1) (apply 'encode-time time))
  11994. nil 'mouse-face 'highlight)
  11995. w2 (length str))
  11996. (if (not (= w2 w1))
  11997. (add-text-properties (1+ beg) (+ 2 beg)
  11998. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11999. (if (featurep 'xemacs)
  12000. (progn
  12001. (put-text-property beg end 'invisible t)
  12002. (put-text-property beg end 'end-glyph (make-glyph str)))
  12003. (put-text-property beg end 'display str))))
  12004. (defun org-translate-time (string)
  12005. "Translate all timestamps in STRING to custom format.
  12006. But do this only if the variable `org-display-custom-times' is set."
  12007. (when org-display-custom-times
  12008. (save-match-data
  12009. (let* ((start 0)
  12010. (re org-ts-regexp-both)
  12011. t1 with-hm inactive tf time str beg end)
  12012. (while (setq start (string-match re string start))
  12013. (setq beg (match-beginning 0)
  12014. end (match-end 0)
  12015. t1 (save-match-data
  12016. (org-parse-time-string (substring string beg end) t))
  12017. with-hm (and (nth 1 t1) (nth 2 t1))
  12018. inactive (equal (substring string beg (1+ beg)) "[")
  12019. tf (funcall (if with-hm 'cdr 'car)
  12020. org-time-stamp-custom-formats)
  12021. time (org-fix-decoded-time t1)
  12022. str (format-time-string
  12023. (concat
  12024. (if inactive "[" "<") (substring tf 1 -1)
  12025. (if inactive "]" ">"))
  12026. (apply 'encode-time time))
  12027. string (replace-match str t t string)
  12028. start (+ start (length str)))))))
  12029. string)
  12030. (defun org-fix-decoded-time (time)
  12031. "Set 0 instead of nil for the first 6 elements of time.
  12032. Don't touch the rest."
  12033. (let ((n 0))
  12034. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12035. (defun org-days-to-time (timestamp-string)
  12036. "Difference between TIMESTAMP-STRING and now in days."
  12037. (- (time-to-days (org-time-string-to-time timestamp-string))
  12038. (time-to-days (current-time))))
  12039. (defun org-deadline-close (timestamp-string &optional ndays)
  12040. "Is the time in TIMESTAMP-STRING close to the current date?"
  12041. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12042. (and (< (org-days-to-time timestamp-string) ndays)
  12043. (not (org-entry-is-done-p))))
  12044. (defun org-get-wdays (ts)
  12045. "Get the deadline lead time appropriate for timestring TS."
  12046. (cond
  12047. ((<= org-deadline-warning-days 0)
  12048. ;; 0 or negative, enforce this value no matter what
  12049. (- org-deadline-warning-days))
  12050. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12051. ;; lead time is specified.
  12052. (floor (* (string-to-number (match-string 1 ts))
  12053. (cdr (assoc (match-string 2 ts)
  12054. '(("d" . 1) ("w" . 7)
  12055. ("m" . 30.4) ("y" . 365.25)))))))
  12056. ;; go for the default.
  12057. (t org-deadline-warning-days)))
  12058. (defun org-calendar-select-mouse (ev)
  12059. "Return to `org-read-date' with the date currently selected.
  12060. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12061. (interactive "e")
  12062. (mouse-set-point ev)
  12063. (when (calendar-cursor-to-date)
  12064. (let* ((date (calendar-cursor-to-date))
  12065. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12066. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12067. (if (active-minibuffer-window) (exit-minibuffer))))
  12068. (defun org-check-deadlines (ndays)
  12069. "Check if there are any deadlines due or past due.
  12070. A deadline is considered due if it happens within `org-deadline-warning-days'
  12071. days from today's date. If the deadline appears in an entry marked DONE,
  12072. it is not shown. The prefix arg NDAYS can be used to test that many
  12073. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12074. (interactive "P")
  12075. (let* ((org-warn-days
  12076. (cond
  12077. ((equal ndays '(4)) 100000)
  12078. (ndays (prefix-numeric-value ndays))
  12079. (t (abs org-deadline-warning-days))))
  12080. (case-fold-search nil)
  12081. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12082. (callback
  12083. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12084. (message "%d deadlines past-due or due within %d days"
  12085. (org-occur regexp nil callback)
  12086. org-warn-days)))
  12087. (defun org-check-before-date (date)
  12088. "Check if there are deadlines or scheduled entries before DATE."
  12089. (interactive (list (org-read-date)))
  12090. (let ((case-fold-search nil)
  12091. (regexp (concat "\\<\\(" org-deadline-string
  12092. "\\|" org-scheduled-string
  12093. "\\) *<\\([^>]+\\)>"))
  12094. (callback
  12095. (lambda () (time-less-p
  12096. (org-time-string-to-time (match-string 2))
  12097. (org-time-string-to-time date)))))
  12098. (message "%d entries before %s"
  12099. (org-occur regexp nil callback) date)))
  12100. (defun org-check-after-date (date)
  12101. "Check if there are deadlines or scheduled entries after DATE."
  12102. (interactive (list (org-read-date)))
  12103. (let ((case-fold-search nil)
  12104. (regexp (concat "\\<\\(" org-deadline-string
  12105. "\\|" org-scheduled-string
  12106. "\\) *<\\([^>]+\\)>"))
  12107. (callback
  12108. (lambda () (not
  12109. (time-less-p
  12110. (org-time-string-to-time (match-string 2))
  12111. (org-time-string-to-time date))))))
  12112. (message "%d entries after %s"
  12113. (org-occur regexp nil callback) date)))
  12114. (defun org-evaluate-time-range (&optional to-buffer)
  12115. "Evaluate a time range by computing the difference between start and end.
  12116. Normally the result is just printed in the echo area, but with prefix arg
  12117. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12118. If the time range is actually in a table, the result is inserted into the
  12119. next column.
  12120. For time difference computation, a year is assumed to be exactly 365
  12121. days in order to avoid rounding problems."
  12122. (interactive "P")
  12123. (or
  12124. (org-clock-update-time-maybe)
  12125. (save-excursion
  12126. (unless (org-at-date-range-p t)
  12127. (goto-char (point-at-bol))
  12128. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12129. (if (not (org-at-date-range-p t))
  12130. (error "Not at a time-stamp range, and none found in current line")))
  12131. (let* ((ts1 (match-string 1))
  12132. (ts2 (match-string 2))
  12133. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12134. (match-end (match-end 0))
  12135. (time1 (org-time-string-to-time ts1))
  12136. (time2 (org-time-string-to-time ts2))
  12137. (t1 (org-float-time time1))
  12138. (t2 (org-float-time time2))
  12139. (diff (abs (- t2 t1)))
  12140. (negative (< (- t2 t1) 0))
  12141. ;; (ys (floor (* 365 24 60 60)))
  12142. (ds (* 24 60 60))
  12143. (hs (* 60 60))
  12144. (fy "%dy %dd %02d:%02d")
  12145. (fy1 "%dy %dd")
  12146. (fd "%dd %02d:%02d")
  12147. (fd1 "%dd")
  12148. (fh "%02d:%02d")
  12149. y d h m align)
  12150. (if havetime
  12151. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12152. y 0
  12153. d (floor (/ diff ds)) diff (mod diff ds)
  12154. h (floor (/ diff hs)) diff (mod diff hs)
  12155. m (floor (/ diff 60)))
  12156. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12157. y 0
  12158. d (floor (+ (/ diff ds) 0.5))
  12159. h 0 m 0))
  12160. (if (not to-buffer)
  12161. (message "%s" (org-make-tdiff-string y d h m))
  12162. (if (org-at-table-p)
  12163. (progn
  12164. (goto-char match-end)
  12165. (setq align t)
  12166. (and (looking-at " *|") (goto-char (match-end 0))))
  12167. (goto-char match-end))
  12168. (if (looking-at
  12169. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12170. (replace-match ""))
  12171. (if negative (insert " -"))
  12172. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12173. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12174. (insert " " (format fh h m))))
  12175. (if align (org-table-align))
  12176. (message "Time difference inserted")))))
  12177. (defun org-make-tdiff-string (y d h m)
  12178. (let ((fmt "")
  12179. (l nil))
  12180. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12181. l (push y l)))
  12182. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12183. l (push d l)))
  12184. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12185. l (push h l)))
  12186. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12187. l (push m l)))
  12188. (apply 'format fmt (nreverse l))))
  12189. (defun org-time-string-to-time (s)
  12190. (apply 'encode-time (org-parse-time-string s)))
  12191. (defun org-time-string-to-seconds (s)
  12192. (org-float-time (org-time-string-to-time s)))
  12193. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12194. "Convert a time stamp to an absolute day number.
  12195. If there is a specifyer for a cyclic time stamp, get the closest date to
  12196. DAYNR.
  12197. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12198. the variable date is bound by the calendar when this is called."
  12199. (cond
  12200. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12201. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12202. daynr
  12203. (+ daynr 1000)))
  12204. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12205. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12206. (time-to-days (current-time))) (match-string 0 s)
  12207. prefer show-all))
  12208. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12209. (defun org-days-to-iso-week (days)
  12210. "Return the iso week number."
  12211. (require 'cal-iso)
  12212. (car (calendar-iso-from-absolute days)))
  12213. (defun org-small-year-to-year (year)
  12214. "Convert 2-digit years into 4-digit years.
  12215. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12216. The year 2000 cannot be abbreviated. Any year larger than 99
  12217. is returned unchanged."
  12218. (if (< year 38)
  12219. (setq year (+ 2000 year))
  12220. (if (< year 100)
  12221. (setq year (+ 1900 year))))
  12222. year)
  12223. (defun org-time-from-absolute (d)
  12224. "Return the time corresponding to date D.
  12225. D may be an absolute day number, or a calendar-type list (month day year)."
  12226. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12227. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12228. (defun org-calendar-holiday ()
  12229. "List of holidays, for Diary display in Org-mode."
  12230. (require 'holidays)
  12231. (let ((hl (funcall
  12232. (if (fboundp 'calendar-check-holidays)
  12233. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12234. (if hl (mapconcat 'identity hl "; "))))
  12235. (defun org-diary-sexp-entry (sexp entry date)
  12236. "Process a SEXP diary ENTRY for DATE."
  12237. (require 'diary-lib)
  12238. (let ((result (if calendar-debug-sexp
  12239. (let ((stack-trace-on-error t))
  12240. (eval (car (read-from-string sexp))))
  12241. (condition-case nil
  12242. (eval (car (read-from-string sexp)))
  12243. (error
  12244. (beep)
  12245. (message "Bad sexp at line %d in %s: %s"
  12246. (org-current-line)
  12247. (buffer-file-name) sexp)
  12248. (sleep-for 2))))))
  12249. (cond ((stringp result) result)
  12250. ((and (consp result)
  12251. (stringp (cdr result))) (cdr result))
  12252. (result entry)
  12253. (t nil))))
  12254. (defun org-diary-to-ical-string (frombuf)
  12255. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12256. This uses the icalendar.el library."
  12257. (let* ((tmpdir (if (featurep 'xemacs)
  12258. (temp-directory)
  12259. temporary-file-directory))
  12260. (tmpfile (make-temp-name
  12261. (expand-file-name "orgics" tmpdir)))
  12262. buf rtn b e)
  12263. (save-excursion
  12264. (set-buffer frombuf)
  12265. (icalendar-export-region (point-min) (point-max) tmpfile)
  12266. (setq buf (find-buffer-visiting tmpfile))
  12267. (set-buffer buf)
  12268. (goto-char (point-min))
  12269. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12270. (setq b (match-beginning 0)))
  12271. (goto-char (point-max))
  12272. (if (re-search-backward "^END:VEVENT" nil t)
  12273. (setq e (match-end 0)))
  12274. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12275. (kill-buffer buf)
  12276. (delete-file tmpfile)
  12277. rtn))
  12278. (defun org-closest-date (start current change prefer show-all)
  12279. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12280. When PREFER is `past' return a date that is either CURRENT or past.
  12281. When PREFER is `future', return a date that is either CURRENT or future.
  12282. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12283. ;; Make the proper lists from the dates
  12284. (catch 'exit
  12285. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12286. dn dw sday cday n1 n2 n0
  12287. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12288. (setq start (org-date-to-gregorian start)
  12289. current (org-date-to-gregorian
  12290. (if show-all
  12291. current
  12292. (time-to-days (current-time))))
  12293. sday (calendar-absolute-from-gregorian start)
  12294. cday (calendar-absolute-from-gregorian current))
  12295. (if (<= cday sday) (throw 'exit sday))
  12296. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12297. (setq dn (string-to-number (match-string 1 change))
  12298. dw (cdr (assoc (match-string 2 change) a1)))
  12299. (error "Invalid change specifyer: %s" change))
  12300. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12301. (cond
  12302. ((eq dw 'day)
  12303. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12304. n2 (+ n1 dn)))
  12305. ((eq dw 'year)
  12306. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12307. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12308. (setq date1 (list m d y1)
  12309. n1 (calendar-absolute-from-gregorian date1)
  12310. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12311. n2 (calendar-absolute-from-gregorian date2)))
  12312. ((eq dw 'month)
  12313. ;; approx number of month between the two dates
  12314. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12315. ;; How often does dn fit in there?
  12316. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12317. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12318. m (+ m nm)
  12319. ny (floor (/ m 12))
  12320. y (+ y ny)
  12321. m (- m (* ny 12)))
  12322. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12323. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12324. (setq m2 (+ m dn) y2 y)
  12325. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12326. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12327. (while (<= n2 cday)
  12328. (setq n1 n2 m m2 y y2)
  12329. (setq m2 (+ m dn) y2 y)
  12330. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12331. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12332. ;; Make sure n1 is the earlier date
  12333. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12334. (if show-all
  12335. (cond
  12336. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12337. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12338. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12339. (cond
  12340. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12341. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12342. (t (if (= cday n1) n1 n2)))))))
  12343. (defun org-date-to-gregorian (date)
  12344. "Turn any specification of DATE into a gregorian date for the calendar."
  12345. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12346. ((and (listp date) (= (length date) 3)) date)
  12347. ((stringp date)
  12348. (setq date (org-parse-time-string date))
  12349. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12350. ((listp date)
  12351. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12352. (defun org-parse-time-string (s &optional nodefault)
  12353. "Parse the standard Org-mode time string.
  12354. This should be a lot faster than the normal `parse-time-string'.
  12355. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12356. hour and minute fields will be nil if not given."
  12357. (if (string-match org-ts-regexp0 s)
  12358. (list 0
  12359. (if (or (match-beginning 8) (not nodefault))
  12360. (string-to-number (or (match-string 8 s) "0")))
  12361. (if (or (match-beginning 7) (not nodefault))
  12362. (string-to-number (or (match-string 7 s) "0")))
  12363. (string-to-number (match-string 4 s))
  12364. (string-to-number (match-string 3 s))
  12365. (string-to-number (match-string 2 s))
  12366. nil nil nil)
  12367. (error "Not a standard Org-mode time string: %s" s)))
  12368. (defun org-timestamp-up (&optional arg)
  12369. "Increase the date item at the cursor by one.
  12370. If the cursor is on the year, change the year. If it is on the month or
  12371. the day, change that.
  12372. With prefix ARG, change by that many units."
  12373. (interactive "p")
  12374. (org-timestamp-change (prefix-numeric-value arg)))
  12375. (defun org-timestamp-down (&optional arg)
  12376. "Decrease the date item at the cursor by one.
  12377. If the cursor is on the year, change the year. If it is on the month or
  12378. the day, change that.
  12379. With prefix ARG, change by that many units."
  12380. (interactive "p")
  12381. (org-timestamp-change (- (prefix-numeric-value arg))))
  12382. (defun org-timestamp-up-day (&optional arg)
  12383. "Increase the date in the time stamp by one day.
  12384. With prefix ARG, change that many days."
  12385. (interactive "p")
  12386. (if (and (not (org-at-timestamp-p t))
  12387. (org-on-heading-p))
  12388. (org-todo 'up)
  12389. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12390. (defun org-timestamp-down-day (&optional arg)
  12391. "Decrease the date in the time stamp by one day.
  12392. With prefix ARG, change that many days."
  12393. (interactive "p")
  12394. (if (and (not (org-at-timestamp-p t))
  12395. (org-on-heading-p))
  12396. (org-todo 'down)
  12397. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12398. (defun org-at-timestamp-p (&optional inactive-ok)
  12399. "Determine if the cursor is in or at a timestamp."
  12400. (interactive)
  12401. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12402. (pos (point))
  12403. (ans (or (looking-at tsr)
  12404. (save-excursion
  12405. (skip-chars-backward "^[<\n\r\t")
  12406. (if (> (point) (point-min)) (backward-char 1))
  12407. (and (looking-at tsr)
  12408. (> (- (match-end 0) pos) -1))))))
  12409. (and ans
  12410. (boundp 'org-ts-what)
  12411. (setq org-ts-what
  12412. (cond
  12413. ((= pos (match-beginning 0)) 'bracket)
  12414. ((= pos (1- (match-end 0))) 'bracket)
  12415. ((org-pos-in-match-range pos 2) 'year)
  12416. ((org-pos-in-match-range pos 3) 'month)
  12417. ((org-pos-in-match-range pos 7) 'hour)
  12418. ((org-pos-in-match-range pos 8) 'minute)
  12419. ((or (org-pos-in-match-range pos 4)
  12420. (org-pos-in-match-range pos 5)) 'day)
  12421. ((and (> pos (or (match-end 8) (match-end 5)))
  12422. (< pos (match-end 0)))
  12423. (- pos (or (match-end 8) (match-end 5))))
  12424. (t 'day))))
  12425. ans))
  12426. (defun org-toggle-timestamp-type ()
  12427. "Toggle the type (<active> or [inactive]) of a time stamp."
  12428. (interactive)
  12429. (when (org-at-timestamp-p t)
  12430. (let ((beg (match-beginning 0)) (end (match-end 0))
  12431. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12432. (save-excursion
  12433. (goto-char beg)
  12434. (while (re-search-forward "[][<>]" end t)
  12435. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12436. t t)))
  12437. (message "Timestamp is now %sactive"
  12438. (if (equal (char-after beg) ?<) "" "in")))))
  12439. (defun org-timestamp-change (n &optional what)
  12440. "Change the date in the time stamp at point.
  12441. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12442. `year', `minute', `second'. If WHAT is not given, the cursor position
  12443. in the timestamp determines what will be changed."
  12444. (let ((pos (point))
  12445. with-hm inactive
  12446. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12447. org-ts-what
  12448. extra rem
  12449. ts time time0)
  12450. (if (not (org-at-timestamp-p t))
  12451. (error "Not at a timestamp"))
  12452. (if (and (not what) (eq org-ts-what 'bracket))
  12453. (org-toggle-timestamp-type)
  12454. (if (and (not what) (not (eq org-ts-what 'day))
  12455. org-display-custom-times
  12456. (get-text-property (point) 'display)
  12457. (not (get-text-property (1- (point)) 'display)))
  12458. (setq org-ts-what 'day))
  12459. (setq org-ts-what (or what org-ts-what)
  12460. inactive (= (char-after (match-beginning 0)) ?\[)
  12461. ts (match-string 0))
  12462. (replace-match "")
  12463. (if (string-match
  12464. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  12465. ts)
  12466. (setq extra (match-string 1 ts)))
  12467. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12468. (setq with-hm t))
  12469. (setq time0 (org-parse-time-string ts))
  12470. (when (and (eq org-ts-what 'minute)
  12471. (eq current-prefix-arg nil))
  12472. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12473. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12474. (setcar (cdr time0) (+ (nth 1 time0)
  12475. (if (> n 0) (- rem) (- dm rem))))))
  12476. (setq time
  12477. (encode-time (or (car time0) 0)
  12478. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12479. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12480. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12481. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12482. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12483. (nthcdr 6 time0)))
  12484. (when (and (member org-ts-what '(hour minute))
  12485. extra
  12486. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12487. (setq extra (org-modify-ts-extra
  12488. extra
  12489. (if (eq org-ts-what 'hour) 2 5)
  12490. n dm)))
  12491. (when (integerp org-ts-what)
  12492. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12493. (if (eq what 'calendar)
  12494. (let ((cal-date (org-get-date-from-calendar)))
  12495. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12496. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12497. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12498. (setcar time0 (or (car time0) 0))
  12499. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12500. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12501. (setq time (apply 'encode-time time0))))
  12502. (setq org-last-changed-timestamp
  12503. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12504. (org-clock-update-time-maybe)
  12505. (goto-char pos)
  12506. ;; Try to recenter the calendar window, if any
  12507. (if (and org-calendar-follow-timestamp-change
  12508. (get-buffer-window "*Calendar*" t)
  12509. (memq org-ts-what '(day month year)))
  12510. (org-recenter-calendar (time-to-days time))))))
  12511. (defun org-modify-ts-extra (s pos n dm)
  12512. "Change the different parts of the lead-time and repeat fields in timestamp."
  12513. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12514. ng h m new rem)
  12515. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12516. (cond
  12517. ((or (org-pos-in-match-range pos 2)
  12518. (org-pos-in-match-range pos 3))
  12519. (setq m (string-to-number (match-string 3 s))
  12520. h (string-to-number (match-string 2 s)))
  12521. (if (org-pos-in-match-range pos 2)
  12522. (setq h (+ h n))
  12523. (setq n (* dm (org-no-warnings (signum n))))
  12524. (when (not (= 0 (setq rem (% m dm))))
  12525. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12526. (setq m (+ m n)))
  12527. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12528. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12529. (setq h (min 24 (max 0 h)))
  12530. (setq ng 1 new (format "-%02d:%02d" h m)))
  12531. ((org-pos-in-match-range pos 6)
  12532. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12533. ((org-pos-in-match-range pos 5)
  12534. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12535. ((org-pos-in-match-range pos 9)
  12536. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12537. ((org-pos-in-match-range pos 8)
  12538. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12539. (when ng
  12540. (setq s (concat
  12541. (substring s 0 (match-beginning ng))
  12542. new
  12543. (substring s (match-end ng))))))
  12544. s))
  12545. (defun org-recenter-calendar (date)
  12546. "If the calendar is visible, recenter it to DATE."
  12547. (let* ((win (selected-window))
  12548. (cwin (get-buffer-window "*Calendar*" t))
  12549. (calendar-move-hook nil))
  12550. (when cwin
  12551. (select-window cwin)
  12552. (calendar-goto-date (if (listp date) date
  12553. (calendar-gregorian-from-absolute date)))
  12554. (select-window win))))
  12555. (defun org-goto-calendar (&optional arg)
  12556. "Go to the Emacs calendar at the current date.
  12557. If there is a time stamp in the current line, go to that date.
  12558. A prefix ARG can be used to force the current date."
  12559. (interactive "P")
  12560. (let ((tsr org-ts-regexp) diff
  12561. (calendar-move-hook nil)
  12562. (calendar-view-holidays-initially-flag nil)
  12563. (view-calendar-holidays-initially nil)
  12564. (calendar-view-diary-initially-flag nil)
  12565. (view-diary-entries-initially nil))
  12566. (if (or (org-at-timestamp-p)
  12567. (save-excursion
  12568. (beginning-of-line 1)
  12569. (looking-at (concat ".*" tsr))))
  12570. (let ((d1 (time-to-days (current-time)))
  12571. (d2 (time-to-days
  12572. (org-time-string-to-time (match-string 1)))))
  12573. (setq diff (- d2 d1))))
  12574. (calendar)
  12575. (calendar-goto-today)
  12576. (if (and diff (not arg)) (calendar-forward-day diff))))
  12577. (defun org-get-date-from-calendar ()
  12578. "Return a list (month day year) of date at point in calendar."
  12579. (with-current-buffer "*Calendar*"
  12580. (save-match-data
  12581. (calendar-cursor-to-date))))
  12582. (defun org-date-from-calendar ()
  12583. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12584. If there is already a time stamp at the cursor position, update it."
  12585. (interactive)
  12586. (if (org-at-timestamp-p t)
  12587. (org-timestamp-change 0 'calendar)
  12588. (let ((cal-date (org-get-date-from-calendar)))
  12589. (org-insert-time-stamp
  12590. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12591. (defun org-minutes-to-hh:mm-string (m)
  12592. "Compute H:MM from a number of minutes."
  12593. (let ((h (/ m 60)))
  12594. (setq m (- m (* 60 h)))
  12595. (format org-time-clocksum-format h m)))
  12596. (defun org-hh:mm-string-to-minutes (s)
  12597. "Convert a string H:MM to a number of minutes.
  12598. If the string is just a number, interprete it as minutes.
  12599. In fact, the first hh:mm or number in the string will be taken,
  12600. there can be extra stuff in the string.
  12601. If no number is found, the return value is 0."
  12602. (cond
  12603. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12604. (+ (* (string-to-number (match-string 1 s)) 60)
  12605. (string-to-number (match-string 2 s))))
  12606. ((string-match "\\([0-9]+\\)" s)
  12607. (string-to-number (match-string 1 s)))
  12608. (t 0)))
  12609. ;;;; Files
  12610. (defun org-save-all-org-buffers ()
  12611. "Save all Org-mode buffers without user confirmation."
  12612. (interactive)
  12613. (message "Saving all Org-mode buffers...")
  12614. (save-some-buffers t 'org-mode-p)
  12615. (when (featurep 'org-id) (org-id-locations-save))
  12616. (message "Saving all Org-mode buffers... done"))
  12617. (defun org-revert-all-org-buffers ()
  12618. "Revert all Org-mode buffers.
  12619. Prompt for confirmation when there are unsaved changes.
  12620. Be sure you know what you are doing before letting this function
  12621. overwrite your changes.
  12622. This function is useful in a setup where one tracks org files
  12623. with a version control system, to revert on one machine after pulling
  12624. changes from another. I believe the procedure must be like this:
  12625. 1. M-x org-save-all-org-buffers
  12626. 2. Pull changes from the other machine, resolve conflicts
  12627. 3. M-x org-revert-all-org-buffers"
  12628. (interactive)
  12629. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12630. (error "Abort"))
  12631. (save-excursion
  12632. (save-window-excursion
  12633. (mapc
  12634. (lambda (b)
  12635. (when (and (with-current-buffer b (org-mode-p))
  12636. (with-current-buffer b buffer-file-name))
  12637. (switch-to-buffer b)
  12638. (revert-buffer t 'no-confirm)))
  12639. (buffer-list))
  12640. (when (and (featurep 'org-id) org-id-track-globally)
  12641. (org-id-locations-load)))))
  12642. ;;;; Agenda files
  12643. ;;;###autoload
  12644. (defun org-iswitchb (&optional arg)
  12645. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12646. With a prefix argument, restrict available to files.
  12647. With two prefix arguments, restrict available buffers to agenda files."
  12648. (interactive "P")
  12649. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12650. ((equal arg '(16)) (org-buffer-list 'agenda))
  12651. (t (org-buffer-list)))))
  12652. (switch-to-buffer
  12653. (org-icompleting-read "Org buffer: "
  12654. (mapcar 'list (mapcar 'buffer-name blist))
  12655. nil t))))
  12656. ;;;###autoload
  12657. (defalias 'org-ido-switchb 'org-iswitchb)
  12658. (defun org-buffer-list (&optional predicate exclude-tmp)
  12659. "Return a list of Org buffers.
  12660. PREDICATE can be `export', `files' or `agenda'.
  12661. export restrict the list to Export buffers.
  12662. files restrict the list to buffers visiting Org files.
  12663. agenda restrict the list to buffers visiting agenda files.
  12664. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12665. (let* ((bfn nil)
  12666. (agenda-files (and (eq predicate 'agenda)
  12667. (mapcar 'file-truename (org-agenda-files t))))
  12668. (filter
  12669. (cond
  12670. ((eq predicate 'files)
  12671. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12672. ((eq predicate 'export)
  12673. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12674. ((eq predicate 'agenda)
  12675. (lambda (b)
  12676. (with-current-buffer b
  12677. (and (eq major-mode 'org-mode)
  12678. (setq bfn (buffer-file-name b))
  12679. (member (file-truename bfn) agenda-files)))))
  12680. (t (lambda (b) (with-current-buffer b
  12681. (or (eq major-mode 'org-mode)
  12682. (string-match "\*Org .*Export"
  12683. (buffer-name b)))))))))
  12684. (delq nil
  12685. (mapcar
  12686. (lambda(b)
  12687. (if (and (funcall filter b)
  12688. (or (not exclude-tmp)
  12689. (not (string-match "tmp" (buffer-name b)))))
  12690. b
  12691. nil))
  12692. (buffer-list)))))
  12693. (defun org-agenda-files (&optional unrestricted archives)
  12694. "Get the list of agenda files.
  12695. Optional UNRESTRICTED means return the full list even if a restriction
  12696. is currently in place.
  12697. When ARCHIVES is t, include all archive files hat are really being
  12698. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12699. `org-agenda-archives-mode' is t."
  12700. (let ((files
  12701. (cond
  12702. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12703. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12704. ((listp org-agenda-files) org-agenda-files)
  12705. (t (error "Invalid value of `org-agenda-files'")))))
  12706. (setq files (apply 'append
  12707. (mapcar (lambda (f)
  12708. (if (file-directory-p f)
  12709. (directory-files
  12710. f t org-agenda-file-regexp)
  12711. (list f)))
  12712. files)))
  12713. (when org-agenda-skip-unavailable-files
  12714. (setq files (delq nil
  12715. (mapcar (function
  12716. (lambda (file)
  12717. (and (file-readable-p file) file)))
  12718. files))))
  12719. (when (or (eq archives t)
  12720. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12721. (setq files (org-add-archive-files files)))
  12722. files))
  12723. (defun org-edit-agenda-file-list ()
  12724. "Edit the list of agenda files.
  12725. Depending on setup, this either uses customize to edit the variable
  12726. `org-agenda-files', or it visits the file that is holding the list. In the
  12727. latter case, the buffer is set up in a way that saving it automatically kills
  12728. the buffer and restores the previous window configuration."
  12729. (interactive)
  12730. (if (stringp org-agenda-files)
  12731. (let ((cw (current-window-configuration)))
  12732. (find-file org-agenda-files)
  12733. (org-set-local 'org-window-configuration cw)
  12734. (org-add-hook 'after-save-hook
  12735. (lambda ()
  12736. (set-window-configuration
  12737. (prog1 org-window-configuration
  12738. (kill-buffer (current-buffer))))
  12739. (org-install-agenda-files-menu)
  12740. (message "New agenda file list installed"))
  12741. nil 'local)
  12742. (message "%s" (substitute-command-keys
  12743. "Edit list and finish with \\[save-buffer]")))
  12744. (customize-variable 'org-agenda-files)))
  12745. (defun org-store-new-agenda-file-list (list)
  12746. "Set new value for the agenda file list and save it correctly."
  12747. (if (stringp org-agenda-files)
  12748. (let ((f org-agenda-files) b)
  12749. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12750. (with-temp-file f
  12751. (insert (mapconcat 'identity list "\n") "\n")))
  12752. (let ((org-mode-hook nil) (org-inhibit-startup t)
  12753. (org-insert-mode-line-in-empty-file nil))
  12754. (setq org-agenda-files list)
  12755. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12756. (defun org-read-agenda-file-list ()
  12757. "Read the list of agenda files from a file."
  12758. (when (file-directory-p org-agenda-files)
  12759. (error "`org-agenda-files' cannot be a single directory"))
  12760. (when (stringp org-agenda-files)
  12761. (with-temp-buffer
  12762. (insert-file-contents org-agenda-files)
  12763. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12764. ;;;###autoload
  12765. (defun org-cycle-agenda-files ()
  12766. "Cycle through the files in `org-agenda-files'.
  12767. If the current buffer visits an agenda file, find the next one in the list.
  12768. If the current buffer does not, find the first agenda file."
  12769. (interactive)
  12770. (let* ((fs (org-agenda-files t))
  12771. (files (append fs (list (car fs))))
  12772. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12773. file)
  12774. (unless files (error "No agenda files"))
  12775. (catch 'exit
  12776. (while (setq file (pop files))
  12777. (if (equal (file-truename file) tcf)
  12778. (when (car files)
  12779. (find-file (car files))
  12780. (throw 'exit t))))
  12781. (find-file (car fs)))
  12782. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12783. (defun org-agenda-file-to-front (&optional to-end)
  12784. "Move/add the current file to the top of the agenda file list.
  12785. If the file is not present in the list, it is added to the front. If it is
  12786. present, it is moved there. With optional argument TO-END, add/move to the
  12787. end of the list."
  12788. (interactive "P")
  12789. (let ((org-agenda-skip-unavailable-files nil)
  12790. (file-alist (mapcar (lambda (x)
  12791. (cons (file-truename x) x))
  12792. (org-agenda-files t)))
  12793. (ctf (file-truename buffer-file-name))
  12794. x had)
  12795. (setq x (assoc ctf file-alist) had x)
  12796. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12797. (if to-end
  12798. (setq file-alist (append (delq x file-alist) (list x)))
  12799. (setq file-alist (cons x (delq x file-alist))))
  12800. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12801. (org-install-agenda-files-menu)
  12802. (message "File %s to %s of agenda file list"
  12803. (if had "moved" "added") (if to-end "end" "front"))))
  12804. (defun org-remove-file (&optional file)
  12805. "Remove current file from the list of files in variable `org-agenda-files'.
  12806. These are the files which are being checked for agenda entries.
  12807. Optional argument FILE means, use this file instead of the current."
  12808. (interactive)
  12809. (let* ((org-agenda-skip-unavailable-files nil)
  12810. (file (or file buffer-file-name))
  12811. (true-file (file-truename file))
  12812. (afile (abbreviate-file-name file))
  12813. (files (delq nil (mapcar
  12814. (lambda (x)
  12815. (if (equal true-file
  12816. (file-truename x))
  12817. nil x))
  12818. (org-agenda-files t)))))
  12819. (if (not (= (length files) (length (org-agenda-files t))))
  12820. (progn
  12821. (org-store-new-agenda-file-list files)
  12822. (org-install-agenda-files-menu)
  12823. (message "Removed file: %s" afile))
  12824. (message "File was not in list: %s (not removed)" afile))))
  12825. (defun org-file-menu-entry (file)
  12826. (vector file (list 'find-file file) t))
  12827. (defun org-check-agenda-file (file)
  12828. "Make sure FILE exists. If not, ask user what to do."
  12829. (when (not (file-exists-p file))
  12830. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  12831. (abbreviate-file-name file))
  12832. (let ((r (downcase (read-char-exclusive))))
  12833. (cond
  12834. ((equal r ?r)
  12835. (org-remove-file file)
  12836. (throw 'nextfile t))
  12837. (t (error "Abort"))))))
  12838. (defun org-get-agenda-file-buffer (file)
  12839. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12840. it to the list of buffers which might be released later."
  12841. (let ((buf (org-find-base-buffer-visiting file)))
  12842. (if buf
  12843. buf ; just return it
  12844. ;; Make a new buffer and remember it
  12845. (setq buf (find-file-noselect file))
  12846. (if buf (push buf org-agenda-new-buffers))
  12847. buf)))
  12848. (defun org-release-buffers (blist)
  12849. "Release all buffers in list, asking the user for confirmation when needed.
  12850. When a buffer is unmodified, it is just killed. When modified, it is saved
  12851. \(if the user agrees) and then killed."
  12852. (let (buf file)
  12853. (while (setq buf (pop blist))
  12854. (setq file (buffer-file-name buf))
  12855. (when (and (buffer-modified-p buf)
  12856. file
  12857. (y-or-n-p (format "Save file %s? " file)))
  12858. (with-current-buffer buf (save-buffer)))
  12859. (kill-buffer buf))))
  12860. (defun org-prepare-agenda-buffers (files)
  12861. "Create buffers for all agenda files, protect archived trees and comments."
  12862. (interactive)
  12863. (let ((pa '(:org-archived t))
  12864. (pc '(:org-comment t))
  12865. (pall '(:org-archived t :org-comment t))
  12866. (inhibit-read-only t)
  12867. (rea (concat ":" org-archive-tag ":"))
  12868. bmp file re)
  12869. (save-excursion
  12870. (save-restriction
  12871. (while (setq file (pop files))
  12872. (catch 'nextfile
  12873. (if (bufferp file)
  12874. (set-buffer file)
  12875. (org-check-agenda-file file)
  12876. (set-buffer (org-get-agenda-file-buffer file)))
  12877. (widen)
  12878. (setq bmp (buffer-modified-p))
  12879. (org-refresh-category-properties)
  12880. (setq org-todo-keywords-for-agenda
  12881. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12882. (setq org-done-keywords-for-agenda
  12883. (append org-done-keywords-for-agenda org-done-keywords))
  12884. (setq org-todo-keyword-alist-for-agenda
  12885. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12886. (setq org-drawers-for-agenda
  12887. (append org-drawers-for-agenda org-drawers))
  12888. (setq org-tag-alist-for-agenda
  12889. (append org-tag-alist-for-agenda org-tag-alist))
  12890. (save-excursion
  12891. (remove-text-properties (point-min) (point-max) pall)
  12892. (when org-agenda-skip-archived-trees
  12893. (goto-char (point-min))
  12894. (while (re-search-forward rea nil t)
  12895. (if (org-on-heading-p t)
  12896. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12897. (goto-char (point-min))
  12898. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12899. (while (re-search-forward re nil t)
  12900. (add-text-properties
  12901. (match-beginning 0) (org-end-of-subtree t) pc)))
  12902. (set-buffer-modified-p bmp)))))
  12903. (setq org-todo-keyword-alist-for-agenda
  12904. (org-uniquify org-todo-keyword-alist-for-agenda)
  12905. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12906. ;;;; Embedded LaTeX
  12907. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12908. "Keymap for the minor `org-cdlatex-mode'.")
  12909. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12910. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12911. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12912. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12913. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12914. (defvar org-cdlatex-texmathp-advice-is-done nil
  12915. "Flag remembering if we have applied the advice to texmathp already.")
  12916. (define-minor-mode org-cdlatex-mode
  12917. "Toggle the minor `org-cdlatex-mode'.
  12918. This mode supports entering LaTeX environment and math in LaTeX fragments
  12919. in Org-mode.
  12920. \\{org-cdlatex-mode-map}"
  12921. nil " OCDL" nil
  12922. (when org-cdlatex-mode (require 'cdlatex))
  12923. (unless org-cdlatex-texmathp-advice-is-done
  12924. (setq org-cdlatex-texmathp-advice-is-done t)
  12925. (defadvice texmathp (around org-math-always-on activate)
  12926. "Always return t in org-mode buffers.
  12927. This is because we want to insert math symbols without dollars even outside
  12928. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12929. an embedded LaTeX fragment, let texmathp do its job.
  12930. \\[org-cdlatex-mode-map]"
  12931. (interactive)
  12932. (let (p)
  12933. (cond
  12934. ((not (org-mode-p)) ad-do-it)
  12935. ((eq this-command 'cdlatex-math-symbol)
  12936. (setq ad-return-value t
  12937. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12938. (t
  12939. (let ((p (org-inside-LaTeX-fragment-p)))
  12940. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12941. (setq ad-return-value t
  12942. texmathp-why '("Org-mode embedded math" . 0))
  12943. (if p ad-do-it)))))))))
  12944. (defun turn-on-org-cdlatex ()
  12945. "Unconditionally turn on `org-cdlatex-mode'."
  12946. (org-cdlatex-mode 1))
  12947. (defun org-inside-LaTeX-fragment-p ()
  12948. "Test if point is inside a LaTeX fragment.
  12949. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12950. sequence appearing also before point.
  12951. Even though the matchers for math are configurable, this function assumes
  12952. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12953. delimiters are skipped when they have been removed by customization.
  12954. The return value is nil, or a cons cell with the delimiter and
  12955. and the position of this delimiter.
  12956. This function does a reasonably good job, but can locally be fooled by
  12957. for example currency specifications. For example it will assume being in
  12958. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12959. fragments that are properly closed, but during editing, we have to live
  12960. with the uncertainty caused by missing closing delimiters. This function
  12961. looks only before point, not after."
  12962. (catch 'exit
  12963. (let ((pos (point))
  12964. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12965. (lim (progn
  12966. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12967. (point)))
  12968. dd-on str (start 0) m re)
  12969. (goto-char pos)
  12970. (when dodollar
  12971. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12972. re (nth 1 (assoc "$" org-latex-regexps)))
  12973. (while (string-match re str start)
  12974. (cond
  12975. ((= (match-end 0) (length str))
  12976. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12977. ((= (match-end 0) (- (length str) 5))
  12978. (throw 'exit nil))
  12979. (t (setq start (match-end 0))))))
  12980. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12981. (goto-char pos)
  12982. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12983. (and (match-beginning 2) (throw 'exit nil))
  12984. ;; count $$
  12985. (while (re-search-backward "\\$\\$" lim t)
  12986. (setq dd-on (not dd-on)))
  12987. (goto-char pos)
  12988. (if dd-on (cons "$$" m))))))
  12989. (defun org-try-cdlatex-tab ()
  12990. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12991. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12992. - inside a LaTeX fragment, or
  12993. - after the first word in a line, where an abbreviation expansion could
  12994. insert a LaTeX environment."
  12995. (when org-cdlatex-mode
  12996. (cond
  12997. ((save-excursion
  12998. (skip-chars-backward "a-zA-Z0-9*")
  12999. (skip-chars-backward " \t")
  13000. (bolp))
  13001. (cdlatex-tab) t)
  13002. ((org-inside-LaTeX-fragment-p)
  13003. (cdlatex-tab) t)
  13004. (t nil))))
  13005. (defun org-cdlatex-underscore-caret (&optional arg)
  13006. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13007. Revert to the normal definition outside of these fragments."
  13008. (interactive "P")
  13009. (if (org-inside-LaTeX-fragment-p)
  13010. (call-interactively 'cdlatex-sub-superscript)
  13011. (let (org-cdlatex-mode)
  13012. (call-interactively (key-binding (vector last-input-event))))))
  13013. (defun org-cdlatex-math-modify (&optional arg)
  13014. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13015. Revert to the normal definition outside of these fragments."
  13016. (interactive "P")
  13017. (if (org-inside-LaTeX-fragment-p)
  13018. (call-interactively 'cdlatex-math-modify)
  13019. (let (org-cdlatex-mode)
  13020. (call-interactively (key-binding (vector last-input-event))))))
  13021. (defvar org-latex-fragment-image-overlays nil
  13022. "List of overlays carrying the images of latex fragments.")
  13023. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13024. (defun org-remove-latex-fragment-image-overlays ()
  13025. "Remove all overlays with LaTeX fragment images in current buffer."
  13026. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13027. (setq org-latex-fragment-image-overlays nil))
  13028. (defun org-preview-latex-fragment (&optional subtree)
  13029. "Preview the LaTeX fragment at point, or all locally or globally.
  13030. If the cursor is in a LaTeX fragment, create the image and overlay
  13031. it over the source code. If there is no fragment at point, display
  13032. all fragments in the current text, from one headline to the next. With
  13033. prefix SUBTREE, display all fragments in the current subtree. With a
  13034. double prefix `C-u C-u', or when the cursor is before the first headline,
  13035. display all fragments in the buffer.
  13036. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13037. (interactive "P")
  13038. (org-remove-latex-fragment-image-overlays)
  13039. (save-excursion
  13040. (save-restriction
  13041. (let (beg end at msg)
  13042. (cond
  13043. ((or (equal subtree '(16))
  13044. (not (save-excursion
  13045. (re-search-backward (concat "^" outline-regexp) nil t))))
  13046. (setq beg (point-min) end (point-max)
  13047. msg "Creating images for buffer...%s"))
  13048. ((equal subtree '(4))
  13049. (org-back-to-heading)
  13050. (setq beg (point) end (org-end-of-subtree t)
  13051. msg "Creating images for subtree...%s"))
  13052. (t
  13053. (if (setq at (org-inside-LaTeX-fragment-p))
  13054. (goto-char (max (point-min) (- (cdr at) 2)))
  13055. (org-back-to-heading))
  13056. (setq beg (point) end (progn (outline-next-heading) (point))
  13057. msg (if at "Creating image...%s"
  13058. "Creating images for entry...%s"))))
  13059. (message msg "")
  13060. (narrow-to-region beg end)
  13061. (goto-char beg)
  13062. (org-format-latex
  13063. (concat "ltxpng/" (file-name-sans-extension
  13064. (file-name-nondirectory
  13065. buffer-file-name)))
  13066. default-directory 'overlays msg at 'forbuffer)
  13067. (message msg "done. Use `C-c C-c' to remove images.")))))
  13068. (defvar org-latex-regexps
  13069. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13070. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13071. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13072. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13073. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13074. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13075. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13076. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13077. "Regular expressions for matching embedded LaTeX.")
  13078. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13079. "Replace LaTeX fragments with links to an image, and produce images.
  13080. Some of the options can be changed using the variable
  13081. `org-format-latex-options'."
  13082. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13083. (let* ((prefixnodir (file-name-nondirectory prefix))
  13084. (absprefix (expand-file-name prefix dir))
  13085. (todir (file-name-directory absprefix))
  13086. (opt org-format-latex-options)
  13087. (matchers (plist-get opt :matchers))
  13088. (re-list org-latex-regexps)
  13089. (cnt 0) txt link beg end re e checkdir
  13090. executables-checked
  13091. m n block linkfile movefile ov)
  13092. ;; Check if there are old images files with this prefix, and remove them
  13093. (when (file-directory-p todir)
  13094. (mapc 'delete-file
  13095. (directory-files
  13096. todir 'full
  13097. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  13098. ;; Check the different regular expressions
  13099. (while (setq e (pop re-list))
  13100. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13101. block (if (nth 3 e) "\n\n" ""))
  13102. (when (member m matchers)
  13103. (goto-char (point-min))
  13104. (while (re-search-forward re nil t)
  13105. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13106. (not (get-text-property (match-beginning n)
  13107. 'org-protected))
  13108. (or (not overlays)
  13109. (not (eq (get-char-property (match-beginning n)
  13110. 'org-overlay-type)
  13111. 'org-latex-overlay))))
  13112. (setq txt (match-string n)
  13113. beg (match-beginning n) end (match-end n)
  13114. cnt (1+ cnt)
  13115. linkfile (format "%s_%04d.png" prefix cnt)
  13116. movefile (format "%s_%04d.png" absprefix cnt)
  13117. link (concat block "[[file:" linkfile "]]" block))
  13118. (if msg (message msg cnt))
  13119. (goto-char beg)
  13120. (unless checkdir ; make sure the directory exists
  13121. (setq checkdir t)
  13122. (or (file-directory-p todir) (make-directory todir)))
  13123. (unless executables-checked
  13124. (org-check-external-command
  13125. "latex" "needed to convert LaTeX fragments to images")
  13126. (org-check-external-command
  13127. "dvipng" "needed to convert LaTeX fragments to images")
  13128. (setq executables-checked t))
  13129. (org-create-formula-image
  13130. txt movefile opt forbuffer)
  13131. (if overlays
  13132. (progn
  13133. (mapc (lambda (o)
  13134. (if (eq (org-overlay-get o 'org-overlay-type)
  13135. 'org-latex-overlay)
  13136. (org-delete-overlay o)))
  13137. (org-overlays-in beg end))
  13138. (setq ov (org-make-overlay beg end))
  13139. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13140. (if (featurep 'xemacs)
  13141. (progn
  13142. (org-overlay-put ov 'invisible t)
  13143. (org-overlay-put
  13144. ov 'end-glyph
  13145. (make-glyph (vector 'png :file movefile))))
  13146. (org-overlay-put
  13147. ov 'display
  13148. (list 'image :type 'png :file movefile :ascent 'center)))
  13149. (push ov org-latex-fragment-image-overlays)
  13150. (goto-char end))
  13151. (delete-region beg end)
  13152. (insert link))))))))
  13153. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  13154. ;; This function borrows from Ganesh Swami's latex2png.el
  13155. (defun org-create-formula-image (string tofile options buffer)
  13156. "This calls dvipng."
  13157. (require 'org-latex)
  13158. (let* ((tmpdir (if (featurep 'xemacs)
  13159. (temp-directory)
  13160. temporary-file-directory))
  13161. (texfilebase (make-temp-name
  13162. (expand-file-name "orgtex" tmpdir)))
  13163. (texfile (concat texfilebase ".tex"))
  13164. (dvifile (concat texfilebase ".dvi"))
  13165. (pngfile (concat texfilebase ".png"))
  13166. (fnh (if (featurep 'xemacs)
  13167. (font-height (get-face-font 'default))
  13168. (face-attribute 'default :height nil)))
  13169. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13170. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13171. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13172. "Black"))
  13173. (bg (or (plist-get options (if buffer :background :html-background))
  13174. "Transparent")))
  13175. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13176. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13177. (with-temp-file texfile
  13178. (insert org-format-latex-header
  13179. (if org-export-latex-packages-alist
  13180. (concat "\n"
  13181. (mapconcat (lambda(p)
  13182. (if (equal "" (car p))
  13183. (format "\\usepackage{%s}" (cadr p))
  13184. (format "\\usepackage[%s]{%s}"
  13185. (car p) (cadr p))))
  13186. org-export-latex-packages-alist "\n"))
  13187. "")
  13188. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13189. (let ((dir default-directory))
  13190. (condition-case nil
  13191. (progn
  13192. (cd tmpdir)
  13193. (call-process "latex" nil nil nil texfile))
  13194. (error nil))
  13195. (cd dir))
  13196. (if (not (file-exists-p dvifile))
  13197. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13198. (condition-case nil
  13199. (call-process "dvipng" nil nil nil
  13200. "-fg" fg "-bg" bg
  13201. "-D" dpi
  13202. ;;"-x" scale "-y" scale
  13203. "-T" "tight"
  13204. "-o" pngfile
  13205. dvifile)
  13206. (error nil))
  13207. (if (not (file-exists-p pngfile))
  13208. (progn (message "Failed to create png file from %s" texfile) nil)
  13209. ;; Use the requested file name and clean up
  13210. (copy-file pngfile tofile 'replace)
  13211. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13212. (delete-file (concat texfilebase e)))
  13213. pngfile))))
  13214. (defun org-dvipng-color (attr)
  13215. "Return an rgb color specification for dvipng."
  13216. (apply 'format "rgb %s %s %s"
  13217. (mapcar 'org-normalize-color
  13218. (color-values (face-attribute 'default attr nil)))))
  13219. (defun org-normalize-color (value)
  13220. "Return string to be used as color value for an RGB component."
  13221. (format "%g" (/ value 65535.0)))
  13222. ;;;; Key bindings
  13223. ;; Make `C-c C-x' a prefix key
  13224. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13225. ;; TAB key with modifiers
  13226. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13227. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13228. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13229. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13230. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13231. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13232. ;; The following line is necessary under Suse GNU/Linux
  13233. (unless (featurep 'xemacs)
  13234. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13235. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13236. (define-key org-mode-map [backtab] 'org-shifttab)
  13237. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13238. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13239. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13240. ;; Cursor keys with modifiers
  13241. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13242. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13243. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13244. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13245. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13246. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13247. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13248. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13249. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13250. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13251. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13252. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13253. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13254. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13255. ;;; Extra keys for tty access.
  13256. ;; We only set them when really needed because otherwise the
  13257. ;; menus don't show the simple keys
  13258. (when (or org-use-extra-keys
  13259. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13260. (not window-system))
  13261. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13262. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13263. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13264. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13265. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13266. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13267. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13268. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13269. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13270. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13271. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13272. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13273. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13274. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13275. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13276. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13277. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13278. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13279. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13280. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13281. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13282. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13283. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13284. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13285. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13286. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13287. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13288. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13289. ;; All the other keys
  13290. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13291. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13292. (if (boundp 'narrow-map)
  13293. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13294. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13295. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13296. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13297. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13298. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13299. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  13300. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13301. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13302. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13303. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13304. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13305. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13306. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13307. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13308. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13309. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13310. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13311. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13312. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13313. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13314. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13315. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13316. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13317. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13318. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13319. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13320. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13321. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13322. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13323. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13324. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13325. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13326. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13327. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13328. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13329. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13330. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13331. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13332. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13333. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13334. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13335. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13336. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13337. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13338. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13339. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13340. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13341. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13342. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13343. (org-defkey org-mode-map "\C-m" 'org-return)
  13344. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13345. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13346. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13347. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13348. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13349. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13350. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13351. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13352. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13353. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13354. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13355. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13356. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13357. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13358. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13359. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13360. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13361. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  13362. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  13363. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13364. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13365. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13366. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13367. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13368. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13369. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13370. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13371. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13372. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13373. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13374. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13375. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13376. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13377. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13378. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13379. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13380. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13381. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13382. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13383. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13384. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13385. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13386. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13387. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13388. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13389. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13390. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13391. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13392. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13393. (when (featurep 'xemacs)
  13394. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13395. (defconst org-speed-commands-default
  13396. '(
  13397. ("c" . org-cycle)
  13398. ("C" . org-shifttab)
  13399. ("n" . outline-next-visible-heading)
  13400. ("p" . outline-previous-visible-heading)
  13401. ("f" . org-forward-same-level)
  13402. ("b" . org-backward-same-level)
  13403. ("u" . outline-up-heading)
  13404. ("U" . org-shiftmetaup)
  13405. ("D" . org-shiftmetadown)
  13406. ("r" . org-metaright)
  13407. ("l" . org-metaleft)
  13408. ("R" . org-shiftmetaright)
  13409. ("L" . org-shiftmetaleft)
  13410. ("i" . (progn (forward-char 1) (call-interactively
  13411. 'org-insert-heading-respect-content)))
  13412. ("o" . org-open-at-point)
  13413. ("t" . org-todo)
  13414. ("j" . org-goto)
  13415. ("1" . (org-priority ?A))
  13416. ("2" . (org-priority ?B))
  13417. ("3" . (org-priority ?C))
  13418. ("." . outline-mark-subtree)
  13419. ("^" . org-sort)
  13420. ("w" . org-refile)
  13421. ("a" . org-archive-subtree-default-with-confirmation)
  13422. ("/" . org-sparse-tree)
  13423. ("?" . org-speed-command-help)
  13424. )
  13425. "The default speed commands.")
  13426. (defun org-print-speed-command (e)
  13427. (princ (car e))
  13428. (princ " ")
  13429. (if (symbolp (cdr e))
  13430. (princ (symbol-name (cdr e)))
  13431. (prin1 (cdr e)))
  13432. (princ "\n"))
  13433. (defun org-speed-command-help ()
  13434. "Show the available speed commands."
  13435. (interactive)
  13436. (if (not org-use-speed-commands)
  13437. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  13438. (with-output-to-temp-buffer "*Help*"
  13439. (princ "Speed commands\n==============\n")
  13440. (mapc 'org-print-speed-command org-speed-commands-user)
  13441. (princ "\n")
  13442. (mapc 'org-print-speed-command org-speed-commands-default))))
  13443. (defvar org-self-insert-command-undo-counter 0)
  13444. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13445. (defvar org-speed-command nil)
  13446. (defun org-self-insert-command (N)
  13447. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13448. If the cursor is in a table looking at whitespace, the whitespace is
  13449. overwritten, and the table is not marked as requiring realignment."
  13450. (interactive "p")
  13451. (cond
  13452. ((and org-use-speed-commands
  13453. (bolp)
  13454. (looking-at outline-regexp)
  13455. (setq
  13456. org-speed-command
  13457. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  13458. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  13459. (cond
  13460. ((commandp org-speed-command)
  13461. (setq this-command org-speed-command)
  13462. (call-interactively org-speed-command))
  13463. ((functionp org-speed-command)
  13464. (funcall org-speed-command))
  13465. ((and org-speed-command (listp org-speed-command))
  13466. (eval org-speed-command))
  13467. (t (let (org-use-speed-commands)
  13468. (call-interactively 'org-self-insert-command)))))
  13469. ((and
  13470. (org-table-p)
  13471. (progn
  13472. ;; check if we blank the field, and if that triggers align
  13473. (and (featurep 'org-table) org-table-auto-blank-field
  13474. (member last-command
  13475. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13476. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13477. ;; got extra space, this field does not determine column width
  13478. (let (org-table-may-need-update) (org-table-blank-field))
  13479. ;; no extra space, this field may determine column width
  13480. (org-table-blank-field)))
  13481. t)
  13482. (eq N 1)
  13483. (looking-at "[^|\n]* |"))
  13484. (let (org-table-may-need-update)
  13485. (goto-char (1- (match-end 0)))
  13486. (delete-backward-char 1)
  13487. (goto-char (match-beginning 0))
  13488. (self-insert-command N)))
  13489. (t
  13490. (setq org-table-may-need-update t)
  13491. (self-insert-command N)
  13492. (org-fix-tags-on-the-fly)
  13493. (if org-self-insert-cluster-for-undo
  13494. (if (not (eq last-command 'org-self-insert-command))
  13495. (setq org-self-insert-command-undo-counter 1)
  13496. (if (>= org-self-insert-command-undo-counter 20)
  13497. (setq org-self-insert-command-undo-counter 1)
  13498. (and (> org-self-insert-command-undo-counter 0)
  13499. buffer-undo-list
  13500. (not (cadr buffer-undo-list)) ; remove nil entry
  13501. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13502. (setq org-self-insert-command-undo-counter
  13503. (1+ org-self-insert-command-undo-counter))))))))
  13504. (defun org-fix-tags-on-the-fly ()
  13505. (when (and (equal (char-after (point-at-bol)) ?*)
  13506. (org-on-heading-p))
  13507. (org-align-tags-here org-tags-column)))
  13508. (defun org-delete-backward-char (N)
  13509. "Like `delete-backward-char', insert whitespace at field end in tables.
  13510. When deleting backwards, in tables this function will insert whitespace in
  13511. front of the next \"|\" separator, to keep the table aligned. The table will
  13512. still be marked for re-alignment if the field did fill the entire column,
  13513. because, in this case the deletion might narrow the column."
  13514. (interactive "p")
  13515. (if (and (org-table-p)
  13516. (eq N 1)
  13517. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13518. (looking-at ".*?|"))
  13519. (let ((pos (point))
  13520. (noalign (looking-at "[^|\n\r]* |"))
  13521. (c org-table-may-need-update))
  13522. (backward-delete-char N)
  13523. (skip-chars-forward "^|")
  13524. (insert " ")
  13525. (goto-char (1- pos))
  13526. ;; noalign: if there were two spaces at the end, this field
  13527. ;; does not determine the width of the column.
  13528. (if noalign (setq org-table-may-need-update c)))
  13529. (backward-delete-char N)
  13530. (org-fix-tags-on-the-fly)))
  13531. (defun org-delete-char (N)
  13532. "Like `delete-char', but insert whitespace at field end in tables.
  13533. When deleting characters, in tables this function will insert whitespace in
  13534. front of the next \"|\" separator, to keep the table aligned. The table will
  13535. still be marked for re-alignment if the field did fill the entire column,
  13536. because, in this case the deletion might narrow the column."
  13537. (interactive "p")
  13538. (if (and (org-table-p)
  13539. (not (bolp))
  13540. (not (= (char-after) ?|))
  13541. (eq N 1))
  13542. (if (looking-at ".*?|")
  13543. (let ((pos (point))
  13544. (noalign (looking-at "[^|\n\r]* |"))
  13545. (c org-table-may-need-update))
  13546. (replace-match (concat
  13547. (substring (match-string 0) 1 -1)
  13548. " |"))
  13549. (goto-char pos)
  13550. ;; noalign: if there were two spaces at the end, this field
  13551. ;; does not determine the width of the column.
  13552. (if noalign (setq org-table-may-need-update c)))
  13553. (delete-char N))
  13554. (delete-char N)
  13555. (org-fix-tags-on-the-fly)))
  13556. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13557. (put 'org-self-insert-command 'delete-selection t)
  13558. (put 'orgtbl-self-insert-command 'delete-selection t)
  13559. (put 'org-delete-char 'delete-selection 'supersede)
  13560. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13561. (put 'org-yank 'delete-selection 'yank)
  13562. ;; Make `flyspell-mode' delay after some commands
  13563. (put 'org-self-insert-command 'flyspell-delayed t)
  13564. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13565. (put 'org-delete-char 'flyspell-delayed t)
  13566. (put 'org-delete-backward-char 'flyspell-delayed t)
  13567. ;; Make pabbrev-mode expand after org-mode commands
  13568. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13569. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13570. ;; How to do this: Measure non-white length of current string
  13571. ;; If equal to column width, we should realign.
  13572. (defun org-remap (map &rest commands)
  13573. "In MAP, remap the functions given in COMMANDS.
  13574. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13575. (let (new old)
  13576. (while commands
  13577. (setq old (pop commands) new (pop commands))
  13578. (if (fboundp 'command-remapping)
  13579. (org-defkey map (vector 'remap old) new)
  13580. (substitute-key-definition old new map global-map)))))
  13581. (when (eq org-enable-table-editor 'optimized)
  13582. ;; If the user wants maximum table support, we need to hijack
  13583. ;; some standard editing functions
  13584. (org-remap org-mode-map
  13585. 'self-insert-command 'org-self-insert-command
  13586. 'delete-char 'org-delete-char
  13587. 'delete-backward-char 'org-delete-backward-char)
  13588. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13589. (defvar org-ctrl-c-ctrl-c-hook nil
  13590. "Hook for functions attaching themselves to `C-c C-c'.
  13591. This can be used to add additional functionality to the C-c C-c key which
  13592. executes context-dependent commands.
  13593. Each function will be called with no arguments. The function must check
  13594. if the context is appropriate for it to act. If yes, it should do its
  13595. thing and then return a non-nil value. If the context is wrong,
  13596. just do nothing and return nil.")
  13597. (defvar org-tab-first-hook nil
  13598. "Hook for functions to attach themselves to TAB.
  13599. See `org-ctrl-c-ctrl-c-hook' for more information.
  13600. This hook runs as the first action when TAB is pressed, even before
  13601. `org-cycle' messes around with the `outline-regexp' to cater for
  13602. inline tasks and plain list item folding.
  13603. If any function in this hook returns t, not other actions like table
  13604. field motion visibility cycling will be done.")
  13605. (defvar org-tab-after-check-for-table-hook nil
  13606. "Hook for functions to attach themselves to TAB.
  13607. See `org-ctrl-c-ctrl-c-hook' for more information.
  13608. This hook runs after it has been established that the cursor is not in a
  13609. table, but before checking if the cursor is in a headline or if global cycling
  13610. should be done.
  13611. If any function in this hook returns t, not other actions like visibility
  13612. cycling will be done.")
  13613. (defvar org-tab-after-check-for-cycling-hook nil
  13614. "Hook for functions to attach themselves to TAB.
  13615. See `org-ctrl-c-ctrl-c-hook' for more information.
  13616. This hook runs after it has been established that not table field motion and
  13617. not visibility should be done because of current context. This is probably
  13618. the place where a package like yasnippets can hook in.")
  13619. (defvar org-metaleft-hook nil
  13620. "Hook for functions attaching themselves to `M-left'.
  13621. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13622. (defvar org-metaright-hook nil
  13623. "Hook for functions attaching themselves to `M-right'.
  13624. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13625. (defvar org-metaup-hook nil
  13626. "Hook for functions attaching themselves to `M-up'.
  13627. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13628. (defvar org-metadown-hook nil
  13629. "Hook for functions attaching themselves to `M-down'.
  13630. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13631. (defvar org-shiftmetaleft-hook nil
  13632. "Hook for functions attaching themselves to `M-S-left'.
  13633. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13634. (defvar org-shiftmetaright-hook nil
  13635. "Hook for functions attaching themselves to `M-S-right'.
  13636. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13637. (defvar org-shiftmetaup-hook nil
  13638. "Hook for functions attaching themselves to `M-S-up'.
  13639. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13640. (defvar org-shiftmetadown-hook nil
  13641. "Hook for functions attaching themselves to `M-S-down'.
  13642. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13643. (defvar org-metareturn-hook nil
  13644. "Hook for functions attaching themselves to `M-RET'.
  13645. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13646. (defun org-modifier-cursor-error ()
  13647. "Throw an error, a modified cursor command was applied in wrong context."
  13648. (error "This command is active in special context like tables, headlines or items"))
  13649. (defun org-shiftselect-error ()
  13650. "Throw an error because Shift-Cursor command was applied in wrong context."
  13651. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13652. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  13653. (error "This command works only in special context like headlines or timestamps")))
  13654. (defun org-call-for-shift-select (cmd)
  13655. (let ((this-command-keys-shift-translated t))
  13656. (call-interactively cmd)))
  13657. (defun org-shifttab (&optional arg)
  13658. "Global visibility cycling or move to previous table field.
  13659. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13660. on context.
  13661. See the individual commands for more information."
  13662. (interactive "P")
  13663. (cond
  13664. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13665. ((integerp arg)
  13666. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  13667. (message "Content view to level: %d" arg)
  13668. (org-content (prefix-numeric-value arg2))
  13669. (setq org-cycle-global-status 'overview)))
  13670. (t (call-interactively 'org-global-cycle))))
  13671. (defun org-shiftmetaleft ()
  13672. "Promote subtree or delete table column.
  13673. Calls `org-promote-subtree', `org-outdent-item',
  13674. or `org-table-delete-column', depending on context.
  13675. See the individual commands for more information."
  13676. (interactive)
  13677. (cond
  13678. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13679. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13680. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13681. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13682. (t (org-modifier-cursor-error))))
  13683. (defun org-shiftmetaright ()
  13684. "Demote subtree or insert table column.
  13685. Calls `org-demote-subtree', `org-indent-item',
  13686. or `org-table-insert-column', depending on context.
  13687. See the individual commands for more information."
  13688. (interactive)
  13689. (cond
  13690. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13691. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13692. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13693. ((org-at-item-p) (call-interactively 'org-indent-item))
  13694. (t (org-modifier-cursor-error))))
  13695. (defun org-shiftmetaup (&optional arg)
  13696. "Move subtree up or kill table row.
  13697. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13698. `org-move-item-up' depending on context. See the individual commands
  13699. for more information."
  13700. (interactive "P")
  13701. (cond
  13702. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13703. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13704. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13705. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13706. (t (org-modifier-cursor-error))))
  13707. (defun org-shiftmetadown (&optional arg)
  13708. "Move subtree down or insert table row.
  13709. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13710. `org-move-item-down', depending on context. See the individual
  13711. commands for more information."
  13712. (interactive "P")
  13713. (cond
  13714. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13715. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13716. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13717. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13718. (t (org-modifier-cursor-error))))
  13719. (defun org-metaleft (&optional arg)
  13720. "Promote heading or move table column to left.
  13721. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13722. With no specific context, calls the Emacs default `backward-word'.
  13723. See the individual commands for more information."
  13724. (interactive "P")
  13725. (cond
  13726. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13727. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13728. ((or (org-on-heading-p)
  13729. (and (org-region-active-p)
  13730. (save-excursion
  13731. (goto-char (region-beginning))
  13732. (org-on-heading-p))))
  13733. (call-interactively 'org-do-promote))
  13734. ((or (org-at-item-p)
  13735. (and (org-region-active-p)
  13736. (save-excursion
  13737. (goto-char (region-beginning))
  13738. (org-at-item-p))))
  13739. (call-interactively 'org-outdent-item))
  13740. (t (call-interactively 'backward-word))))
  13741. (defun org-metaright (&optional arg)
  13742. "Demote subtree or move table column to right.
  13743. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13744. With no specific context, calls the Emacs default `forward-word'.
  13745. See the individual commands for more information."
  13746. (interactive "P")
  13747. (cond
  13748. ((run-hook-with-args-until-success 'org-metaright-hook))
  13749. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13750. ((or (org-on-heading-p)
  13751. (and (org-region-active-p)
  13752. (save-excursion
  13753. (goto-char (region-beginning))
  13754. (org-on-heading-p))))
  13755. (call-interactively 'org-do-demote))
  13756. ((or (org-at-item-p)
  13757. (and (org-region-active-p)
  13758. (save-excursion
  13759. (goto-char (region-beginning))
  13760. (org-at-item-p))))
  13761. (call-interactively 'org-indent-item))
  13762. (t (call-interactively 'forward-word))))
  13763. (defun org-metaup (&optional arg)
  13764. "Move subtree up or move table row up.
  13765. Calls `org-move-subtree-up' or `org-table-move-row' or
  13766. `org-move-item-up', depending on context. See the individual commands
  13767. for more information."
  13768. (interactive "P")
  13769. (cond
  13770. ((run-hook-with-args-until-success 'org-metaup-hook))
  13771. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13772. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13773. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13774. (t (transpose-lines 1) (beginning-of-line -1))))
  13775. (defun org-metadown (&optional arg)
  13776. "Move subtree down or move table row down.
  13777. Calls `org-move-subtree-down' or `org-table-move-row' or
  13778. `org-move-item-down', depending on context. See the individual
  13779. commands for more information."
  13780. (interactive "P")
  13781. (cond
  13782. ((run-hook-with-args-until-success 'org-metadown-hook))
  13783. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13784. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13785. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13786. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13787. (defun org-shiftup (&optional arg)
  13788. "Increase item in timestamp or increase priority of current headline.
  13789. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13790. depending on context. See the individual commands for more information."
  13791. (interactive "P")
  13792. (cond
  13793. ((and org-support-shift-select (org-region-active-p))
  13794. (org-call-for-shift-select 'previous-line))
  13795. ((org-at-timestamp-p t)
  13796. (call-interactively (if org-edit-timestamp-down-means-later
  13797. 'org-timestamp-down 'org-timestamp-up)))
  13798. ((and (not (eq org-support-shift-select 'always))
  13799. org-enable-priority-commands
  13800. (org-on-heading-p))
  13801. (call-interactively 'org-priority-up))
  13802. ((and (not org-support-shift-select) (org-at-item-p))
  13803. (call-interactively 'org-previous-item))
  13804. ((org-clocktable-try-shift 'up arg))
  13805. (org-support-shift-select
  13806. (org-call-for-shift-select 'previous-line))
  13807. (t (org-shiftselect-error))))
  13808. (defun org-shiftdown (&optional arg)
  13809. "Decrease item in timestamp or decrease priority of current headline.
  13810. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13811. depending on context. See the individual commands for more information."
  13812. (interactive "P")
  13813. (cond
  13814. ((and org-support-shift-select (org-region-active-p))
  13815. (org-call-for-shift-select 'next-line))
  13816. ((org-at-timestamp-p t)
  13817. (call-interactively (if org-edit-timestamp-down-means-later
  13818. 'org-timestamp-up 'org-timestamp-down)))
  13819. ((and (not (eq org-support-shift-select 'always))
  13820. org-enable-priority-commands
  13821. (org-on-heading-p))
  13822. (call-interactively 'org-priority-down))
  13823. ((and (not org-support-shift-select) (org-at-item-p))
  13824. (call-interactively 'org-next-item))
  13825. ((org-clocktable-try-shift 'down arg))
  13826. (org-support-shift-select
  13827. (org-call-for-shift-select 'next-line))
  13828. (t (org-shiftselect-error))))
  13829. (defun org-shiftright (&optional arg)
  13830. "Cycle the thing at point or in the current line, depending on context.
  13831. Depending on context, this does one of the following:
  13832. - switch a timestamp at point one day into the future
  13833. - on a headline, switch to the next TODO keyword.
  13834. - on an item, switch entire list to the next bullet type
  13835. - on a property line, switch to the next allowed value
  13836. - on a clocktable definition line, move time block into the future"
  13837. (interactive "P")
  13838. (cond
  13839. ((and org-support-shift-select (org-region-active-p))
  13840. (org-call-for-shift-select 'forward-char))
  13841. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13842. ((and (not (eq org-support-shift-select 'always))
  13843. (org-on-heading-p))
  13844. (let ((org-inhibit-logging
  13845. (not org-treat-S-cursor-todo-selection-as-state-change))
  13846. (org-inhibit-blocking
  13847. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13848. (org-call-with-arg 'org-todo 'right)))
  13849. ((or (and org-support-shift-select
  13850. (not (eq org-support-shift-select 'always))
  13851. (org-at-item-bullet-p))
  13852. (and (not org-support-shift-select) (org-at-item-p)))
  13853. (org-call-with-arg 'org-cycle-list-bullet nil))
  13854. ((and (not (eq org-support-shift-select 'always))
  13855. (org-at-property-p))
  13856. (call-interactively 'org-property-next-allowed-value))
  13857. ((org-clocktable-try-shift 'right arg))
  13858. (org-support-shift-select
  13859. (org-call-for-shift-select 'forward-char))
  13860. (t (org-shiftselect-error))))
  13861. (defun org-shiftleft (&optional arg)
  13862. "Cycle the thing at point or in the current line, depending on context.
  13863. Depending on context, this does one of the following:
  13864. - switch a timestamp at point one day into the past
  13865. - on a headline, switch to the previous TODO keyword.
  13866. - on an item, switch entire list to the previous bullet type
  13867. - on a property line, switch to the previous allowed value
  13868. - on a clocktable definition line, move time block into the past"
  13869. (interactive "P")
  13870. (cond
  13871. ((and org-support-shift-select (org-region-active-p))
  13872. (org-call-for-shift-select 'backward-char))
  13873. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13874. ((and (not (eq org-support-shift-select 'always))
  13875. (org-on-heading-p))
  13876. (let ((org-inhibit-logging
  13877. (not org-treat-S-cursor-todo-selection-as-state-change))
  13878. (org-inhibit-blocking
  13879. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13880. (org-call-with-arg 'org-todo 'left)))
  13881. ((or (and org-support-shift-select
  13882. (not (eq org-support-shift-select 'always))
  13883. (org-at-item-bullet-p))
  13884. (and (not org-support-shift-select) (org-at-item-p)))
  13885. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13886. ((and (not (eq org-support-shift-select 'always))
  13887. (org-at-property-p))
  13888. (call-interactively 'org-property-previous-allowed-value))
  13889. ((org-clocktable-try-shift 'left arg))
  13890. (org-support-shift-select
  13891. (org-call-for-shift-select 'backward-char))
  13892. (t (org-shiftselect-error))))
  13893. (defun org-shiftcontrolright ()
  13894. "Switch to next TODO set."
  13895. (interactive)
  13896. (cond
  13897. ((and org-support-shift-select (org-region-active-p))
  13898. (org-call-for-shift-select 'forward-word))
  13899. ((and (not (eq org-support-shift-select 'always))
  13900. (org-on-heading-p))
  13901. (org-call-with-arg 'org-todo 'nextset))
  13902. (org-support-shift-select
  13903. (org-call-for-shift-select 'forward-word))
  13904. (t (org-shiftselect-error))))
  13905. (defun org-shiftcontrolleft ()
  13906. "Switch to previous TODO set."
  13907. (interactive)
  13908. (cond
  13909. ((and org-support-shift-select (org-region-active-p))
  13910. (org-call-for-shift-select 'backward-word))
  13911. ((and (not (eq org-support-shift-select 'always))
  13912. (org-on-heading-p))
  13913. (org-call-with-arg 'org-todo 'previousset))
  13914. (org-support-shift-select
  13915. (org-call-for-shift-select 'backward-word))
  13916. (t (org-shiftselect-error))))
  13917. (defun org-ctrl-c-ret ()
  13918. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13919. (interactive)
  13920. (cond
  13921. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13922. (t (call-interactively 'org-insert-heading))))
  13923. (defun org-copy-special ()
  13924. "Copy region in table or copy current subtree.
  13925. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13926. See the individual commands for more information."
  13927. (interactive)
  13928. (call-interactively
  13929. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13930. (defun org-cut-special ()
  13931. "Cut region in table or cut current subtree.
  13932. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13933. See the individual commands for more information."
  13934. (interactive)
  13935. (call-interactively
  13936. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13937. (defun org-paste-special (arg)
  13938. "Paste rectangular region into table, or past subtree relative to level.
  13939. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13940. See the individual commands for more information."
  13941. (interactive "P")
  13942. (if (org-at-table-p)
  13943. (org-table-paste-rectangle)
  13944. (org-paste-subtree arg)))
  13945. (defun org-edit-special ()
  13946. "Call a special editor for the stuff at point.
  13947. When at a table, call the formula editor with `org-table-edit-formulas'.
  13948. When at the first line of an src example, call `org-edit-src-code'.
  13949. When in an #+include line, visit the include file. Otherwise call
  13950. `ffap' to visit the file at point."
  13951. (interactive)
  13952. (cond
  13953. ((org-at-table-p)
  13954. (call-interactively 'org-table-edit-formulas))
  13955. ((save-excursion
  13956. (beginning-of-line 1)
  13957. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13958. (find-file (org-trim (match-string 1))))
  13959. ((org-edit-src-code))
  13960. ((org-edit-fixed-width-region))
  13961. (t (call-interactively 'ffap))))
  13962. (defun org-ctrl-c-ctrl-c (&optional arg)
  13963. "Set tags in headline, or update according to changed information at point.
  13964. This command does many different things, depending on context:
  13965. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13966. this is what we do.
  13967. - If the cursor is on a statistics cookie, update it.
  13968. - If the cursor is in a headline, prompt for tags and insert them
  13969. into the current line, aligned to `org-tags-column'. When called
  13970. with prefix arg, realign all tags in the current buffer.
  13971. - If the cursor is in one of the special #+KEYWORD lines, this
  13972. triggers scanning the buffer for these lines and updating the
  13973. information.
  13974. - If the cursor is inside a table, realign the table. This command
  13975. works even if the automatic table editor has been turned off.
  13976. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13977. the entire table.
  13978. - If the cursor is at a footnote reference or definition, jump to
  13979. the corresponding definition or references, respectively.
  13980. - If the cursor is a the beginning of a dynamic block, update it.
  13981. - If the cursor is inside a table created by the table.el package,
  13982. activate that table.
  13983. - If the current buffer is a remember buffer, close note and file
  13984. it. A prefix argument of 1 files to the default location
  13985. without further interaction. A prefix argument of 2 files to
  13986. the currently clocking task.
  13987. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13988. links in this buffer.
  13989. - If the cursor is on a numbered item in a plain list, renumber the
  13990. ordered list.
  13991. - If the cursor is on a checkbox, toggle it."
  13992. (interactive "P")
  13993. (let ((org-enable-table-editor t))
  13994. (cond
  13995. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13996. org-occur-highlights
  13997. org-latex-fragment-image-overlays)
  13998. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13999. (org-remove-occur-highlights)
  14000. (org-remove-latex-fragment-image-overlays)
  14001. (message "Temporary highlights/overlays removed from current buffer"))
  14002. ((and (local-variable-p 'org-finish-function (current-buffer))
  14003. (fboundp org-finish-function))
  14004. (funcall org-finish-function))
  14005. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14006. ((org-at-property-p)
  14007. (call-interactively 'org-property-action))
  14008. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14009. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14010. (or (org-on-heading-p) (org-at-item-p)))
  14011. (call-interactively 'org-update-statistics-cookies))
  14012. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14013. ((org-at-table.el-p)
  14014. (require 'table)
  14015. (beginning-of-line 1)
  14016. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14017. (call-interactively 'table-recognize-table))
  14018. ((org-at-table-p)
  14019. (org-table-maybe-eval-formula)
  14020. (if arg
  14021. (call-interactively 'org-table-recalculate)
  14022. (org-table-maybe-recalculate-line))
  14023. (call-interactively 'org-table-align))
  14024. ((or (org-footnote-at-reference-p)
  14025. (org-footnote-at-definition-p))
  14026. (call-interactively 'org-footnote-action))
  14027. ((org-at-item-checkbox-p)
  14028. (call-interactively 'org-toggle-checkbox))
  14029. ((org-at-item-p)
  14030. (if arg
  14031. (call-interactively 'org-toggle-checkbox)
  14032. (call-interactively 'org-maybe-renumber-ordered-list)))
  14033. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14034. ;; Dynamic block
  14035. (beginning-of-line 1)
  14036. (save-excursion (org-update-dblock)))
  14037. ((save-excursion
  14038. (beginning-of-line 1)
  14039. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14040. (cond
  14041. ((equal (match-string 1) "TBLFM")
  14042. ;; Recalculate the table before this line
  14043. (save-excursion
  14044. (beginning-of-line 1)
  14045. (skip-chars-backward " \r\n\t")
  14046. (if (org-at-table-p)
  14047. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14048. (t
  14049. ; (org-set-regexps-and-options)
  14050. ; (org-restart-font-lock)
  14051. (let ((org-inhibit-startup t)) (org-mode-restart))
  14052. (message "Local setup has been refreshed"))))
  14053. ((org-clock-update-time-maybe))
  14054. (t (error "C-c C-c can do nothing useful at this location")))))
  14055. (defun org-mode-restart ()
  14056. "Restart Org-mode, to scan again for special lines.
  14057. Also updates the keyword regular expressions."
  14058. (interactive)
  14059. (org-mode)
  14060. (message "Org-mode restarted"))
  14061. (defun org-kill-note-or-show-branches ()
  14062. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14063. (interactive)
  14064. (if (not org-finish-function)
  14065. (call-interactively 'show-branches)
  14066. (let ((org-note-abort t))
  14067. (funcall org-finish-function))))
  14068. (defun org-return (&optional indent)
  14069. "Goto next table row or insert a newline.
  14070. Calls `org-table-next-row' or `newline', depending on context.
  14071. See the individual commands for more information."
  14072. (interactive)
  14073. (cond
  14074. ((bobp) (if indent (newline-and-indent) (newline)))
  14075. ((org-at-table-p)
  14076. (org-table-justify-field-maybe)
  14077. (call-interactively 'org-table-next-row))
  14078. ((and org-return-follows-link
  14079. (eq (get-text-property (point) 'face) 'org-link))
  14080. (call-interactively 'org-open-at-point))
  14081. ((and (org-at-heading-p)
  14082. (looking-at
  14083. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14084. (org-show-entry)
  14085. (end-of-line 1)
  14086. (newline))
  14087. (t (if indent (newline-and-indent) (newline)))))
  14088. (defun org-return-indent ()
  14089. "Goto next table row or insert a newline and indent.
  14090. Calls `org-table-next-row' or `newline-and-indent', depending on
  14091. context. See the individual commands for more information."
  14092. (interactive)
  14093. (org-return t))
  14094. (defun org-ctrl-c-star ()
  14095. "Compute table, or change heading status of lines.
  14096. Calls `org-table-recalculate' or `org-toggle-heading',
  14097. depending on context."
  14098. (interactive)
  14099. (cond
  14100. ((org-at-table-p)
  14101. (call-interactively 'org-table-recalculate))
  14102. (t
  14103. ;; Convert all lines in region to list items
  14104. (call-interactively 'org-toggle-heading))))
  14105. (defun org-ctrl-c-minus ()
  14106. "Insert separator line in table or modify bullet status of line.
  14107. Also turns a plain line or a region of lines into list items.
  14108. Calls `org-table-insert-hline', `org-toggle-item', or
  14109. `org-cycle-list-bullet', depending on context."
  14110. (interactive)
  14111. (cond
  14112. ((org-at-table-p)
  14113. (call-interactively 'org-table-insert-hline))
  14114. ((org-region-active-p)
  14115. (call-interactively 'org-toggle-item))
  14116. ((org-in-item-p)
  14117. (call-interactively 'org-cycle-list-bullet))
  14118. (t
  14119. (call-interactively 'org-toggle-item))))
  14120. (defun org-toggle-item ()
  14121. "Convert headings or normal lines to items, items to normal lines.
  14122. If there is no active region, only the current line is considered.
  14123. If the first line in the region is a headline, convert all headlines to items.
  14124. If the first line in the region is an item, convert all items to normal lines.
  14125. If the first line is normal text, add an item bullet to each line."
  14126. (interactive)
  14127. (let (l2 l beg end)
  14128. (if (org-region-active-p)
  14129. (setq beg (region-beginning) end (region-end))
  14130. (setq beg (point-at-bol)
  14131. end (min (1+ (point-at-eol)) (point-max))))
  14132. (save-excursion
  14133. (goto-char end)
  14134. (setq l2 (org-current-line))
  14135. (goto-char beg)
  14136. (beginning-of-line 1)
  14137. (setq l (1- (org-current-line)))
  14138. (if (org-at-item-p)
  14139. ;; We already have items, de-itemize
  14140. (while (< (setq l (1+ l)) l2)
  14141. (when (org-at-item-p)
  14142. (goto-char (match-beginning 2))
  14143. (delete-region (match-beginning 2) (match-end 2))
  14144. (and (looking-at "[ \t]+") (replace-match "")))
  14145. (beginning-of-line 2))
  14146. (if (org-on-heading-p)
  14147. ;; Headings, convert to items
  14148. (while (< (setq l (1+ l)) l2)
  14149. (if (looking-at org-outline-regexp)
  14150. (replace-match "- " t t))
  14151. (beginning-of-line 2))
  14152. ;; normal lines, turn them into items
  14153. (while (< (setq l (1+ l)) l2)
  14154. (unless (org-at-item-p)
  14155. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14156. (replace-match "\\1- \\2")))
  14157. (beginning-of-line 2)))))))
  14158. (defun org-toggle-heading (&optional nstars)
  14159. "Convert headings to normal text, or items or text to headings.
  14160. If there is no active region, only the current line is considered.
  14161. If the first line is a heading, remove the stars from all headlines
  14162. in the region.
  14163. If the first line is a plain list item, turn all plain list items
  14164. into headings.
  14165. If the first line is a normal line, turn each and every line in the
  14166. region into a heading.
  14167. When converting a line into a heading, the number of stars is chosen
  14168. such that the lines become children of the current entry. However,
  14169. when a prefix argument is given, its value determines the number of
  14170. stars to add."
  14171. (interactive "P")
  14172. (let (l2 l itemp beg end)
  14173. (if (org-region-active-p)
  14174. (setq beg (region-beginning) end (region-end))
  14175. (setq beg (point-at-bol)
  14176. end (min (1+ (point-at-eol)) (point-max))))
  14177. (save-excursion
  14178. (goto-char end)
  14179. (setq l2 (org-current-line))
  14180. (goto-char beg)
  14181. (beginning-of-line 1)
  14182. (setq l (1- (org-current-line)))
  14183. (if (org-on-heading-p)
  14184. ;; We already have headlines, de-star them
  14185. (while (< (setq l (1+ l)) l2)
  14186. (when (org-on-heading-p t)
  14187. (and (looking-at outline-regexp) (replace-match "")))
  14188. (beginning-of-line 2))
  14189. (setq itemp (org-at-item-p))
  14190. (let* ((stars
  14191. (if nstars
  14192. (make-string (prefix-numeric-value current-prefix-arg)
  14193. ?*)
  14194. (save-excursion
  14195. (if (re-search-backward org-complex-heading-regexp nil t)
  14196. (match-string 1) ""))))
  14197. (add-stars (cond (nstars "")
  14198. ((equal stars "") "*")
  14199. (org-odd-levels-only "**")
  14200. (t "*")))
  14201. (rpl (concat stars add-stars " ")))
  14202. (while (< (setq l (1+ l)) l2)
  14203. (if itemp
  14204. (and (org-at-item-p) (replace-match rpl t t))
  14205. (unless (org-on-heading-p)
  14206. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14207. (replace-match (concat rpl (match-string 2))))))
  14208. (beginning-of-line 2)))))))
  14209. (defun org-meta-return (&optional arg)
  14210. "Insert a new heading or wrap a region in a table.
  14211. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14212. See the individual commands for more information."
  14213. (interactive "P")
  14214. (cond
  14215. ((run-hook-with-args-until-success 'org-metareturn-hook))
  14216. ((org-at-table-p)
  14217. (call-interactively 'org-table-wrap-region))
  14218. (t (call-interactively 'org-insert-heading))))
  14219. ;;; Menu entries
  14220. ;; Define the Org-mode menus
  14221. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14222. '("Tbl"
  14223. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  14224. ["Next Field" org-cycle (org-at-table-p)]
  14225. ["Previous Field" org-shifttab (org-at-table-p)]
  14226. ["Next Row" org-return (org-at-table-p)]
  14227. "--"
  14228. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14229. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14230. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14231. "--"
  14232. ("Column"
  14233. ["Move Column Left" org-metaleft (org-at-table-p)]
  14234. ["Move Column Right" org-metaright (org-at-table-p)]
  14235. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14236. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  14237. ("Row"
  14238. ["Move Row Up" org-metaup (org-at-table-p)]
  14239. ["Move Row Down" org-metadown (org-at-table-p)]
  14240. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14241. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14242. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14243. "--"
  14244. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  14245. ("Rectangle"
  14246. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14247. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14248. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14249. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14250. "--"
  14251. ("Calculate"
  14252. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14253. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14254. ["Edit Formulas" org-edit-special (org-at-table-p)]
  14255. "--"
  14256. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14257. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14258. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  14259. "--"
  14260. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14261. "--"
  14262. ["Sum Column/Rectangle" org-table-sum
  14263. (or (org-at-table-p) (org-region-active-p))]
  14264. ["Which Column?" org-table-current-column (org-at-table-p)])
  14265. ["Debug Formulas"
  14266. org-table-toggle-formula-debugger
  14267. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  14268. ["Show Col/Row Numbers"
  14269. org-table-toggle-coordinate-overlays
  14270. :style toggle
  14271. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14272. "--"
  14273. ["Create" org-table-create (and (not (org-at-table-p))
  14274. org-enable-table-editor)]
  14275. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14276. ["Import from File" org-table-import (not (org-at-table-p))]
  14277. ["Export to File" org-table-export (org-at-table-p)]
  14278. "--"
  14279. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14280. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14281. '("Org"
  14282. ("Show/Hide"
  14283. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14284. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14285. ["Sparse Tree..." org-sparse-tree t]
  14286. ["Reveal Context" org-reveal t]
  14287. ["Show All" show-all t]
  14288. "--"
  14289. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14290. "--"
  14291. ["New Heading" org-insert-heading t]
  14292. ("Navigate Headings"
  14293. ["Up" outline-up-heading t]
  14294. ["Next" outline-next-visible-heading t]
  14295. ["Previous" outline-previous-visible-heading t]
  14296. ["Next Same Level" outline-forward-same-level t]
  14297. ["Previous Same Level" outline-backward-same-level t]
  14298. "--"
  14299. ["Jump" org-goto t])
  14300. ("Edit Structure"
  14301. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14302. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14303. "--"
  14304. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14305. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14306. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14307. "--"
  14308. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14309. "--"
  14310. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14311. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14312. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14313. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14314. "--"
  14315. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14316. "--"
  14317. ["Convert to odd levels" org-convert-to-odd-levels t]
  14318. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14319. ("Editing"
  14320. ["Emphasis..." org-emphasize t]
  14321. ["Edit Source Example" org-edit-special t]
  14322. "--"
  14323. ["Footnote new/jump" org-footnote-action t]
  14324. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14325. ("Archive"
  14326. ["Archive (default method)" org-archive-subtree-default t]
  14327. "--"
  14328. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  14329. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14330. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  14331. )
  14332. "--"
  14333. ("Hyperlinks"
  14334. ["Store Link (Global)" org-store-link t]
  14335. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14336. ["Insert Link" org-insert-link t]
  14337. ["Follow Link" org-open-at-point t]
  14338. "--"
  14339. ["Next link" org-next-link t]
  14340. ["Previous link" org-previous-link t]
  14341. "--"
  14342. ["Descriptive Links"
  14343. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14344. :style radio
  14345. :selected (member '(org-link) buffer-invisibility-spec)]
  14346. ["Literal Links"
  14347. (progn
  14348. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14349. :style radio
  14350. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14351. "--"
  14352. ("TODO Lists"
  14353. ["TODO/DONE/-" org-todo t]
  14354. ("Select keyword"
  14355. ["Next keyword" org-shiftright (org-on-heading-p)]
  14356. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14357. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14358. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14359. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14360. ["Show TODO Tree" org-show-todo-tree t]
  14361. ["Global TODO list" org-todo-list t]
  14362. "--"
  14363. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14364. :selected org-enforce-todo-dependencies :style toggle :active t]
  14365. "Settings for tree at point"
  14366. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14367. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14368. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14369. ["Do Children parallel" org-toggle-ordered-property :style radio
  14370. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14371. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14372. "--"
  14373. ["Set Priority" org-priority t]
  14374. ["Priority Up" org-shiftup t]
  14375. ["Priority Down" org-shiftdown t]
  14376. "--"
  14377. ["Get news from all feeds" org-feed-update-all t]
  14378. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14379. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14380. ("TAGS and Properties"
  14381. ["Set Tags" org-set-tags-command t]
  14382. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14383. "--"
  14384. ["Set property" org-set-property t]
  14385. ["Column view of properties" org-columns t]
  14386. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14387. ("Dates and Scheduling"
  14388. ["Timestamp" org-time-stamp t]
  14389. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14390. ("Change Date"
  14391. ["1 Day Later" org-shiftright t]
  14392. ["1 Day Earlier" org-shiftleft t]
  14393. ["1 ... Later" org-shiftup t]
  14394. ["1 ... Earlier" org-shiftdown t])
  14395. ["Compute Time Range" org-evaluate-time-range t]
  14396. ["Schedule Item" org-schedule t]
  14397. ["Deadline" org-deadline t]
  14398. "--"
  14399. ["Custom time format" org-toggle-time-stamp-overlays
  14400. :style radio :selected org-display-custom-times]
  14401. "--"
  14402. ["Goto Calendar" org-goto-calendar t]
  14403. ["Date from Calendar" org-date-from-calendar t]
  14404. "--"
  14405. ["Start/Restart Timer" org-timer-start t]
  14406. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14407. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14408. ["Insert Timer String" org-timer t]
  14409. ["Insert Timer Item" org-timer-item t])
  14410. ("Logging work"
  14411. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14412. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14413. ["Clock out" org-clock-out t]
  14414. ["Clock cancel" org-clock-cancel t]
  14415. "--"
  14416. ["Mark as default task" org-clock-mark-default-task t]
  14417. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14418. ["Goto running clock" org-clock-goto t]
  14419. "--"
  14420. ["Display times" org-clock-display t]
  14421. ["Create clock table" org-clock-report t]
  14422. "--"
  14423. ["Record DONE time"
  14424. (progn (setq org-log-done (not org-log-done))
  14425. (message "Switching to %s will %s record a timestamp"
  14426. (car org-done-keywords)
  14427. (if org-log-done "automatically" "not")))
  14428. :style toggle :selected org-log-done])
  14429. "--"
  14430. ["Agenda Command..." org-agenda t]
  14431. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14432. ("File List for Agenda")
  14433. ("Special views current file"
  14434. ["TODO Tree" org-show-todo-tree t]
  14435. ["Check Deadlines" org-check-deadlines t]
  14436. ["Timeline" org-timeline t]
  14437. ["Tags/Property tree" org-match-sparse-tree t])
  14438. "--"
  14439. ["Export/Publish..." org-export t]
  14440. ("LaTeX"
  14441. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14442. :selected org-cdlatex-mode]
  14443. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14444. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14445. ["Modify math symbol" org-cdlatex-math-modify
  14446. (org-inside-LaTeX-fragment-p)]
  14447. ["Insert citation" org-reftex-citation t]
  14448. "--"
  14449. ["Export LaTeX fragments as images"
  14450. (if (featurep 'org-exp)
  14451. (setq org-export-with-LaTeX-fragments
  14452. (not org-export-with-LaTeX-fragments))
  14453. (require 'org-exp))
  14454. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14455. org-export-with-LaTeX-fragments)])
  14456. "--"
  14457. ("MobileOrg"
  14458. ["Push Files and Views" org-mobile-push t]
  14459. ["Get Captured and Flagged" org-mobile-pull t]
  14460. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  14461. "--"
  14462. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  14463. "--"
  14464. ("Documentation"
  14465. ["Show Version" org-version t]
  14466. ["Info Documentation" org-info t])
  14467. ("Customize"
  14468. ["Browse Org Group" org-customize t]
  14469. "--"
  14470. ["Expand This Menu" org-create-customize-menu
  14471. (fboundp 'customize-menu-create)])
  14472. ["Send bug report" org-submit-bug-report t]
  14473. "--"
  14474. ("Refresh/Reload"
  14475. ["Refresh setup current buffer" org-mode-restart t]
  14476. ["Reload Org (after update)" org-reload t]
  14477. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14478. ))
  14479. (defun org-info (&optional node)
  14480. "Read documentation for Org-mode in the info system.
  14481. With optional NODE, go directly to that node."
  14482. (interactive)
  14483. (info (format "(org)%s" (or node ""))))
  14484. ;;;###autoload
  14485. (defun org-submit-bug-report ()
  14486. "Submit a bug report on Org-mode via mail.
  14487. Don't hesitate to report any problems or inaccurate documentation.
  14488. If you don't have setup sending mail from (X)Emacs, please copy the
  14489. output buffer into your mail program, as it gives us important
  14490. information about your Org-mode version and configuration."
  14491. (interactive)
  14492. (require 'reporter)
  14493. (org-load-modules-maybe)
  14494. (org-require-autoloaded-modules)
  14495. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14496. (reporter-submit-bug-report
  14497. "emacs-orgmode@gnu.org"
  14498. (org-version)
  14499. (let (list)
  14500. (save-window-excursion
  14501. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14502. (delete-other-windows)
  14503. (erase-buffer)
  14504. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14505. We would like to add your full Org-mode and Outline configuration to the
  14506. bug report. This greatly simplifies the work of the maintainer and
  14507. other experts on the mailing list.
  14508. HOWEVER, some variables you have customized may contain private
  14509. information. The names of customers, colleagues, or friends, might
  14510. appear in the form of file names, tags, todo states, or search strings.
  14511. If you answer yes to the prompt, you might want to check and remove
  14512. such private information before sending the email.")
  14513. (add-text-properties (point-min) (point-max) '(face org-warning))
  14514. (when (yes-or-no-p "Include your Org-mode configuration ")
  14515. (mapatoms
  14516. (lambda (v)
  14517. (and (boundp v)
  14518. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14519. (or (and (symbol-value v)
  14520. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14521. (and
  14522. (get v 'custom-type) (get v 'standard-value)
  14523. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14524. (push v list)))))
  14525. (kill-buffer (get-buffer "*Warn about privacy*"))
  14526. list))
  14527. nil nil
  14528. "Remember to cover the basics, that is, what you expected to happen and
  14529. what in fact did happen. You don't know how to make a good report? See
  14530. http://orgmode.org/manual/Feedback.html#Feedback
  14531. Your bug report will be posted to the Org-mode mailing list.
  14532. ------------------------------------------------------------------------")))
  14533. (defun org-install-agenda-files-menu ()
  14534. (let ((bl (buffer-list)))
  14535. (save-excursion
  14536. (while bl
  14537. (set-buffer (pop bl))
  14538. (if (org-mode-p) (setq bl nil)))
  14539. (when (org-mode-p)
  14540. (easy-menu-change
  14541. '("Org") "File List for Agenda"
  14542. (append
  14543. (list
  14544. ["Edit File List" (org-edit-agenda-file-list) t]
  14545. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14546. ["Remove Current File from List" org-remove-file t]
  14547. ["Cycle through agenda files" org-cycle-agenda-files t]
  14548. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14549. "--")
  14550. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14551. ;;;; Documentation
  14552. ;;;###autoload
  14553. (defun org-require-autoloaded-modules ()
  14554. (interactive)
  14555. (mapc 'require
  14556. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14557. org-docbook org-exp org-html org-icalendar
  14558. org-id org-latex
  14559. org-publish org-remember org-table
  14560. org-timer org-xoxo)))
  14561. ;;;###autoload
  14562. (defun org-reload (&optional uncompiled)
  14563. "Reload all org lisp files.
  14564. With prefix arg UNCOMPILED, load the uncompiled versions."
  14565. (interactive "P")
  14566. (require 'find-func)
  14567. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14568. (dir-org (file-name-directory (org-find-library-name "org")))
  14569. (dir-org-contrib (ignore-errors
  14570. (file-name-directory
  14571. (org-find-library-name "org-contribdir"))))
  14572. (files
  14573. (append (directory-files dir-org t file-re)
  14574. (and dir-org-contrib
  14575. (directory-files dir-org-contrib t file-re))))
  14576. (remove-re (concat (if (featurep 'xemacs)
  14577. "org-colview" "org-colview-xemacs")
  14578. "\\'")))
  14579. (setq files (mapcar 'file-name-sans-extension files))
  14580. (setq files (mapcar
  14581. (lambda (x) (if (string-match remove-re x) nil x))
  14582. files))
  14583. (setq files (delq nil files))
  14584. (mapc
  14585. (lambda (f)
  14586. (when (featurep (intern (file-name-nondirectory f)))
  14587. (if (and (not uncompiled)
  14588. (file-exists-p (concat f ".elc")))
  14589. (load (concat f ".elc") nil nil t)
  14590. (load (concat f ".el") nil nil t))))
  14591. files))
  14592. (org-version))
  14593. ;;;###autoload
  14594. (defun org-customize ()
  14595. "Call the customize function with org as argument."
  14596. (interactive)
  14597. (org-load-modules-maybe)
  14598. (org-require-autoloaded-modules)
  14599. (customize-browse 'org))
  14600. (defun org-create-customize-menu ()
  14601. "Create a full customization menu for Org-mode, insert it into the menu."
  14602. (interactive)
  14603. (org-load-modules-maybe)
  14604. (org-require-autoloaded-modules)
  14605. (if (fboundp 'customize-menu-create)
  14606. (progn
  14607. (easy-menu-change
  14608. '("Org") "Customize"
  14609. `(["Browse Org group" org-customize t]
  14610. "--"
  14611. ,(customize-menu-create 'org)
  14612. ["Set" Custom-set t]
  14613. ["Save" Custom-save t]
  14614. ["Reset to Current" Custom-reset-current t]
  14615. ["Reset to Saved" Custom-reset-saved t]
  14616. ["Reset to Standard Settings" Custom-reset-standard t]))
  14617. (message "\"Org\"-menu now contains full customization menu"))
  14618. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14619. ;;;; Miscellaneous stuff
  14620. ;;; Generally useful functions
  14621. (defun org-get-at-bol (property)
  14622. "Get text property PROPERTY at beginning of line."
  14623. (get-text-property (point-at-bol) property))
  14624. (defun org-find-text-property-in-string (prop s)
  14625. "Return the first non-nil value of property PROP in string S."
  14626. (or (get-text-property 0 prop s)
  14627. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14628. prop s)))
  14629. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14630. "Display the given MESSAGE as a warning."
  14631. (if (fboundp 'display-warning)
  14632. (display-warning 'org message
  14633. (if (featurep 'xemacs)
  14634. 'warning
  14635. :warning))
  14636. (let ((buf (get-buffer-create "*Org warnings*")))
  14637. (with-current-buffer buf
  14638. (goto-char (point-max))
  14639. (insert "Warning (Org): " message)
  14640. (unless (bolp)
  14641. (newline)))
  14642. (display-buffer buf)
  14643. (sit-for 0))))
  14644. (defun org-in-commented-line ()
  14645. "Is point in a line starting with `#'?"
  14646. (equal (char-after (point-at-bol)) ?#))
  14647. (defun org-in-verbatim-emphasis ()
  14648. (save-match-data
  14649. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  14650. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14651. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14652. (if (and marker (marker-buffer marker)
  14653. (buffer-live-p (marker-buffer marker)))
  14654. (progn
  14655. (switch-to-buffer (marker-buffer marker))
  14656. (if (or (> marker (point-max)) (< marker (point-min)))
  14657. (widen))
  14658. (goto-char marker)
  14659. (org-show-context 'org-goto))
  14660. (if bookmark
  14661. (bookmark-jump bookmark)
  14662. (error "Cannot find location"))))
  14663. (defun org-quote-csv-field (s)
  14664. "Quote field for inclusion in CSV material."
  14665. (if (string-match "[\",]" s)
  14666. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14667. s))
  14668. (defun org-plist-delete (plist property)
  14669. "Delete PROPERTY from PLIST.
  14670. This is in contrast to merely setting it to 0."
  14671. (let (p)
  14672. (while plist
  14673. (if (not (eq property (car plist)))
  14674. (setq p (plist-put p (car plist) (nth 1 plist))))
  14675. (setq plist (cddr plist)))
  14676. p))
  14677. (defun org-force-self-insert (N)
  14678. "Needed to enforce self-insert under remapping."
  14679. (interactive "p")
  14680. (self-insert-command N))
  14681. (defun org-string-width (s)
  14682. "Compute width of string, ignoring invisible characters.
  14683. This ignores character with invisibility property `org-link', and also
  14684. characters with property `org-cwidth', because these will become invisible
  14685. upon the next fontification round."
  14686. (let (b l)
  14687. (when (or (eq t buffer-invisibility-spec)
  14688. (assq 'org-link buffer-invisibility-spec))
  14689. (while (setq b (text-property-any 0 (length s)
  14690. 'invisible 'org-link s))
  14691. (setq s (concat (substring s 0 b)
  14692. (substring s (or (next-single-property-change
  14693. b 'invisible s) (length s)))))))
  14694. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14695. (setq s (concat (substring s 0 b)
  14696. (substring s (or (next-single-property-change
  14697. b 'org-cwidth s) (length s))))))
  14698. (setq l (string-width s) b -1)
  14699. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14700. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14701. l))
  14702. (defun org-get-indentation (&optional line)
  14703. "Get the indentation of the current line, interpreting tabs.
  14704. When LINE is given, assume it represents a line and compute its indentation."
  14705. (if line
  14706. (if (string-match "^ *" (org-remove-tabs line))
  14707. (match-end 0))
  14708. (save-excursion
  14709. (beginning-of-line 1)
  14710. (skip-chars-forward " \t")
  14711. (current-column))))
  14712. (defun org-remove-tabs (s &optional width)
  14713. "Replace tabulators in S with spaces.
  14714. Assumes that s is a single line, starting in column 0."
  14715. (setq width (or width tab-width))
  14716. (while (string-match "\t" s)
  14717. (setq s (replace-match
  14718. (make-string
  14719. (- (* width (/ (+ (match-beginning 0) width) width))
  14720. (match-beginning 0)) ?\ )
  14721. t t s)))
  14722. s)
  14723. (defun org-fix-indentation (line ind)
  14724. "Fix indentation in LINE.
  14725. IND is a cons cell with target and minimum indentation.
  14726. If the current indentation in LINE is smaller than the minimum,
  14727. leave it alone. If it is larger than ind, set it to the target."
  14728. (let* ((l (org-remove-tabs line))
  14729. (i (org-get-indentation l))
  14730. (i1 (car ind)) (i2 (cdr ind)))
  14731. (if (>= i i2) (setq l (substring line i2)))
  14732. (if (> i1 0)
  14733. (concat (make-string i1 ?\ ) l)
  14734. l)))
  14735. (defun org-remove-indentation (code &optional n)
  14736. "Remove the maximum common indentation from the lines in CODE.
  14737. N may optionally be the number of spaces to remove."
  14738. (with-temp-buffer
  14739. (insert code)
  14740. (org-do-remove-indentation n)
  14741. (buffer-string)))
  14742. (defun org-do-remove-indentation (&optional n)
  14743. "Remove the maximum common indentation from the buffer."
  14744. (untabify (point-min) (point-max))
  14745. (let ((min 10000) re)
  14746. (if n
  14747. (setq min n)
  14748. (goto-char (point-min))
  14749. (while (re-search-forward "^ *[^ \n]" nil t)
  14750. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14751. (unless (or (= min 0) (= min 10000))
  14752. (setq re (format "^ \\{%d\\}" min))
  14753. (goto-char (point-min))
  14754. (while (re-search-forward re nil t)
  14755. (replace-match "")
  14756. (end-of-line 1))
  14757. min)))
  14758. (defun org-fill-template (template alist)
  14759. "Find each %key of ALIST in TEMPLATE and replace it."
  14760. (let (entry key value)
  14761. (setq alist (sort (copy-sequence alist)
  14762. (lambda (a b) (< (length (car a)) (length (car b))))))
  14763. (while (setq entry (pop alist))
  14764. (setq template
  14765. (replace-regexp-in-string
  14766. (concat "%" (regexp-quote (car entry)))
  14767. (cdr entry) template t t)))
  14768. template))
  14769. (defun org-base-buffer (buffer)
  14770. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14771. (if (not buffer)
  14772. buffer
  14773. (or (buffer-base-buffer buffer)
  14774. buffer)))
  14775. (defun org-trim (s)
  14776. "Remove whitespace at beginning and end of string."
  14777. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14778. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14779. s)
  14780. (defun org-wrap (string &optional width lines)
  14781. "Wrap string to either a number of lines, or a width in characters.
  14782. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14783. that costs. If there is a word longer than WIDTH, the text is actually
  14784. wrapped to the length of that word.
  14785. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14786. many lines, whatever width that takes.
  14787. The return value is a list of lines, without newlines at the end."
  14788. (let* ((words (org-split-string string "[ \t\n]+"))
  14789. (maxword (apply 'max (mapcar 'org-string-width words)))
  14790. w ll)
  14791. (cond (width
  14792. (org-do-wrap words (max maxword width)))
  14793. (lines
  14794. (setq w maxword)
  14795. (setq ll (org-do-wrap words maxword))
  14796. (if (<= (length ll) lines)
  14797. ll
  14798. (setq ll words)
  14799. (while (> (length ll) lines)
  14800. (setq w (1+ w))
  14801. (setq ll (org-do-wrap words w)))
  14802. ll))
  14803. (t (error "Cannot wrap this")))))
  14804. (defun org-do-wrap (words width)
  14805. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14806. (let (lines line)
  14807. (while words
  14808. (setq line (pop words))
  14809. (while (and words (< (+ (length line) (length (car words))) width))
  14810. (setq line (concat line " " (pop words))))
  14811. (setq lines (push line lines)))
  14812. (nreverse lines)))
  14813. (defun org-split-string (string &optional separators)
  14814. "Splits STRING into substrings at SEPARATORS.
  14815. No empty strings are returned if there are matches at the beginning
  14816. and end of string."
  14817. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14818. (start 0)
  14819. notfirst
  14820. (list nil))
  14821. (while (and (string-match rexp string
  14822. (if (and notfirst
  14823. (= start (match-beginning 0))
  14824. (< start (length string)))
  14825. (1+ start) start))
  14826. (< (match-beginning 0) (length string)))
  14827. (setq notfirst t)
  14828. (or (eq (match-beginning 0) 0)
  14829. (and (eq (match-beginning 0) (match-end 0))
  14830. (eq (match-beginning 0) start))
  14831. (setq list
  14832. (cons (substring string start (match-beginning 0))
  14833. list)))
  14834. (setq start (match-end 0)))
  14835. (or (eq start (length string))
  14836. (setq list
  14837. (cons (substring string start)
  14838. list)))
  14839. (nreverse list)))
  14840. (defun org-quote-vert (s)
  14841. "Replace \"|\" with \"\\vert\"."
  14842. (while (string-match "|" s)
  14843. (setq s (replace-match "\\vert" t t s)))
  14844. s)
  14845. (defun org-uuidgen-p (s)
  14846. "Is S an ID created by UUIDGEN?"
  14847. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14848. (defun org-context ()
  14849. "Return a list of contexts of the current cursor position.
  14850. If several contexts apply, all are returned.
  14851. Each context entry is a list with a symbol naming the context, and
  14852. two positions indicating start and end of the context. Possible
  14853. contexts are:
  14854. :headline anywhere in a headline
  14855. :headline-stars on the leading stars in a headline
  14856. :todo-keyword on a TODO keyword (including DONE) in a headline
  14857. :tags on the TAGS in a headline
  14858. :priority on the priority cookie in a headline
  14859. :item on the first line of a plain list item
  14860. :item-bullet on the bullet/number of a plain list item
  14861. :checkbox on the checkbox in a plain list item
  14862. :table in an org-mode table
  14863. :table-special on a special filed in a table
  14864. :table-table in a table.el table
  14865. :link on a hyperlink
  14866. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14867. :target on a <<target>>
  14868. :radio-target on a <<<radio-target>>>
  14869. :latex-fragment on a LaTeX fragment
  14870. :latex-preview on a LaTeX fragment with overlayed preview image
  14871. This function expects the position to be visible because it uses font-lock
  14872. faces as a help to recognize the following contexts: :table-special, :link,
  14873. and :keyword."
  14874. (let* ((f (get-text-property (point) 'face))
  14875. (faces (if (listp f) f (list f)))
  14876. (p (point)) clist o)
  14877. ;; First the large context
  14878. (cond
  14879. ((org-on-heading-p t)
  14880. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14881. (when (progn
  14882. (beginning-of-line 1)
  14883. (looking-at org-todo-line-tags-regexp))
  14884. (push (org-point-in-group p 1 :headline-stars) clist)
  14885. (push (org-point-in-group p 2 :todo-keyword) clist)
  14886. (push (org-point-in-group p 4 :tags) clist))
  14887. (goto-char p)
  14888. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  14889. (if (looking-at "\\[#[A-Z0-9]\\]")
  14890. (push (org-point-in-group p 0 :priority) clist)))
  14891. ((org-at-item-p)
  14892. (push (org-point-in-group p 2 :item-bullet) clist)
  14893. (push (list :item (point-at-bol)
  14894. (save-excursion (org-end-of-item) (point)))
  14895. clist)
  14896. (and (org-at-item-checkbox-p)
  14897. (push (org-point-in-group p 0 :checkbox) clist)))
  14898. ((org-at-table-p)
  14899. (push (list :table (org-table-begin) (org-table-end)) clist)
  14900. (if (memq 'org-formula faces)
  14901. (push (list :table-special
  14902. (previous-single-property-change p 'face)
  14903. (next-single-property-change p 'face)) clist)))
  14904. ((org-at-table-p 'any)
  14905. (push (list :table-table) clist)))
  14906. (goto-char p)
  14907. ;; Now the small context
  14908. (cond
  14909. ((org-at-timestamp-p)
  14910. (push (org-point-in-group p 0 :timestamp) clist))
  14911. ((memq 'org-link faces)
  14912. (push (list :link
  14913. (previous-single-property-change p 'face)
  14914. (next-single-property-change p 'face)) clist))
  14915. ((memq 'org-special-keyword faces)
  14916. (push (list :keyword
  14917. (previous-single-property-change p 'face)
  14918. (next-single-property-change p 'face)) clist))
  14919. ((org-on-target-p)
  14920. (push (org-point-in-group p 0 :target) clist)
  14921. (goto-char (1- (match-beginning 0)))
  14922. (if (looking-at org-radio-target-regexp)
  14923. (push (org-point-in-group p 0 :radio-target) clist))
  14924. (goto-char p))
  14925. ((setq o (car (delq nil
  14926. (mapcar
  14927. (lambda (x)
  14928. (if (memq x org-latex-fragment-image-overlays) x))
  14929. (org-overlays-at (point))))))
  14930. (push (list :latex-fragment
  14931. (org-overlay-start o) (org-overlay-end o)) clist)
  14932. (push (list :latex-preview
  14933. (org-overlay-start o) (org-overlay-end o)) clist))
  14934. ((org-inside-LaTeX-fragment-p)
  14935. ;; FIXME: positions wrong.
  14936. (push (list :latex-fragment (point) (point)) clist)))
  14937. (setq clist (nreverse (delq nil clist)))
  14938. clist))
  14939. ;; FIXME: Compare with at-regexp-p Do we need both?
  14940. (defun org-in-regexp (re &optional nlines visually)
  14941. "Check if point is inside a match of regexp.
  14942. Normally only the current line is checked, but you can include NLINES extra
  14943. lines both before and after point into the search.
  14944. If VISUALLY is set, require that the cursor is not after the match but
  14945. really on, so that the block visually is on the match."
  14946. (catch 'exit
  14947. (let ((pos (point))
  14948. (eol (point-at-eol (+ 1 (or nlines 0))))
  14949. (inc (if visually 1 0)))
  14950. (save-excursion
  14951. (beginning-of-line (- 1 (or nlines 0)))
  14952. (while (re-search-forward re eol t)
  14953. (if (and (<= (match-beginning 0) pos)
  14954. (>= (+ inc (match-end 0)) pos))
  14955. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14956. (defun org-at-regexp-p (regexp)
  14957. "Is point inside a match of REGEXP in the current line?"
  14958. (catch 'exit
  14959. (save-excursion
  14960. (let ((pos (point)) (end (point-at-eol)))
  14961. (beginning-of-line 1)
  14962. (while (re-search-forward regexp end t)
  14963. (if (and (<= (match-beginning 0) pos)
  14964. (>= (match-end 0) pos))
  14965. (throw 'exit t)))
  14966. nil))))
  14967. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14968. "Call `multi-occur' with buffers for all agenda files."
  14969. (interactive "sOrg-files matching: \np")
  14970. (let* ((files (org-agenda-files))
  14971. (tnames (mapcar 'file-truename files))
  14972. (extra org-agenda-text-search-extra-files)
  14973. f)
  14974. (when (eq (car extra) 'agenda-archives)
  14975. (setq extra (cdr extra))
  14976. (setq files (org-add-archive-files files)))
  14977. (while (setq f (pop extra))
  14978. (unless (member (file-truename f) tnames)
  14979. (add-to-list 'files f 'append)
  14980. (add-to-list 'tnames (file-truename f) 'append)))
  14981. (multi-occur
  14982. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14983. regexp)))
  14984. (if (boundp 'occur-mode-find-occurrence-hook)
  14985. ;; Emacs 23
  14986. (add-hook 'occur-mode-find-occurrence-hook
  14987. (lambda ()
  14988. (when (org-mode-p)
  14989. (org-reveal))))
  14990. ;; Emacs 22
  14991. (defadvice occur-mode-goto-occurrence
  14992. (after org-occur-reveal activate)
  14993. (and (org-mode-p) (org-reveal)))
  14994. (defadvice occur-mode-goto-occurrence-other-window
  14995. (after org-occur-reveal activate)
  14996. (and (org-mode-p) (org-reveal)))
  14997. (defadvice occur-mode-display-occurrence
  14998. (after org-occur-reveal activate)
  14999. (when (org-mode-p)
  15000. (let ((pos (occur-mode-find-occurrence)))
  15001. (with-current-buffer (marker-buffer pos)
  15002. (save-excursion
  15003. (goto-char pos)
  15004. (org-reveal)))))))
  15005. (defun org-occur-link-in-agenda-files ()
  15006. "Create a link and search for it in the agendas.
  15007. The link is not stored in `org-stored-links', it is just created
  15008. for the search purpose."
  15009. (interactive)
  15010. (let ((link (condition-case nil
  15011. (org-store-link nil)
  15012. (error "Unable to create a link to here"))))
  15013. (org-occur-in-agenda-files (regexp-quote link))))
  15014. (defun org-uniquify (list)
  15015. "Remove duplicate elements from LIST."
  15016. (let (res)
  15017. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15018. res))
  15019. (defun org-delete-all (elts list)
  15020. "Remove all elements in ELTS from LIST."
  15021. (while elts
  15022. (setq list (delete (pop elts) list)))
  15023. list)
  15024. (defun org-back-over-empty-lines ()
  15025. "Move backwards over whitespace, to the beginning of the first empty line.
  15026. Returns the number of empty lines passed."
  15027. (let ((pos (point)))
  15028. (skip-chars-backward " \t\n\r")
  15029. (beginning-of-line 2)
  15030. (goto-char (min (point) pos))
  15031. (count-lines (point) pos)))
  15032. (defun org-skip-whitespace ()
  15033. (skip-chars-forward " \t\n\r"))
  15034. (defun org-point-in-group (point group &optional context)
  15035. "Check if POINT is in match-group GROUP.
  15036. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15037. match. If the match group does ot exist or point is not inside it,
  15038. return nil."
  15039. (and (match-beginning group)
  15040. (>= point (match-beginning group))
  15041. (<= point (match-end group))
  15042. (if context
  15043. (list context (match-beginning group) (match-end group))
  15044. t)))
  15045. (defun org-switch-to-buffer-other-window (&rest args)
  15046. "Switch to buffer in a second window on the current frame.
  15047. In particular, do not allow pop-up frames."
  15048. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15049. special-display-function)
  15050. (apply 'switch-to-buffer-other-window args)))
  15051. (defun org-combine-plists (&rest plists)
  15052. "Create a single property list from all plists in PLISTS.
  15053. The process starts by copying the first list, and then setting properties
  15054. from the other lists. Settings in the last list are the most significant
  15055. ones and overrule settings in the other lists."
  15056. (let ((rtn (copy-sequence (pop plists)))
  15057. p v ls)
  15058. (while plists
  15059. (setq ls (pop plists))
  15060. (while ls
  15061. (setq p (pop ls) v (pop ls))
  15062. (setq rtn (plist-put rtn p v))))
  15063. rtn))
  15064. (defun org-move-line-down (arg)
  15065. "Move the current line down. With prefix argument, move it past ARG lines."
  15066. (interactive "p")
  15067. (let ((col (current-column))
  15068. beg end pos)
  15069. (beginning-of-line 1) (setq beg (point))
  15070. (beginning-of-line 2) (setq end (point))
  15071. (beginning-of-line (+ 1 arg))
  15072. (setq pos (move-marker (make-marker) (point)))
  15073. (insert (delete-and-extract-region beg end))
  15074. (goto-char pos)
  15075. (org-move-to-column col)))
  15076. (defun org-move-line-up (arg)
  15077. "Move the current line up. With prefix argument, move it past ARG lines."
  15078. (interactive "p")
  15079. (let ((col (current-column))
  15080. beg end pos)
  15081. (beginning-of-line 1) (setq beg (point))
  15082. (beginning-of-line 2) (setq end (point))
  15083. (beginning-of-line (- arg))
  15084. (setq pos (move-marker (make-marker) (point)))
  15085. (insert (delete-and-extract-region beg end))
  15086. (goto-char pos)
  15087. (org-move-to-column col)))
  15088. (defun org-replace-escapes (string table)
  15089. "Replace %-escapes in STRING with values in TABLE.
  15090. TABLE is an association list with keys like \"%a\" and string values.
  15091. The sequences in STRING may contain normal field width and padding information,
  15092. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15093. so values can contain further %-escapes if they are define later in TABLE."
  15094. (let ((case-fold-search nil)
  15095. e re rpl)
  15096. (while (setq e (pop table))
  15097. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15098. (while (string-match re string)
  15099. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15100. (cdr e)))
  15101. (setq string (replace-match rpl t t string))))
  15102. string))
  15103. (defun org-sublist (list start end)
  15104. "Return a section of LIST, from START to END.
  15105. Counting starts at 1."
  15106. (let (rtn (c start))
  15107. (setq list (nthcdr (1- start) list))
  15108. (while (and list (<= c end))
  15109. (push (pop list) rtn)
  15110. (setq c (1+ c)))
  15111. (nreverse rtn)))
  15112. (defun org-find-base-buffer-visiting (file)
  15113. "Like `find-buffer-visiting' but always return the base buffer and
  15114. not an indirect buffer."
  15115. (let ((buf (or (get-file-buffer file)
  15116. (find-buffer-visiting file))))
  15117. (if buf
  15118. (or (buffer-base-buffer buf) buf)
  15119. nil)))
  15120. (defun org-image-file-name-regexp (&optional extensions)
  15121. "Return regexp matching the file names of images.
  15122. If EXTENSIONS is given, only match these."
  15123. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15124. (image-file-name-regexp)
  15125. (let ((image-file-name-extensions
  15126. (or extensions
  15127. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15128. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15129. (concat "\\."
  15130. (regexp-opt (nconc (mapcar 'upcase
  15131. image-file-name-extensions)
  15132. image-file-name-extensions)
  15133. t)
  15134. "\\'"))))
  15135. (defun org-file-image-p (file &optional extensions)
  15136. "Return non-nil if FILE is an image."
  15137. (save-match-data
  15138. (string-match (org-image-file-name-regexp extensions) file)))
  15139. (defun org-get-cursor-date ()
  15140. "Return the date at cursor in as a time.
  15141. This works in the calendar and in the agenda, anywhere else it just
  15142. returns the current time."
  15143. (let (date day defd)
  15144. (cond
  15145. ((eq major-mode 'calendar-mode)
  15146. (setq date (calendar-cursor-to-date)
  15147. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15148. ((eq major-mode 'org-agenda-mode)
  15149. (setq day (get-text-property (point) 'day))
  15150. (if day
  15151. (setq date (calendar-gregorian-from-absolute day)
  15152. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15153. (nth 2 date))))))
  15154. (or defd (current-time))))
  15155. (defvar org-agenda-action-marker (make-marker)
  15156. "Marker pointing to the entry for the next agenda action.")
  15157. (defun org-mark-entry-for-agenda-action ()
  15158. "Mark the current entry as target of an agenda action.
  15159. Agenda actions are actions executed from the agenda with the key `k',
  15160. which make use of the date at the cursor."
  15161. (interactive)
  15162. (move-marker org-agenda-action-marker
  15163. (save-excursion (org-back-to-heading t) (point))
  15164. (current-buffer))
  15165. (message
  15166. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15167. ;;; Paragraph filling stuff.
  15168. ;; We want this to be just right, so use the full arsenal.
  15169. (defun org-indent-line-function ()
  15170. "Indent line like previous, but further if previous was headline or item."
  15171. (interactive)
  15172. (let* ((pos (point))
  15173. (itemp (org-at-item-p))
  15174. (case-fold-search t)
  15175. (org-drawer-regexp (or org-drawer-regexp "\000"))
  15176. column bpos bcol tpos tcol bullet btype bullet-type)
  15177. ;; Find the previous relevant line
  15178. (beginning-of-line 1)
  15179. (cond
  15180. ((looking-at "#") (setq column 0))
  15181. ((looking-at "\\*+ ") (setq column 0))
  15182. ((and (looking-at "[ \t]*:END:")
  15183. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  15184. (save-excursion
  15185. (goto-char (1- (match-beginning 1)))
  15186. (setq column (current-column))))
  15187. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  15188. (save-excursion
  15189. (re-search-backward
  15190. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  15191. (setq column (org-get-indentation (match-string 0))))
  15192. (t
  15193. (beginning-of-line 0)
  15194. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  15195. (not (looking-at "[ \t]*:END:"))
  15196. (not (looking-at org-drawer-regexp)))
  15197. (beginning-of-line 0))
  15198. (cond
  15199. ((looking-at "\\*+[ \t]+")
  15200. (if (not org-adapt-indentation)
  15201. (setq column 0)
  15202. (goto-char (match-end 0))
  15203. (setq column (current-column))))
  15204. ((looking-at org-drawer-regexp)
  15205. (goto-char (1- (match-beginning 1)))
  15206. (setq column (current-column)))
  15207. ((looking-at "\\([ \t]*\\):END:")
  15208. (goto-char (match-end 1))
  15209. (setq column (current-column)))
  15210. ((org-in-item-p)
  15211. (org-beginning-of-item)
  15212. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  15213. (setq bpos (match-beginning 1) tpos (match-end 0)
  15214. bcol (progn (goto-char bpos) (current-column))
  15215. tcol (progn (goto-char tpos) (current-column))
  15216. bullet (match-string 1)
  15217. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  15218. (if (> tcol (+ bcol org-description-max-indent))
  15219. (setq tcol (+ bcol 5)))
  15220. (if (not itemp)
  15221. (setq column tcol)
  15222. (goto-char pos)
  15223. (beginning-of-line 1)
  15224. (if (looking-at "\\S-")
  15225. (progn
  15226. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  15227. (setq bullet (match-string 1)
  15228. btype (if (string-match "[0-9]" bullet) "n" bullet))
  15229. (setq column (if (equal btype bullet-type) bcol tcol)))
  15230. (setq column (org-get-indentation)))))
  15231. (t (setq column (org-get-indentation))))))
  15232. (goto-char pos)
  15233. (if (<= (current-column) (current-indentation))
  15234. (org-indent-line-to column)
  15235. (save-excursion (org-indent-line-to column)))
  15236. (setq column (current-column))
  15237. (beginning-of-line 1)
  15238. (if (looking-at
  15239. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  15240. (replace-match (concat (match-string 1)
  15241. (format org-property-format
  15242. (match-string 2) (match-string 3)))
  15243. t t))
  15244. (org-move-to-column column)))
  15245. (defun org-set-autofill-regexps ()
  15246. (interactive)
  15247. ;; In the paragraph separator we include headlines, because filling
  15248. ;; text in a line directly attached to a headline would otherwise
  15249. ;; fill the headline as well.
  15250. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15251. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  15252. ;; The paragraph starter includes hand-formatted lists.
  15253. (org-set-local
  15254. 'paragraph-start
  15255. (concat
  15256. "\f" "\\|"
  15257. "[ ]*$" "\\|"
  15258. "\\*+ " "\\|"
  15259. "[ \t]*#" "\\|"
  15260. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  15261. "[ \t]*[:|]" "\\|"
  15262. "\\$\\$" "\\|"
  15263. "\\\\\\(begin\\|end\\|[][]\\)"))
  15264. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15265. ;; But only if the user has not turned off tables or fixed-width regions
  15266. (org-set-local
  15267. 'auto-fill-inhibit-regexp
  15268. (concat "\\*+ \\|#\\+"
  15269. "\\|[ \t]*" org-keyword-time-regexp
  15270. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15271. (concat
  15272. "\\|[ \t]*["
  15273. (if org-enable-table-editor "|" "")
  15274. (if org-enable-fixed-width-editor ":" "")
  15275. "]"))))
  15276. ;; We use our own fill-paragraph function, to make sure that tables
  15277. ;; and fixed-width regions are not wrapped. That function will pass
  15278. ;; through to `fill-paragraph' when appropriate.
  15279. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15280. ; Adaptive filling: To get full control, first make sure that
  15281. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15282. (org-set-local 'adaptive-fill-regexp "\000")
  15283. (org-set-local 'adaptive-fill-function
  15284. 'org-adaptive-fill-function)
  15285. (org-set-local
  15286. 'align-mode-rules-list
  15287. '((org-in-buffer-settings
  15288. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15289. (modes . '(org-mode))))))
  15290. (defun org-fill-paragraph (&optional justify)
  15291. "Re-align a table, pass through to fill-paragraph if no table."
  15292. (let ((table-p (org-at-table-p))
  15293. (table.el-p (org-at-table.el-p)))
  15294. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15295. (save-excursion (goto-char (point-at-bol))
  15296. (looking-at outline-regexp)))
  15297. t) ; skip headlines
  15298. (table.el-p t) ; skip table.el tables
  15299. (table-p (org-table-align) t) ; align org-mode tables
  15300. (t nil)))) ; call paragraph-fill
  15301. ;; For reference, this is the default value of adaptive-fill-regexp
  15302. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15303. (defun org-adaptive-fill-function ()
  15304. "Return a fill prefix for org-mode files.
  15305. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15306. work correctly."
  15307. (cond ((looking-at "#[ \t]+")
  15308. (match-string 0))
  15309. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15310. (save-excursion
  15311. (if (> (match-end 1) (+ (match-beginning 1)
  15312. org-description-max-indent))
  15313. (goto-char (+ (match-beginning 1) 5))
  15314. (goto-char (match-end 0)))
  15315. (make-string (current-column) ?\ )))
  15316. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15317. (save-excursion
  15318. (goto-char (match-end 0))
  15319. (make-string (current-column) ?\ )))
  15320. (t nil)))
  15321. ;;; Other stuff.
  15322. (defun org-toggle-fixed-width-section (arg)
  15323. "Toggle the fixed-width export.
  15324. If there is no active region, the QUOTE keyword at the current headline is
  15325. inserted or removed. When present, it causes the text between this headline
  15326. and the next to be exported as fixed-width text, and unmodified.
  15327. If there is an active region, this command adds or removes a colon as the
  15328. first character of this line. If the first character of a line is a colon,
  15329. this line is also exported in fixed-width font."
  15330. (interactive "P")
  15331. (let* ((cc 0)
  15332. (regionp (org-region-active-p))
  15333. (beg (if regionp (region-beginning) (point)))
  15334. (end (if regionp (region-end)))
  15335. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15336. (case-fold-search nil)
  15337. (re "[ \t]*\\(: \\)")
  15338. off)
  15339. (if regionp
  15340. (save-excursion
  15341. (goto-char beg)
  15342. (setq cc (current-column))
  15343. (beginning-of-line 1)
  15344. (setq off (looking-at re))
  15345. (while (> nlines 0)
  15346. (setq nlines (1- nlines))
  15347. (beginning-of-line 1)
  15348. (cond
  15349. (arg
  15350. (org-move-to-column cc t)
  15351. (insert ": \n")
  15352. (forward-line -1))
  15353. ((and off (looking-at re))
  15354. (replace-match "" t t nil 1))
  15355. ((not off) (org-move-to-column cc t) (insert ": ")))
  15356. (forward-line 1)))
  15357. (save-excursion
  15358. (org-back-to-heading)
  15359. (if (looking-at (concat outline-regexp
  15360. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15361. (replace-match "" t t nil 1)
  15362. (if (looking-at outline-regexp)
  15363. (progn
  15364. (goto-char (match-end 0))
  15365. (insert org-quote-string " "))))))))
  15366. (defun org-reftex-citation ()
  15367. "Use reftex-citation to insert a citation into the buffer.
  15368. This looks for a line like
  15369. #+BIBLIOGRAPHY: foo plain option:-d
  15370. and derives from it that foo.bib is the bbliography file relevant
  15371. for this document. It then installs the necessary environment for RefTeX
  15372. to work in this buffer and calls `reftex-citation' to insert a citation
  15373. into the buffer.
  15374. Export of such citations to both LaTeX and HTML is handled by the contributed
  15375. package org-exp-bibtex by Taru Karttunen."
  15376. (interactive)
  15377. (let ((reftex-docstruct-symbol 'rds)
  15378. (reftex-cite-format "\\cite{%l}")
  15379. rds bib)
  15380. (save-excursion
  15381. (save-restriction
  15382. (widen)
  15383. (let ((case-fold-search t)
  15384. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15385. (if (not (save-excursion
  15386. (or (re-search-forward re nil t)
  15387. (re-search-backward re nil t))))
  15388. (error "No bibliography defined in file")
  15389. (setq bib (concat (match-string 1) ".bib")
  15390. rds (list (list 'bib bib)))))))
  15391. (call-interactively 'reftex-citation)))
  15392. ;;;; Functions extending outline functionality
  15393. (defun org-beginning-of-line (&optional arg)
  15394. "Go to the beginning of the current line. If that is invisible, continue
  15395. to a visible line beginning. This makes the function of C-a more intuitive.
  15396. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15397. first attempt, and only move to after the tags when the cursor is already
  15398. beyond the end of the headline."
  15399. (interactive "P")
  15400. (let ((pos (point))
  15401. (special (if (consp org-special-ctrl-a/e)
  15402. (car org-special-ctrl-a/e)
  15403. org-special-ctrl-a/e))
  15404. refpos)
  15405. (if (org-bound-and-true-p line-move-visual)
  15406. (beginning-of-visual-line 1)
  15407. (beginning-of-line 1))
  15408. (if (and arg (fboundp 'move-beginning-of-line))
  15409. (call-interactively 'move-beginning-of-line)
  15410. (if (bobp)
  15411. nil
  15412. (backward-char 1)
  15413. (if (org-invisible-p)
  15414. (while (and (not (bobp)) (org-invisible-p))
  15415. (backward-char 1)
  15416. (beginning-of-line 1))
  15417. (forward-char 1))))
  15418. (when special
  15419. (cond
  15420. ((and (looking-at org-complex-heading-regexp)
  15421. (= (char-after (match-end 1)) ?\ ))
  15422. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15423. (point-at-eol)))
  15424. (goto-char
  15425. (if (eq special t)
  15426. (cond ((> pos refpos) refpos)
  15427. ((= pos (point)) refpos)
  15428. (t (point)))
  15429. (cond ((> pos (point)) (point))
  15430. ((not (eq last-command this-command)) (point))
  15431. (t refpos)))))
  15432. ((org-at-item-p)
  15433. (goto-char
  15434. (if (eq special t)
  15435. (cond ((> pos (match-end 4)) (match-end 4))
  15436. ((= pos (point)) (match-end 4))
  15437. (t (point)))
  15438. (cond ((> pos (point)) (point))
  15439. ((not (eq last-command this-command)) (point))
  15440. (t (match-end 4))))))))
  15441. (org-no-warnings
  15442. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15443. (defun org-end-of-line (&optional arg)
  15444. "Go to the end of the line.
  15445. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15446. first attempt, and only move to after the tags when the cursor is already
  15447. beyond the end of the headline."
  15448. (interactive "P")
  15449. (let ((special (if (consp org-special-ctrl-a/e)
  15450. (cdr org-special-ctrl-a/e)
  15451. org-special-ctrl-a/e)))
  15452. (if (or (not special)
  15453. (not (org-on-heading-p))
  15454. arg)
  15455. (call-interactively
  15456. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15457. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15458. (t 'end-of-line)))
  15459. (let ((pos (point)))
  15460. (beginning-of-line 1)
  15461. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  15462. (if (eq special t)
  15463. (if (or (< pos (match-beginning 1))
  15464. (= pos (match-end 0)))
  15465. (goto-char (match-beginning 1))
  15466. (goto-char (match-end 0)))
  15467. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15468. (goto-char (match-end 0))
  15469. (goto-char (match-beginning 1))))
  15470. (call-interactively (if (fboundp 'move-end-of-line)
  15471. 'move-end-of-line
  15472. 'end-of-line)))))
  15473. (org-no-warnings
  15474. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15475. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15476. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15477. (define-key org-mode-map [home] 'org-beginning-of-line)
  15478. (define-key org-mode-map [end] 'org-end-of-line)
  15479. (defun org-backward-sentence (&optional arg)
  15480. "Go to beginning of sentence, or beginning of table field.
  15481. This will call `backward-sentence' or `org-table-beginning-of-field',
  15482. depending on context."
  15483. (interactive "P")
  15484. (cond
  15485. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15486. (t (call-interactively 'backward-sentence))))
  15487. (defun org-forward-sentence (&optional arg)
  15488. "Go to end of sentence, or end of table field.
  15489. This will call `forward-sentence' or `org-table-end-of-field',
  15490. depending on context."
  15491. (interactive "P")
  15492. (cond
  15493. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15494. (t (call-interactively 'forward-sentence))))
  15495. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15496. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15497. (defun org-kill-line (&optional arg)
  15498. "Kill line, to tags or end of line."
  15499. (interactive "P")
  15500. (cond
  15501. ((or (not org-special-ctrl-k)
  15502. (bolp)
  15503. (not (org-on-heading-p)))
  15504. (call-interactively 'kill-line))
  15505. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15506. (kill-region (point) (match-beginning 1))
  15507. (org-set-tags nil t))
  15508. (t (kill-region (point) (point-at-eol)))))
  15509. (define-key org-mode-map "\C-k" 'org-kill-line)
  15510. (defun org-yank (&optional arg)
  15511. "Yank. If the kill is a subtree, treat it specially.
  15512. This command will look at the current kill and check if is a single
  15513. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15514. cursor is at the beginning of a line or after the stars of a currently
  15515. empty headline, then the yank is handled specially. How exactly depends
  15516. on the value of the following variables, both set by default.
  15517. org-yank-folded-subtrees
  15518. When set, the subtree(s) will be folded after insertion, but only
  15519. if doing so would now swallow text after the yanked text.
  15520. org-yank-adjusted-subtrees
  15521. When set, the subtree will be promoted or demoted in order to
  15522. fit into the local outline tree structure, which means that the level
  15523. will be adjusted so that it becomes the smaller one of the two
  15524. *visible* surrounding headings.
  15525. Any prefix to this command will cause `yank' to be called directly with
  15526. no special treatment. In particular, a simple `C-u' prefix will just
  15527. plainly yank the text as it is.
  15528. \[1] The test checks if the first non-white line is a heading
  15529. and if there are no other headings with fewer stars."
  15530. (interactive "P")
  15531. (org-yank-generic 'yank arg))
  15532. (defun org-yank-generic (command arg)
  15533. "Perform some yank-like command.
  15534. This function implements the behavior described in the `org-yank'
  15535. documentation. However, it has been generalized to work for any
  15536. interactive command with similar behavior."
  15537. ;; pretend to be command COMMAND
  15538. (setq this-command command)
  15539. (if arg
  15540. (call-interactively command)
  15541. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15542. (and (org-kill-is-subtree-p)
  15543. (or (bolp)
  15544. (and (looking-at "[ \t]*$")
  15545. (string-match
  15546. "\\`\\*+\\'"
  15547. (buffer-substring (point-at-bol) (point)))))))
  15548. swallowp)
  15549. (cond
  15550. ((and subtreep org-yank-folded-subtrees)
  15551. (let ((beg (point))
  15552. end)
  15553. (if (and subtreep org-yank-adjusted-subtrees)
  15554. (org-paste-subtree nil nil 'for-yank)
  15555. (call-interactively command))
  15556. (setq end (point))
  15557. (goto-char beg)
  15558. (when (and (bolp) subtreep
  15559. (not (setq swallowp
  15560. (org-yank-folding-would-swallow-text beg end))))
  15561. (or (looking-at outline-regexp)
  15562. (re-search-forward (concat "^" outline-regexp) end t))
  15563. (while (and (< (point) end) (looking-at outline-regexp))
  15564. (hide-subtree)
  15565. (org-cycle-show-empty-lines 'folded)
  15566. (condition-case nil
  15567. (outline-forward-same-level 1)
  15568. (error (goto-char end)))))
  15569. (when swallowp
  15570. (message
  15571. "Inserted text not folded because that would swallow text"))
  15572. (goto-char end)
  15573. (skip-chars-forward " \t\n\r")
  15574. (beginning-of-line 1)
  15575. (push-mark beg 'nomsg)))
  15576. ((and subtreep org-yank-adjusted-subtrees)
  15577. (let ((beg (point-at-bol)))
  15578. (org-paste-subtree nil nil 'for-yank)
  15579. (push-mark beg 'nomsg)))
  15580. (t
  15581. (call-interactively command))))))
  15582. (defun org-yank-folding-would-swallow-text (beg end)
  15583. "Would hide-subtree at BEG swallow any text after END?"
  15584. (let (level)
  15585. (save-excursion
  15586. (goto-char beg)
  15587. (when (or (looking-at outline-regexp)
  15588. (re-search-forward (concat "^" outline-regexp) end t))
  15589. (setq level (org-outline-level)))
  15590. (goto-char end)
  15591. (skip-chars-forward " \t\r\n\v\f")
  15592. (if (or (eobp)
  15593. (and (bolp) (looking-at org-outline-regexp)
  15594. (<= (org-outline-level) level)))
  15595. nil ; Nothing would be swallowed
  15596. t)))) ; something would swallow
  15597. (define-key org-mode-map "\C-y" 'org-yank)
  15598. (defun org-invisible-p ()
  15599. "Check if point is at a character currently not visible."
  15600. ;; Early versions of noutline don't have `outline-invisible-p'.
  15601. (if (fboundp 'outline-invisible-p)
  15602. (outline-invisible-p)
  15603. (get-char-property (point) 'invisible)))
  15604. (defun org-invisible-p2 ()
  15605. "Check if point is at a character currently not visible."
  15606. (save-excursion
  15607. (if (and (eolp) (not (bobp))) (backward-char 1))
  15608. ;; Early versions of noutline don't have `outline-invisible-p'.
  15609. (if (fboundp 'outline-invisible-p)
  15610. (outline-invisible-p)
  15611. (get-char-property (point) 'invisible))))
  15612. (defun org-back-to-heading (&optional invisible-ok)
  15613. "Call `outline-back-to-heading', but provide a better error message."
  15614. (condition-case nil
  15615. (outline-back-to-heading invisible-ok)
  15616. (error (error "Before first headline at position %d in buffer %s"
  15617. (point) (current-buffer)))))
  15618. (defun org-before-first-heading-p ()
  15619. "Before first heading?"
  15620. (save-excursion
  15621. (null (re-search-backward "^\\*+ " nil t))))
  15622. (defun org-on-heading-p (&optional ignored)
  15623. (outline-on-heading-p t))
  15624. (defun org-at-heading-p (&optional ignored)
  15625. (outline-on-heading-p t))
  15626. (defun org-at-heading-or-item-p ()
  15627. (or (org-on-heading-p) (org-at-item-p)))
  15628. (defun org-on-target-p ()
  15629. (or (org-in-regexp org-radio-target-regexp)
  15630. (org-in-regexp org-target-regexp)))
  15631. (defun org-up-heading-all (arg)
  15632. "Move to the heading line of which the present line is a subheading.
  15633. This function considers both visible and invisible heading lines.
  15634. With argument, move up ARG levels."
  15635. (if (fboundp 'outline-up-heading-all)
  15636. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15637. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15638. (defun org-up-heading-safe ()
  15639. "Move to the heading line of which the present line is a subheading.
  15640. This version will not throw an error. It will return the level of the
  15641. headline found, or nil if no higher level is found.
  15642. Also, this function will be a lot faster than `outline-up-heading',
  15643. because it relies on stars being the outline starters. This can really
  15644. make a significant difference in outlines with very many siblings."
  15645. (let (start-level re)
  15646. (org-back-to-heading t)
  15647. (setq start-level (funcall outline-level))
  15648. (if (equal start-level 1)
  15649. nil
  15650. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15651. (if (re-search-backward re nil t)
  15652. (funcall outline-level)))))
  15653. (defun org-first-sibling-p ()
  15654. "Is this heading the first child of its parents?"
  15655. (interactive)
  15656. (let ((re (concat "^" outline-regexp))
  15657. level l)
  15658. (unless (org-at-heading-p t)
  15659. (error "Not at a heading"))
  15660. (setq level (funcall outline-level))
  15661. (save-excursion
  15662. (if (not (re-search-backward re nil t))
  15663. t
  15664. (setq l (funcall outline-level))
  15665. (< l level)))))
  15666. (defun org-goto-sibling (&optional previous)
  15667. "Goto the next sibling, even if it is invisible.
  15668. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15669. when a sibling was found. When none is found, return nil and don't
  15670. move point."
  15671. (let ((fun (if previous 're-search-backward 're-search-forward))
  15672. (pos (point))
  15673. (re (concat "^" outline-regexp))
  15674. level l)
  15675. (when (condition-case nil (org-back-to-heading t) (error nil))
  15676. (setq level (funcall outline-level))
  15677. (catch 'exit
  15678. (or previous (forward-char 1))
  15679. (while (funcall fun re nil t)
  15680. (setq l (funcall outline-level))
  15681. (when (< l level) (goto-char pos) (throw 'exit nil))
  15682. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15683. (goto-char pos)
  15684. nil))))
  15685. (defun org-show-siblings ()
  15686. "Show all siblings of the current headline."
  15687. (save-excursion
  15688. (while (org-goto-sibling) (org-flag-heading nil)))
  15689. (save-excursion
  15690. (while (org-goto-sibling 'previous)
  15691. (org-flag-heading nil))))
  15692. (defun org-show-hidden-entry ()
  15693. "Show an entry where even the heading is hidden."
  15694. (save-excursion
  15695. (org-show-entry)))
  15696. (defun org-flag-heading (flag &optional entry)
  15697. "Flag the current heading. FLAG non-nil means make invisible.
  15698. When ENTRY is non-nil, show the entire entry."
  15699. (save-excursion
  15700. (org-back-to-heading t)
  15701. ;; Check if we should show the entire entry
  15702. (if entry
  15703. (progn
  15704. (org-show-entry)
  15705. (save-excursion
  15706. (and (outline-next-heading)
  15707. (org-flag-heading nil))))
  15708. (outline-flag-region (max (point-min) (1- (point)))
  15709. (save-excursion (outline-end-of-heading) (point))
  15710. flag))))
  15711. (defun org-get-next-sibling ()
  15712. "Move to next heading of the same level, and return point.
  15713. If there is no such heading, return nil.
  15714. This is like outline-next-sibling, but invisible headings are ok."
  15715. (let ((level (funcall outline-level)))
  15716. (outline-next-heading)
  15717. (while (and (not (eobp)) (> (funcall outline-level) level))
  15718. (outline-next-heading))
  15719. (if (or (eobp) (< (funcall outline-level) level))
  15720. nil
  15721. (point))))
  15722. (defun org-get-last-sibling ()
  15723. "Move to previous heading of the same level, and return point.
  15724. If there is no such heading, return nil."
  15725. (let ((opoint (point))
  15726. (level (funcall outline-level)))
  15727. (outline-previous-heading)
  15728. (when (and (/= (point) opoint) (outline-on-heading-p t))
  15729. (while (and (> (funcall outline-level) level)
  15730. (not (bobp)))
  15731. (outline-previous-heading))
  15732. (if (< (funcall outline-level) level)
  15733. nil
  15734. (point)))))
  15735. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15736. ;; This contains an exact copy of the original function, but it uses
  15737. ;; `org-back-to-heading', to make it work also in invisible
  15738. ;; trees. And is uses an invisible-OK argument.
  15739. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15740. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15741. ;; with many children and grandchildren etc, this can be much faster
  15742. ;; than the outline version.
  15743. (org-back-to-heading invisible-OK)
  15744. (let ((first t)
  15745. (level (funcall outline-level)))
  15746. (if (and (org-mode-p) (< level 1000))
  15747. ;; A true heading (not a plain list item), in Org-mode
  15748. ;; This means we can easily find the end by looking
  15749. ;; only for the right number of stars. Using a regexp to do
  15750. ;; this is so much faster than using a Lisp loop.
  15751. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15752. (forward-char 1)
  15753. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15754. ;; something else, do it the slow way
  15755. (while (and (not (eobp))
  15756. (or first (> (funcall outline-level) level)))
  15757. (setq first nil)
  15758. (outline-next-heading)))
  15759. (unless to-heading
  15760. (if (memq (preceding-char) '(?\n ?\^M))
  15761. (progn
  15762. ;; Go to end of line before heading
  15763. (forward-char -1)
  15764. (if (memq (preceding-char) '(?\n ?\^M))
  15765. ;; leave blank line before heading
  15766. (forward-char -1))))))
  15767. (point))
  15768. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15769. "Use Org version in org-mode, for dramatic speed-up."
  15770. (if (eq major-mode 'org-mode)
  15771. (progn
  15772. (org-end-of-subtree nil t)
  15773. (unless (eobp) (backward-char 1)))
  15774. ad-do-it))
  15775. (defun org-forward-same-level (arg &optional invisible-ok)
  15776. "Move forward to the arg'th subheading at same level as this one.
  15777. Stop at the first and last subheadings of a superior heading."
  15778. (interactive "p")
  15779. (org-back-to-heading invisible-ok)
  15780. (org-on-heading-p)
  15781. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15782. (re (format "^\\*\\{1,%d\\} " level))
  15783. l)
  15784. (forward-char 1)
  15785. (while (> arg 0)
  15786. (while (and (re-search-forward re nil 'move)
  15787. (setq l (- (match-end 0) (match-beginning 0) 1))
  15788. (= l level)
  15789. (not invisible-ok)
  15790. (org-invisible-p))
  15791. (if (< l level) (setq arg 1)))
  15792. (setq arg (1- arg)))
  15793. (beginning-of-line 1)))
  15794. (defun org-backward-same-level (arg &optional invisible-ok)
  15795. "Move backward to the arg'th subheading at same level as this one.
  15796. Stop at the first and last subheadings of a superior heading."
  15797. (interactive "p")
  15798. (org-back-to-heading)
  15799. (org-on-heading-p)
  15800. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15801. (re (format "^\\*\\{1,%d\\} " level))
  15802. l)
  15803. (while (> arg 0)
  15804. (while (and (re-search-backward re nil 'move)
  15805. (setq l (- (match-end 0) (match-beginning 0) 1))
  15806. (= l level)
  15807. (not invisible-ok)
  15808. (org-invisible-p))
  15809. (if (< l level) (setq arg 1)))
  15810. (setq arg (1- arg)))))
  15811. (defun org-show-subtree ()
  15812. "Show everything after this heading at deeper levels."
  15813. (outline-flag-region
  15814. (point)
  15815. (save-excursion
  15816. (org-end-of-subtree t t))
  15817. nil))
  15818. (defun org-show-entry ()
  15819. "Show the body directly following this heading.
  15820. Show the heading too, if it is currently invisible."
  15821. (interactive)
  15822. (save-excursion
  15823. (condition-case nil
  15824. (progn
  15825. (org-back-to-heading t)
  15826. (outline-flag-region
  15827. (max (point-min) (1- (point)))
  15828. (save-excursion
  15829. (if (re-search-forward
  15830. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15831. (match-beginning 1)
  15832. (point-max)))
  15833. nil)
  15834. (org-cycle-hide-drawers 'children))
  15835. (error nil))))
  15836. (defun org-make-options-regexp (kwds &optional extra)
  15837. "Make a regular expression for keyword lines."
  15838. (concat
  15839. "^"
  15840. "#?[ \t]*\\+\\("
  15841. (mapconcat 'regexp-quote kwds "\\|")
  15842. (if extra (concat "\\|" extra))
  15843. "\\):[ \t]*"
  15844. "\\(.*\\)"))
  15845. ;; Make isearch reveal the necessary context
  15846. (defun org-isearch-end ()
  15847. "Reveal context after isearch exits."
  15848. (when isearch-success ; only if search was successful
  15849. (if (featurep 'xemacs)
  15850. ;; Under XEmacs, the hook is run in the correct place,
  15851. ;; we directly show the context.
  15852. (org-show-context 'isearch)
  15853. ;; In Emacs the hook runs *before* restoring the overlays.
  15854. ;; So we have to use a one-time post-command-hook to do this.
  15855. ;; (Emacs 22 has a special variable, see function `org-mode')
  15856. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15857. isearch-mode-end-hook-quit)
  15858. ;; Only when the isearch was not quitted.
  15859. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15860. 'append 'local)))))
  15861. (defun org-isearch-post-command ()
  15862. "Remove self from hook, and show context."
  15863. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15864. (org-show-context 'isearch))
  15865. ;;;; Integration with and fixes for other packages
  15866. ;;; Imenu support
  15867. (defvar org-imenu-markers nil
  15868. "All markers currently used by Imenu.")
  15869. (make-variable-buffer-local 'org-imenu-markers)
  15870. (defun org-imenu-new-marker (&optional pos)
  15871. "Return a new marker for use by Imenu, and remember the marker."
  15872. (let ((m (make-marker)))
  15873. (move-marker m (or pos (point)))
  15874. (push m org-imenu-markers)
  15875. m))
  15876. (defun org-imenu-get-tree ()
  15877. "Produce the index for Imenu."
  15878. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15879. (setq org-imenu-markers nil)
  15880. (let* ((n org-imenu-depth)
  15881. (re (concat "^" outline-regexp))
  15882. (subs (make-vector (1+ n) nil))
  15883. (last-level 0)
  15884. m level head)
  15885. (save-excursion
  15886. (save-restriction
  15887. (widen)
  15888. (goto-char (point-max))
  15889. (while (re-search-backward re nil t)
  15890. (setq level (org-reduced-level (funcall outline-level)))
  15891. (when (<= level n)
  15892. (looking-at org-complex-heading-regexp)
  15893. (setq head (org-link-display-format
  15894. (org-match-string-no-properties 4))
  15895. m (org-imenu-new-marker))
  15896. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15897. (if (>= level last-level)
  15898. (push (cons head m) (aref subs level))
  15899. (push (cons head (aref subs (1+ level))) (aref subs level))
  15900. (loop for i from (1+ level) to n do (aset subs i nil)))
  15901. (setq last-level level)))))
  15902. (aref subs 1)))
  15903. (eval-after-load "imenu"
  15904. '(progn
  15905. (add-hook 'imenu-after-jump-hook
  15906. (lambda ()
  15907. (if (eq major-mode 'org-mode)
  15908. (org-show-context 'org-goto))))))
  15909. (defun org-link-display-format (link)
  15910. "Replace a link with either the description, or the link target
  15911. if no description is present"
  15912. (save-match-data
  15913. (if (string-match org-bracket-link-analytic-regexp link)
  15914. (replace-match (or (match-string 5 link)
  15915. (concat (match-string 1 link)
  15916. (match-string 3 link)))
  15917. nil nil link)
  15918. link)))
  15919. ;; Speedbar support
  15920. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15921. "Overlay marking the agenda restriction line in speedbar.")
  15922. (org-overlay-put org-speedbar-restriction-lock-overlay
  15923. 'face 'org-agenda-restriction-lock)
  15924. (org-overlay-put org-speedbar-restriction-lock-overlay
  15925. 'help-echo "Agendas are currently limited to this item.")
  15926. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15927. (defun org-speedbar-set-agenda-restriction ()
  15928. "Restrict future agenda commands to the location at point in speedbar.
  15929. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15930. (interactive)
  15931. (require 'org-agenda)
  15932. (let (p m tp np dir txt)
  15933. (cond
  15934. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15935. 'org-imenu t))
  15936. (setq m (get-text-property p 'org-imenu-marker))
  15937. (save-excursion
  15938. (save-restriction
  15939. (set-buffer (marker-buffer m))
  15940. (goto-char m)
  15941. (org-agenda-set-restriction-lock 'subtree))))
  15942. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15943. 'speedbar-function 'speedbar-find-file))
  15944. (setq tp (previous-single-property-change
  15945. (1+ p) 'speedbar-function)
  15946. np (next-single-property-change
  15947. tp 'speedbar-function)
  15948. dir (speedbar-line-directory)
  15949. txt (buffer-substring-no-properties (or tp (point-min))
  15950. (or np (point-max))))
  15951. (save-excursion
  15952. (save-restriction
  15953. (set-buffer (find-file-noselect
  15954. (let ((default-directory dir))
  15955. (expand-file-name txt))))
  15956. (unless (org-mode-p)
  15957. (error "Cannot restrict to non-Org-mode file"))
  15958. (org-agenda-set-restriction-lock 'file))))
  15959. (t (error "Don't know how to restrict Org-mode's agenda")))
  15960. (org-move-overlay org-speedbar-restriction-lock-overlay
  15961. (point-at-bol) (point-at-eol))
  15962. (setq current-prefix-arg nil)
  15963. (org-agenda-maybe-redo)))
  15964. (eval-after-load "speedbar"
  15965. '(progn
  15966. (speedbar-add-supported-extension ".org")
  15967. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15968. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15969. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15970. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15971. (add-hook 'speedbar-visiting-tag-hook
  15972. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15973. ;;; Fixes and Hacks for problems with other packages
  15974. ;; Make flyspell not check words in links, to not mess up our keymap
  15975. (defun org-mode-flyspell-verify ()
  15976. "Don't let flyspell put overlays at active buttons."
  15977. (and (not (get-text-property (point) 'keymap))
  15978. (not (get-text-property (point) 'org-no-flyspell))))
  15979. (defun org-remove-flyspell-overlays-in (beg end)
  15980. "Remove flyspell overlays in region."
  15981. (and (org-bound-and-true-p flyspell-mode)
  15982. (fboundp 'flyspell-delete-region-overlays)
  15983. (flyspell-delete-region-overlays beg end))
  15984. (add-text-properties beg end '(org-no-flyspell t)))
  15985. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  15986. (eval-after-load "bookmark"
  15987. '(if (boundp 'bookmark-after-jump-hook)
  15988. ;; We can use the hook
  15989. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15990. ;; Hook not available, use advice
  15991. (defadvice bookmark-jump (after org-make-visible activate)
  15992. "Make the position visible."
  15993. (org-bookmark-jump-unhide))))
  15994. ;; Make sure saveplace shows the location if it was hidden
  15995. (eval-after-load "saveplace"
  15996. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15997. "Make the position visible."
  15998. (org-bookmark-jump-unhide)))
  15999. ;; Make sure ecb shows the location if it was hidden
  16000. (eval-after-load "ecb"
  16001. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16002. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16003. (if (eq major-mode 'org-mode)
  16004. (org-show-context))))
  16005. (defun org-bookmark-jump-unhide ()
  16006. "Unhide the current position, to show the bookmark location."
  16007. (and (org-mode-p)
  16008. (or (org-invisible-p)
  16009. (save-excursion (goto-char (max (point-min) (1- (point))))
  16010. (org-invisible-p)))
  16011. (org-show-context 'bookmark-jump)))
  16012. ;; Make session.el ignore our circular variable
  16013. (eval-after-load "session"
  16014. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16015. ;;;; Experimental code
  16016. (defun org-closed-in-range ()
  16017. "Sparse tree of items closed in a certain time range.
  16018. Still experimental, may disappear in the future."
  16019. (interactive)
  16020. ;; Get the time interval from the user.
  16021. (let* ((time1 (org-float-time
  16022. (org-read-date nil 'to-time nil "Starting date: ")))
  16023. (time2 (org-float-time
  16024. (org-read-date nil 'to-time nil "End date:")))
  16025. ;; callback function
  16026. (callback (lambda ()
  16027. (let ((time
  16028. (org-float-time
  16029. (apply 'encode-time
  16030. (org-parse-time-string
  16031. (match-string 1))))))
  16032. ;; check if time in interval
  16033. (and (>= time time1) (<= time time2))))))
  16034. ;; make tree, check each match with the callback
  16035. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16036. ;;;; Finish up
  16037. (provide 'org)
  16038. (run-hooks 'org-load-hook)
  16039. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16040. ;;; org.el ends here