org.el 620 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651
  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.27trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.27trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  155. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  156. (const :tag " vm: Links to VM folders/messages" org-vm)
  157. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  158. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  159. (const :tag " mouse: Additional mouse support" org-mouse)
  160. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  161. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  162. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  163. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  164. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  165. (const :tag "C collector: Collect properties into tables" org-collector)
  166. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  167. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  168. (const :tag "C eval: Include command output as text" org-eval)
  169. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  170. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  171. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  172. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  173. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  174. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  175. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  176. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  177. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  178. (const :tag "C R: Computation using the R language" org-R)
  179. (const :tag "C registry: A registry for Org links" org-registry)
  180. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  181. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  182. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  183. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  184. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  185. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  186. (defcustom org-support-shift-select nil
  187. "Non-nil means, make shift-cursor commands select text when possible.
  188. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  189. selecting a region, or enlarge thusly regions started in this way.
  190. In Org-mode, in special contexts, these same keys are used for other
  191. purposes, important enough to compete with shift selection. Org tries
  192. to balance these needs by supporting `shift-select-mode' outside these
  193. special contexts, under control of this variable.
  194. The default of this variable is nil, to avoid confusing behavior. Shifted
  195. cursor keys will then execute Org commands in the following contexts:
  196. - on a headline, changing TODO state (left/right) and priority (up/down)
  197. - on a time stamp, changing the time
  198. - in a plain list item, changing the bullet type
  199. - in a property definition line, switching between allowed values
  200. - in the BEGIN line of a clock table (changing the time block).
  201. Outside these contexts, the commands will throw an error.
  202. When this variable is t and the cursor is not in a special context,
  203. Org-mode will support shift-selection for making and enlarging regions.
  204. To make this more effective, the bullet cycling will no longer happen
  205. anywhere in an item line, but only if the cursor is exactly on the bullet.
  206. If you set this variable to the symbol `always', then the keys
  207. will not be special in headlines, property lines, and item lines, to make
  208. shift selection work there as well. If this is what you want, you can
  209. use the following alternative commands: `C-c C-t' and `C-c ,' to
  210. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  211. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  212. edited by hand or in column view.
  213. However, when the cursor is on a timestamp, shift-cursor commands
  214. will still edit the time stamp - this is just too good to give up.
  215. XEmacs user should have this variable set to nil, because shift-select-mode
  216. is Emacs 23 only."
  217. :group 'org
  218. :type '(choice
  219. (const :tag "Never" nil)
  220. (const :tag "When outside special context" t)
  221. (const :tag "Everywhere except timestamps" always)))
  222. (defgroup org-startup nil
  223. "Options concerning startup of Org-mode."
  224. :tag "Org Startup"
  225. :group 'org)
  226. (defcustom org-startup-folded t
  227. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  228. This can also be configured on a per-file basis by adding one of
  229. the following lines anywhere in the buffer:
  230. #+STARTUP: fold
  231. #+STARTUP: nofold
  232. #+STARTUP: content"
  233. :group 'org-startup
  234. :type '(choice
  235. (const :tag "nofold: show all" nil)
  236. (const :tag "fold: overview" t)
  237. (const :tag "content: all headlines" content)))
  238. (defcustom org-startup-truncated t
  239. "Non-nil means, entering Org-mode will set `truncate-lines'.
  240. This is useful since some lines containing links can be very long and
  241. uninteresting. Also tables look terrible when wrapped."
  242. :group 'org-startup
  243. :type 'boolean)
  244. (defcustom org-startup-align-all-tables nil
  245. "Non-nil means, align all tables when visiting a file.
  246. This is useful when the column width in tables is forced with <N> cookies
  247. in table fields. Such tables will look correct only after the first re-align.
  248. This can also be configured on a per-file basis by adding one of
  249. the following lines anywhere in the buffer:
  250. #+STARTUP: align
  251. #+STARTUP: noalign"
  252. :group 'org-startup
  253. :type 'boolean)
  254. (defcustom org-insert-mode-line-in-empty-file nil
  255. "Non-nil means insert the first line setting Org-mode in empty files.
  256. When the function `org-mode' is called interactively in an empty file, this
  257. normally means that the file name does not automatically trigger Org-mode.
  258. To ensure that the file will always be in Org-mode in the future, a
  259. line enforcing Org-mode will be inserted into the buffer, if this option
  260. has been set."
  261. :group 'org-startup
  262. :type 'boolean)
  263. (defcustom org-replace-disputed-keys nil
  264. "Non-nil means use alternative key bindings for some keys.
  265. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  266. These keys are also used by other packages like shift-selection-mode'
  267. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  268. If you want to use Org-mode together with one of these other modes,
  269. or more generally if you would like to move some Org-mode commands to
  270. other keys, set this variable and configure the keys with the variable
  271. `org-disputed-keys'.
  272. This option is only relevant at load-time of Org-mode, and must be set
  273. *before* org.el is loaded. Changing it requires a restart of Emacs to
  274. become effective."
  275. :group 'org-startup
  276. :type 'boolean)
  277. (defcustom org-use-extra-keys nil
  278. "Non-nil means use extra key sequence definitions for certain
  279. commands. This happens automatically if you run XEmacs or if
  280. window-system is nil. This variable lets you do the same
  281. manually. You must set it before loading org.
  282. Example: on Carbon Emacs 22 running graphically, with an external
  283. keyboard on a Powerbook, the default way of setting M-left might
  284. not work for either Alt or ESC. Setting this variable will make
  285. it work for ESC."
  286. :group 'org-startup
  287. :type 'boolean)
  288. (if (fboundp 'defvaralias)
  289. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  290. (defcustom org-disputed-keys
  291. '(([(shift up)] . [(meta p)])
  292. ([(shift down)] . [(meta n)])
  293. ([(shift left)] . [(meta -)])
  294. ([(shift right)] . [(meta +)])
  295. ([(control shift right)] . [(meta shift +)])
  296. ([(control shift left)] . [(meta shift -)]))
  297. "Keys for which Org-mode and other modes compete.
  298. This is an alist, cars are the default keys, second element specifies
  299. the alternative to use when `org-replace-disputed-keys' is t.
  300. Keys can be specified in any syntax supported by `define-key'.
  301. The value of this option takes effect only at Org-mode's startup,
  302. therefore you'll have to restart Emacs to apply it after changing."
  303. :group 'org-startup
  304. :type 'alist)
  305. (defun org-key (key)
  306. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  307. Or return the original if not disputed."
  308. (if org-replace-disputed-keys
  309. (let* ((nkey (key-description key))
  310. (x (org-find-if (lambda (x)
  311. (equal (key-description (car x)) nkey))
  312. org-disputed-keys)))
  313. (if x (cdr x) key))
  314. key))
  315. (defun org-find-if (predicate seq)
  316. (catch 'exit
  317. (while seq
  318. (if (funcall predicate (car seq))
  319. (throw 'exit (car seq))
  320. (pop seq)))))
  321. (defun org-defkey (keymap key def)
  322. "Define a key, possibly translated, as returned by `org-key'."
  323. (define-key keymap (org-key key) def))
  324. (defcustom org-ellipsis nil
  325. "The ellipsis to use in the Org-mode outline.
  326. When nil, just use the standard three dots. When a string, use that instead,
  327. When a face, use the standard 3 dots, but with the specified face.
  328. The change affects only Org-mode (which will then use its own display table).
  329. Changing this requires executing `M-x org-mode' in a buffer to become
  330. effective."
  331. :group 'org-startup
  332. :type '(choice (const :tag "Default" nil)
  333. (face :tag "Face" :value org-warning)
  334. (string :tag "String" :value "...#")))
  335. (defvar org-display-table nil
  336. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  337. (defgroup org-keywords nil
  338. "Keywords in Org-mode."
  339. :tag "Org Keywords"
  340. :group 'org)
  341. (defcustom org-deadline-string "DEADLINE:"
  342. "String to mark deadline entries.
  343. A deadline is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-deadline].
  346. Changes become only effective after restarting Emacs."
  347. :group 'org-keywords
  348. :type 'string)
  349. (defcustom org-scheduled-string "SCHEDULED:"
  350. "String to mark scheduled TODO entries.
  351. A schedule is this string, followed by a time stamp. Should be a word,
  352. terminated by a colon. You can insert a schedule keyword and
  353. a timestamp with \\[org-schedule].
  354. Changes become only effective after restarting Emacs."
  355. :group 'org-keywords
  356. :type 'string)
  357. (defcustom org-closed-string "CLOSED:"
  358. "String used as the prefix for timestamps logging closing a TODO entry."
  359. :group 'org-keywords
  360. :type 'string)
  361. (defcustom org-clock-string "CLOCK:"
  362. "String used as prefix for timestamps clocking work hours on an item."
  363. :group 'org-keywords
  364. :type 'string)
  365. (defcustom org-comment-string "COMMENT"
  366. "Entries starting with this keyword will never be exported.
  367. An entry can be toggled between COMMENT and normal with
  368. \\[org-toggle-comment].
  369. Changes become only effective after restarting Emacs."
  370. :group 'org-keywords
  371. :type 'string)
  372. (defcustom org-quote-string "QUOTE"
  373. "Entries starting with this keyword will be exported in fixed-width font.
  374. Quoting applies only to the text in the entry following the headline, and does
  375. not extend beyond the next headline, even if that is lower level.
  376. An entry can be toggled between QUOTE and normal with
  377. \\[org-toggle-fixed-width-section]."
  378. :group 'org-keywords
  379. :type 'string)
  380. (defconst org-repeat-re
  381. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  382. "Regular expression for specifying repeated events.
  383. After a match, group 1 contains the repeat expression.")
  384. (defgroup org-structure nil
  385. "Options concerning the general structure of Org-mode files."
  386. :tag "Org Structure"
  387. :group 'org)
  388. (defgroup org-reveal-location nil
  389. "Options about how to make context of a location visible."
  390. :tag "Org Reveal Location"
  391. :group 'org-structure)
  392. (defconst org-context-choice
  393. '(choice
  394. (const :tag "Always" t)
  395. (const :tag "Never" nil)
  396. (repeat :greedy t :tag "Individual contexts"
  397. (cons
  398. (choice :tag "Context"
  399. (const agenda)
  400. (const org-goto)
  401. (const occur-tree)
  402. (const tags-tree)
  403. (const link-search)
  404. (const mark-goto)
  405. (const bookmark-jump)
  406. (const isearch)
  407. (const default))
  408. (boolean))))
  409. "Contexts for the reveal options.")
  410. (defcustom org-show-hierarchy-above '((default . t))
  411. "Non-nil means, show full hierarchy when revealing a location.
  412. Org-mode often shows locations in an org-mode file which might have
  413. been invisible before. When this is set, the hierarchy of headings
  414. above the exposed location is shown.
  415. Turning this off for example for sparse trees makes them very compact.
  416. Instead of t, this can also be an alist specifying this option for different
  417. contexts. Valid contexts are
  418. agenda when exposing an entry from the agenda
  419. org-goto when using the command `org-goto' on key C-c C-j
  420. occur-tree when using the command `org-occur' on key C-c /
  421. tags-tree when constructing a sparse tree based on tags matches
  422. link-search when exposing search matches associated with a link
  423. mark-goto when exposing the jump goal of a mark
  424. bookmark-jump when exposing a bookmark location
  425. isearch when exiting from an incremental search
  426. default default for all contexts not set explicitly"
  427. :group 'org-reveal-location
  428. :type org-context-choice)
  429. (defcustom org-show-following-heading '((default . nil))
  430. "Non-nil means, show following heading when revealing a location.
  431. Org-mode often shows locations in an org-mode file which might have
  432. been invisible before. When this is set, the heading following the
  433. match is shown.
  434. Turning this off for example for sparse trees makes them very compact,
  435. but makes it harder to edit the location of the match. In such a case,
  436. use the command \\[org-reveal] to show more context.
  437. Instead of t, this can also be an alist specifying this option for different
  438. contexts. See `org-show-hierarchy-above' for valid contexts."
  439. :group 'org-reveal-location
  440. :type org-context-choice)
  441. (defcustom org-show-siblings '((default . nil) (isearch t))
  442. "Non-nil means, show all sibling heading when revealing a location.
  443. Org-mode often shows locations in an org-mode file which might have
  444. been invisible before. When this is set, the sibling of the current entry
  445. heading are all made visible. If `org-show-hierarchy-above' is t,
  446. the same happens on each level of the hierarchy above the current entry.
  447. By default this is on for the isearch context, off for all other contexts.
  448. Turning this off for example for sparse trees makes them very compact,
  449. but makes it harder to edit the location of the match. In such a case,
  450. use the command \\[org-reveal] to show more context.
  451. Instead of t, this can also be an alist specifying this option for different
  452. contexts. See `org-show-hierarchy-above' for valid contexts."
  453. :group 'org-reveal-location
  454. :type org-context-choice)
  455. (defcustom org-show-entry-below '((default . nil))
  456. "Non-nil means, show the entry below a headline when revealing a location.
  457. Org-mode often shows locations in an org-mode file which might have
  458. been invisible before. When this is set, the text below the headline that is
  459. exposed is also shown.
  460. By default this is off for all contexts.
  461. Instead of t, this can also be an alist specifying this option for different
  462. contexts. See `org-show-hierarchy-above' for valid contexts."
  463. :group 'org-reveal-location
  464. :type org-context-choice)
  465. (defcustom org-indirect-buffer-display 'other-window
  466. "How should indirect tree buffers be displayed?
  467. This applies to indirect buffers created with the commands
  468. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  469. Valid values are:
  470. current-window Display in the current window
  471. other-window Just display in another window.
  472. dedicated-frame Create one new frame, and re-use it each time.
  473. new-frame Make a new frame each time. Note that in this case
  474. previously-made indirect buffers are kept, and you need to
  475. kill these buffers yourself."
  476. :group 'org-structure
  477. :group 'org-agenda-windows
  478. :type '(choice
  479. (const :tag "In current window" current-window)
  480. (const :tag "In current frame, other window" other-window)
  481. (const :tag "Each time a new frame" new-frame)
  482. (const :tag "One dedicated frame" dedicated-frame)))
  483. (defgroup org-cycle nil
  484. "Options concerning visibility cycling in Org-mode."
  485. :tag "Org Cycle"
  486. :group 'org-structure)
  487. (defcustom org-cycle-max-level nil
  488. "Maximum level which should still be subject to visibility cycling.
  489. Levels higher than this will, for cycling, be treated as text, not a headline.
  490. When `org-odd-levels-only' is set, a value of N in this variable actually
  491. means 2N-1 stars as the limiting headline.
  492. When nil, cycle all levels.
  493. Note that the limiting level of cycling is also influenced by
  494. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  495. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  496. than its value."
  497. :group 'org-cycle
  498. :type '(choice
  499. (const :tag "No limit" nil)
  500. (integer :tag "Maximum level")))
  501. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  502. "Names of drawers. Drawers are not opened by cycling on the headline above.
  503. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  504. this:
  505. :DRAWERNAME:
  506. .....
  507. :END:
  508. The drawer \"PROPERTIES\" is special for capturing properties through
  509. the property API.
  510. Drawers can be defined on the per-file basis with a line like:
  511. #+DRAWERS: HIDDEN STATE PROPERTIES"
  512. :group 'org-structure
  513. :group 'org-cycle
  514. :type '(repeat (string :tag "Drawer Name")))
  515. (defcustom org-cycle-global-at-bob nil
  516. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  517. This makes it possible to do global cycling without having to use S-TAB or
  518. C-u TAB. For this special case to work, the first line of the buffer
  519. must not be a headline - it may be empty or some other text. When used in
  520. this way, `org-cycle-hook' is disables temporarily, to make sure the
  521. cursor stays at the beginning of the buffer.
  522. When this option is nil, don't do anything special at the beginning
  523. of the buffer."
  524. :group 'org-cycle
  525. :type 'boolean)
  526. (defcustom org-cycle-emulate-tab t
  527. "Where should `org-cycle' emulate TAB.
  528. nil Never
  529. white Only in completely white lines
  530. whitestart Only at the beginning of lines, before the first non-white char
  531. t Everywhere except in headlines
  532. exc-hl-bol Everywhere except at the start of a headline
  533. If TAB is used in a place where it does not emulate TAB, the current subtree
  534. visibility is cycled."
  535. :group 'org-cycle
  536. :type '(choice (const :tag "Never" nil)
  537. (const :tag "Only in completely white lines" white)
  538. (const :tag "Before first char in a line" whitestart)
  539. (const :tag "Everywhere except in headlines" t)
  540. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  541. ))
  542. (defcustom org-cycle-separator-lines 2
  543. "Number of empty lines needed to keep an empty line between collapsed trees.
  544. If you leave an empty line between the end of a subtree and the following
  545. headline, this empty line is hidden when the subtree is folded.
  546. Org-mode will leave (exactly) one empty line visible if the number of
  547. empty lines is equal or larger to the number given in this variable.
  548. So the default 2 means, at least 2 empty lines after the end of a subtree
  549. are needed to produce free space between a collapsed subtree and the
  550. following headline.
  551. Special case: when 0, never leave empty lines in collapsed view."
  552. :group 'org-cycle
  553. :type 'integer)
  554. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  555. (defcustom org-pre-cycle-hook nil
  556. "Hook that is run before visibility cycling is happening.
  557. The function(s) in this hook must accept a single argument which indicates
  558. the new state that will be set right after running this hook. The
  559. argument is a symbol. Before a global state change, it can have the values
  560. `overview', `content', or `all'. Before a local state change, it can have
  561. the values `folded', `children', or `subtree'."
  562. :group 'org-cycle
  563. :type 'hook)
  564. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  565. org-cycle-hide-drawers
  566. org-cycle-show-empty-lines
  567. org-optimize-window-after-visibility-change)
  568. "Hook that is run after `org-cycle' has changed the buffer visibility.
  569. The function(s) in this hook must accept a single argument which indicates
  570. the new state that was set by the most recent `org-cycle' command. The
  571. argument is a symbol. After a global state change, it can have the values
  572. `overview', `content', or `all'. After a local state change, it can have
  573. the values `folded', `children', or `subtree'."
  574. :group 'org-cycle
  575. :type 'hook)
  576. (defgroup org-edit-structure nil
  577. "Options concerning structure editing in Org-mode."
  578. :tag "Org Edit Structure"
  579. :group 'org-structure)
  580. (defcustom org-odd-levels-only nil
  581. "Non-nil means, skip even levels and only use odd levels for the outline.
  582. This has the effect that two stars are being added/taken away in
  583. promotion/demotion commands. It also influences how levels are
  584. handled by the exporters.
  585. Changing it requires restart of `font-lock-mode' to become effective
  586. for fontification also in regions already fontified.
  587. You may also set this on a per-file basis by adding one of the following
  588. lines to the buffer:
  589. #+STARTUP: odd
  590. #+STARTUP: oddeven"
  591. :group 'org-edit-structure
  592. :group 'org-font-lock
  593. :type 'boolean)
  594. (defcustom org-adapt-indentation t
  595. "Non-nil means, adapt indentation when promoting and demoting.
  596. When this is set and the *entire* text in an entry is indented, the
  597. indentation is increased by one space in a demotion command, and
  598. decreased by one in a promotion command. If any line in the entry
  599. body starts at column 0, indentation is not changed at all."
  600. :group 'org-edit-structure
  601. :type 'boolean)
  602. (defcustom org-special-ctrl-a/e nil
  603. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  604. When t, `C-a' will bring back the cursor to the beginning of the
  605. headline text, i.e. after the stars and after a possible TODO keyword.
  606. In an item, this will be the position after the bullet.
  607. When the cursor is already at that position, another `C-a' will bring
  608. it to the beginning of the line.
  609. `C-e' will jump to the end of the headline, ignoring the presence of tags
  610. in the headline. A second `C-e' will then jump to the true end of the
  611. line, after any tags.
  612. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  613. and only a directly following, identical keypress will bring the cursor
  614. to the special positions.
  615. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  616. set separately."
  617. :group 'org-edit-structure
  618. :type '(choice
  619. (const :tag "off" nil)
  620. (const :tag "after stars/bullet and before tags first" t)
  621. (const :tag "true line boundary first" reversed)
  622. (cons :tag "Set C-a and C-e separately"
  623. (choice :tag "Special C-a"
  624. (const :tag "off" nil)
  625. (const :tag "after stars/bullet first" t)
  626. (const :tag "before stars/bullet first" reversed))
  627. (choice :tag "Special C-e"
  628. (const :tag "off" nil)
  629. (const :tag "before tags first" t)
  630. (const :tag "after tags first" reversed)))))
  631. (if (fboundp 'defvaralias)
  632. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  633. (defcustom org-special-ctrl-k nil
  634. "Non-nil means `C-k' will behave specially in headlines.
  635. When nil, `C-k' will call the default `kill-line' command.
  636. When t, the following will happen while the cursor is in the headline:
  637. - When the cursor is at the beginning of a headline, kill the entire
  638. line and possible the folded subtree below the line.
  639. - When in the middle of the headline text, kill the headline up to the tags.
  640. - When after the headline text, kill the tags."
  641. :group 'org-edit-structure
  642. :type 'boolean)
  643. (defcustom org-yank-folded-subtrees t
  644. "Non-nil means, when yanking subtrees, fold them.
  645. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  646. it starts with a heading and all other headings in it are either children
  647. or siblings, then fold all the subtrees. However, do this only if no
  648. text after the yank would be swallowed into a folded tree by this action."
  649. :group 'org-edit-structure
  650. :type 'boolean)
  651. (defcustom org-yank-adjusted-subtrees nil
  652. "Non-nil means, when yanking subtrees, adjust the level.
  653. With this setting, `org-paste-subtree' is used to insert the subtree, see
  654. this function for details."
  655. :group 'org-edit-structure
  656. :type 'boolean)
  657. (defcustom org-M-RET-may-split-line '((default . t))
  658. "Non-nil means, M-RET will split the line at the cursor position.
  659. When nil, it will go to the end of the line before making a
  660. new line.
  661. You may also set this option in a different way for different
  662. contexts. Valid contexts are:
  663. headline when creating a new headline
  664. item when creating a new item
  665. table in a table field
  666. default the value to be used for all contexts not explicitly
  667. customized"
  668. :group 'org-structure
  669. :group 'org-table
  670. :type '(choice
  671. (const :tag "Always" t)
  672. (const :tag "Never" nil)
  673. (repeat :greedy t :tag "Individual contexts"
  674. (cons
  675. (choice :tag "Context"
  676. (const headline)
  677. (const item)
  678. (const table)
  679. (const default))
  680. (boolean)))))
  681. (defcustom org-insert-heading-respect-content nil
  682. "Non-nil means, insert new headings after the current subtree.
  683. When nil, the new heading is created directly after the current line.
  684. The commands \\[org-insert-heading-respect-content] and
  685. \\[org-insert-todo-heading-respect-content] turn this variable on
  686. for the duration of the command."
  687. :group 'org-structure
  688. :type 'boolean)
  689. (defcustom org-blank-before-new-entry '((heading . auto)
  690. (plain-list-item . auto))
  691. "Should `org-insert-heading' leave a blank line before new heading/item?
  692. The value is an alist, with `heading' and `plain-list-item' as car,
  693. and a boolean flag as cdr. For plain lists, if the variable
  694. `org-empty-line-terminates-plain-lists' is set, the setting here
  695. is ignored and no empty line is inserted, to keep the list in tact."
  696. :group 'org-edit-structure
  697. :type '(list
  698. (cons (const heading)
  699. (choice (const :tag "Never" nil)
  700. (const :tag "Always" t)
  701. (const :tag "Auto" auto)))
  702. (cons (const plain-list-item)
  703. (choice (const :tag "Never" nil)
  704. (const :tag "Always" t)
  705. (const :tag "Auto" auto)))))
  706. (defcustom org-insert-heading-hook nil
  707. "Hook being run after inserting a new heading."
  708. :group 'org-edit-structure
  709. :type 'hook)
  710. (defcustom org-enable-fixed-width-editor t
  711. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  712. This currently only means, they are never auto-wrapped.
  713. When nil, such lines will be treated like ordinary lines.
  714. See also the QUOTE keyword."
  715. :group 'org-edit-structure
  716. :type 'boolean)
  717. (defcustom org-edit-src-region-extra nil
  718. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  719. For examples see the function `org-edit-src-find-region-and-lang'.
  720. The regular expression identifying the begin marker should end with a newline,
  721. and the regexp marking the end line should start with a newline, to make sure
  722. there are kept outside the narrowed region."
  723. :group 'org-edit-structure
  724. :type '(repeat
  725. (list
  726. (regexp :tag "begin regexp")
  727. (regexp :tag "end regexp")
  728. (choice :tag "language"
  729. (string :tag "specify")
  730. (integer :tag "from match group")
  731. (const :tag "from `lang' element")
  732. (const :tag "from `style' element")))))
  733. (defcustom org-coderef-label-format "(ref:%s)"
  734. "The default coderef format.
  735. This format string will be used to search for coderef labels in literal
  736. examples (EXAMPLE and SRC blocks). The format can be overwritten
  737. an individual literal example with the -f option, like
  738. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  739. ...
  740. #+END_SRC
  741. If you want to use this for HTML export, make sure that the format does
  742. not introduce special font-locking, and avoid the HTML special
  743. characters `<', `>', and `&'. The reason for this restriction is that
  744. the labels are searched for only after htmlize has done its job."
  745. :group 'org-edit-structure ; FIXME this is not in the right group
  746. :type 'string)
  747. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  748. "The mode that should be used to edit fixed-width regions.
  749. These are the regions where each line starts with a colon."
  750. :group 'org-edit-structure
  751. :type '(choice
  752. (const artist-mode)
  753. (const picture-mode)
  754. (const fundamental-mode)
  755. (function :tag "Other (specify)")))
  756. (defcustom org-edit-src-persistent-message t
  757. "Non-nil means show persistent exit help message while editing src examples.
  758. The message is shown in the header-line, which will be created in the
  759. first line of the window showing the editing buffer.
  760. When nil, the message will only be shown intermittently in the echo area."
  761. :group 'org-edit-structure
  762. :type 'boolean)
  763. (defcustom org-goto-auto-isearch t
  764. "Non-nil means, typing characters in org-goto starts incremental search."
  765. :group 'org-edit-structure
  766. :type 'boolean)
  767. (defgroup org-sparse-trees nil
  768. "Options concerning sparse trees in Org-mode."
  769. :tag "Org Sparse Trees"
  770. :group 'org-structure)
  771. (defcustom org-highlight-sparse-tree-matches t
  772. "Non-nil means, highlight all matches that define a sparse tree.
  773. The highlights will automatically disappear the next time the buffer is
  774. changed by an edit command."
  775. :group 'org-sparse-trees
  776. :type 'boolean)
  777. (defcustom org-remove-highlights-with-change t
  778. "Non-nil means, any change to the buffer will remove temporary highlights.
  779. Such highlights are created by `org-occur' and `org-clock-display'.
  780. When nil, `C-c C-c needs to be used to get rid of the highlights.
  781. The highlights created by `org-preview-latex-fragment' always need
  782. `C-c C-c' to be removed."
  783. :group 'org-sparse-trees
  784. :group 'org-time
  785. :type 'boolean)
  786. (defcustom org-occur-hook '(org-first-headline-recenter)
  787. "Hook that is run after `org-occur' has constructed a sparse tree.
  788. This can be used to recenter the window to show as much of the structure
  789. as possible."
  790. :group 'org-sparse-trees
  791. :type 'hook)
  792. (defgroup org-imenu-and-speedbar nil
  793. "Options concerning imenu and speedbar in Org-mode."
  794. :tag "Org Imenu and Speedbar"
  795. :group 'org-structure)
  796. (defcustom org-imenu-depth 2
  797. "The maximum level for Imenu access to Org-mode headlines.
  798. This also applied for speedbar access."
  799. :group 'org-imenu-and-speedbar
  800. :type 'integer)
  801. (defgroup org-table nil
  802. "Options concerning tables in Org-mode."
  803. :tag "Org Table"
  804. :group 'org)
  805. (defcustom org-enable-table-editor 'optimized
  806. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  807. When nil, such lines will be treated like ordinary lines.
  808. When equal to the symbol `optimized', the table editor will be optimized to
  809. do the following:
  810. - Automatic overwrite mode in front of whitespace in table fields.
  811. This makes the structure of the table stay in tact as long as the edited
  812. field does not exceed the column width.
  813. - Minimize the number of realigns. Normally, the table is aligned each time
  814. TAB or RET are pressed to move to another field. With optimization this
  815. happens only if changes to a field might have changed the column width.
  816. Optimization requires replacing the functions `self-insert-command',
  817. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  818. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  819. very good at guessing when a re-align will be necessary, but you can always
  820. force one with \\[org-ctrl-c-ctrl-c].
  821. If you would like to use the optimized version in Org-mode, but the
  822. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  823. This variable can be used to turn on and off the table editor during a session,
  824. but in order to toggle optimization, a restart is required.
  825. See also the variable `org-table-auto-blank-field'."
  826. :group 'org-table
  827. :type '(choice
  828. (const :tag "off" nil)
  829. (const :tag "on" t)
  830. (const :tag "on, optimized" optimized)))
  831. (defcustom org-self-insert-cluster-for-undo t
  832. "Non-nil means cluster self-insert commands for undo when possible.
  833. If this is set, then, like in the Emacs command loop, 20 consequtive
  834. characters will be undone together.
  835. This is configurable, because there is some impact on typing performance."
  836. :group 'org-table
  837. :type 'boolean)
  838. (defcustom org-table-tab-recognizes-table.el t
  839. "Non-nil means, TAB will automatically notice a table.el table.
  840. When it sees such a table, it moves point into it and - if necessary -
  841. calls `table-recognize-table'."
  842. :group 'org-table-editing
  843. :type 'boolean)
  844. (defgroup org-link nil
  845. "Options concerning links in Org-mode."
  846. :tag "Org Link"
  847. :group 'org)
  848. (defvar org-link-abbrev-alist-local nil
  849. "Buffer-local version of `org-link-abbrev-alist', which see.
  850. The value of this is taken from the #+LINK lines.")
  851. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  852. (defcustom org-link-abbrev-alist nil
  853. "Alist of link abbreviations.
  854. The car of each element is a string, to be replaced at the start of a link.
  855. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  856. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  857. [[linkkey:tag][description]]
  858. The 'linkkey' must be a word word, starting with a letter, followed
  859. by letters, numbers, '-' or '_'.
  860. If REPLACE is a string, the tag will simply be appended to create the link.
  861. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  862. the placeholder \"%h\" will cause a url-encoded version of the tag to
  863. be inserted at that point (see the function `url-hexify-string').
  864. REPLACE may also be a function that will be called with the tag as the
  865. only argument to create the link, which should be returned as a string.
  866. See the manual for examples."
  867. :group 'org-link
  868. :type '(repeat
  869. (cons
  870. (string :tag "Protocol")
  871. (choice
  872. (string :tag "Format")
  873. (function)))))
  874. (defcustom org-descriptive-links t
  875. "Non-nil means, hide link part and only show description of bracket links.
  876. Bracket links are like [[link][description]]. This variable sets the initial
  877. state in new org-mode buffers. The setting can then be toggled on a
  878. per-buffer basis from the Org->Hyperlinks menu."
  879. :group 'org-link
  880. :type 'boolean)
  881. (defcustom org-link-file-path-type 'adaptive
  882. "How the path name in file links should be stored.
  883. Valid values are:
  884. relative Relative to the current directory, i.e. the directory of the file
  885. into which the link is being inserted.
  886. absolute Absolute path, if possible with ~ for home directory.
  887. noabbrev Absolute path, no abbreviation of home directory.
  888. adaptive Use relative path for files in the current directory and sub-
  889. directories of it. For other files, use an absolute path."
  890. :group 'org-link
  891. :type '(choice
  892. (const relative)
  893. (const absolute)
  894. (const noabbrev)
  895. (const adaptive)))
  896. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  897. "Types of links that should be activated in Org-mode files.
  898. This is a list of symbols, each leading to the activation of a certain link
  899. type. In principle, it does not hurt to turn on most link types - there may
  900. be a small gain when turning off unused link types. The types are:
  901. bracket The recommended [[link][description]] or [[link]] links with hiding.
  902. angular Links in angular brackets that may contain whitespace like
  903. <bbdb:Carsten Dominik>.
  904. plain Plain links in normal text, no whitespace, like http://google.com.
  905. radio Text that is matched by a radio target, see manual for details.
  906. tag Tag settings in a headline (link to tag search).
  907. date Time stamps (link to calendar).
  908. footnote Footnote labels.
  909. Changing this variable requires a restart of Emacs to become effective."
  910. :group 'org-link
  911. :type '(set :greedy t
  912. (const :tag "Double bracket links (new style)" bracket)
  913. (const :tag "Angular bracket links (old style)" angular)
  914. (const :tag "Plain text links" plain)
  915. (const :tag "Radio target matches" radio)
  916. (const :tag "Tags" tag)
  917. (const :tag "Timestamps" date)
  918. (const :tag "Footnotes" footnote)))
  919. (defcustom org-make-link-description-function nil
  920. "Function to use to generate link descriptions from links. If
  921. nil the link location will be used. This function must take two
  922. parameters; the first is the link and the second the description
  923. org-insert-link has generated, and should return the description
  924. to use."
  925. :group 'org-link
  926. :type 'function)
  927. (defgroup org-link-store nil
  928. "Options concerning storing links in Org-mode."
  929. :tag "Org Store Link"
  930. :group 'org-link)
  931. (defcustom org-email-link-description-format "Email %c: %.30s"
  932. "Format of the description part of a link to an email or usenet message.
  933. The following %-escapes will be replaced by corresponding information:
  934. %F full \"From\" field
  935. %f name, taken from \"From\" field, address if no name
  936. %T full \"To\" field
  937. %t first name in \"To\" field, address if no name
  938. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  939. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  940. %s subject
  941. %m message-id.
  942. You may use normal field width specification between the % and the letter.
  943. This is for example useful to limit the length of the subject.
  944. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  945. :group 'org-link-store
  946. :type 'string)
  947. (defcustom org-from-is-user-regexp
  948. (let (r1 r2)
  949. (when (and user-mail-address (not (string= user-mail-address "")))
  950. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  951. (when (and user-full-name (not (string= user-full-name "")))
  952. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  953. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  954. "Regexp matched against the \"From:\" header of an email or usenet message.
  955. It should match if the message is from the user him/herself."
  956. :group 'org-link-store
  957. :type 'regexp)
  958. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  959. "Non-nil means, storing a link to an Org file will use entry IDs.
  960. Note that before this variable is even considered, org-id must be loaded,
  961. to please customize `org-modules' and turn it on.
  962. The variable can have the following values:
  963. t Create an ID if needed to make a link to the current entry.
  964. create-if-interactive
  965. If `org-store-link' is called directly (interactively, as a user
  966. command), do create an ID to support the link. But when doing the
  967. job for remember, only use the ID if it already exists. The
  968. purpose of this setting is to avoid proliferation of unwanted
  969. IDs, just because you happen to be in an Org file when you
  970. call `org-remember' that automatically and preemptively
  971. creates a link. If you do want to get an ID link in a remember
  972. template to an entry not having an ID, create it first by
  973. explicitly creating a link to it, using `C-c C-l' first.
  974. use-existing
  975. Use existing ID, do not create one.
  976. nil Never use an ID to make a link, instead link using a text search for
  977. the headline text."
  978. :group 'org-link-store
  979. :type '(choice
  980. (const :tag "Create ID to make link" t)
  981. (const :tag "Create if storing link interactively"
  982. create-if-interactive)
  983. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  984. create-if-interactive-and-no-custom-id)
  985. (const :tag "Only use existing" use-existing)
  986. (const :tag "Do not use ID to create link" nil)))
  987. (defcustom org-context-in-file-links t
  988. "Non-nil means, file links from `org-store-link' contain context.
  989. A search string will be added to the file name with :: as separator and
  990. used to find the context when the link is activated by the command
  991. `org-open-at-point'.
  992. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  993. negates this setting for the duration of the command."
  994. :group 'org-link-store
  995. :type 'boolean)
  996. (defcustom org-keep-stored-link-after-insertion nil
  997. "Non-nil means, keep link in list for entire session.
  998. The command `org-store-link' adds a link pointing to the current
  999. location to an internal list. These links accumulate during a session.
  1000. The command `org-insert-link' can be used to insert links into any
  1001. Org-mode file (offering completion for all stored links). When this
  1002. option is nil, every link which has been inserted once using \\[org-insert-link]
  1003. will be removed from the list, to make completing the unused links
  1004. more efficient."
  1005. :group 'org-link-store
  1006. :type 'boolean)
  1007. (defgroup org-link-follow nil
  1008. "Options concerning following links in Org-mode."
  1009. :tag "Org Follow Link"
  1010. :group 'org-link)
  1011. (defcustom org-link-translation-function nil
  1012. "Function to translate links with different syntax to Org syntax.
  1013. This can be used to translate links created for example by the Planner
  1014. or emacs-wiki packages to Org syntax.
  1015. The function must accept two parameters, a TYPE containing the link
  1016. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1017. which is everything after the link protocol. It should return a cons
  1018. with possibly modified values of type and path.
  1019. Org contains a function for this, so if you set this variable to
  1020. `org-translate-link-from-planner', you should be able follow many
  1021. links created by planner."
  1022. :group 'org-link-follow
  1023. :type 'function)
  1024. (defcustom org-follow-link-hook nil
  1025. "Hook that is run after a link has been followed."
  1026. :group 'org-link-follow
  1027. :type 'hook)
  1028. (defcustom org-tab-follows-link nil
  1029. "Non-nil means, on links TAB will follow the link.
  1030. Needs to be set before org.el is loaded.
  1031. This really should not be used, it does not make sense, and the
  1032. implementation is bad."
  1033. :group 'org-link-follow
  1034. :type 'boolean)
  1035. (defcustom org-return-follows-link nil
  1036. "Non-nil means, on links RET will follow the link.
  1037. Needs to be set before org.el is loaded."
  1038. :group 'org-link-follow
  1039. :type 'boolean)
  1040. (defcustom org-mouse-1-follows-link
  1041. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1042. "Non-nil means, mouse-1 on a link will follow the link.
  1043. A longer mouse click will still set point. Does not work on XEmacs.
  1044. Needs to be set before org.el is loaded."
  1045. :group 'org-link-follow
  1046. :type 'boolean)
  1047. (defcustom org-mark-ring-length 4
  1048. "Number of different positions to be recorded in the ring
  1049. Changing this requires a restart of Emacs to work correctly."
  1050. :group 'org-link-follow
  1051. :type 'integer)
  1052. (defcustom org-link-frame-setup
  1053. '((vm . vm-visit-folder-other-frame)
  1054. (gnus . gnus-other-frame)
  1055. (file . find-file-other-window))
  1056. "Setup the frame configuration for following links.
  1057. When following a link with Emacs, it may often be useful to display
  1058. this link in another window or frame. This variable can be used to
  1059. set this up for the different types of links.
  1060. For VM, use any of
  1061. `vm-visit-folder'
  1062. `vm-visit-folder-other-frame'
  1063. For Gnus, use any of
  1064. `gnus'
  1065. `gnus-other-frame'
  1066. `org-gnus-no-new-news'
  1067. For FILE, use any of
  1068. `find-file'
  1069. `find-file-other-window'
  1070. `find-file-other-frame'
  1071. For the calendar, use the variable `calendar-setup'.
  1072. For BBDB, it is currently only possible to display the matches in
  1073. another window."
  1074. :group 'org-link-follow
  1075. :type '(list
  1076. (cons (const vm)
  1077. (choice
  1078. (const vm-visit-folder)
  1079. (const vm-visit-folder-other-window)
  1080. (const vm-visit-folder-other-frame)))
  1081. (cons (const gnus)
  1082. (choice
  1083. (const gnus)
  1084. (const gnus-other-frame)
  1085. (const org-gnus-no-new-news)))
  1086. (cons (const file)
  1087. (choice
  1088. (const find-file)
  1089. (const find-file-other-window)
  1090. (const find-file-other-frame)))))
  1091. (defcustom org-display-internal-link-with-indirect-buffer nil
  1092. "Non-nil means, use indirect buffer to display infile links.
  1093. Activating internal links (from one location in a file to another location
  1094. in the same file) normally just jumps to the location. When the link is
  1095. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1096. another window. When this option is set, the other window actually displays
  1097. an indirect buffer clone of the current buffer, to avoid any visibility
  1098. changes to the current buffer."
  1099. :group 'org-link-follow
  1100. :type 'boolean)
  1101. (defcustom org-open-non-existing-files nil
  1102. "Non-nil means, `org-open-file' will open non-existing files.
  1103. When nil, an error will be generated."
  1104. :group 'org-link-follow
  1105. :type 'boolean)
  1106. (defcustom org-open-directory-means-index-dot-org nil
  1107. "Non-nil means, a link to a directory really means to index.org.
  1108. When nil, following a directory link will run dired or open a finder/explorer
  1109. window on that directory."
  1110. :group 'org-link-follow
  1111. :type 'boolean)
  1112. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1113. "Function and arguments to call for following mailto links.
  1114. This is a list with the first element being a lisp function, and the
  1115. remaining elements being arguments to the function. In string arguments,
  1116. %a will be replaced by the address, and %s will be replaced by the subject
  1117. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1118. :group 'org-link-follow
  1119. :type '(choice
  1120. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1121. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1122. (const :tag "message-mail" (message-mail "%a" "%s"))
  1123. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1124. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1125. "Non-nil means, ask for confirmation before executing shell links.
  1126. Shell links can be dangerous: just think about a link
  1127. [[shell:rm -rf ~/*][Google Search]]
  1128. This link would show up in your Org-mode document as \"Google Search\",
  1129. but really it would remove your entire home directory.
  1130. Therefore we advise against setting this variable to nil.
  1131. Just change it to `y-or-n-p' if you want to confirm with a
  1132. single keystroke rather than having to type \"yes\"."
  1133. :group 'org-link-follow
  1134. :type '(choice
  1135. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1136. (const :tag "with y-or-n (faster)" y-or-n-p)
  1137. (const :tag "no confirmation (dangerous)" nil)))
  1138. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1139. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1140. Elisp links can be dangerous: just think about a link
  1141. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1142. This link would show up in your Org-mode document as \"Google Search\",
  1143. but really it would remove your entire home directory.
  1144. Therefore we advise against setting this variable to nil.
  1145. Just change it to `y-or-n-p' if you want to confirm with a
  1146. single keystroke rather than having to type \"yes\"."
  1147. :group 'org-link-follow
  1148. :type '(choice
  1149. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1150. (const :tag "with y-or-n (faster)" y-or-n-p)
  1151. (const :tag "no confirmation (dangerous)" nil)))
  1152. (defconst org-file-apps-defaults-gnu
  1153. '((remote . emacs)
  1154. (system . mailcap)
  1155. (t . mailcap))
  1156. "Default file applications on a UNIX or GNU/Linux system.
  1157. See `org-file-apps'.")
  1158. (defconst org-file-apps-defaults-macosx
  1159. '((remote . emacs)
  1160. (t . "open %s")
  1161. (system . "open %s")
  1162. ("ps.gz" . "gv %s")
  1163. ("eps.gz" . "gv %s")
  1164. ("dvi" . "xdvi %s")
  1165. ("fig" . "xfig %s"))
  1166. "Default file applications on a MacOS X system.
  1167. The system \"open\" is known as a default, but we use X11 applications
  1168. for some files for which the OS does not have a good default.
  1169. See `org-file-apps'.")
  1170. (defconst org-file-apps-defaults-windowsnt
  1171. (list
  1172. '(remote . emacs)
  1173. (cons t
  1174. (list (if (featurep 'xemacs)
  1175. 'mswindows-shell-execute
  1176. 'w32-shell-execute)
  1177. "open" 'file))
  1178. (cons 'system
  1179. (list (if (featurep 'xemacs)
  1180. 'mswindows-shell-execute
  1181. 'w32-shell-execute)
  1182. "open" 'file)))
  1183. "Default file applications on a Windows NT system.
  1184. The system \"open\" is used for most files.
  1185. See `org-file-apps'.")
  1186. (defcustom org-file-apps
  1187. '(
  1188. (auto-mode . emacs)
  1189. ("\\.x?html?\\'" . default)
  1190. ("\\.pdf\\'" . default)
  1191. )
  1192. "External applications for opening `file:path' items in a document.
  1193. Org-mode uses system defaults for different file types, but
  1194. you can use this variable to set the application for a given file
  1195. extension. The entries in this list are cons cells where the car identifies
  1196. files and the cdr the corresponding command. Possible values for the
  1197. file identifier are
  1198. \"regex\" Regular expression matched against the file name. For backward
  1199. compatibility, this can also be a string with only alphanumeric
  1200. characters, which is then interpreted as an extension.
  1201. `directory' Matches a directory
  1202. `remote' Matches a remote file, accessible through tramp or efs.
  1203. Remote files most likely should be visited through Emacs
  1204. because external applications cannot handle such paths.
  1205. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1206. so all files Emacs knows how to handle. Using this with
  1207. command `emacs' will open most files in Emacs. Beware that this
  1208. will also open html files inside Emacs, unless you add
  1209. (\"html\" . default) to the list as well.
  1210. t Default for files not matched by any of the other options.
  1211. `system' The system command to open files, like `open' on Windows
  1212. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1213. that will be selected if you call `C-c C-o' with a double
  1214. `C-u C-u' prefix.
  1215. Possible values for the command are:
  1216. `emacs' The file will be visited by the current Emacs process.
  1217. `default' Use the default application for this file type, which is the
  1218. association for t in the list, most likely in the system-specific
  1219. part.
  1220. This can be used to overrule an unwanted setting in the
  1221. system-specific variable.
  1222. `system' Use the system command for opening files, like \"open\".
  1223. This command is specified by the entry whose car is `system'.
  1224. Most likely, the system-specific version of this variable
  1225. does define this command, but you can overrule/replace it
  1226. here.
  1227. string A command to be executed by a shell; %s will be replaced
  1228. by the path to the file.
  1229. sexp A Lisp form which will be evaluated. The file path will
  1230. be available in the Lisp variable `file'.
  1231. For more examples, see the system specific constants
  1232. `org-file-apps-defaults-macosx'
  1233. `org-file-apps-defaults-windowsnt'
  1234. `org-file-apps-defaults-gnu'."
  1235. :group 'org-link-follow
  1236. :type '(repeat
  1237. (cons (choice :value ""
  1238. (string :tag "Extension")
  1239. (const :tag "System command to open files" system)
  1240. (const :tag "Default for unrecognized files" t)
  1241. (const :tag "Remote file" remote)
  1242. (const :tag "Links to a directory" directory)
  1243. (const :tag "Any files that have Emacs modes"
  1244. auto-mode))
  1245. (choice :value ""
  1246. (const :tag "Visit with Emacs" emacs)
  1247. (const :tag "Use default" default)
  1248. (const :tag "Use the system command" system)
  1249. (string :tag "Command")
  1250. (sexp :tag "Lisp form")))))
  1251. (defgroup org-refile nil
  1252. "Options concerning refiling entries in Org-mode."
  1253. :tag "Org Refile"
  1254. :group 'org)
  1255. (defcustom org-directory "~/org"
  1256. "Directory with org files.
  1257. This is just a default location to look for Org files. There is no need
  1258. at all to put your files into this directory. It is only used in the
  1259. following situations:
  1260. 1. When a remember template specifies a target file that is not an
  1261. absolute path. The path will then be interpreted relative to
  1262. `org-directory'
  1263. 2. When a remember note is filed away in an interactive way (when exiting the
  1264. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1265. with `org-directory' as the default path."
  1266. :group 'org-refile
  1267. :group 'org-remember
  1268. :type 'directory)
  1269. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1270. "Default target for storing notes.
  1271. Used by the hooks for remember.el. This can be a string, or nil to mean
  1272. the value of `remember-data-file'.
  1273. You can set this on a per-template basis with the variable
  1274. `org-remember-templates'."
  1275. :group 'org-refile
  1276. :group 'org-remember
  1277. :type '(choice
  1278. (const :tag "Default from remember-data-file" nil)
  1279. file))
  1280. (defcustom org-goto-interface 'outline
  1281. "The default interface to be used for `org-goto'.
  1282. Allowed values are:
  1283. outline The interface shows an outline of the relevant file
  1284. and the correct heading is found by moving through
  1285. the outline or by searching with incremental search.
  1286. outline-path-completion Headlines in the current buffer are offered via
  1287. completion. This is the interface also used by
  1288. the refile command."
  1289. :group 'org-refile
  1290. :type '(choice
  1291. (const :tag "Outline" outline)
  1292. (const :tag "Outline-path-completion" outline-path-completion)))
  1293. (defcustom org-goto-max-level 5
  1294. "Maximum level to be considered when running org-goto with refile interface."
  1295. :group 'org-refile
  1296. :type 'integer)
  1297. (defcustom org-reverse-note-order nil
  1298. "Non-nil means, store new notes at the beginning of a file or entry.
  1299. When nil, new notes will be filed to the end of a file or entry.
  1300. This can also be a list with cons cells of regular expressions that
  1301. are matched against file names, and values."
  1302. :group 'org-remember
  1303. :group 'org-refile
  1304. :type '(choice
  1305. (const :tag "Reverse always" t)
  1306. (const :tag "Reverse never" nil)
  1307. (repeat :tag "By file name regexp"
  1308. (cons regexp boolean))))
  1309. (defcustom org-refile-targets nil
  1310. "Targets for refiling entries with \\[org-refile].
  1311. This is list of cons cells. Each cell contains:
  1312. - a specification of the files to be considered, either a list of files,
  1313. or a symbol whose function or variable value will be used to retrieve
  1314. a file name or a list of file names. If you use `org-agenda-files' for
  1315. that, all agenda files will be scanned for targets. Nil means, consider
  1316. headings in the current buffer.
  1317. - A specification of how to find candidate refile targets. This may be
  1318. any of:
  1319. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1320. This tag has to be present in all target headlines, inheritance will
  1321. not be considered.
  1322. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1323. todo keyword.
  1324. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1325. headlines that are refiling targets.
  1326. - a cons cell (:level . N). Any headline of level N is considered a target.
  1327. Note that, when `org-odd-levels-only' is set, level corresponds to
  1328. order in hierarchy, not to the number of stars.
  1329. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1330. Note that, when `org-odd-levels-only' is set, level corresponds to
  1331. order in hierarchy, not to the number of stars.
  1332. You can set the variable `org-refile-target-verify-function' to a function
  1333. to verify each headline found by the simple critery above.
  1334. When this variable is nil, all top-level headlines in the current buffer
  1335. are used, equivalent to the value `((nil . (:level . 1))'."
  1336. :group 'org-refile
  1337. :type '(repeat
  1338. (cons
  1339. (choice :value org-agenda-files
  1340. (const :tag "All agenda files" org-agenda-files)
  1341. (const :tag "Current buffer" nil)
  1342. (function) (variable) (file))
  1343. (choice :tag "Identify target headline by"
  1344. (cons :tag "Specific tag" (const :value :tag) (string))
  1345. (cons :tag "TODO keyword" (const :value :todo) (string))
  1346. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1347. (cons :tag "Level number" (const :value :level) (integer))
  1348. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1349. (defcustom org-refile-target-verify-function nil
  1350. "Function to verify if the headline at point should be a refile target.
  1351. The function will be called without arguments, with point at the
  1352. beginning of the headline. It should return t and leave point
  1353. where it is if the headline is a valid target for refiling.
  1354. If the target should not be selected, the function must return nil.
  1355. In addition to this, it may move point to a place from where the search
  1356. should be continued. For example, the function may decide that the entire
  1357. subtree of the current entry should be excluded and move point to the end
  1358. of the subtree."
  1359. :group 'org-refile
  1360. :type 'function)
  1361. (defcustom org-refile-use-outline-path nil
  1362. "Non-nil means, provide refile targets as paths.
  1363. So a level 3 headline will be available as level1/level2/level3.
  1364. When the value is `file', also include the file name (without directory)
  1365. into the path. In this case, you can also stop the completion after
  1366. the file name, to get entries inserted as top level in the file.
  1367. When `full-file-path', include the full file path."
  1368. :group 'org-refile
  1369. :type '(choice
  1370. (const :tag "Not" nil)
  1371. (const :tag "Yes" t)
  1372. (const :tag "Start with file name" file)
  1373. (const :tag "Start with full file path" full-file-path)))
  1374. (defcustom org-outline-path-complete-in-steps t
  1375. "Non-nil means, complete the outline path in hierarchical steps.
  1376. When Org-mode uses the refile interface to select an outline path
  1377. \(see variable `org-refile-use-outline-path'), the completion of
  1378. the path can be done is a single go, or if can be done in steps down
  1379. the headline hierarchy. Going in steps is probably the best if you
  1380. do not use a special completion package like `ido' or `icicles'.
  1381. However, when using these packages, going in one step can be very
  1382. fast, while still showing the whole path to the entry."
  1383. :group 'org-refile
  1384. :type 'boolean)
  1385. (defcustom org-refile-allow-creating-parent-nodes nil
  1386. "Non-nil means, allow to create new nodes as refile targets.
  1387. New nodes are then created by adding \"/new node name\" to the completion
  1388. of an existing node. When the value of this variable is `confirm',
  1389. new node creation must be confirmed by the user (recommended)
  1390. When nil, the completion must match an existing entry.
  1391. Note that, if the new heading is not seen by the criteria
  1392. listed in `org-refile-targets', multiple instances of the same
  1393. heading would be created by trying again to file under the new
  1394. heading."
  1395. :group 'org-refile
  1396. :type '(choice
  1397. (const :tag "Never" nil)
  1398. (const :tag "Always" t)
  1399. (const :tag "Prompt for confirmation" confirm)))
  1400. (defgroup org-todo nil
  1401. "Options concerning TODO items in Org-mode."
  1402. :tag "Org TODO"
  1403. :group 'org)
  1404. (defgroup org-progress nil
  1405. "Options concerning Progress logging in Org-mode."
  1406. :tag "Org Progress"
  1407. :group 'org-time)
  1408. (defvar org-todo-interpretation-widgets
  1409. '(
  1410. (:tag "Sequence (cycling hits every state)" sequence)
  1411. (:tag "Type (cycling directly to DONE)" type))
  1412. "The available interpretation symbols for customizing
  1413. `org-todo-keywords'.
  1414. Interested libraries should add to this list.")
  1415. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1416. "List of TODO entry keyword sequences and their interpretation.
  1417. \\<org-mode-map>This is a list of sequences.
  1418. Each sequence starts with a symbol, either `sequence' or `type',
  1419. indicating if the keywords should be interpreted as a sequence of
  1420. action steps, or as different types of TODO items. The first
  1421. keywords are states requiring action - these states will select a headline
  1422. for inclusion into the global TODO list Org-mode produces. If one of
  1423. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1424. signify that no further action is necessary. If \"|\" is not found,
  1425. the last keyword is treated as the only DONE state of the sequence.
  1426. The command \\[org-todo] cycles an entry through these states, and one
  1427. additional state where no keyword is present. For details about this
  1428. cycling, see the manual.
  1429. TODO keywords and interpretation can also be set on a per-file basis with
  1430. the special #+SEQ_TODO and #+TYP_TODO lines.
  1431. Each keyword can optionally specify a character for fast state selection
  1432. \(in combination with the variable `org-use-fast-todo-selection')
  1433. and specifiers for state change logging, using the same syntax
  1434. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1435. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1436. indicates to record a time stamp each time this state is selected.
  1437. Each keyword may also specify if a timestamp or a note should be
  1438. recorded when entering or leaving the state, by adding additional
  1439. characters in the parenthesis after the keyword. This looks like this:
  1440. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1441. record only the time of the state change. With X and Y being either
  1442. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1443. Y when leaving the state if and only if the *target* state does not
  1444. define X. You may omit any of the fast-selection key or X or /Y,
  1445. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1446. For backward compatibility, this variable may also be just a list
  1447. of keywords - in this case the interpretation (sequence or type) will be
  1448. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1449. :group 'org-todo
  1450. :group 'org-keywords
  1451. :type '(choice
  1452. (repeat :tag "Old syntax, just keywords"
  1453. (string :tag "Keyword"))
  1454. (repeat :tag "New syntax"
  1455. (cons
  1456. (choice
  1457. :tag "Interpretation"
  1458. ;;Quick and dirty way to see
  1459. ;;`org-todo-interpretations'. This takes the
  1460. ;;place of item arguments
  1461. :convert-widget
  1462. (lambda (widget)
  1463. (widget-put widget
  1464. :args (mapcar
  1465. #'(lambda (x)
  1466. (widget-convert
  1467. (cons 'const x)))
  1468. org-todo-interpretation-widgets))
  1469. widget))
  1470. (repeat
  1471. (string :tag "Keyword"))))))
  1472. (defvar org-todo-keywords-1 nil
  1473. "All TODO and DONE keywords active in a buffer.")
  1474. (make-variable-buffer-local 'org-todo-keywords-1)
  1475. (defvar org-todo-keywords-for-agenda nil)
  1476. (defvar org-done-keywords-for-agenda nil)
  1477. (defvar org-todo-keyword-alist-for-agenda nil)
  1478. (defvar org-tag-alist-for-agenda nil)
  1479. (defvar org-agenda-contributing-files nil)
  1480. (defvar org-not-done-keywords nil)
  1481. (make-variable-buffer-local 'org-not-done-keywords)
  1482. (defvar org-done-keywords nil)
  1483. (make-variable-buffer-local 'org-done-keywords)
  1484. (defvar org-todo-heads nil)
  1485. (make-variable-buffer-local 'org-todo-heads)
  1486. (defvar org-todo-sets nil)
  1487. (make-variable-buffer-local 'org-todo-sets)
  1488. (defvar org-todo-log-states nil)
  1489. (make-variable-buffer-local 'org-todo-log-states)
  1490. (defvar org-todo-kwd-alist nil)
  1491. (make-variable-buffer-local 'org-todo-kwd-alist)
  1492. (defvar org-todo-key-alist nil)
  1493. (make-variable-buffer-local 'org-todo-key-alist)
  1494. (defvar org-todo-key-trigger nil)
  1495. (make-variable-buffer-local 'org-todo-key-trigger)
  1496. (defcustom org-todo-interpretation 'sequence
  1497. "Controls how TODO keywords are interpreted.
  1498. This variable is in principle obsolete and is only used for
  1499. backward compatibility, if the interpretation of todo keywords is
  1500. not given already in `org-todo-keywords'. See that variable for
  1501. more information."
  1502. :group 'org-todo
  1503. :group 'org-keywords
  1504. :type '(choice (const sequence)
  1505. (const type)))
  1506. (defcustom org-use-fast-todo-selection t
  1507. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1508. This variable describes if and under what circumstances the cycling
  1509. mechanism for TODO keywords will be replaced by a single-key, direct
  1510. selection scheme.
  1511. When nil, fast selection is never used.
  1512. When the symbol `prefix', it will be used when `org-todo' is called with
  1513. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1514. in an agenda buffer.
  1515. When t, fast selection is used by default. In this case, the prefix
  1516. argument forces cycling instead.
  1517. In all cases, the special interface is only used if access keys have actually
  1518. been assigned by the user, i.e. if keywords in the configuration are followed
  1519. by a letter in parenthesis, like TODO(t)."
  1520. :group 'org-todo
  1521. :type '(choice
  1522. (const :tag "Never" nil)
  1523. (const :tag "By default" t)
  1524. (const :tag "Only with C-u C-c C-t" prefix)))
  1525. (defcustom org-provide-todo-statistics t
  1526. "Non-nil means, update todo statistics after insert and toggle.
  1527. When this is set, todo statistics is updated in the parent of the current
  1528. entry each time a todo state is changed."
  1529. :group 'org-todo
  1530. :type 'boolean)
  1531. (defcustom org-hierarchical-todo-statistics t
  1532. "Non-nil means, TODO statistics covers just direct children.
  1533. When nil, all entries in the subtree are considered.
  1534. This has only an effect if `org-provide-todo-statistics' is set."
  1535. :group 'org-todo
  1536. :type 'boolean)
  1537. (defcustom org-after-todo-state-change-hook nil
  1538. "Hook which is run after the state of a TODO item was changed.
  1539. The new state (a string with a TODO keyword, or nil) is available in the
  1540. Lisp variable `state'."
  1541. :group 'org-todo
  1542. :type 'hook)
  1543. (defvar org-blocker-hook nil
  1544. "Hook for functions that are allowed to block a state change.
  1545. Each function gets as its single argument a property list, see
  1546. `org-trigger-hook' for more information about this list.
  1547. If any of the functions in this hook returns nil, the state change
  1548. is blocked.")
  1549. (defvar org-trigger-hook nil
  1550. "Hook for functions that are triggered by a state change.
  1551. Each function gets as its single argument a property list with at least
  1552. the following elements:
  1553. (:type type-of-change :position pos-at-entry-start
  1554. :from old-state :to new-state)
  1555. Depending on the type, more properties may be present.
  1556. This mechanism is currently implemented for:
  1557. TODO state changes
  1558. ------------------
  1559. :type todo-state-change
  1560. :from previous state (keyword as a string), or nil, or a symbol
  1561. 'todo' or 'done', to indicate the general type of state.
  1562. :to new state, like in :from")
  1563. (defcustom org-enforce-todo-dependencies nil
  1564. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1565. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1566. be blocked if any prior sibling is not yet done.
  1567. Finally, if the parent is blocked because of ordered siblings of its own,
  1568. the child will also be blocked.
  1569. This variable needs to be set before org.el is loaded, and you need to
  1570. restart Emacs after a change to make the change effective. The only way
  1571. to change is while Emacs is running is through the customize interface."
  1572. :set (lambda (var val)
  1573. (set var val)
  1574. (if val
  1575. (add-hook 'org-blocker-hook
  1576. 'org-block-todo-from-children-or-siblings-or-parent)
  1577. (remove-hook 'org-blocker-hook
  1578. 'org-block-todo-from-children-or-siblings-or-parent)))
  1579. :group 'org-todo
  1580. :type 'boolean)
  1581. (defcustom org-enforce-todo-checkbox-dependencies nil
  1582. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1583. When this is nil, checkboxes have no influence on switching TODO states.
  1584. When non-nil, you first need to check off all check boxes before the TODO
  1585. entry can be switched to DONE.
  1586. This variable needs to be set before org.el is loaded, and you need to
  1587. restart Emacs after a change to make the change effective. The only way
  1588. to change is while Emacs is running is through the customize interface."
  1589. :set (lambda (var val)
  1590. (set var val)
  1591. (if val
  1592. (add-hook 'org-blocker-hook
  1593. 'org-block-todo-from-checkboxes)
  1594. (remove-hook 'org-blocker-hook
  1595. 'org-block-todo-from-checkboxes)))
  1596. :group 'org-todo
  1597. :type 'boolean)
  1598. (defcustom org-todo-state-tags-triggers nil
  1599. "Tag changes that should be triggered by TODO state changes.
  1600. This is a list. Each entry is
  1601. (state-change (tag . flag) .......)
  1602. State-change can be a string with a state, and empty string to indicate the
  1603. state that has no TODO keyword, or it can be one of the symbols `todo'
  1604. or `done', meaning any not-done or done state, respectively."
  1605. :group 'org-todo
  1606. :group 'org-tags
  1607. :type '(repeat
  1608. (cons (choice :tag "When changing to"
  1609. (const :tag "Not-done state" todo)
  1610. (const :tag "Done state" done)
  1611. (string :tag "State"))
  1612. (repeat
  1613. (cons :tag "Tag action"
  1614. (string :tag "Tag")
  1615. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1616. (defcustom org-log-done nil
  1617. "Information to record when a task moves to the DONE state.
  1618. Possible values are:
  1619. nil Don't add anything, just change the keyword
  1620. time Add a time stamp to the task
  1621. note Prompt a closing note and add it with template `org-log-note-headings'
  1622. This option can also be set with on a per-file-basis with
  1623. #+STARTUP: nologdone
  1624. #+STARTUP: logdone
  1625. #+STARTUP: lognotedone
  1626. You can have local logging settings for a subtree by setting the LOGGING
  1627. property to one or more of these keywords."
  1628. :group 'org-todo
  1629. :group 'org-progress
  1630. :type '(choice
  1631. (const :tag "No logging" nil)
  1632. (const :tag "Record CLOSED timestamp" time)
  1633. (const :tag "Record CLOSED timestamp with closing note." note)))
  1634. ;; Normalize old uses of org-log-done.
  1635. (cond
  1636. ((eq org-log-done t) (setq org-log-done 'time))
  1637. ((and (listp org-log-done) (memq 'done org-log-done))
  1638. (setq org-log-done 'note)))
  1639. (defcustom org-log-note-clock-out nil
  1640. "Non-nil means, record a note when clocking out of an item.
  1641. This can also be configured on a per-file basis by adding one of
  1642. the following lines anywhere in the buffer:
  1643. #+STARTUP: lognoteclock-out
  1644. #+STARTUP: nolognoteclock-out"
  1645. :group 'org-todo
  1646. :group 'org-progress
  1647. :type 'boolean)
  1648. (defcustom org-log-done-with-time t
  1649. "Non-nil means, the CLOSED time stamp will contain date and time.
  1650. When nil, only the date will be recorded."
  1651. :group 'org-progress
  1652. :type 'boolean)
  1653. (defcustom org-log-note-headings
  1654. '((done . "CLOSING NOTE %t")
  1655. (state . "State %-12s from %-12S %t")
  1656. (note . "Note taken on %t")
  1657. (clock-out . ""))
  1658. "Headings for notes added to entries.
  1659. The value is an alist, with the car being a symbol indicating the note
  1660. context, and the cdr is the heading to be used. The heading may also be the
  1661. empty string.
  1662. %t in the heading will be replaced by a time stamp.
  1663. %s will be replaced by the new TODO state, in double quotes.
  1664. %S will be replaced by the old TODO state, in double quotes.
  1665. %u will be replaced by the user name.
  1666. %U will be replaced by the full user name."
  1667. :group 'org-todo
  1668. :group 'org-progress
  1669. :type '(list :greedy t
  1670. (cons (const :tag "Heading when closing an item" done) string)
  1671. (cons (const :tag
  1672. "Heading when changing todo state (todo sequence only)"
  1673. state) string)
  1674. (cons (const :tag "Heading when just taking a note" note) string)
  1675. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1676. (unless (assq 'note org-log-note-headings)
  1677. (push '(note . "%t") org-log-note-headings))
  1678. (defcustom org-log-into-drawer nil
  1679. "Non-nil means, insert state change notes and time stamps into a drawer.
  1680. When nil, state changes notes will be inserted after the headline and
  1681. any scheduling and clock lines, but not inside a drawer.
  1682. The value of this variable should be the name of the drawer to use.
  1683. LOGBOOK is proposed at the default drawer for this purpose, you can
  1684. also set this to a string to define the drawer of your choice.
  1685. A value of t is also allowed, representing \"LOGBOOK\".
  1686. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1687. will be ignored.
  1688. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1689. a subtree."
  1690. :group 'org-todo
  1691. :group 'org-progress
  1692. :type '(choice
  1693. (const :tag "Not into a drawer" nil)
  1694. (const :tag "LOGBOOK" t)
  1695. (string :tag "Other")))
  1696. (if (fboundp 'defvaralias)
  1697. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1698. (defun org-log-into-drawer ()
  1699. "Return the value of `org-log-into-drawer', but let properties overrule.
  1700. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1701. used instead of the default value."
  1702. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1703. (cond
  1704. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1705. ((equal p "t") "LOGBOOK")
  1706. (t p))))
  1707. (defcustom org-log-state-notes-insert-after-drawers nil
  1708. "Non-nil means, insert state change notes after any drawers in entry.
  1709. Only the drawers that *immediately* follow the headline and the
  1710. deadline/scheduled line are skipped.
  1711. When nil, insert notes right after the heading and perhaps the line
  1712. with deadline/scheduling if present.
  1713. This variable will have no effect if `org-log-into-drawer' is
  1714. set."
  1715. :group 'org-todo
  1716. :group 'org-progress
  1717. :type 'boolean)
  1718. (defcustom org-log-states-order-reversed t
  1719. "Non-nil means, the latest state change note will be directly after heading.
  1720. When nil, the notes will be orderer according to time."
  1721. :group 'org-todo
  1722. :group 'org-progress
  1723. :type 'boolean)
  1724. (defcustom org-log-repeat 'time
  1725. "Non-nil means, record moving through the DONE state when triggering repeat.
  1726. An auto-repeating tasks is immediately switched back to TODO when marked
  1727. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1728. the TODO keyword definition, or recording a closing note by setting
  1729. `org-log-done', there will be no record of the task moving through DONE.
  1730. This variable forces taking a note anyway. Possible values are:
  1731. nil Don't force a record
  1732. time Record a time stamp
  1733. note Record a note
  1734. This option can also be set with on a per-file-basis with
  1735. #+STARTUP: logrepeat
  1736. #+STARTUP: lognoterepeat
  1737. #+STARTUP: nologrepeat
  1738. You can have local logging settings for a subtree by setting the LOGGING
  1739. property to one or more of these keywords."
  1740. :group 'org-todo
  1741. :group 'org-progress
  1742. :type '(choice
  1743. (const :tag "Don't force a record" nil)
  1744. (const :tag "Force recording the DONE state" time)
  1745. (const :tag "Force recording a note with the DONE state" note)))
  1746. (defgroup org-priorities nil
  1747. "Priorities in Org-mode."
  1748. :tag "Org Priorities"
  1749. :group 'org-todo)
  1750. (defcustom org-enable-priority-commands t
  1751. "Non-nil means, priority commands are active.
  1752. When nil, these commands will be disabled, so that you never accidentally
  1753. set a priority."
  1754. :group 'org-priorities
  1755. :type 'boolean)
  1756. (defcustom org-highest-priority ?A
  1757. "The highest priority of TODO items. A character like ?A, ?B etc.
  1758. Must have a smaller ASCII number than `org-lowest-priority'."
  1759. :group 'org-priorities
  1760. :type 'character)
  1761. (defcustom org-lowest-priority ?C
  1762. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1763. Must have a larger ASCII number than `org-highest-priority'."
  1764. :group 'org-priorities
  1765. :type 'character)
  1766. (defcustom org-default-priority ?B
  1767. "The default priority of TODO items.
  1768. This is the priority an item get if no explicit priority is given."
  1769. :group 'org-priorities
  1770. :type 'character)
  1771. (defcustom org-priority-start-cycle-with-default t
  1772. "Non-nil means, start with default priority when starting to cycle.
  1773. When this is nil, the first step in the cycle will be (depending on the
  1774. command used) one higher or lower that the default priority."
  1775. :group 'org-priorities
  1776. :type 'boolean)
  1777. (defgroup org-time nil
  1778. "Options concerning time stamps and deadlines in Org-mode."
  1779. :tag "Org Time"
  1780. :group 'org)
  1781. (defcustom org-insert-labeled-timestamps-at-point nil
  1782. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1783. When nil, these labeled time stamps are forces into the second line of an
  1784. entry, just after the headline. When scheduling from the global TODO list,
  1785. the time stamp will always be forced into the second line."
  1786. :group 'org-time
  1787. :type 'boolean)
  1788. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1789. "Formats for `format-time-string' which are used for time stamps.
  1790. It is not recommended to change this constant.")
  1791. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1792. "Number of minutes to round time stamps to.
  1793. These are two values, the first applies when first creating a time stamp.
  1794. The second applies when changing it with the commands `S-up' and `S-down'.
  1795. When changing the time stamp, this means that it will change in steps
  1796. of N minutes, as given by the second value.
  1797. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1798. numbers should be factors of 60, so for example 5, 10, 15.
  1799. When this is larger than 1, you can still force an exact time-stamp by using
  1800. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1801. and by using a prefix arg to `S-up/down' to specify the exact number
  1802. of minutes to shift."
  1803. :group 'org-time
  1804. :get '(lambda (var) ; Make sure all entries have 5 elements
  1805. (if (integerp (default-value var))
  1806. (list (default-value var) 5)
  1807. (default-value var)))
  1808. :type '(list
  1809. (integer :tag "when inserting times")
  1810. (integer :tag "when modifying times")))
  1811. ;; Normalize old customizations of this variable.
  1812. (when (integerp org-time-stamp-rounding-minutes)
  1813. (setq org-time-stamp-rounding-minutes
  1814. (list org-time-stamp-rounding-minutes
  1815. org-time-stamp-rounding-minutes)))
  1816. (defcustom org-display-custom-times nil
  1817. "Non-nil means, overlay custom formats over all time stamps.
  1818. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1819. To turn this on on a per-file basis, insert anywhere in the file:
  1820. #+STARTUP: customtime"
  1821. :group 'org-time
  1822. :set 'set-default
  1823. :type 'sexp)
  1824. (make-variable-buffer-local 'org-display-custom-times)
  1825. (defcustom org-time-stamp-custom-formats
  1826. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1827. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1828. These are overlayed over the default ISO format if the variable
  1829. `org-display-custom-times' is set. Time like %H:%M should be at the
  1830. end of the second format. The custom formats are also honored by export
  1831. commands, if custom time display is turned on at the time of export."
  1832. :group 'org-time
  1833. :type 'sexp)
  1834. (defun org-time-stamp-format (&optional long inactive)
  1835. "Get the right format for a time string."
  1836. (let ((f (if long (cdr org-time-stamp-formats)
  1837. (car org-time-stamp-formats))))
  1838. (if inactive
  1839. (concat "[" (substring f 1 -1) "]")
  1840. f)))
  1841. (defcustom org-time-clocksum-format "%d:%02d"
  1842. "The format string used when creating CLOCKSUM lines, or when
  1843. org-mode generates a time duration."
  1844. :group 'org-time
  1845. :type 'string)
  1846. (defcustom org-deadline-warning-days 14
  1847. "No. of days before expiration during which a deadline becomes active.
  1848. This variable governs the display in sparse trees and in the agenda.
  1849. When 0 or negative, it means use this number (the absolute value of it)
  1850. even if a deadline has a different individual lead time specified.
  1851. Custom commands can set this variable in the options section."
  1852. :group 'org-time
  1853. :group 'org-agenda-daily/weekly
  1854. :type 'integer)
  1855. (defcustom org-read-date-prefer-future t
  1856. "Non-nil means, assume future for incomplete date input from user.
  1857. This affects the following situations:
  1858. 1. The user gives a day, but no month.
  1859. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1860. read this as the third of *next* month. However, if you enter \"17\",
  1861. it will be considered as *this* month.
  1862. 2. The user gives a month but not a year.
  1863. For example, if it is april and you enter \"feb 2\", this will be read
  1864. as feb 2, *next* year. \"May 5\", however, will be this year.
  1865. Currently this does not work for ISO week specifications.
  1866. When this option is nil, the current month and year will always be used
  1867. as defaults."
  1868. :group 'org-time
  1869. :type 'boolean)
  1870. (defcustom org-read-date-display-live t
  1871. "Non-nil means, display current interpretation of date prompt live.
  1872. This display will be in an overlay, in the minibuffer."
  1873. :group 'org-time
  1874. :type 'boolean)
  1875. (defcustom org-read-date-popup-calendar t
  1876. "Non-nil means, pop up a calendar when prompting for a date.
  1877. In the calendar, the date can be selected with mouse-1. However, the
  1878. minibuffer will also be active, and you can simply enter the date as well.
  1879. When nil, only the minibuffer will be available."
  1880. :group 'org-time
  1881. :type 'boolean)
  1882. (if (fboundp 'defvaralias)
  1883. (defvaralias 'org-popup-calendar-for-date-prompt
  1884. 'org-read-date-popup-calendar))
  1885. (defcustom org-read-date-minibuffer-setup-hook nil
  1886. "Hook to be used to set up keys for the date/time interface.
  1887. Add key definitions to `minibuffer-local-map', which will be a temporary
  1888. copy."
  1889. :group 'org-time
  1890. :type 'hook)
  1891. (defcustom org-extend-today-until 0
  1892. "The hour when your day really ends. Must be an integer.
  1893. This has influence for the following applications:
  1894. - When switching the agenda to \"today\". It it is still earlier than
  1895. the time given here, the day recognized as TODAY is actually yesterday.
  1896. - When a date is read from the user and it is still before the time given
  1897. here, the current date and time will be assumed to be yesterday, 23:59.
  1898. Also, timestamps inserted in remember templates follow this rule.
  1899. IMPORTANT: This is a feature whose implementation is and likely will
  1900. remain incomplete. Really, it is only here because past midnight seems to
  1901. be the favorite working time of John Wiegley :-)"
  1902. :group 'org-time
  1903. :type 'integer)
  1904. (defcustom org-edit-timestamp-down-means-later nil
  1905. "Non-nil means, S-down will increase the time in a time stamp.
  1906. When nil, S-up will increase."
  1907. :group 'org-time
  1908. :type 'boolean)
  1909. (defcustom org-calendar-follow-timestamp-change t
  1910. "Non-nil means, make the calendar window follow timestamp changes.
  1911. When a timestamp is modified and the calendar window is visible, it will be
  1912. moved to the new date."
  1913. :group 'org-time
  1914. :type 'boolean)
  1915. (defgroup org-tags nil
  1916. "Options concerning tags in Org-mode."
  1917. :tag "Org Tags"
  1918. :group 'org)
  1919. (defcustom org-tag-alist nil
  1920. "List of tags allowed in Org-mode files.
  1921. When this list is nil, Org-mode will base TAG input on what is already in the
  1922. buffer.
  1923. The value of this variable is an alist, the car of each entry must be a
  1924. keyword as a string, the cdr may be a character that is used to select
  1925. that tag through the fast-tag-selection interface.
  1926. See the manual for details."
  1927. :group 'org-tags
  1928. :type '(repeat
  1929. (choice
  1930. (cons (string :tag "Tag name")
  1931. (character :tag "Access char"))
  1932. (const :tag "Start radio group" (:startgroup))
  1933. (const :tag "End radio group" (:endgroup))
  1934. (const :tag "New line" (:newline)))))
  1935. (defcustom org-tag-persistent-alist nil
  1936. "List of tags that will always appear in all Org-mode files.
  1937. This is in addition to any in buffer settings or customizations
  1938. of `org-tag-alist'.
  1939. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1940. The value of this variable is an alist, the car of each entry must be a
  1941. keyword as a string, the cdr may be a character that is used to select
  1942. that tag through the fast-tag-selection interface.
  1943. See the manual for details.
  1944. To disable these tags on a per-file basis, insert anywhere in the file:
  1945. #+STARTUP: noptag"
  1946. :group 'org-tags
  1947. :type '(repeat
  1948. (choice
  1949. (cons (string :tag "Tag name")
  1950. (character :tag "Access char"))
  1951. (const :tag "Start radio group" (:startgroup))
  1952. (const :tag "End radio group" (:endgroup))
  1953. (const :tag "New line" (:newline)))))
  1954. (defvar org-file-tags nil
  1955. "List of tags that can be inherited by all entries in the file.
  1956. The tags will be inherited if the variable `org-use-tag-inheritance'
  1957. says they should be.
  1958. This variable is populated from #+TAG lines.")
  1959. (defcustom org-use-fast-tag-selection 'auto
  1960. "Non-nil means, use fast tag selection scheme.
  1961. This is a special interface to select and deselect tags with single keys.
  1962. When nil, fast selection is never used.
  1963. When the symbol `auto', fast selection is used if and only if selection
  1964. characters for tags have been configured, either through the variable
  1965. `org-tag-alist' or through a #+TAGS line in the buffer.
  1966. When t, fast selection is always used and selection keys are assigned
  1967. automatically if necessary."
  1968. :group 'org-tags
  1969. :type '(choice
  1970. (const :tag "Always" t)
  1971. (const :tag "Never" nil)
  1972. (const :tag "When selection characters are configured" 'auto)))
  1973. (defcustom org-fast-tag-selection-single-key nil
  1974. "Non-nil means, fast tag selection exits after first change.
  1975. When nil, you have to press RET to exit it.
  1976. During fast tag selection, you can toggle this flag with `C-c'.
  1977. This variable can also have the value `expert'. In this case, the window
  1978. displaying the tags menu is not even shown, until you press C-c again."
  1979. :group 'org-tags
  1980. :type '(choice
  1981. (const :tag "No" nil)
  1982. (const :tag "Yes" t)
  1983. (const :tag "Expert" expert)))
  1984. (defvar org-fast-tag-selection-include-todo nil
  1985. "Non-nil means, fast tags selection interface will also offer TODO states.
  1986. This is an undocumented feature, you should not rely on it.")
  1987. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1988. "The column to which tags should be indented in a headline.
  1989. If this number is positive, it specifies the column. If it is negative,
  1990. it means that the tags should be flushright to that column. For example,
  1991. -80 works well for a normal 80 character screen."
  1992. :group 'org-tags
  1993. :type 'integer)
  1994. (defcustom org-auto-align-tags t
  1995. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1996. These operations change the length of a headline and therefore shift
  1997. the tags around. With this options turned on, after each such operation
  1998. the tags are again aligned to `org-tags-column'."
  1999. :group 'org-tags
  2000. :type 'boolean)
  2001. (defcustom org-use-tag-inheritance t
  2002. "Non-nil means, tags in levels apply also for sublevels.
  2003. When nil, only the tags directly given in a specific line apply there.
  2004. This may also be a list of tags that should be inherited, or a regexp that
  2005. matches tags that should be inherited. Additional control is possible
  2006. with the variable `org-tags-exclude-from-inheritance' which gives an
  2007. explicit list of tags to be excluded from inheritance., even if the value of
  2008. `org-use-tag-inheritance' would select it for inheritance.
  2009. If this option is t, a match early-on in a tree can lead to a large
  2010. number of matches in the subtree when constructing the agenda or creating
  2011. a sparse tree. If you only want to see the first match in a tree during
  2012. a search, check out the variable `org-tags-match-list-sublevels'."
  2013. :group 'org-tags
  2014. :type '(choice
  2015. (const :tag "Not" nil)
  2016. (const :tag "Always" t)
  2017. (repeat :tag "Specific tags" (string :tag "Tag"))
  2018. (regexp :tag "Tags matched by regexp")))
  2019. (defcustom org-tags-exclude-from-inheritance nil
  2020. "List of tags that should never be inherited.
  2021. This is a way to exclude a few tags from inheritance. For way to do
  2022. the opposite, to actively allow inheritance for selected tags,
  2023. see the variable `org-use-tag-inheritance'."
  2024. :group 'org-tags
  2025. :type '(repeat (string :tag "Tag")))
  2026. (defun org-tag-inherit-p (tag)
  2027. "Check if TAG is one that should be inherited."
  2028. (cond
  2029. ((member tag org-tags-exclude-from-inheritance) nil)
  2030. ((eq org-use-tag-inheritance t) t)
  2031. ((not org-use-tag-inheritance) nil)
  2032. ((stringp org-use-tag-inheritance)
  2033. (string-match org-use-tag-inheritance tag))
  2034. ((listp org-use-tag-inheritance)
  2035. (member tag org-use-tag-inheritance))
  2036. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2037. (defcustom org-tags-match-list-sublevels t
  2038. "Non-nil means list also sublevels of headlines matching a search.
  2039. This variable applies to tags/property searches, and also to stuck
  2040. projects because this search is based on a tags match as well.
  2041. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2042. the sublevels of a headline matching a tag search often also match
  2043. the same search. Listing all of them can create very long lists.
  2044. Setting this variable to nil causes subtrees of a match to be skipped.
  2045. As a special case, if the tag search is restricted to TODO items, the
  2046. value of this variable is ignored and sublevels are always checked, to
  2047. make sure all corresponding TODO items find their way into the list.
  2048. This variable is semi-obsolete and probably should always be true. It
  2049. is better to limit inheritance to certain tags using the variables
  2050. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2051. :group 'org-tags
  2052. :type '(choice
  2053. (const :tag "No, don't list them" nil)
  2054. (const :tag "Yes, do list them" t)
  2055. (const :tag "List them, indented with leading dots" indented)))
  2056. (defvar org-tags-history nil
  2057. "History of minibuffer reads for tags.")
  2058. (defvar org-last-tags-completion-table nil
  2059. "The last used completion table for tags.")
  2060. (defvar org-after-tags-change-hook nil
  2061. "Hook that is run after the tags in a line have changed.")
  2062. (defgroup org-properties nil
  2063. "Options concerning properties in Org-mode."
  2064. :tag "Org Properties"
  2065. :group 'org)
  2066. (defcustom org-property-format "%-10s %s"
  2067. "How property key/value pairs should be formatted by `indent-line'.
  2068. When `indent-line' hits a property definition, it will format the line
  2069. according to this format, mainly to make sure that the values are
  2070. lined-up with respect to each other."
  2071. :group 'org-properties
  2072. :type 'string)
  2073. (defcustom org-use-property-inheritance nil
  2074. "Non-nil means, properties apply also for sublevels.
  2075. This setting is chiefly used during property searches. Turning it on can
  2076. cause significant overhead when doing a search, which is why it is not
  2077. on by default.
  2078. When nil, only the properties directly given in the current entry count.
  2079. When t, every property is inherited. The value may also be a list of
  2080. properties that should have inheritance, or a regular expression matching
  2081. properties that should be inherited.
  2082. However, note that some special properties use inheritance under special
  2083. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2084. and the properties ending in \"_ALL\" when they are used as descriptor
  2085. for valid values of a property.
  2086. Note for programmers:
  2087. When querying an entry with `org-entry-get', you can control if inheritance
  2088. should be used. By default, `org-entry-get' looks only at the local
  2089. properties. You can request inheritance by setting the inherit argument
  2090. to t (to force inheritance) or to `selective' (to respect the setting
  2091. in this variable)."
  2092. :group 'org-properties
  2093. :type '(choice
  2094. (const :tag "Not" nil)
  2095. (const :tag "Always" t)
  2096. (repeat :tag "Specific properties" (string :tag "Property"))
  2097. (regexp :tag "Properties matched by regexp")))
  2098. (defun org-property-inherit-p (property)
  2099. "Check if PROPERTY is one that should be inherited."
  2100. (cond
  2101. ((eq org-use-property-inheritance t) t)
  2102. ((not org-use-property-inheritance) nil)
  2103. ((stringp org-use-property-inheritance)
  2104. (string-match org-use-property-inheritance property))
  2105. ((listp org-use-property-inheritance)
  2106. (member property org-use-property-inheritance))
  2107. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2108. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2109. "The default column format, if no other format has been defined.
  2110. This variable can be set on the per-file basis by inserting a line
  2111. #+COLUMNS: %25ITEM ....."
  2112. :group 'org-properties
  2113. :type 'string)
  2114. (defcustom org-columns-ellipses ".."
  2115. "The ellipses to be used when a field in column view is truncated.
  2116. When this is the empty string, as many characters as possible are shown,
  2117. but then there will be no visual indication that the field has been truncated.
  2118. When this is a string of length N, the last N characters of a truncated
  2119. field are replaced by this string. If the column is narrower than the
  2120. ellipses string, only part of the ellipses string will be shown."
  2121. :group 'org-properties
  2122. :type 'string)
  2123. (defcustom org-columns-modify-value-for-display-function nil
  2124. "Function that modifies values for display in column view.
  2125. For example, it can be used to cut out a certain part from a time stamp.
  2126. The function must take 2 arguments:
  2127. column-title The title of the column (*not* the property name)
  2128. value The value that should be modified.
  2129. The function should return the value that should be displayed,
  2130. or nil if the normal value should be used."
  2131. :group 'org-properties
  2132. :type 'function)
  2133. (defcustom org-effort-property "Effort"
  2134. "The property that is being used to keep track of effort estimates.
  2135. Effort estimates given in this property need to have the format H:MM."
  2136. :group 'org-properties
  2137. :group 'org-progress
  2138. :type '(string :tag "Property"))
  2139. (defconst org-global-properties-fixed
  2140. '(("VISIBILITY_ALL" . "folded children content all"))
  2141. "List of property/value pairs that can be inherited by any entry.
  2142. These are fixed values, for the preset properties. The user variable
  2143. that can be used to add to this list is `org-global-properties'.
  2144. The entries in this list are cons cells where the car is a property
  2145. name and cdr is a string with the value. If the value represents
  2146. multiple items like an \"_ALL\" property, separate the items by
  2147. spaces.")
  2148. (defcustom org-global-properties nil
  2149. "List of property/value pairs that can be inherited by any entry.
  2150. This list will be combined with the constant `org-global-properties-fixed'.
  2151. The entries in this list are cons cells where the car is a property
  2152. name and cdr is a string with the value.
  2153. You can set buffer-local values for the same purpose in the variable
  2154. `org-file-properties' this by adding lines like
  2155. #+PROPERTY: NAME VALUE"
  2156. :group 'org-properties
  2157. :type '(repeat
  2158. (cons (string :tag "Property")
  2159. (string :tag "Value"))))
  2160. (defvar org-file-properties nil
  2161. "List of property/value pairs that can be inherited by any entry.
  2162. Valid for the current buffer.
  2163. This variable is populated from #+PROPERTY lines.")
  2164. (make-variable-buffer-local 'org-file-properties)
  2165. (defgroup org-agenda nil
  2166. "Options concerning agenda views in Org-mode."
  2167. :tag "Org Agenda"
  2168. :group 'org)
  2169. (defvar org-category nil
  2170. "Variable used by org files to set a category for agenda display.
  2171. Such files should use a file variable to set it, for example
  2172. # -*- mode: org; org-category: \"ELisp\"
  2173. or contain a special line
  2174. #+CATEGORY: ELisp
  2175. If the file does not specify a category, then file's base name
  2176. is used instead.")
  2177. (make-variable-buffer-local 'org-category)
  2178. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2179. (defcustom org-agenda-files nil
  2180. "The files to be used for agenda display.
  2181. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2182. \\[org-remove-file]. You can also use customize to edit the list.
  2183. If an entry is a directory, all files in that directory that are matched by
  2184. `org-agenda-file-regexp' will be part of the file list.
  2185. If the value of the variable is not a list but a single file name, then
  2186. the list of agenda files is actually stored and maintained in that file, one
  2187. agenda file per line."
  2188. :group 'org-agenda
  2189. :type '(choice
  2190. (repeat :tag "List of files and directories" file)
  2191. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2192. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2193. "Regular expression to match files for `org-agenda-files'.
  2194. If any element in the list in that variable contains a directory instead
  2195. of a normal file, all files in that directory that are matched by this
  2196. regular expression will be included."
  2197. :group 'org-agenda
  2198. :type 'regexp)
  2199. (defcustom org-agenda-text-search-extra-files nil
  2200. "List of extra files to be searched by text search commands.
  2201. These files will be search in addition to the agenda files by the
  2202. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2203. Note that these files will only be searched for text search commands,
  2204. not for the other agenda views like todo lists, tag searches or the weekly
  2205. agenda. This variable is intended to list notes and possibly archive files
  2206. that should also be searched by these two commands.
  2207. In fact, if the first element in the list is the symbol `agenda-archives',
  2208. than all archive files of all agenda files will be added to the search
  2209. scope."
  2210. :group 'org-agenda
  2211. :type '(set :greedy t
  2212. (const :tag "Agenda Archives" agenda-archives)
  2213. (repeat :inline t (file))))
  2214. (if (fboundp 'defvaralias)
  2215. (defvaralias 'org-agenda-multi-occur-extra-files
  2216. 'org-agenda-text-search-extra-files))
  2217. (defcustom org-agenda-skip-unavailable-files nil
  2218. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2219. A nil value means to remove them, after a query, from the list."
  2220. :group 'org-agenda
  2221. :type 'boolean)
  2222. (defcustom org-calendar-to-agenda-key [?c]
  2223. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2224. The command `org-calendar-goto-agenda' will be bound to this key. The
  2225. default is the character `c' because then `c' can be used to switch back and
  2226. forth between agenda and calendar."
  2227. :group 'org-agenda
  2228. :type 'sexp)
  2229. (defcustom org-calendar-agenda-action-key [?k]
  2230. "The key to be installed in `calendar-mode-map' for agenda-action.
  2231. The command `org-agenda-action' will be bound to this key. The
  2232. default is the character `k' because we use the same key in the agenda."
  2233. :group 'org-agenda
  2234. :type 'sexp)
  2235. (eval-after-load "calendar"
  2236. '(progn
  2237. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2238. 'org-calendar-goto-agenda)
  2239. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2240. 'org-agenda-action)))
  2241. (defgroup org-latex nil
  2242. "Options for embedding LaTeX code into Org-mode."
  2243. :tag "Org LaTeX"
  2244. :group 'org)
  2245. (defcustom org-format-latex-options
  2246. '(:foreground default :background default :scale 1.0
  2247. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2248. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2249. "Options for creating images from LaTeX fragments.
  2250. This is a property list with the following properties:
  2251. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2252. `default' means use the foreground of the default face.
  2253. :background the background color, or \"Transparent\".
  2254. `default' means use the background of the default face.
  2255. :scale a scaling factor for the size of the images.
  2256. :html-foreground, :html-background, :html-scale
  2257. the same numbers for HTML export.
  2258. :matchers a list indicating which matchers should be used to
  2259. find LaTeX fragments. Valid members of this list are:
  2260. \"begin\" find environments
  2261. \"$1\" find single characters surrounded by $.$
  2262. \"$\" find math expressions surrounded by $...$
  2263. \"$$\" find math expressions surrounded by $$....$$
  2264. \"\\(\" find math expressions surrounded by \\(...\\)
  2265. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2266. :group 'org-latex
  2267. :type 'plist)
  2268. (defcustom org-format-latex-header "\\documentclass{article}
  2269. \\usepackage{fullpage} % do not remove
  2270. \\usepackage{amssymb}
  2271. \\usepackage[usenames]{color}
  2272. \\usepackage{amsmath}
  2273. \\usepackage{latexsym}
  2274. \\usepackage[mathscr]{eucal}
  2275. \\pagestyle{empty} % do not remove"
  2276. "The document header used for processing LaTeX fragments."
  2277. :group 'org-latex
  2278. :type 'string)
  2279. (defgroup org-font-lock nil
  2280. "Font-lock settings for highlighting in Org-mode."
  2281. :tag "Org Font Lock"
  2282. :group 'org)
  2283. (defcustom org-level-color-stars-only nil
  2284. "Non-nil means fontify only the stars in each headline.
  2285. When nil, the entire headline is fontified.
  2286. Changing it requires restart of `font-lock-mode' to become effective
  2287. also in regions already fontified."
  2288. :group 'org-font-lock
  2289. :type 'boolean)
  2290. (defcustom org-hide-leading-stars nil
  2291. "Non-nil means, hide the first N-1 stars in a headline.
  2292. This works by using the face `org-hide' for these stars. This
  2293. face is white for a light background, and black for a dark
  2294. background. You may have to customize the face `org-hide' to
  2295. make this work.
  2296. Changing it requires restart of `font-lock-mode' to become effective
  2297. also in regions already fontified.
  2298. You may also set this on a per-file basis by adding one of the following
  2299. lines to the buffer:
  2300. #+STARTUP: hidestars
  2301. #+STARTUP: showstars"
  2302. :group 'org-font-lock
  2303. :type 'boolean)
  2304. (defcustom org-fontify-done-headline nil
  2305. "Non-nil means, change the face of a headline if it is marked DONE.
  2306. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2307. When this is non-nil, the headline after the keyword is set to the
  2308. `org-headline-done' as an additional indication."
  2309. :group 'org-font-lock
  2310. :type 'boolean)
  2311. (defcustom org-fontify-emphasized-text t
  2312. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2313. Changing this variable requires a restart of Emacs to take effect."
  2314. :group 'org-font-lock
  2315. :type 'boolean)
  2316. (defcustom org-highlight-latex-fragments-and-specials nil
  2317. "Non-nil means, fontify what is treated specially by the exporters."
  2318. :group 'org-font-lock
  2319. :type 'boolean)
  2320. (defcustom org-hide-emphasis-markers nil
  2321. "Non-nil mean font-lock should hide the emphasis marker characters."
  2322. :group 'org-font-lock
  2323. :type 'boolean)
  2324. (defvar org-emph-re nil
  2325. "Regular expression for matching emphasis.")
  2326. (defvar org-verbatim-re nil
  2327. "Regular expression for matching verbatim text.")
  2328. (defvar org-emphasis-regexp-components) ; defined just below
  2329. (defvar org-emphasis-alist) ; defined just below
  2330. (defun org-set-emph-re (var val)
  2331. "Set variable and compute the emphasis regular expression."
  2332. (set var val)
  2333. (when (and (boundp 'org-emphasis-alist)
  2334. (boundp 'org-emphasis-regexp-components)
  2335. org-emphasis-alist org-emphasis-regexp-components)
  2336. (let* ((e org-emphasis-regexp-components)
  2337. (pre (car e))
  2338. (post (nth 1 e))
  2339. (border (nth 2 e))
  2340. (body (nth 3 e))
  2341. (nl (nth 4 e))
  2342. (body1 (concat body "*?"))
  2343. (markers (mapconcat 'car org-emphasis-alist ""))
  2344. (vmarkers (mapconcat
  2345. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2346. org-emphasis-alist "")))
  2347. ;; make sure special characters appear at the right position in the class
  2348. (if (string-match "\\^" markers)
  2349. (setq markers (concat (replace-match "" t t markers) "^")))
  2350. (if (string-match "-" markers)
  2351. (setq markers (concat (replace-match "" t t markers) "-")))
  2352. (if (string-match "\\^" vmarkers)
  2353. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2354. (if (string-match "-" vmarkers)
  2355. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2356. (if (> nl 0)
  2357. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2358. (int-to-string nl) "\\}")))
  2359. ;; Make the regexp
  2360. (setq org-emph-re
  2361. (concat "\\([" pre "]\\|^\\)"
  2362. "\\("
  2363. "\\([" markers "]\\)"
  2364. "\\("
  2365. "[^" border "]\\|"
  2366. "[^" border "]"
  2367. body1
  2368. "[^" border "]"
  2369. "\\)"
  2370. "\\3\\)"
  2371. "\\([" post "]\\|$\\)"))
  2372. (setq org-verbatim-re
  2373. (concat "\\([" pre "]\\|^\\)"
  2374. "\\("
  2375. "\\([" vmarkers "]\\)"
  2376. "\\("
  2377. "[^" border "]\\|"
  2378. "[^" border "]"
  2379. body1
  2380. "[^" border "]"
  2381. "\\)"
  2382. "\\3\\)"
  2383. "\\([" post "]\\|$\\)")))))
  2384. (defcustom org-emphasis-regexp-components
  2385. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2386. "Components used to build the regular expression for emphasis.
  2387. This is a list with 6 entries. Terminology: In an emphasis string
  2388. like \" *strong word* \", we call the initial space PREMATCH, the final
  2389. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2390. and \"trong wor\" is the body. The different components in this variable
  2391. specify what is allowed/forbidden in each part:
  2392. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2393. post Chars allowed as postmatch. End of line will be allowed too.
  2394. border The chars *forbidden* as border characters.
  2395. body-regexp A regexp like \".\" to match a body character. Don't use
  2396. non-shy groups here, and don't allow newline here.
  2397. newline The maximum number of newlines allowed in an emphasis exp.
  2398. Use customize to modify this, or restart Emacs after changing it."
  2399. :group 'org-font-lock
  2400. :set 'org-set-emph-re
  2401. :type '(list
  2402. (sexp :tag "Allowed chars in pre ")
  2403. (sexp :tag "Allowed chars in post ")
  2404. (sexp :tag "Forbidden chars in border ")
  2405. (sexp :tag "Regexp for body ")
  2406. (integer :tag "number of newlines allowed")
  2407. (option (boolean :tag "Please ignore this button"))))
  2408. (defcustom org-emphasis-alist
  2409. `(("*" bold "<b>" "</b>")
  2410. ("/" italic "<i>" "</i>")
  2411. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2412. ("=" org-code "<code>" "</code>" verbatim)
  2413. ("~" org-verbatim "<code>" "</code>" verbatim)
  2414. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2415. "<del>" "</del>")
  2416. )
  2417. "Special syntax for emphasized text.
  2418. Text starting and ending with a special character will be emphasized, for
  2419. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2420. characters, the face to be used by font-lock for highlighting in Org-mode
  2421. Emacs buffers, and the HTML tags to be used for this.
  2422. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2423. Use customize to modify this, or restart Emacs after changing it."
  2424. :group 'org-font-lock
  2425. :set 'org-set-emph-re
  2426. :type '(repeat
  2427. (list
  2428. (string :tag "Marker character")
  2429. (choice
  2430. (face :tag "Font-lock-face")
  2431. (plist :tag "Face property list"))
  2432. (string :tag "HTML start tag")
  2433. (string :tag "HTML end tag")
  2434. (option (const verbatim)))))
  2435. ;;; Miscellaneous options
  2436. (defgroup org-completion nil
  2437. "Completion in Org-mode."
  2438. :tag "Org Completion"
  2439. :group 'org)
  2440. (defcustom org-completion-use-ido nil
  2441. "Non-nil means, use ido completion wherever possible.
  2442. Note that `ido-mode' must be active for this variable to be relevant.
  2443. If you decide to turn this variable on, you might well want to turn off
  2444. `org-outline-path-complete-in-steps'."
  2445. :group 'org-completion
  2446. :type 'boolean)
  2447. (defcustom org-completion-fallback-command 'hippie-expand
  2448. "The expansion command called by \\[org-complete] in normal context.
  2449. Normal means, no org-mode-specific context."
  2450. :group 'org-completion
  2451. :type 'function)
  2452. ;;; Functions and variables from ther packages
  2453. ;; Declared here to avoid compiler warnings
  2454. ;; XEmacs only
  2455. (defvar outline-mode-menu-heading)
  2456. (defvar outline-mode-menu-show)
  2457. (defvar outline-mode-menu-hide)
  2458. (defvar zmacs-regions) ; XEmacs regions
  2459. ;; Emacs only
  2460. (defvar mark-active)
  2461. ;; Various packages
  2462. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2463. (declare-function calendar-forward-day "cal-move" (arg))
  2464. (declare-function calendar-goto-date "cal-move" (date))
  2465. (declare-function calendar-goto-today "cal-move" ())
  2466. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2467. (defvar calc-embedded-close-formula)
  2468. (defvar calc-embedded-open-formula)
  2469. (declare-function cdlatex-tab "ext:cdlatex" ())
  2470. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2471. (defvar font-lock-unfontify-region-function)
  2472. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2473. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2474. (defvar iswitchb-temp-buflist)
  2475. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2476. (defvar org-agenda-tags-todo-honor-ignore-options)
  2477. (declare-function org-agenda-skip "org-agenda" ())
  2478. (declare-function org-format-agenda-item "org-agenda"
  2479. (extra txt &optional category tags dotime noprefix remove-re))
  2480. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2481. (declare-function org-agenda-change-all-lines "org-agenda"
  2482. (newhead hdmarker &optional fixface just-this))
  2483. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2484. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2485. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2486. (beg end))
  2487. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2488. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2489. "org-agenda" (&optional end))
  2490. (declare-function parse-time-string "parse-time" (string))
  2491. (declare-function remember "remember" (&optional initial))
  2492. (declare-function remember-buffer-desc "remember" ())
  2493. (declare-function remember-finalize "remember" ())
  2494. (defvar remember-save-after-remembering)
  2495. (defvar remember-data-file)
  2496. (defvar remember-register)
  2497. (defvar remember-buffer)
  2498. (defvar remember-handler-functions)
  2499. (defvar remember-annotation-functions)
  2500. (defvar texmathp-why)
  2501. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2502. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2503. (defvar w3m-current-url)
  2504. (defvar w3m-current-title)
  2505. (defvar org-latex-regexps)
  2506. ;;; Autoload and prepare some org modules
  2507. ;; Some table stuff that needs to be defined here, because it is used
  2508. ;; by the functions setting up org-mode or checking for table context.
  2509. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2510. "Detects an org-type or table-type table.")
  2511. (defconst org-table-line-regexp "^[ \t]*|"
  2512. "Detects an org-type table line.")
  2513. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2514. "Detects an org-type table line.")
  2515. (defconst org-table-hline-regexp "^[ \t]*|-"
  2516. "Detects an org-type table hline.")
  2517. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2518. "Detects a table-type table hline.")
  2519. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2520. "Searching from within a table (any type) this finds the first line
  2521. outside the table.")
  2522. ;; Autoload the functions in org-table.el that are needed by functions here.
  2523. (eval-and-compile
  2524. (org-autoload "org-table"
  2525. '(org-table-align org-table-begin org-table-blank-field
  2526. org-table-convert org-table-convert-region org-table-copy-down
  2527. org-table-copy-region org-table-create
  2528. org-table-create-or-convert-from-region
  2529. org-table-create-with-table.el org-table-current-dline
  2530. org-table-cut-region org-table-delete-column org-table-edit-field
  2531. org-table-edit-formulas org-table-end org-table-eval-formula
  2532. org-table-export org-table-field-info
  2533. org-table-get-stored-formulas org-table-goto-column
  2534. org-table-hline-and-move org-table-import org-table-insert-column
  2535. org-table-insert-hline org-table-insert-row org-table-iterate
  2536. org-table-justify-field-maybe org-table-kill-row
  2537. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2538. org-table-move-column org-table-move-column-left
  2539. org-table-move-column-right org-table-move-row
  2540. org-table-move-row-down org-table-move-row-up
  2541. org-table-next-field org-table-next-row org-table-paste-rectangle
  2542. org-table-previous-field org-table-recalculate
  2543. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2544. org-table-toggle-coordinate-overlays
  2545. org-table-toggle-formula-debugger org-table-wrap-region
  2546. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2547. (defun org-at-table-p (&optional table-type)
  2548. "Return t if the cursor is inside an org-type table.
  2549. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2550. (if org-enable-table-editor
  2551. (save-excursion
  2552. (beginning-of-line 1)
  2553. (looking-at (if table-type org-table-any-line-regexp
  2554. org-table-line-regexp)))
  2555. nil))
  2556. (defsubst org-table-p () (org-at-table-p))
  2557. (defun org-at-table.el-p ()
  2558. "Return t if and only if we are at a table.el table."
  2559. (and (org-at-table-p 'any)
  2560. (save-excursion
  2561. (goto-char (org-table-begin 'any))
  2562. (looking-at org-table1-hline-regexp))))
  2563. (defun org-table-recognize-table.el ()
  2564. "If there is a table.el table nearby, recognize it and move into it."
  2565. (if org-table-tab-recognizes-table.el
  2566. (if (org-at-table.el-p)
  2567. (progn
  2568. (beginning-of-line 1)
  2569. (if (looking-at org-table-dataline-regexp)
  2570. nil
  2571. (if (looking-at org-table1-hline-regexp)
  2572. (progn
  2573. (beginning-of-line 2)
  2574. (if (looking-at org-table-any-border-regexp)
  2575. (beginning-of-line -1)))))
  2576. (if (re-search-forward "|" (org-table-end t) t)
  2577. (progn
  2578. (require 'table)
  2579. (if (table--at-cell-p (point))
  2580. t
  2581. (message "recognizing table.el table...")
  2582. (table-recognize-table)
  2583. (message "recognizing table.el table...done")))
  2584. (error "This should not happen..."))
  2585. t)
  2586. nil)
  2587. nil))
  2588. (defun org-at-table-hline-p ()
  2589. "Return t if the cursor is inside a hline in a table."
  2590. (if org-enable-table-editor
  2591. (save-excursion
  2592. (beginning-of-line 1)
  2593. (looking-at org-table-hline-regexp))
  2594. nil))
  2595. (defvar org-table-clean-did-remove-column nil)
  2596. (defun org-table-map-tables (function)
  2597. "Apply FUNCTION to the start of all tables in the buffer."
  2598. (save-excursion
  2599. (save-restriction
  2600. (widen)
  2601. (goto-char (point-min))
  2602. (while (re-search-forward org-table-any-line-regexp nil t)
  2603. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2604. (beginning-of-line 1)
  2605. (if (looking-at org-table-line-regexp)
  2606. (save-excursion (funcall function)))
  2607. (re-search-forward org-table-any-border-regexp nil 1))))
  2608. (message "Mapping tables: done"))
  2609. ;; Declare and autoload functions from org-exp.el & Co
  2610. (declare-function org-default-export-plist "org-exp")
  2611. (declare-function org-infile-export-plist "org-exp")
  2612. (declare-function org-get-current-options "org-exp")
  2613. (eval-and-compile
  2614. (org-autoload "org-exp"
  2615. '(org-export org-export-visible
  2616. org-insert-export-options-template
  2617. org-table-clean-before-export))
  2618. (org-autoload "org-ascii"
  2619. '(org-export-as-ascii org-export-ascii-preprocess
  2620. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2621. org-export-region-as-ascii))
  2622. (org-autoload "org-html"
  2623. '(org-export-as-html-and-open
  2624. org-export-as-html-batch org-export-as-html-to-buffer
  2625. org-replace-region-by-html org-export-region-as-html
  2626. org-export-as-html))
  2627. (org-autoload "org-icalendar"
  2628. '(org-export-icalendar-this-file
  2629. org-export-icalendar-all-agenda-files
  2630. org-export-icalendar-combine-agenda-files))
  2631. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2632. ;; Declare and autoload functions from org-agenda.el
  2633. (eval-and-compile
  2634. (org-autoload "org-agenda"
  2635. '(org-agenda org-agenda-list org-search-view
  2636. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2637. org-diary org-agenda-to-appt
  2638. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2639. ;; Autoload org-remember
  2640. (eval-and-compile
  2641. (org-autoload "org-remember"
  2642. '(org-remember-insinuate org-remember-annotation
  2643. org-remember-apply-template org-remember org-remember-handler)))
  2644. ;; Autoload org-clock.el
  2645. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2646. (beg end))
  2647. (declare-function org-clock-update-mode-line "org-clock" ())
  2648. (defvar org-clock-start-time)
  2649. (defvar org-clock-marker (make-marker)
  2650. "Marker recording the last clock-in.")
  2651. (eval-and-compile
  2652. (org-autoload
  2653. "org-clock"
  2654. '(org-clock-in org-clock-out org-clock-cancel
  2655. org-clock-goto org-clock-sum org-clock-display
  2656. org-clock-remove-overlays org-clock-report
  2657. org-clocktable-shift org-dblock-write:clocktable
  2658. org-get-clocktable)))
  2659. (defun org-clock-update-time-maybe ()
  2660. "If this is a CLOCK line, update it and return t.
  2661. Otherwise, return nil."
  2662. (interactive)
  2663. (save-excursion
  2664. (beginning-of-line 1)
  2665. (skip-chars-forward " \t")
  2666. (when (looking-at org-clock-string)
  2667. (let ((re (concat "[ \t]*" org-clock-string
  2668. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2669. "\\([ \t]*=>.*\\)?\\)?"))
  2670. ts te h m s neg)
  2671. (cond
  2672. ((not (looking-at re))
  2673. nil)
  2674. ((not (match-end 2))
  2675. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2676. (> org-clock-marker (point))
  2677. (<= org-clock-marker (point-at-eol)))
  2678. ;; The clock is running here
  2679. (setq org-clock-start-time
  2680. (apply 'encode-time
  2681. (org-parse-time-string (match-string 1))))
  2682. (org-clock-update-mode-line)))
  2683. (t
  2684. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2685. (end-of-line 1)
  2686. (setq ts (match-string 1)
  2687. te (match-string 3))
  2688. (setq s (- (time-to-seconds
  2689. (apply 'encode-time (org-parse-time-string te)))
  2690. (time-to-seconds
  2691. (apply 'encode-time (org-parse-time-string ts))))
  2692. neg (< s 0)
  2693. s (abs s)
  2694. h (floor (/ s 3600))
  2695. s (- s (* 3600 h))
  2696. m (floor (/ s 60))
  2697. s (- s (* 60 s)))
  2698. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2699. t))))))
  2700. (defun org-check-running-clock ()
  2701. "Check if the current buffer contains the running clock.
  2702. If yes, offer to stop it and to save the buffer with the changes."
  2703. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2704. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2705. (buffer-name))))
  2706. (org-clock-out)
  2707. (when (y-or-n-p "Save changed buffer?")
  2708. (save-buffer))))
  2709. (defun org-clocktable-try-shift (dir n)
  2710. "Check if this line starts a clock table, if yes, shift the time block."
  2711. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2712. (org-clocktable-shift dir n)))
  2713. ;; Autoload org-timer.el
  2714. (eval-and-compile
  2715. (org-autoload
  2716. "org-timer"
  2717. '(org-timer-start org-timer org-timer-item
  2718. org-timer-change-times-in-region)))
  2719. ;; Autoload org-feed.el
  2720. (eval-and-compile
  2721. (org-autoload
  2722. "org-feed"
  2723. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2724. ;; Autoload archiving code
  2725. ;; The stuff that is needed for cycling and tags has to be defined here.
  2726. (defgroup org-archive nil
  2727. "Options concerning archiving in Org-mode."
  2728. :tag "Org Archive"
  2729. :group 'org-structure)
  2730. (defcustom org-archive-location "%s_archive::"
  2731. "The location where subtrees should be archived.
  2732. The value of this variable is a string, consisting of two parts,
  2733. separated by a double-colon. The first part is a filename and
  2734. the second part is a headline.
  2735. When the filename is omitted, archiving happens in the same file.
  2736. %s in the filename will be replaced by the current file
  2737. name (without the directory part). Archiving to a different file
  2738. is useful to keep archived entries from contributing to the
  2739. Org-mode Agenda.
  2740. The archived entries will be filed as subtrees of the specified
  2741. headline. When the headline is omitted, the subtrees are simply
  2742. filed away at the end of the file, as top-level entries. Also in
  2743. the heading you can use %s to represent the file name, this can be
  2744. useful when using the same archive for a number of different files.
  2745. Here are a few examples:
  2746. \"%s_archive::\"
  2747. If the current file is Projects.org, archive in file
  2748. Projects.org_archive, as top-level trees. This is the default.
  2749. \"::* Archived Tasks\"
  2750. Archive in the current file, under the top-level headline
  2751. \"* Archived Tasks\".
  2752. \"~/org/archive.org::\"
  2753. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2754. \"~/org/archive.org::From %s\"
  2755. Archive in file ~/org/archive.org (absolute path), und headlines
  2756. \"From FILENAME\" where file name is the current file name.
  2757. \"basement::** Finished Tasks\"
  2758. Archive in file ./basement (relative path), as level 3 trees
  2759. below the level 2 heading \"** Finished Tasks\".
  2760. You may set this option on a per-file basis by adding to the buffer a
  2761. line like
  2762. #+ARCHIVE: basement::** Finished Tasks
  2763. You may also define it locally for a subtree by setting an ARCHIVE property
  2764. in the entry. If such a property is found in an entry, or anywhere up
  2765. the hierarchy, it will be used."
  2766. :group 'org-archive
  2767. :type 'string)
  2768. (defcustom org-archive-tag "ARCHIVE"
  2769. "The tag that marks a subtree as archived.
  2770. An archived subtree does not open during visibility cycling, and does
  2771. not contribute to the agenda listings.
  2772. After changing this, font-lock must be restarted in the relevant buffers to
  2773. get the proper fontification."
  2774. :group 'org-archive
  2775. :group 'org-keywords
  2776. :type 'string)
  2777. (defcustom org-agenda-skip-archived-trees t
  2778. "Non-nil means, the agenda will skip any items located in archived trees.
  2779. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2780. variable is no longer recommended, you should leave it at the value t.
  2781. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2782. :group 'org-archive
  2783. :group 'org-agenda-skip
  2784. :type 'boolean)
  2785. (defcustom org-columns-skip-arrchived-trees t
  2786. "Non-nil means, irgnore archived trees when creating column view."
  2787. :group 'org-archive
  2788. :group 'org-properties
  2789. :type 'boolean)
  2790. (defcustom org-cycle-open-archived-trees nil
  2791. "Non-nil means, `org-cycle' will open archived trees.
  2792. An archived tree is a tree marked with the tag ARCHIVE.
  2793. When nil, archived trees will stay folded. You can still open them with
  2794. normal outline commands like `show-all', but not with the cycling commands."
  2795. :group 'org-archive
  2796. :group 'org-cycle
  2797. :type 'boolean)
  2798. (defcustom org-sparse-tree-open-archived-trees nil
  2799. "Non-nil means sparse tree construction shows matches in archived trees.
  2800. When nil, matches in these trees are highlighted, but the trees are kept in
  2801. collapsed state."
  2802. :group 'org-archive
  2803. :group 'org-sparse-trees
  2804. :type 'boolean)
  2805. (defun org-cycle-hide-archived-subtrees (state)
  2806. "Re-hide all archived subtrees after a visibility state change."
  2807. (when (and (not org-cycle-open-archived-trees)
  2808. (not (memq state '(overview folded))))
  2809. (save-excursion
  2810. (let* ((globalp (memq state '(contents all)))
  2811. (beg (if globalp (point-min) (point)))
  2812. (end (if globalp (point-max) (org-end-of-subtree t))))
  2813. (org-hide-archived-subtrees beg end)
  2814. (goto-char beg)
  2815. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2816. (message "%s" (substitute-command-keys
  2817. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2818. (defun org-force-cycle-archived ()
  2819. "Cycle subtree even if it is archived."
  2820. (interactive)
  2821. (setq this-command 'org-cycle)
  2822. (let ((org-cycle-open-archived-trees t))
  2823. (call-interactively 'org-cycle)))
  2824. (defun org-hide-archived-subtrees (beg end)
  2825. "Re-hide all archived subtrees after a visibility state change."
  2826. (save-excursion
  2827. (let* ((re (concat ":" org-archive-tag ":")))
  2828. (goto-char beg)
  2829. (while (re-search-forward re end t)
  2830. (and (org-on-heading-p) (hide-subtree))
  2831. (org-end-of-subtree t)))))
  2832. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2833. (eval-and-compile
  2834. (org-autoload "org-archive"
  2835. '(org-add-archive-files org-archive-subtree
  2836. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2837. ;; Autoload Column View Code
  2838. (declare-function org-columns-number-to-string "org-colview")
  2839. (declare-function org-columns-get-format-and-top-level "org-colview")
  2840. (declare-function org-columns-compute "org-colview")
  2841. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2842. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2843. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2844. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2845. ;; Autoload ID code
  2846. (declare-function org-id-store-link "org-id")
  2847. (declare-function org-id-locations-load "org-id")
  2848. (declare-function org-id-locations-save "org-id")
  2849. (defvar org-id-track-globally)
  2850. (org-autoload "org-id"
  2851. '(org-id-get-create org-id-new org-id-copy org-id-get
  2852. org-id-get-with-outline-path-completion
  2853. org-id-get-with-outline-drilling
  2854. org-id-goto org-id-find org-id-store-link))
  2855. ;; Autoload Plotting Code
  2856. (org-autoload "org-plot"
  2857. '(org-plot/gnuplot))
  2858. ;;; Variables for pre-computed regular expressions, all buffer local
  2859. (defvar org-drawer-regexp nil
  2860. "Matches first line of a hidden block.")
  2861. (make-variable-buffer-local 'org-drawer-regexp)
  2862. (defvar org-todo-regexp nil
  2863. "Matches any of the TODO state keywords.")
  2864. (make-variable-buffer-local 'org-todo-regexp)
  2865. (defvar org-not-done-regexp nil
  2866. "Matches any of the TODO state keywords except the last one.")
  2867. (make-variable-buffer-local 'org-not-done-regexp)
  2868. (defvar org-not-done-heading-regexp nil
  2869. "Matches a TODO headline that is not done.")
  2870. (make-variable-buffer-local 'org-not-done-regexp)
  2871. (defvar org-todo-line-regexp nil
  2872. "Matches a headline and puts TODO state into group 2 if present.")
  2873. (make-variable-buffer-local 'org-todo-line-regexp)
  2874. (defvar org-complex-heading-regexp nil
  2875. "Matches a headline and puts everything into groups:
  2876. group 1: the stars
  2877. group 2: The todo keyword, maybe
  2878. group 3: Priority cookie
  2879. group 4: True headline
  2880. group 5: Tags")
  2881. (make-variable-buffer-local 'org-complex-heading-regexp)
  2882. (defvar org-todo-line-tags-regexp nil
  2883. "Matches a headline and puts TODO state into group 2 if present.
  2884. Also put tags into group 4 if tags are present.")
  2885. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2886. (defvar org-nl-done-regexp nil
  2887. "Matches newline followed by a headline with the DONE keyword.")
  2888. (make-variable-buffer-local 'org-nl-done-regexp)
  2889. (defvar org-looking-at-done-regexp nil
  2890. "Matches the DONE keyword a point.")
  2891. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2892. (defvar org-ds-keyword-length 12
  2893. "Maximum length of the Deadline and SCHEDULED keywords.")
  2894. (make-variable-buffer-local 'org-ds-keyword-length)
  2895. (defvar org-deadline-regexp nil
  2896. "Matches the DEADLINE keyword.")
  2897. (make-variable-buffer-local 'org-deadline-regexp)
  2898. (defvar org-deadline-time-regexp nil
  2899. "Matches the DEADLINE keyword together with a time stamp.")
  2900. (make-variable-buffer-local 'org-deadline-time-regexp)
  2901. (defvar org-deadline-line-regexp nil
  2902. "Matches the DEADLINE keyword and the rest of the line.")
  2903. (make-variable-buffer-local 'org-deadline-line-regexp)
  2904. (defvar org-scheduled-regexp nil
  2905. "Matches the SCHEDULED keyword.")
  2906. (make-variable-buffer-local 'org-scheduled-regexp)
  2907. (defvar org-scheduled-time-regexp nil
  2908. "Matches the SCHEDULED keyword together with a time stamp.")
  2909. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2910. (defvar org-closed-time-regexp nil
  2911. "Matches the CLOSED keyword together with a time stamp.")
  2912. (make-variable-buffer-local 'org-closed-time-regexp)
  2913. (defvar org-keyword-time-regexp nil
  2914. "Matches any of the 4 keywords, together with the time stamp.")
  2915. (make-variable-buffer-local 'org-keyword-time-regexp)
  2916. (defvar org-keyword-time-not-clock-regexp nil
  2917. "Matches any of the 3 keywords, together with the time stamp.")
  2918. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2919. (defvar org-maybe-keyword-time-regexp nil
  2920. "Matches a timestamp, possibly preceeded by a keyword.")
  2921. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2922. (defvar org-planning-or-clock-line-re nil
  2923. "Matches a line with planning or clock info.")
  2924. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2925. (defconst org-plain-time-of-day-regexp
  2926. (concat
  2927. "\\(\\<[012]?[0-9]"
  2928. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2929. "\\(--?"
  2930. "\\(\\<[012]?[0-9]"
  2931. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2932. "\\)?")
  2933. "Regular expression to match a plain time or time range.
  2934. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2935. groups carry important information:
  2936. 0 the full match
  2937. 1 the first time, range or not
  2938. 8 the second time, if it is a range.")
  2939. (defconst org-plain-time-extension-regexp
  2940. (concat
  2941. "\\(\\<[012]?[0-9]"
  2942. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2943. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2944. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2945. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2946. groups carry important information:
  2947. 0 the full match
  2948. 7 hours of duration
  2949. 9 minutes of duration")
  2950. (defconst org-stamp-time-of-day-regexp
  2951. (concat
  2952. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2953. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2954. "\\(--?"
  2955. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2956. "Regular expression to match a timestamp time or time range.
  2957. After a match, the following groups carry important information:
  2958. 0 the full match
  2959. 1 date plus weekday, for backreferencing to make sure both times on same day
  2960. 2 the first time, range or not
  2961. 4 the second time, if it is a range.")
  2962. (defconst org-startup-options
  2963. '(("fold" org-startup-folded t)
  2964. ("overview" org-startup-folded t)
  2965. ("nofold" org-startup-folded nil)
  2966. ("showall" org-startup-folded nil)
  2967. ("content" org-startup-folded content)
  2968. ("hidestars" org-hide-leading-stars t)
  2969. ("showstars" org-hide-leading-stars nil)
  2970. ("odd" org-odd-levels-only t)
  2971. ("oddeven" org-odd-levels-only nil)
  2972. ("align" org-startup-align-all-tables t)
  2973. ("noalign" org-startup-align-all-tables nil)
  2974. ("customtime" org-display-custom-times t)
  2975. ("logdone" org-log-done time)
  2976. ("lognotedone" org-log-done note)
  2977. ("nologdone" org-log-done nil)
  2978. ("lognoteclock-out" org-log-note-clock-out t)
  2979. ("nolognoteclock-out" org-log-note-clock-out nil)
  2980. ("logrepeat" org-log-repeat state)
  2981. ("lognoterepeat" org-log-repeat note)
  2982. ("nologrepeat" org-log-repeat nil)
  2983. ("fninline" org-footnote-define-inline t)
  2984. ("nofninline" org-footnote-define-inline nil)
  2985. ("fnlocal" org-footnote-section nil)
  2986. ("fnauto" org-footnote-auto-label t)
  2987. ("fnprompt" org-footnote-auto-label nil)
  2988. ("fnconfirm" org-footnote-auto-label confirm)
  2989. ("fnplain" org-footnote-auto-label plain)
  2990. ("constcgs" constants-unit-system cgs)
  2991. ("constSI" constants-unit-system SI)
  2992. ("noptag" org-tag-persistent-alist nil))
  2993. "Variable associated with STARTUP options for org-mode.
  2994. Each element is a list of three items: The startup options as written
  2995. in the #+STARTUP line, the corresponding variable, and the value to
  2996. set this variable to if the option is found. An optional forth element PUSH
  2997. means to push this value onto the list in the variable.")
  2998. (defun org-set-regexps-and-options ()
  2999. "Precompute regular expressions for current buffer."
  3000. (when (org-mode-p)
  3001. (org-set-local 'org-todo-kwd-alist nil)
  3002. (org-set-local 'org-todo-key-alist nil)
  3003. (org-set-local 'org-todo-key-trigger nil)
  3004. (org-set-local 'org-todo-keywords-1 nil)
  3005. (org-set-local 'org-done-keywords nil)
  3006. (org-set-local 'org-todo-heads nil)
  3007. (org-set-local 'org-todo-sets nil)
  3008. (org-set-local 'org-todo-log-states nil)
  3009. (org-set-local 'org-file-properties nil)
  3010. (org-set-local 'org-file-tags nil)
  3011. (let ((re (org-make-options-regexp
  3012. '("CATEGORY" "TODO" "COLUMNS"
  3013. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3014. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3015. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3016. (splitre "[ \t]+")
  3017. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3018. tail sep kws1 prio props ftags drawers
  3019. ext-setup-or-nil setup-contents (start 0))
  3020. (save-excursion
  3021. (save-restriction
  3022. (widen)
  3023. (goto-char (point-min))
  3024. (while (or (and ext-setup-or-nil
  3025. (string-match re ext-setup-or-nil start)
  3026. (setq start (match-end 0)))
  3027. (and (setq ext-setup-or-nil nil start 0)
  3028. (re-search-forward re nil t)))
  3029. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3030. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3031. (cond
  3032. ((equal key "CATEGORY")
  3033. (if (string-match "[ \t]+$" value)
  3034. (setq value (replace-match "" t t value)))
  3035. (setq cat value))
  3036. ((member key '("SEQ_TODO" "TODO"))
  3037. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3038. ((equal key "TYP_TODO")
  3039. (push (cons 'type (org-split-string value splitre)) kwds))
  3040. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3041. ;; general TODO-like setup
  3042. (push (cons (intern (downcase (match-string 1 key)))
  3043. (org-split-string value splitre)) kwds))
  3044. ((equal key "TAGS")
  3045. (setq tags (append tags (if tags '("\\n") nil)
  3046. (org-split-string value splitre))))
  3047. ((equal key "COLUMNS")
  3048. (org-set-local 'org-columns-default-format value))
  3049. ((equal key "LINK")
  3050. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3051. (push (cons (match-string 1 value)
  3052. (org-trim (match-string 2 value)))
  3053. links)))
  3054. ((equal key "PRIORITIES")
  3055. (setq prio (org-split-string value " +")))
  3056. ((equal key "PROPERTY")
  3057. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3058. (push (cons (match-string 1 value) (match-string 2 value))
  3059. props)))
  3060. ((equal key "FILETAGS")
  3061. (when (string-match "\\S-" value)
  3062. (setq ftags
  3063. (append
  3064. ftags
  3065. (apply 'append
  3066. (mapcar (lambda (x) (org-split-string x ":"))
  3067. (org-split-string value)))))))
  3068. ((equal key "DRAWERS")
  3069. (setq drawers (org-split-string value splitre)))
  3070. ((equal key "CONSTANTS")
  3071. (setq const (append const (org-split-string value splitre))))
  3072. ((equal key "STARTUP")
  3073. (let ((opts (org-split-string value splitre))
  3074. l var val)
  3075. (while (setq l (pop opts))
  3076. (when (setq l (assoc l org-startup-options))
  3077. (setq var (nth 1 l) val (nth 2 l))
  3078. (if (not (nth 3 l))
  3079. (set (make-local-variable var) val)
  3080. (if (not (listp (symbol-value var)))
  3081. (set (make-local-variable var) nil))
  3082. (set (make-local-variable var) (symbol-value var))
  3083. (add-to-list var val))))))
  3084. ((equal key "ARCHIVE")
  3085. (string-match " *$" value)
  3086. (setq arch (replace-match "" t t value))
  3087. (remove-text-properties 0 (length arch)
  3088. '(face t fontified t) arch))
  3089. ((equal key "SETUPFILE")
  3090. (setq setup-contents (org-file-contents
  3091. (expand-file-name
  3092. (org-remove-double-quotes value))
  3093. 'noerror))
  3094. (if (not ext-setup-or-nil)
  3095. (setq ext-setup-or-nil setup-contents start 0)
  3096. (setq ext-setup-or-nil
  3097. (concat (substring ext-setup-or-nil 0 start)
  3098. "\n" setup-contents "\n"
  3099. (substring ext-setup-or-nil start)))))
  3100. ))))
  3101. (when cat
  3102. (org-set-local 'org-category (intern cat))
  3103. (push (cons "CATEGORY" cat) props))
  3104. (when prio
  3105. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3106. (setq prio (mapcar 'string-to-char prio))
  3107. (org-set-local 'org-highest-priority (nth 0 prio))
  3108. (org-set-local 'org-lowest-priority (nth 1 prio))
  3109. (org-set-local 'org-default-priority (nth 2 prio)))
  3110. (and props (org-set-local 'org-file-properties (nreverse props)))
  3111. (and ftags (org-set-local 'org-file-tags
  3112. (mapcar 'org-add-prop-inherited ftags)))
  3113. (and drawers (org-set-local 'org-drawers drawers))
  3114. (and arch (org-set-local 'org-archive-location arch))
  3115. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3116. ;; Process the TODO keywords
  3117. (unless kwds
  3118. ;; Use the global values as if they had been given locally.
  3119. (setq kwds (default-value 'org-todo-keywords))
  3120. (if (stringp (car kwds))
  3121. (setq kwds (list (cons org-todo-interpretation
  3122. (default-value 'org-todo-keywords)))))
  3123. (setq kwds (reverse kwds)))
  3124. (setq kwds (nreverse kwds))
  3125. (let (inter kws kw)
  3126. (while (setq kws (pop kwds))
  3127. (let ((kws (or
  3128. (run-hook-with-args-until-success
  3129. 'org-todo-setup-filter-hook kws)
  3130. kws)))
  3131. (setq inter (pop kws) sep (member "|" kws)
  3132. kws0 (delete "|" (copy-sequence kws))
  3133. kwsa nil
  3134. kws1 (mapcar
  3135. (lambda (x)
  3136. ;; 1 2
  3137. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3138. (progn
  3139. (setq kw (match-string 1 x)
  3140. key (and (match-end 2) (match-string 2 x))
  3141. log (org-extract-log-state-settings x))
  3142. (push (cons kw (and key (string-to-char key))) kwsa)
  3143. (and log (push log org-todo-log-states))
  3144. kw)
  3145. (error "Invalid TODO keyword %s" x)))
  3146. kws0)
  3147. kwsa (if kwsa (append '((:startgroup))
  3148. (nreverse kwsa)
  3149. '((:endgroup))))
  3150. hw (car kws1)
  3151. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3152. tail (list inter hw (car dws) (org-last dws))))
  3153. (add-to-list 'org-todo-heads hw 'append)
  3154. (push kws1 org-todo-sets)
  3155. (setq org-done-keywords (append org-done-keywords dws nil))
  3156. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3157. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3158. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3159. (setq org-todo-sets (nreverse org-todo-sets)
  3160. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3161. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3162. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3163. ;; Process the constants
  3164. (when const
  3165. (let (e cst)
  3166. (while (setq e (pop const))
  3167. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3168. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3169. (setq org-table-formula-constants-local cst)))
  3170. ;; Process the tags.
  3171. (when tags
  3172. (let (e tgs)
  3173. (while (setq e (pop tags))
  3174. (cond
  3175. ((equal e "{") (push '(:startgroup) tgs))
  3176. ((equal e "}") (push '(:endgroup) tgs))
  3177. ((equal e "\\n") (push '(:newline) tgs))
  3178. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3179. (push (cons (match-string 1 e)
  3180. (string-to-char (match-string 2 e)))
  3181. tgs))
  3182. (t (push (list e) tgs))))
  3183. (org-set-local 'org-tag-alist nil)
  3184. (while (setq e (pop tgs))
  3185. (or (and (stringp (car e))
  3186. (assoc (car e) org-tag-alist))
  3187. (push e org-tag-alist)))))
  3188. ;; Compute the regular expressions and other local variables
  3189. (if (not org-done-keywords)
  3190. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3191. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3192. (length org-scheduled-string)
  3193. (length org-clock-string)
  3194. (length org-closed-string)))
  3195. org-drawer-regexp
  3196. (concat "^[ \t]*:\\("
  3197. (mapconcat 'regexp-quote org-drawers "\\|")
  3198. "\\):[ \t]*$")
  3199. org-not-done-keywords
  3200. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3201. org-todo-regexp
  3202. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3203. "\\|") "\\)\\>")
  3204. org-not-done-regexp
  3205. (concat "\\<\\("
  3206. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3207. "\\)\\>")
  3208. org-not-done-heading-regexp
  3209. (concat "^\\(\\*+\\)[ \t]+\\("
  3210. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3211. "\\)\\>")
  3212. org-todo-line-regexp
  3213. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3214. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3215. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3216. org-complex-heading-regexp
  3217. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3218. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3219. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3220. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3221. org-nl-done-regexp
  3222. (concat "\n\\*+[ \t]+"
  3223. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3224. "\\)" "\\>")
  3225. org-todo-line-tags-regexp
  3226. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3227. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3228. (org-re
  3229. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3230. org-looking-at-done-regexp
  3231. (concat "^" "\\(?:"
  3232. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3233. "\\>")
  3234. org-deadline-regexp (concat "\\<" org-deadline-string)
  3235. org-deadline-time-regexp
  3236. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3237. org-deadline-line-regexp
  3238. (concat "\\<\\(" org-deadline-string "\\).*")
  3239. org-scheduled-regexp
  3240. (concat "\\<" org-scheduled-string)
  3241. org-scheduled-time-regexp
  3242. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3243. org-closed-time-regexp
  3244. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3245. org-keyword-time-regexp
  3246. (concat "\\<\\(" org-scheduled-string
  3247. "\\|" org-deadline-string
  3248. "\\|" org-closed-string
  3249. "\\|" org-clock-string "\\)"
  3250. " *[[<]\\([^]>]+\\)[]>]")
  3251. org-keyword-time-not-clock-regexp
  3252. (concat "\\<\\(" org-scheduled-string
  3253. "\\|" org-deadline-string
  3254. "\\|" org-closed-string
  3255. "\\)"
  3256. " *[[<]\\([^]>]+\\)[]>]")
  3257. org-maybe-keyword-time-regexp
  3258. (concat "\\(\\<\\(" org-scheduled-string
  3259. "\\|" org-deadline-string
  3260. "\\|" org-closed-string
  3261. "\\|" org-clock-string "\\)\\)?"
  3262. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3263. org-planning-or-clock-line-re
  3264. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3265. "\\|" org-deadline-string
  3266. "\\|" org-closed-string "\\|" org-clock-string
  3267. "\\)\\>\\)")
  3268. )
  3269. (org-compute-latex-and-specials-regexp)
  3270. (org-set-font-lock-defaults))))
  3271. (defun org-file-contents (file &optional noerror)
  3272. "Return the contents of FILE, as a string."
  3273. (if (or (not file)
  3274. (not (file-readable-p file)))
  3275. (if noerror
  3276. (progn
  3277. (message "Cannot read file %s" file)
  3278. (ding) (sit-for 2)
  3279. "")
  3280. (error "Cannot read file %s" file))
  3281. (with-temp-buffer
  3282. (insert-file-contents file)
  3283. (buffer-string))))
  3284. (defun org-extract-log-state-settings (x)
  3285. "Extract the log state setting from a TODO keyword string.
  3286. This will extract info from a string like \"WAIT(w@/!)\"."
  3287. (let (kw key log1 log2)
  3288. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3289. (setq kw (match-string 1 x)
  3290. key (and (match-end 2) (match-string 2 x))
  3291. log1 (and (match-end 3) (match-string 3 x))
  3292. log2 (and (match-end 4) (match-string 4 x)))
  3293. (and (or log1 log2)
  3294. (list kw
  3295. (and log1 (if (equal log1 "!") 'time 'note))
  3296. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3297. (defun org-remove-keyword-keys (list)
  3298. "Remove a pair of parenthesis at the end of each string in LIST."
  3299. (mapcar (lambda (x)
  3300. (if (string-match "(.*)$" x)
  3301. (substring x 0 (match-beginning 0))
  3302. x))
  3303. list))
  3304. ;; FIXME: this could be done much better, using second characters etc.
  3305. (defun org-assign-fast-keys (alist)
  3306. "Assign fast keys to a keyword-key alist.
  3307. Respect keys that are already there."
  3308. (let (new e k c c1 c2 (char ?a))
  3309. (while (setq e (pop alist))
  3310. (cond
  3311. ((equal e '(:startgroup)) (push e new))
  3312. ((equal e '(:endgroup)) (push e new))
  3313. ((equal e '(:newline)) (push e new))
  3314. (t
  3315. (setq k (car e) c2 nil)
  3316. (if (cdr e)
  3317. (setq c (cdr e))
  3318. ;; automatically assign a character.
  3319. (setq c1 (string-to-char
  3320. (downcase (substring
  3321. k (if (= (string-to-char k) ?@) 1 0)))))
  3322. (if (or (rassoc c1 new) (rassoc c1 alist))
  3323. (while (or (rassoc char new) (rassoc char alist))
  3324. (setq char (1+ char)))
  3325. (setq c2 c1))
  3326. (setq c (or c2 char)))
  3327. (push (cons k c) new))))
  3328. (nreverse new)))
  3329. ;;; Some variables used in various places
  3330. (defvar org-window-configuration nil
  3331. "Used in various places to store a window configuration.")
  3332. (defvar org-finish-function nil
  3333. "Function to be called when `C-c C-c' is used.
  3334. This is for getting out of special buffers like remember.")
  3335. ;; FIXME: Occasionally check by commenting these, to make sure
  3336. ;; no other functions uses these, forgetting to let-bind them.
  3337. (defvar entry)
  3338. (defvar state)
  3339. (defvar last-state)
  3340. (defvar date)
  3341. (defvar description)
  3342. ;; Defined somewhere in this file, but used before definition.
  3343. (defvar org-html-entities)
  3344. (defvar org-struct-menu)
  3345. (defvar org-org-menu)
  3346. (defvar org-tbl-menu)
  3347. (defvar org-agenda-keymap)
  3348. ;;;; Define the Org-mode
  3349. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3350. (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."))
  3351. ;; We use a before-change function to check if a table might need
  3352. ;; an update.
  3353. (defvar org-table-may-need-update t
  3354. "Indicates that a table might need an update.
  3355. This variable is set by `org-before-change-function'.
  3356. `org-table-align' sets it back to nil.")
  3357. (defun org-before-change-function (beg end)
  3358. "Every change indicates that a table might need an update."
  3359. (setq org-table-may-need-update t))
  3360. (defvar org-mode-map)
  3361. (defvar org-mode-hook nil
  3362. "Mode hook for Org-mode, run after the mode was turned on.")
  3363. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3364. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3365. (defvar org-table-buffer-is-an nil)
  3366. (defconst org-outline-regexp "\\*+ ")
  3367. ;;;###autoload
  3368. (define-derived-mode org-mode outline-mode "Org"
  3369. "Outline-based notes management and organizer, alias
  3370. \"Carsten's outline-mode for keeping track of everything.\"
  3371. Org-mode develops organizational tasks around a NOTES file which
  3372. contains information about projects as plain text. Org-mode is
  3373. implemented on top of outline-mode, which is ideal to keep the content
  3374. of large files well structured. It supports ToDo items, deadlines and
  3375. time stamps, which magically appear in the diary listing of the Emacs
  3376. calendar. Tables are easily created with a built-in table editor.
  3377. Plain text URL-like links connect to websites, emails (VM), Usenet
  3378. messages (Gnus), BBDB entries, and any files related to the project.
  3379. For printing and sharing of notes, an Org-mode file (or a part of it)
  3380. can be exported as a structured ASCII or HTML file.
  3381. The following commands are available:
  3382. \\{org-mode-map}"
  3383. ;; Get rid of Outline menus, they are not needed
  3384. ;; Need to do this here because define-derived-mode sets up
  3385. ;; the keymap so late. Still, it is a waste to call this each time
  3386. ;; we switch another buffer into org-mode.
  3387. (if (featurep 'xemacs)
  3388. (when (boundp 'outline-mode-menu-heading)
  3389. ;; Assume this is Greg's port, it used easymenu
  3390. (easy-menu-remove outline-mode-menu-heading)
  3391. (easy-menu-remove outline-mode-menu-show)
  3392. (easy-menu-remove outline-mode-menu-hide))
  3393. (define-key org-mode-map [menu-bar headings] 'undefined)
  3394. (define-key org-mode-map [menu-bar hide] 'undefined)
  3395. (define-key org-mode-map [menu-bar show] 'undefined))
  3396. (org-load-modules-maybe)
  3397. (easy-menu-add org-org-menu)
  3398. (easy-menu-add org-tbl-menu)
  3399. (org-install-agenda-files-menu)
  3400. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3401. (org-add-to-invisibility-spec '(org-cwidth))
  3402. (when (featurep 'xemacs)
  3403. (org-set-local 'line-move-ignore-invisible t))
  3404. (org-set-local 'outline-regexp org-outline-regexp)
  3405. (org-set-local 'outline-level 'org-outline-level)
  3406. (when (and org-ellipsis
  3407. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3408. (fboundp 'make-glyph-code))
  3409. (unless org-display-table
  3410. (setq org-display-table (make-display-table)))
  3411. (set-display-table-slot
  3412. org-display-table 4
  3413. (vconcat (mapcar
  3414. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3415. org-ellipsis)))
  3416. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3417. (setq buffer-display-table org-display-table))
  3418. (org-set-regexps-and-options)
  3419. (when (and org-tag-faces (not org-tags-special-faces-re))
  3420. ;; tag faces set outside customize.... force initialization.
  3421. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3422. ;; Calc embedded
  3423. (org-set-local 'calc-embedded-open-mode "# ")
  3424. (modify-syntax-entry ?# "<")
  3425. (modify-syntax-entry ?@ "w")
  3426. (if org-startup-truncated (setq truncate-lines t))
  3427. (org-set-local 'font-lock-unfontify-region-function
  3428. 'org-unfontify-region)
  3429. ;; Activate before-change-function
  3430. (org-set-local 'org-table-may-need-update t)
  3431. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3432. 'local)
  3433. ;; Check for running clock before killing a buffer
  3434. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3435. ;; Paragraphs and auto-filling
  3436. (org-set-autofill-regexps)
  3437. (setq indent-line-function 'org-indent-line-function)
  3438. (org-update-radio-target-regexp)
  3439. ;; Make sure dependence stuff works reliably, even for users who set it
  3440. ;; too late :-(
  3441. (if org-enforce-todo-dependencies
  3442. (add-hook 'org-blocker-hook
  3443. 'org-block-todo-from-children-or-siblings-or-parent)
  3444. (remove-hook 'org-blocker-hook
  3445. 'org-block-todo-from-children-or-siblings-or-parent))
  3446. (if org-enforce-todo-checkbox-dependencies
  3447. (add-hook 'org-blocker-hook
  3448. 'org-block-todo-from-checkboxes)
  3449. (remove-hook 'org-blocker-hook
  3450. 'org-block-todo-from-checkboxes))
  3451. ;; Comment characters
  3452. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3453. (org-set-local 'comment-padding " ")
  3454. ;; Align options lines
  3455. (org-set-local
  3456. 'align-mode-rules-list
  3457. '((org-in-buffer-settings
  3458. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3459. (modes . '(org-mode)))))
  3460. ;; Imenu
  3461. (org-set-local 'imenu-create-index-function
  3462. 'org-imenu-get-tree)
  3463. ;; Make isearch reveal context
  3464. (if (or (featurep 'xemacs)
  3465. (not (boundp 'outline-isearch-open-invisible-function)))
  3466. ;; Emacs 21 and XEmacs make use of the hook
  3467. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3468. ;; Emacs 22 deals with this through a special variable
  3469. (org-set-local 'outline-isearch-open-invisible-function
  3470. (lambda (&rest ignore) (org-show-context 'isearch))))
  3471. ;; If empty file that did not turn on org-mode automatically, make it to.
  3472. (if (and org-insert-mode-line-in-empty-file
  3473. (interactive-p)
  3474. (= (point-min) (point-max)))
  3475. (insert "# -*- mode: org -*-\n\n"))
  3476. (unless org-inhibit-startup
  3477. (when org-startup-align-all-tables
  3478. (let ((bmp (buffer-modified-p)))
  3479. (org-table-map-tables 'org-table-align)
  3480. (set-buffer-modified-p bmp)))
  3481. (org-set-startup-visibility)))
  3482. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3483. (defun org-current-time ()
  3484. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3485. (if (> (car org-time-stamp-rounding-minutes) 1)
  3486. (let ((r (car org-time-stamp-rounding-minutes))
  3487. (time (decode-time)))
  3488. (apply 'encode-time
  3489. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3490. (nthcdr 2 time))))
  3491. (current-time)))
  3492. ;;;; Font-Lock stuff, including the activators
  3493. (defvar org-mouse-map (make-sparse-keymap))
  3494. (org-defkey org-mouse-map
  3495. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3496. (org-defkey org-mouse-map
  3497. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3498. (when org-mouse-1-follows-link
  3499. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3500. (when org-tab-follows-link
  3501. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3502. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3503. (require 'font-lock)
  3504. (defconst org-non-link-chars "]\t\n\r<>")
  3505. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3506. "shell" "elisp"))
  3507. (defvar org-link-types-re nil
  3508. "Matches a link that has a url-like prefix like \"http:\"")
  3509. (defvar org-link-re-with-space nil
  3510. "Matches a link with spaces, optional angular brackets around it.")
  3511. (defvar org-link-re-with-space2 nil
  3512. "Matches a link with spaces, optional angular brackets around it.")
  3513. (defvar org-link-re-with-space3 nil
  3514. "Matches a link with spaces, only for internal part in bracket links.")
  3515. (defvar org-angle-link-re nil
  3516. "Matches link with angular brackets, spaces are allowed.")
  3517. (defvar org-plain-link-re nil
  3518. "Matches plain link, without spaces.")
  3519. (defvar org-bracket-link-regexp nil
  3520. "Matches a link in double brackets.")
  3521. (defvar org-bracket-link-analytic-regexp nil
  3522. "Regular expression used to analyze links.
  3523. Here is what the match groups contain after a match:
  3524. 1: http:
  3525. 2: http
  3526. 3: path
  3527. 4: [desc]
  3528. 5: desc")
  3529. (defvar org-bracket-link-analytic-regexp++ nil
  3530. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3531. (defvar org-any-link-re nil
  3532. "Regular expression matching any link.")
  3533. (defun org-make-link-regexps ()
  3534. "Update the link regular expressions.
  3535. This should be called after the variable `org-link-types' has changed."
  3536. (setq org-link-types-re
  3537. (concat
  3538. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3539. org-link-re-with-space
  3540. (concat
  3541. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3542. "\\([^" org-non-link-chars " ]"
  3543. "[^" org-non-link-chars "]*"
  3544. "[^" org-non-link-chars " ]\\)>?")
  3545. org-link-re-with-space2
  3546. (concat
  3547. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3548. "\\([^" org-non-link-chars " ]"
  3549. "[^\t\n\r]*"
  3550. "[^" org-non-link-chars " ]\\)>?")
  3551. org-link-re-with-space3
  3552. (concat
  3553. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3554. "\\([^" org-non-link-chars " ]"
  3555. "[^\t\n\r]*\\)")
  3556. org-angle-link-re
  3557. (concat
  3558. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3559. "\\([^" org-non-link-chars " ]"
  3560. "[^" org-non-link-chars "]*"
  3561. "\\)>")
  3562. org-plain-link-re
  3563. (concat
  3564. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3565. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3566. org-bracket-link-regexp
  3567. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3568. org-bracket-link-analytic-regexp
  3569. (concat
  3570. "\\[\\["
  3571. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3572. "\\([^]]+\\)"
  3573. "\\]"
  3574. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3575. "\\]")
  3576. org-bracket-link-analytic-regexp++
  3577. (concat
  3578. "\\[\\["
  3579. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3580. "\\([^]]+\\)"
  3581. "\\]"
  3582. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3583. "\\]")
  3584. org-any-link-re
  3585. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3586. org-angle-link-re "\\)\\|\\("
  3587. org-plain-link-re "\\)")))
  3588. (org-make-link-regexps)
  3589. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3590. "Regular expression for fast time stamp matching.")
  3591. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3592. "Regular expression for fast time stamp matching.")
  3593. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3594. "Regular expression matching time strings for analysis.
  3595. This one does not require the space after the date, so it can be used
  3596. on a string that terminates immediately after the date.")
  3597. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3598. "Regular expression matching time strings for analysis.")
  3599. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3600. "Regular expression matching time stamps, with groups.")
  3601. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3602. "Regular expression matching time stamps (also [..]), with groups.")
  3603. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3604. "Regular expression matching a time stamp range.")
  3605. (defconst org-tr-regexp-both
  3606. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3607. "Regular expression matching a time stamp range.")
  3608. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3609. org-ts-regexp "\\)?")
  3610. "Regular expression matching a time stamp or time stamp range.")
  3611. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3612. org-ts-regexp-both "\\)?")
  3613. "Regular expression matching a time stamp or time stamp range.
  3614. The time stamps may be either active or inactive.")
  3615. (defvar org-emph-face nil)
  3616. (defun org-do-emphasis-faces (limit)
  3617. "Run through the buffer and add overlays to links."
  3618. (let (rtn)
  3619. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3620. (if (not (= (char-after (match-beginning 3))
  3621. (char-after (match-beginning 4))))
  3622. (progn
  3623. (setq rtn t)
  3624. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3625. 'face
  3626. (nth 1 (assoc (match-string 3)
  3627. org-emphasis-alist)))
  3628. (add-text-properties (match-beginning 2) (match-end 2)
  3629. '(font-lock-multiline t))
  3630. (when org-hide-emphasis-markers
  3631. (add-text-properties (match-end 4) (match-beginning 5)
  3632. '(invisible org-link))
  3633. (add-text-properties (match-beginning 3) (match-end 3)
  3634. '(invisible org-link)))))
  3635. (backward-char 1))
  3636. rtn))
  3637. (defun org-emphasize (&optional char)
  3638. "Insert or change an emphasis, i.e. a font like bold or italic.
  3639. If there is an active region, change that region to a new emphasis.
  3640. If there is no region, just insert the marker characters and position
  3641. the cursor between them.
  3642. CHAR should be either the marker character, or the first character of the
  3643. HTML tag associated with that emphasis. If CHAR is a space, the means
  3644. to remove the emphasis of the selected region.
  3645. If char is not given (for example in an interactive call) it
  3646. will be prompted for."
  3647. (interactive)
  3648. (let ((eal org-emphasis-alist) e det
  3649. (erc org-emphasis-regexp-components)
  3650. (prompt "")
  3651. (string "") beg end move tag c s)
  3652. (if (org-region-active-p)
  3653. (setq beg (region-beginning) end (region-end)
  3654. string (buffer-substring beg end))
  3655. (setq move t))
  3656. (while (setq e (pop eal))
  3657. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3658. c (aref tag 0))
  3659. (push (cons c (string-to-char (car e))) det)
  3660. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3661. (substring tag 1)))))
  3662. (setq det (nreverse det))
  3663. (unless char
  3664. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3665. (setq char (read-char-exclusive)))
  3666. (setq char (or (cdr (assoc char det)) char))
  3667. (if (equal char ?\ )
  3668. (setq s "" move nil)
  3669. (unless (assoc (char-to-string char) org-emphasis-alist)
  3670. (error "No such emphasis marker: \"%c\"" char))
  3671. (setq s (char-to-string char)))
  3672. (while (and (> (length string) 1)
  3673. (equal (substring string 0 1) (substring string -1))
  3674. (assoc (substring string 0 1) org-emphasis-alist))
  3675. (setq string (substring string 1 -1)))
  3676. (setq string (concat s string s))
  3677. (if beg (delete-region beg end))
  3678. (unless (or (bolp)
  3679. (string-match (concat "[" (nth 0 erc) "\n]")
  3680. (char-to-string (char-before (point)))))
  3681. (insert " "))
  3682. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3683. (char-to-string (char-after (point))))
  3684. (insert " ") (backward-char 1))
  3685. (insert string)
  3686. (and move (backward-char 1))))
  3687. (defconst org-nonsticky-props
  3688. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3689. (defsubst org-rear-nonsticky-at (pos)
  3690. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3691. (defun org-activate-plain-links (limit)
  3692. "Run through the buffer and add overlays to links."
  3693. (catch 'exit
  3694. (let (f)
  3695. (while (re-search-forward org-plain-link-re limit t)
  3696. (setq f (get-text-property (match-beginning 0) 'face))
  3697. (if (or (eq f 'org-tag)
  3698. (and (listp f) (memq 'org-tag f)))
  3699. nil
  3700. (add-text-properties (match-beginning 0) (match-end 0)
  3701. (list 'mouse-face 'highlight
  3702. 'keymap org-mouse-map))
  3703. (org-rear-nonsticky-at (match-end 0))
  3704. (throw 'exit t))))))
  3705. (defun org-activate-code (limit)
  3706. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3707. (progn
  3708. (remove-text-properties (match-beginning 0) (match-end 0)
  3709. '(display t invisible t intangible t))
  3710. t)))
  3711. (defun org-activate-angle-links (limit)
  3712. "Run through the buffer and add overlays to links."
  3713. (if (re-search-forward org-angle-link-re limit t)
  3714. (progn
  3715. (add-text-properties (match-beginning 0) (match-end 0)
  3716. (list 'mouse-face 'highlight
  3717. 'keymap org-mouse-map))
  3718. (org-rear-nonsticky-at (match-end 0))
  3719. t)))
  3720. (defun org-activate-footnote-links (limit)
  3721. "Run through the buffer and add overlays to links."
  3722. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3723. limit t)
  3724. (progn
  3725. (add-text-properties (match-beginning 2) (match-end 2)
  3726. (list 'mouse-face 'highlight
  3727. 'keymap org-mouse-map
  3728. 'help-echo
  3729. (if (= (point-at-bol) (match-beginning 2))
  3730. "Footnote definition"
  3731. "Footnote reference")
  3732. ))
  3733. (org-rear-nonsticky-at (match-end 2))
  3734. t)))
  3735. (defun org-activate-bracket-links (limit)
  3736. "Run through the buffer and add overlays to bracketed links."
  3737. (if (re-search-forward org-bracket-link-regexp limit t)
  3738. (let* ((help (concat "LINK: "
  3739. (org-match-string-no-properties 1)))
  3740. ;; FIXME: above we should remove the escapes.
  3741. ;; but that requires another match, protecting match data,
  3742. ;; a lot of overhead for font-lock.
  3743. (ip (org-maybe-intangible
  3744. (list 'invisible 'org-link
  3745. 'keymap org-mouse-map 'mouse-face 'highlight
  3746. 'font-lock-multiline t 'help-echo help)))
  3747. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3748. 'font-lock-multiline t 'help-echo help)))
  3749. ;; We need to remove the invisible property here. Table narrowing
  3750. ;; may have made some of this invisible.
  3751. (remove-text-properties (match-beginning 0) (match-end 0)
  3752. '(invisible nil))
  3753. (if (match-end 3)
  3754. (progn
  3755. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3756. (org-rear-nonsticky-at (match-beginning 3))
  3757. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3758. (org-rear-nonsticky-at (match-end 3))
  3759. (add-text-properties (match-end 3) (match-end 0) ip)
  3760. (org-rear-nonsticky-at (match-end 0)))
  3761. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3762. (org-rear-nonsticky-at (match-beginning 1))
  3763. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3764. (org-rear-nonsticky-at (match-end 1))
  3765. (add-text-properties (match-end 1) (match-end 0) ip)
  3766. (org-rear-nonsticky-at (match-end 0)))
  3767. t)))
  3768. (defun org-activate-dates (limit)
  3769. "Run through the buffer and add overlays to dates."
  3770. (if (re-search-forward org-tsr-regexp-both limit t)
  3771. (progn
  3772. (add-text-properties (match-beginning 0) (match-end 0)
  3773. (list 'mouse-face 'highlight
  3774. 'keymap org-mouse-map))
  3775. (org-rear-nonsticky-at (match-end 0))
  3776. (when org-display-custom-times
  3777. (if (match-end 3)
  3778. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3779. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3780. t)))
  3781. (defvar org-target-link-regexp nil
  3782. "Regular expression matching radio targets in plain text.")
  3783. (make-variable-buffer-local 'org-target-link-regexp)
  3784. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3785. "Regular expression matching a link target.")
  3786. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3787. "Regular expression matching a radio target.")
  3788. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3789. "Regular expression matching any target.")
  3790. (defun org-activate-target-links (limit)
  3791. "Run through the buffer and add overlays to target matches."
  3792. (when org-target-link-regexp
  3793. (let ((case-fold-search t))
  3794. (if (re-search-forward org-target-link-regexp limit t)
  3795. (progn
  3796. (add-text-properties (match-beginning 0) (match-end 0)
  3797. (list 'mouse-face 'highlight
  3798. 'keymap org-mouse-map
  3799. 'help-echo "Radio target link"
  3800. 'org-linked-text t))
  3801. (org-rear-nonsticky-at (match-end 0))
  3802. t)))))
  3803. (defun org-update-radio-target-regexp ()
  3804. "Find all radio targets in this file and update the regular expression."
  3805. (interactive)
  3806. (when (memq 'radio org-activate-links)
  3807. (setq org-target-link-regexp
  3808. (org-make-target-link-regexp (org-all-targets 'radio)))
  3809. (org-restart-font-lock)))
  3810. (defun org-hide-wide-columns (limit)
  3811. (let (s e)
  3812. (setq s (text-property-any (point) (or limit (point-max))
  3813. 'org-cwidth t))
  3814. (when s
  3815. (setq e (next-single-property-change s 'org-cwidth))
  3816. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3817. (goto-char e)
  3818. t)))
  3819. (defvar org-latex-and-specials-regexp nil
  3820. "Regular expression for highlighting export special stuff.")
  3821. (defvar org-match-substring-regexp)
  3822. (defvar org-match-substring-with-braces-regexp)
  3823. (defvar org-export-html-special-string-regexps)
  3824. (defun org-compute-latex-and-specials-regexp ()
  3825. "Compute regular expression for stuff treated specially by exporters."
  3826. (if (not org-highlight-latex-fragments-and-specials)
  3827. (org-set-local 'org-latex-and-specials-regexp nil)
  3828. (require 'org-exp)
  3829. (let*
  3830. ((matchers (plist-get org-format-latex-options :matchers))
  3831. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3832. org-latex-regexps)))
  3833. (options (org-combine-plists (org-default-export-plist)
  3834. (org-infile-export-plist)))
  3835. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3836. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3837. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3838. (org-export-html-expand (plist-get options :expand-quoted-html))
  3839. (org-export-with-special-strings (plist-get options :special-strings))
  3840. (re-sub
  3841. (cond
  3842. ((equal org-export-with-sub-superscripts '{})
  3843. (list org-match-substring-with-braces-regexp))
  3844. (org-export-with-sub-superscripts
  3845. (list org-match-substring-regexp))
  3846. (t nil)))
  3847. (re-latex
  3848. (if org-export-with-LaTeX-fragments
  3849. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3850. (re-macros
  3851. (if org-export-with-TeX-macros
  3852. (list (concat "\\\\"
  3853. (regexp-opt
  3854. (append (mapcar 'car org-html-entities)
  3855. (if (boundp 'org-latex-entities)
  3856. org-latex-entities nil))
  3857. 'words))) ; FIXME
  3858. ))
  3859. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3860. (re-special (if org-export-with-special-strings
  3861. (mapcar (lambda (x) (car x))
  3862. org-export-html-special-string-regexps)))
  3863. (re-rest
  3864. (delq nil
  3865. (list
  3866. (if org-export-html-expand "@<[^>\n]+>")
  3867. ))))
  3868. (org-set-local
  3869. 'org-latex-and-specials-regexp
  3870. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3871. re-rest) "\\|")))))
  3872. (defun org-do-latex-and-special-faces (limit)
  3873. "Run through the buffer and add overlays to links."
  3874. (when org-latex-and-specials-regexp
  3875. (let (rtn d)
  3876. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3877. limit t))
  3878. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3879. 'face))
  3880. '(org-code org-verbatim underline)))
  3881. (progn
  3882. (setq rtn t
  3883. d (cond ((member (char-after (1+ (match-beginning 0)))
  3884. '(?_ ?^)) 1)
  3885. (t 0)))
  3886. (font-lock-prepend-text-property
  3887. (+ d (match-beginning 0)) (match-end 0)
  3888. 'face 'org-latex-and-export-specials)
  3889. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3890. '(font-lock-multiline t)))))
  3891. rtn)))
  3892. (defun org-restart-font-lock ()
  3893. "Restart font-lock-mode, to force refontification."
  3894. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3895. (font-lock-mode -1)
  3896. (font-lock-mode 1)))
  3897. (defun org-all-targets (&optional radio)
  3898. "Return a list of all targets in this file.
  3899. With optional argument RADIO, only find radio targets."
  3900. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3901. rtn)
  3902. (save-excursion
  3903. (goto-char (point-min))
  3904. (while (re-search-forward re nil t)
  3905. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3906. rtn)))
  3907. (defun org-make-target-link-regexp (targets)
  3908. "Make regular expression matching all strings in TARGETS.
  3909. The regular expression finds the targets also if there is a line break
  3910. between words."
  3911. (and targets
  3912. (concat
  3913. "\\<\\("
  3914. (mapconcat
  3915. (lambda (x)
  3916. (while (string-match " +" x)
  3917. (setq x (replace-match "\\s-+" t t x)))
  3918. x)
  3919. targets
  3920. "\\|")
  3921. "\\)\\>")))
  3922. (defun org-activate-tags (limit)
  3923. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3924. (progn
  3925. (add-text-properties (match-beginning 1) (match-end 1)
  3926. (list 'mouse-face 'highlight
  3927. 'keymap org-mouse-map))
  3928. (org-rear-nonsticky-at (match-end 1))
  3929. t)))
  3930. (defun org-outline-level ()
  3931. (save-excursion
  3932. (looking-at outline-regexp)
  3933. (if (match-beginning 1)
  3934. (+ (org-get-string-indentation (match-string 1)) 1000)
  3935. (1- (- (match-end 0) (match-beginning 0))))))
  3936. (defvar org-font-lock-keywords nil)
  3937. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3938. "Regular expression matching a property line.")
  3939. (defvar org-font-lock-hook nil
  3940. "Functions to be called for special font lock stuff.")
  3941. (defun org-font-lock-hook (limit)
  3942. (run-hook-with-args 'org-font-lock-hook limit))
  3943. (defun org-set-font-lock-defaults ()
  3944. (let* ((em org-fontify-emphasized-text)
  3945. (lk org-activate-links)
  3946. (org-font-lock-extra-keywords
  3947. (list
  3948. ;; Call the hook
  3949. '(org-font-lock-hook)
  3950. ;; Headlines
  3951. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3952. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3953. ;; Table lines
  3954. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3955. (1 'org-table t))
  3956. ;; Table internals
  3957. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3958. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3959. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3960. ;; Drawers
  3961. (list org-drawer-regexp '(0 'org-special-keyword t))
  3962. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3963. ;; Properties
  3964. (list org-property-re
  3965. '(1 'org-special-keyword t)
  3966. '(3 'org-property-value t))
  3967. (if org-format-transports-properties-p
  3968. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3969. ;; Links
  3970. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3971. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3972. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3973. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3974. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3975. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3976. (if (memq 'footnote lk) '(org-activate-footnote-links
  3977. (2 'org-footnote t)))
  3978. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3979. '(org-hide-wide-columns (0 nil append))
  3980. ;; TODO lines
  3981. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  3982. '(1 (org-get-todo-face 1) t))
  3983. ;; DONE
  3984. (if org-fontify-done-headline
  3985. (list (concat "^[*]+ +\\<\\("
  3986. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3987. "\\)\\(.*\\)")
  3988. '(2 'org-headline-done t))
  3989. nil)
  3990. ;; Priorities
  3991. '(org-font-lock-add-priority-faces)
  3992. ;; Tags
  3993. '(org-font-lock-add-tag-faces)
  3994. ;; Special keywords
  3995. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3996. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3997. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3998. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3999. ;; Emphasis
  4000. (if em
  4001. (if (featurep 'xemacs)
  4002. '(org-do-emphasis-faces (0 nil append))
  4003. '(org-do-emphasis-faces)))
  4004. ;; Checkboxes
  4005. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4006. 2 'org-checkbox prepend)
  4007. (if org-provide-checkbox-statistics
  4008. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4009. (0 (org-get-checkbox-statistics-face) t)))
  4010. ;; Description list items
  4011. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4012. 2 'bold prepend)
  4013. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4014. '(1 'org-archived prepend))
  4015. ;; Specials
  4016. '(org-do-latex-and-special-faces)
  4017. ;; Code
  4018. '(org-activate-code (1 'org-code t))
  4019. ;; COMMENT
  4020. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4021. "\\|" org-quote-string "\\)\\>")
  4022. '(1 'org-special-keyword t))
  4023. '("^#.*" (0 'font-lock-comment-face t))
  4024. )))
  4025. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4026. ;; Now set the full font-lock-keywords
  4027. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4028. (org-set-local 'font-lock-defaults
  4029. '(org-font-lock-keywords t nil nil backward-paragraph))
  4030. (kill-local-variable 'font-lock-keywords) nil))
  4031. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4032. "Fontify string S like in Org-mode"
  4033. (with-temp-buffer
  4034. (insert s)
  4035. (let ((org-odd-levels-only odd-levels))
  4036. (org-mode)
  4037. (font-lock-fontify-buffer)
  4038. (buffer-string))))
  4039. (defvar org-m nil)
  4040. (defvar org-l nil)
  4041. (defvar org-f nil)
  4042. (defun org-get-level-face (n)
  4043. "Get the right face for match N in font-lock matching of headlines."
  4044. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4045. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4046. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4047. (cond
  4048. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4049. ((eq n 2) org-f)
  4050. (t (if org-level-color-stars-only nil org-f))))
  4051. (defun org-get-todo-face (kwd)
  4052. "Get the right face for a TODO keyword KWD.
  4053. If KWD is a number, get the corresponding match group."
  4054. (if (numberp kwd) (setq kwd (match-string kwd)))
  4055. (or (cdr (assoc kwd org-todo-keyword-faces))
  4056. (and (member kwd org-done-keywords) 'org-done)
  4057. 'org-todo))
  4058. (defun org-font-lock-add-tag-faces (limit)
  4059. "Add the special tag faces."
  4060. (when (and org-tag-faces org-tags-special-faces-re)
  4061. (while (re-search-forward org-tags-special-faces-re limit t)
  4062. (add-text-properties (match-beginning 1) (match-end 1)
  4063. (list 'face (org-get-tag-face 1)
  4064. 'font-lock-fontified t))
  4065. (backward-char 1))))
  4066. (defun org-font-lock-add-priority-faces (limit)
  4067. "Add the special priority faces."
  4068. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4069. (add-text-properties
  4070. (match-beginning 0) (match-end 0)
  4071. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4072. org-priority-faces))
  4073. 'org-special-keyword)
  4074. 'font-lock-fontified t))))
  4075. (defun org-get-tag-face (kwd)
  4076. "Get the right face for a TODO keyword KWD.
  4077. If KWD is a number, get the corresponding match group."
  4078. (if (numberp kwd) (setq kwd (match-string kwd)))
  4079. (or (cdr (assoc kwd org-tag-faces))
  4080. 'org-tag))
  4081. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4082. "Remove fontification and activation overlays from links."
  4083. (font-lock-default-unfontify-region beg end)
  4084. (let* ((buffer-undo-list t)
  4085. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4086. (inhibit-modification-hooks t)
  4087. deactivate-mark buffer-file-name buffer-file-truename)
  4088. (remove-text-properties beg end
  4089. '(mouse-face t keymap t org-linked-text t
  4090. invisible t intangible t))))
  4091. ;;;; Visibility cycling, including org-goto and indirect buffer
  4092. ;;; Cycling
  4093. (defvar org-cycle-global-status nil)
  4094. (make-variable-buffer-local 'org-cycle-global-status)
  4095. (defvar org-cycle-subtree-status nil)
  4096. (make-variable-buffer-local 'org-cycle-subtree-status)
  4097. ;;;###autoload
  4098. (defvar org-inlinetask-min-level)
  4099. (defun org-cycle (&optional arg)
  4100. "TAB-action and visibility cycling for Org-mode.
  4101. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4102. is outine visibility cycling, but it also invokes other actions
  4103. in special contexts.
  4104. - When this function is called with a prefix argument, rotate the entire
  4105. buffer through 3 states (global cycling)
  4106. 1. OVERVIEW: Show only top-level headlines.
  4107. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4108. 3. SHOW ALL: Show everything.
  4109. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4110. determined by the variable `org-startup-folded', and by any VISIBILITY
  4111. properties in the buffer.
  4112. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4113. including any drawers.
  4114. - When inside a table, re-align the table and move to the next field.
  4115. - When point is at the beginning of a headline, rotate the subtree started
  4116. by this line through 3 different states (local cycling)
  4117. 1. FOLDED: Only the main headline is shown.
  4118. 2. CHILDREN: The main headline and the direct children are shown.
  4119. From this state, you can move to one of the children
  4120. and zoom in further.
  4121. 3. SUBTREE: Show the entire subtree, including body text.
  4122. - When there is a numeric prefix, go up to a heading with level ARG, do
  4123. a `show-subtree' and return to the previous cursor position. If ARG
  4124. is negative, go up that many levels.
  4125. - When point is not at the beginning of a headline, execute the global
  4126. binding for TAB, which is re-indenting the line. See the option
  4127. `org-cycle-emulate-tab' for details.
  4128. - Special case: if point is at the beginning of the buffer and there is
  4129. no headline in line 1, this function will act as if called with prefix arg.
  4130. But only if also the variable `org-cycle-global-at-bob' is t."
  4131. (interactive "P")
  4132. (org-load-modules-maybe)
  4133. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4134. (let* ((limit-level
  4135. (or org-cycle-max-level
  4136. (and (boundp 'org-inlinetask-min-level)
  4137. org-inlinetask-min-level
  4138. (1- org-inlinetask-min-level))))
  4139. (nstars (and limit-level
  4140. (if org-odd-levels-only
  4141. (and limit-level (1- (* limit-level 2)))
  4142. limit-level)))
  4143. (outline-regexp
  4144. (cond
  4145. ((not (org-mode-p)) outline-regexp)
  4146. (org-cycle-include-plain-lists
  4147. (concat "\\(?:\\*"
  4148. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4149. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4150. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4151. (bob-special (and org-cycle-global-at-bob (bobp)
  4152. (not (looking-at outline-regexp))))
  4153. (org-cycle-hook
  4154. (if bob-special
  4155. (delq 'org-optimize-window-after-visibility-change
  4156. (copy-sequence org-cycle-hook))
  4157. org-cycle-hook))
  4158. (pos (point)))
  4159. (if (or bob-special (equal arg '(4)))
  4160. ;; special case: use global cycling
  4161. (setq arg t))
  4162. (cond
  4163. ((equal arg '(16))
  4164. (org-set-startup-visibility)
  4165. (message "Startup visibility, plus VISIBILITY properties"))
  4166. ((equal arg '(64))
  4167. (show-all)
  4168. (message "Entire buffer visible, including drawers"))
  4169. ((org-at-table-p 'any)
  4170. ;; Enter the table or move to the next field in the table
  4171. (or (org-table-recognize-table.el)
  4172. (progn
  4173. (if arg (org-table-edit-field t)
  4174. (org-table-justify-field-maybe)
  4175. (call-interactively 'org-table-next-field)))))
  4176. ((run-hook-with-args-until-success
  4177. 'org-tab-after-check-for-table-hook))
  4178. ((eq arg t) ;; Global cycling
  4179. (org-cycle-internal-global))
  4180. ((and org-drawers org-drawer-regexp
  4181. (save-excursion
  4182. (beginning-of-line 1)
  4183. (looking-at org-drawer-regexp)))
  4184. ;; Toggle block visibility
  4185. (org-flag-drawer
  4186. (not (get-char-property (match-end 0) 'invisible))))
  4187. ((integerp arg)
  4188. ;; Show-subtree, ARG levels up from here.
  4189. (save-excursion
  4190. (org-back-to-heading)
  4191. (outline-up-heading (if (< arg 0) (- arg)
  4192. (- (funcall outline-level) arg)))
  4193. (org-show-subtree)))
  4194. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4195. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4196. (org-cycle-internal-local))
  4197. ;; TAB emulation and template completion
  4198. (buffer-read-only (org-back-to-heading))
  4199. ((run-hook-with-args-until-success
  4200. 'org-tab-after-check-for-cycling-hook))
  4201. ((org-try-structure-completion))
  4202. ((org-try-cdlatex-tab))
  4203. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4204. (or (not (bolp))
  4205. (not (looking-at outline-regexp))))
  4206. (call-interactively (global-key-binding "\t")))
  4207. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4208. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4209. (or (and (eq org-cycle-emulate-tab 'white)
  4210. (= (match-end 0) (point-at-eol)))
  4211. (and (eq org-cycle-emulate-tab 'whitestart)
  4212. (>= (match-end 0) pos))))
  4213. t
  4214. (eq org-cycle-emulate-tab t))
  4215. (call-interactively (global-key-binding "\t")))
  4216. (t (save-excursion
  4217. (org-back-to-heading)
  4218. (org-cycle)))))))
  4219. (defun org-cycle-internal-global ()
  4220. "Do the global cycling action."
  4221. (cond
  4222. ((and (eq last-command this-command)
  4223. (eq org-cycle-global-status 'overview))
  4224. ;; We just created the overview - now do table of contents
  4225. ;; This can be slow in very large buffers, so indicate action
  4226. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4227. (message "CONTENTS...")
  4228. (org-content)
  4229. (message "CONTENTS...done")
  4230. (setq org-cycle-global-status 'contents)
  4231. (run-hook-with-args 'org-cycle-hook 'contents))
  4232. ((and (eq last-command this-command)
  4233. (eq org-cycle-global-status 'contents))
  4234. ;; We just showed the table of contents - now show everything
  4235. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4236. (show-all)
  4237. (message "SHOW ALL")
  4238. (setq org-cycle-global-status 'all)
  4239. (run-hook-with-args 'org-cycle-hook 'all))
  4240. (t
  4241. ;; Default action: go to overview
  4242. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4243. (org-overview)
  4244. (message "OVERVIEW")
  4245. (setq org-cycle-global-status 'overview)
  4246. (run-hook-with-args 'org-cycle-hook 'overview))))
  4247. (defun org-cycle-internal-local ()
  4248. "Do the local cycling action."
  4249. (org-back-to-heading)
  4250. (let ((goal-column 0) eoh eol eos)
  4251. ;; First, some boundaries
  4252. (save-excursion
  4253. (org-back-to-heading)
  4254. (save-excursion
  4255. (beginning-of-line 2)
  4256. (while (and (not (eobp)) ;; this is like `next-line'
  4257. (get-char-property (1- (point)) 'invisible))
  4258. (beginning-of-line 2)) (setq eol (point)))
  4259. (outline-end-of-heading) (setq eoh (point))
  4260. (org-end-of-subtree t)
  4261. (unless (eobp)
  4262. (skip-chars-forward " \t\n")
  4263. (beginning-of-line 1) ; in case this is an item
  4264. )
  4265. (setq eos (1- (point))))
  4266. ;; Find out what to do next and set `this-command'
  4267. (cond
  4268. ((= eos eoh)
  4269. ;; Nothing is hidden behind this heading
  4270. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4271. (message "EMPTY ENTRY")
  4272. (setq org-cycle-subtree-status nil)
  4273. (save-excursion
  4274. (goto-char eos)
  4275. (outline-next-heading)
  4276. (if (org-invisible-p) (org-flag-heading nil))))
  4277. ((or (>= eol eos)
  4278. (not (string-match "\\S-" (buffer-substring eol eos))))
  4279. ;; Entire subtree is hidden in one line: open it
  4280. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4281. (org-show-entry)
  4282. (show-children)
  4283. (message "CHILDREN")
  4284. (save-excursion
  4285. (goto-char eos)
  4286. (outline-next-heading)
  4287. (if (org-invisible-p) (org-flag-heading nil)))
  4288. (setq org-cycle-subtree-status 'children)
  4289. (run-hook-with-args 'org-cycle-hook 'children))
  4290. ((and (eq last-command this-command)
  4291. (eq org-cycle-subtree-status 'children))
  4292. ;; We just showed the children, now show everything.
  4293. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4294. (org-show-subtree)
  4295. (message "SUBTREE")
  4296. (setq org-cycle-subtree-status 'subtree)
  4297. (run-hook-with-args 'org-cycle-hook 'subtree))
  4298. (t
  4299. ;; Default action: hide the subtree.
  4300. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4301. (hide-subtree)
  4302. (message "FOLDED")
  4303. (setq org-cycle-subtree-status 'folded)
  4304. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4305. ;;;###autoload
  4306. (defun org-global-cycle (&optional arg)
  4307. "Cycle the global visibility. For details see `org-cycle'.
  4308. With C-u prefix arg, switch to startup visibility.
  4309. With a numeric prefix, show all headlines up to that level."
  4310. (interactive "P")
  4311. (let ((org-cycle-include-plain-lists
  4312. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4313. (cond
  4314. ((integerp arg)
  4315. (show-all)
  4316. (hide-sublevels arg)
  4317. (setq org-cycle-global-status 'contents))
  4318. ((equal arg '(4))
  4319. (org-set-startup-visibility)
  4320. (message "Startup visibility, plus VISIBILITY properties."))
  4321. (t
  4322. (org-cycle '(4))))))
  4323. (defun org-set-startup-visibility ()
  4324. "Set the visibility required by startup options and properties."
  4325. (cond
  4326. ((eq org-startup-folded t)
  4327. (org-cycle '(4)))
  4328. ((eq org-startup-folded 'content)
  4329. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4330. (org-cycle '(4)) (org-cycle '(4)))))
  4331. (org-set-visibility-according-to-property 'no-cleanup)
  4332. (org-cycle-hide-archived-subtrees 'all)
  4333. (org-cycle-hide-drawers 'all)
  4334. (org-cycle-show-empty-lines 'all))
  4335. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4336. "Switch subtree visibilities according to :VISIBILITY: property."
  4337. (interactive)
  4338. (let (org-show-entry-below state)
  4339. (save-excursion
  4340. (goto-char (point-min))
  4341. (while (re-search-forward
  4342. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4343. nil t)
  4344. (setq state (match-string 1))
  4345. (save-excursion
  4346. (org-back-to-heading t)
  4347. (hide-subtree)
  4348. (org-reveal)
  4349. (cond
  4350. ((equal state '("fold" "folded"))
  4351. (hide-subtree))
  4352. ((equal state "children")
  4353. (org-show-hidden-entry)
  4354. (show-children))
  4355. ((equal state "content")
  4356. (save-excursion
  4357. (save-restriction
  4358. (org-narrow-to-subtree)
  4359. (org-content))))
  4360. ((member state '("all" "showall"))
  4361. (show-subtree)))))
  4362. (unless no-cleanup
  4363. (org-cycle-hide-archived-subtrees 'all)
  4364. (org-cycle-hide-drawers 'all)
  4365. (org-cycle-show-empty-lines 'all)))))
  4366. (defun org-overview ()
  4367. "Switch to overview mode, showing only top-level headlines.
  4368. Really, this shows all headlines with level equal or greater than the level
  4369. of the first headline in the buffer. This is important, because if the
  4370. first headline is not level one, then (hide-sublevels 1) gives confusing
  4371. results."
  4372. (interactive)
  4373. (let ((level (save-excursion
  4374. (goto-char (point-min))
  4375. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4376. (progn
  4377. (goto-char (match-beginning 0))
  4378. (funcall outline-level))))))
  4379. (and level (hide-sublevels level))))
  4380. (defun org-content (&optional arg)
  4381. "Show all headlines in the buffer, like a table of contents.
  4382. With numerical argument N, show content up to level N."
  4383. (interactive "P")
  4384. (save-excursion
  4385. ;; Visit all headings and show their offspring
  4386. (and (integerp arg) (org-overview))
  4387. (goto-char (point-max))
  4388. (catch 'exit
  4389. (while (and (progn (condition-case nil
  4390. (outline-previous-visible-heading 1)
  4391. (error (goto-char (point-min))))
  4392. t)
  4393. (looking-at outline-regexp))
  4394. (if (integerp arg)
  4395. (show-children (1- arg))
  4396. (show-branches))
  4397. (if (bobp) (throw 'exit nil))))))
  4398. (defun org-optimize-window-after-visibility-change (state)
  4399. "Adjust the window after a change in outline visibility.
  4400. This function is the default value of the hook `org-cycle-hook'."
  4401. (when (get-buffer-window (current-buffer))
  4402. (cond
  4403. ((eq state 'content) nil)
  4404. ((eq state 'all) nil)
  4405. ((eq state 'folded) nil)
  4406. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4407. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4408. (defun org-compact-display-after-subtree-move ()
  4409. "Show a compacter version of the tree of the entry's parent."
  4410. (save-excursion
  4411. (if (org-up-heading-safe)
  4412. (progn
  4413. (hide-subtree)
  4414. (show-entry)
  4415. (show-children)
  4416. (org-cycle-show-empty-lines 'children)
  4417. (org-cycle-hide-drawers 'children))
  4418. (org-overview))))
  4419. (defun org-cycle-show-empty-lines (state)
  4420. "Show empty lines above all visible headlines.
  4421. The region to be covered depends on STATE when called through
  4422. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4423. entire buffer covered. Note that an empty line is only shown if there
  4424. are at least `org-cycle-separator-lines' empty lines before the headline."
  4425. (when (> org-cycle-separator-lines 0)
  4426. (save-excursion
  4427. (let* ((n org-cycle-separator-lines)
  4428. (re (cond
  4429. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4430. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4431. (t (let ((ns (number-to-string (- n 2))))
  4432. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4433. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4434. beg end)
  4435. (cond
  4436. ((memq state '(overview contents t))
  4437. (setq beg (point-min) end (point-max)))
  4438. ((memq state '(children folded))
  4439. (setq beg (point) end (progn (org-end-of-subtree t t)
  4440. (beginning-of-line 2)
  4441. (point)))))
  4442. (when beg
  4443. (goto-char beg)
  4444. (while (re-search-forward re end t)
  4445. (if (not (get-char-property (match-end 1) 'invisible))
  4446. (outline-flag-region
  4447. (match-beginning 1) (match-end 1) nil)))))))
  4448. ;; Never hide empty lines at the end of the file.
  4449. (save-excursion
  4450. (goto-char (point-max))
  4451. (outline-previous-heading)
  4452. (outline-end-of-heading)
  4453. (if (and (looking-at "[ \t\n]+")
  4454. (= (match-end 0) (point-max)))
  4455. (outline-flag-region (point) (match-end 0) nil))))
  4456. (defun org-show-empty-lines-in-parent ()
  4457. "Move to the parent and re-show empty lines before visible headlines."
  4458. (save-excursion
  4459. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4460. (org-cycle-show-empty-lines context))))
  4461. (defun org-cycle-hide-drawers (state)
  4462. "Re-hide all drawers after a visibility state change."
  4463. (when (and (org-mode-p)
  4464. (not (memq state '(overview folded))))
  4465. (save-excursion
  4466. (let* ((globalp (memq state '(contents all)))
  4467. (beg (if globalp (point-min) (point)))
  4468. (end (if globalp (point-max) (org-end-of-subtree t))))
  4469. (goto-char beg)
  4470. (while (re-search-forward org-drawer-regexp end t)
  4471. (org-flag-drawer t))))))
  4472. (defun org-flag-drawer (flag)
  4473. (save-excursion
  4474. (beginning-of-line 1)
  4475. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4476. (let ((b (match-end 0))
  4477. (outline-regexp org-outline-regexp))
  4478. (if (re-search-forward
  4479. "^[ \t]*:END:"
  4480. (save-excursion (outline-next-heading) (point)) t)
  4481. (outline-flag-region b (point-at-eol) flag)
  4482. (error ":END: line missing"))))))
  4483. (defun org-subtree-end-visible-p ()
  4484. "Is the end of the current subtree visible?"
  4485. (pos-visible-in-window-p
  4486. (save-excursion (org-end-of-subtree t) (point))))
  4487. (defun org-first-headline-recenter (&optional N)
  4488. "Move cursor to the first headline and recenter the headline.
  4489. Optional argument N means, put the headline into the Nth line of the window."
  4490. (goto-char (point-min))
  4491. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4492. (beginning-of-line)
  4493. (recenter (prefix-numeric-value N))))
  4494. ;;; Org-goto
  4495. (defvar org-goto-window-configuration nil)
  4496. (defvar org-goto-marker nil)
  4497. (defvar org-goto-map
  4498. (let ((map (make-sparse-keymap)))
  4499. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4500. (while (setq cmd (pop cmds))
  4501. (substitute-key-definition cmd cmd map global-map)))
  4502. (suppress-keymap map)
  4503. (org-defkey map "\C-m" 'org-goto-ret)
  4504. (org-defkey map [(return)] 'org-goto-ret)
  4505. (org-defkey map [(left)] 'org-goto-left)
  4506. (org-defkey map [(right)] 'org-goto-right)
  4507. (org-defkey map [(control ?g)] 'org-goto-quit)
  4508. (org-defkey map "\C-i" 'org-cycle)
  4509. (org-defkey map [(tab)] 'org-cycle)
  4510. (org-defkey map [(down)] 'outline-next-visible-heading)
  4511. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4512. (if org-goto-auto-isearch
  4513. (if (fboundp 'define-key-after)
  4514. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4515. nil)
  4516. (org-defkey map "q" 'org-goto-quit)
  4517. (org-defkey map "n" 'outline-next-visible-heading)
  4518. (org-defkey map "p" 'outline-previous-visible-heading)
  4519. (org-defkey map "f" 'outline-forward-same-level)
  4520. (org-defkey map "b" 'outline-backward-same-level)
  4521. (org-defkey map "u" 'outline-up-heading))
  4522. (org-defkey map "/" 'org-occur)
  4523. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4524. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4525. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4526. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4527. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4528. map))
  4529. (defconst org-goto-help
  4530. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4531. RET=jump to location [Q]uit and return to previous location
  4532. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4533. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4534. ;; FIXME: Docstring doe not mention both interfaces
  4535. (defun org-goto (&optional alternative-interface)
  4536. "Look up a different location in the current file, keeping current visibility.
  4537. When you want look-up or go to a different location in a document, the
  4538. fastest way is often to fold the entire buffer and then dive into the tree.
  4539. This method has the disadvantage, that the previous location will be folded,
  4540. which may not be what you want.
  4541. This command works around this by showing a copy of the current buffer
  4542. in an indirect buffer, in overview mode. You can dive into the tree in
  4543. that copy, use org-occur and incremental search to find a location.
  4544. When pressing RET or `Q', the command returns to the original buffer in
  4545. which the visibility is still unchanged. After RET is will also jump to
  4546. the location selected in the indirect buffer and expose the
  4547. the headline hierarchy above."
  4548. (interactive "P")
  4549. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4550. (org-refile-use-outline-path t)
  4551. (org-refile-target-verify-function nil)
  4552. (interface
  4553. (if (not alternative-interface)
  4554. org-goto-interface
  4555. (if (eq org-goto-interface 'outline)
  4556. 'outline-path-completion
  4557. 'outline)))
  4558. (org-goto-start-pos (point))
  4559. (selected-point
  4560. (if (eq interface 'outline)
  4561. (car (org-get-location (current-buffer) org-goto-help))
  4562. (nth 3 (org-refile-get-location "Goto: ")))))
  4563. (if selected-point
  4564. (progn
  4565. (org-mark-ring-push org-goto-start-pos)
  4566. (goto-char selected-point)
  4567. (if (or (org-invisible-p) (org-invisible-p2))
  4568. (org-show-context 'org-goto)))
  4569. (message "Quit"))))
  4570. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4571. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4572. (defvar org-goto-local-auto-isearch-map) ; defined below
  4573. (defun org-get-location (buf help)
  4574. "Let the user select a location in the Org-mode buffer BUF.
  4575. This function uses a recursive edit. It returns the selected position
  4576. or nil."
  4577. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4578. (isearch-hide-immediately nil)
  4579. (isearch-search-fun-function
  4580. (lambda () 'org-goto-local-search-headings))
  4581. (org-goto-selected-point org-goto-exit-command))
  4582. (save-excursion
  4583. (save-window-excursion
  4584. (delete-other-windows)
  4585. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4586. (switch-to-buffer
  4587. (condition-case nil
  4588. (make-indirect-buffer (current-buffer) "*org-goto*")
  4589. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4590. (with-output-to-temp-buffer "*Help*"
  4591. (princ help))
  4592. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4593. (setq buffer-read-only nil)
  4594. (let ((org-startup-truncated t)
  4595. (org-startup-folded nil)
  4596. (org-startup-align-all-tables nil))
  4597. (org-mode)
  4598. (org-overview))
  4599. (setq buffer-read-only t)
  4600. (if (and (boundp 'org-goto-start-pos)
  4601. (integer-or-marker-p org-goto-start-pos))
  4602. (let ((org-show-hierarchy-above t)
  4603. (org-show-siblings t)
  4604. (org-show-following-heading t))
  4605. (goto-char org-goto-start-pos)
  4606. (and (org-invisible-p) (org-show-context)))
  4607. (goto-char (point-min)))
  4608. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4609. (message "Select location and press RET")
  4610. (use-local-map org-goto-map)
  4611. (recursive-edit)
  4612. ))
  4613. (kill-buffer "*org-goto*")
  4614. (cons org-goto-selected-point org-goto-exit-command)))
  4615. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4616. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4617. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4618. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4619. (defun org-goto-local-search-headings (string bound noerror)
  4620. "Search and make sure that any matches are in headlines."
  4621. (catch 'return
  4622. (while (if isearch-forward
  4623. (search-forward string bound noerror)
  4624. (search-backward string bound noerror))
  4625. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4626. (and (member :headline context)
  4627. (not (member :tags context))))
  4628. (throw 'return (point))))))
  4629. (defun org-goto-local-auto-isearch ()
  4630. "Start isearch."
  4631. (interactive)
  4632. (goto-char (point-min))
  4633. (let ((keys (this-command-keys)))
  4634. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4635. (isearch-mode t)
  4636. (isearch-process-search-char (string-to-char keys)))))
  4637. (defun org-goto-ret (&optional arg)
  4638. "Finish `org-goto' by going to the new location."
  4639. (interactive "P")
  4640. (setq org-goto-selected-point (point)
  4641. org-goto-exit-command 'return)
  4642. (throw 'exit nil))
  4643. (defun org-goto-left ()
  4644. "Finish `org-goto' by going to the new location."
  4645. (interactive)
  4646. (if (org-on-heading-p)
  4647. (progn
  4648. (beginning-of-line 1)
  4649. (setq org-goto-selected-point (point)
  4650. org-goto-exit-command 'left)
  4651. (throw 'exit nil))
  4652. (error "Not on a heading")))
  4653. (defun org-goto-right ()
  4654. "Finish `org-goto' by going to the new location."
  4655. (interactive)
  4656. (if (org-on-heading-p)
  4657. (progn
  4658. (setq org-goto-selected-point (point)
  4659. org-goto-exit-command 'right)
  4660. (throw 'exit nil))
  4661. (error "Not on a heading")))
  4662. (defun org-goto-quit ()
  4663. "Finish `org-goto' without cursor motion."
  4664. (interactive)
  4665. (setq org-goto-selected-point nil)
  4666. (setq org-goto-exit-command 'quit)
  4667. (throw 'exit nil))
  4668. ;;; Indirect buffer display of subtrees
  4669. (defvar org-indirect-dedicated-frame nil
  4670. "This is the frame being used for indirect tree display.")
  4671. (defvar org-last-indirect-buffer nil)
  4672. (defun org-tree-to-indirect-buffer (&optional arg)
  4673. "Create indirect buffer and narrow it to current subtree.
  4674. With numerical prefix ARG, go up to this level and then take that tree.
  4675. If ARG is negative, go up that many levels.
  4676. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4677. indirect buffer previously made with this command, to avoid proliferation of
  4678. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4679. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4680. is kept so that you can work with several indirect buffers at the same time.
  4681. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4682. requests that a new frame be made for the new buffer, so that the dedicated
  4683. frame is not changed."
  4684. (interactive "P")
  4685. (let ((cbuf (current-buffer))
  4686. (cwin (selected-window))
  4687. (pos (point))
  4688. beg end level heading ibuf)
  4689. (save-excursion
  4690. (org-back-to-heading t)
  4691. (when (numberp arg)
  4692. (setq level (org-outline-level))
  4693. (if (< arg 0) (setq arg (+ level arg)))
  4694. (while (> (setq level (org-outline-level)) arg)
  4695. (outline-up-heading 1 t)))
  4696. (setq beg (point)
  4697. heading (org-get-heading))
  4698. (org-end-of-subtree t) (setq end (point)))
  4699. (if (and (buffer-live-p org-last-indirect-buffer)
  4700. (not (eq org-indirect-buffer-display 'new-frame))
  4701. (not arg))
  4702. (kill-buffer org-last-indirect-buffer))
  4703. (setq ibuf (org-get-indirect-buffer cbuf)
  4704. org-last-indirect-buffer ibuf)
  4705. (cond
  4706. ((or (eq org-indirect-buffer-display 'new-frame)
  4707. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4708. (select-frame (make-frame))
  4709. (delete-other-windows)
  4710. (switch-to-buffer ibuf)
  4711. (org-set-frame-title heading))
  4712. ((eq org-indirect-buffer-display 'dedicated-frame)
  4713. (raise-frame
  4714. (select-frame (or (and org-indirect-dedicated-frame
  4715. (frame-live-p org-indirect-dedicated-frame)
  4716. org-indirect-dedicated-frame)
  4717. (setq org-indirect-dedicated-frame (make-frame)))))
  4718. (delete-other-windows)
  4719. (switch-to-buffer ibuf)
  4720. (org-set-frame-title (concat "Indirect: " heading)))
  4721. ((eq org-indirect-buffer-display 'current-window)
  4722. (switch-to-buffer ibuf))
  4723. ((eq org-indirect-buffer-display 'other-window)
  4724. (pop-to-buffer ibuf))
  4725. (t (error "Invalid value.")))
  4726. (if (featurep 'xemacs)
  4727. (save-excursion (org-mode) (turn-on-font-lock)))
  4728. (narrow-to-region beg end)
  4729. (show-all)
  4730. (goto-char pos)
  4731. (and (window-live-p cwin) (select-window cwin))))
  4732. (defun org-get-indirect-buffer (&optional buffer)
  4733. (setq buffer (or buffer (current-buffer)))
  4734. (let ((n 1) (base (buffer-name buffer)) bname)
  4735. (while (buffer-live-p
  4736. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4737. (setq n (1+ n)))
  4738. (condition-case nil
  4739. (make-indirect-buffer buffer bname 'clone)
  4740. (error (make-indirect-buffer buffer bname)))))
  4741. (defun org-set-frame-title (title)
  4742. "Set the title of the current frame to the string TITLE."
  4743. ;; FIXME: how to name a single frame in XEmacs???
  4744. (unless (featurep 'xemacs)
  4745. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4746. ;;;; Structure editing
  4747. ;;; Inserting headlines
  4748. (defun org-previous-line-empty-p ()
  4749. (save-excursion
  4750. (and (not (bobp))
  4751. (or (beginning-of-line 0) t)
  4752. (save-match-data
  4753. (looking-at "[ \t]*$")))))
  4754. (defun org-insert-heading (&optional force-heading)
  4755. "Insert a new heading or item with same depth at point.
  4756. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4757. If point is at the beginning of a headline, insert a sibling before the
  4758. current headline. If point is not at the beginning, do not split the line,
  4759. but create the new headline after the current line."
  4760. (interactive "P")
  4761. (if (= (buffer-size) 0)
  4762. (insert "\n* ")
  4763. (when (or force-heading (not (org-insert-item)))
  4764. (let* ((empty-line-p nil)
  4765. (head (save-excursion
  4766. (condition-case nil
  4767. (progn
  4768. (org-back-to-heading)
  4769. (setq empty-line-p (org-previous-line-empty-p))
  4770. (match-string 0))
  4771. (error "*"))))
  4772. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4773. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4774. pos hide-previous previous-pos)
  4775. (cond
  4776. ((and (org-on-heading-p) (bolp)
  4777. (or (bobp)
  4778. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4779. ;; insert before the current line
  4780. (open-line (if blank 2 1)))
  4781. ((and (bolp)
  4782. (or (bobp)
  4783. (save-excursion
  4784. (backward-char 1) (not (org-invisible-p)))))
  4785. ;; insert right here
  4786. nil)
  4787. (t
  4788. ;; somewhere in the line
  4789. (save-excursion
  4790. (setq previous-pos (point-at-bol))
  4791. (end-of-line)
  4792. (setq hide-previous (org-invisible-p)))
  4793. (and org-insert-heading-respect-content (org-show-subtree))
  4794. (let ((split
  4795. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4796. (save-excursion
  4797. (let ((p (point)))
  4798. (goto-char (point-at-bol))
  4799. (and (looking-at org-complex-heading-regexp)
  4800. (> p (match-beginning 4)))))))
  4801. tags pos)
  4802. (cond
  4803. (org-insert-heading-respect-content
  4804. (org-end-of-subtree nil t)
  4805. (or (bolp) (newline))
  4806. (or (org-previous-line-empty-p)
  4807. (and blank (newline)))
  4808. (open-line 1))
  4809. ((org-on-heading-p)
  4810. (when hide-previous
  4811. (show-children)
  4812. (org-show-entry))
  4813. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4814. (setq tags (and (match-end 2) (match-string 2)))
  4815. (and (match-end 1)
  4816. (delete-region (match-beginning 1) (match-end 1)))
  4817. (setq pos (point-at-bol))
  4818. (or split (end-of-line 1))
  4819. (delete-horizontal-space)
  4820. (newline (if blank 2 1))
  4821. (when tags
  4822. (save-excursion
  4823. (goto-char pos)
  4824. (end-of-line 1)
  4825. (insert " " tags)
  4826. (org-set-tags nil 'align))))
  4827. (t
  4828. (or split (end-of-line 1))
  4829. (newline (if blank 2 1)))))))
  4830. (insert head) (just-one-space)
  4831. (setq pos (point))
  4832. (end-of-line 1)
  4833. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4834. (when (and org-insert-heading-respect-content hide-previous)
  4835. (save-excursion
  4836. (goto-char previous-pos)
  4837. (hide-subtree)))
  4838. (run-hooks 'org-insert-heading-hook)))))
  4839. (defun org-get-heading (&optional no-tags)
  4840. "Return the heading of the current entry, without the stars."
  4841. (save-excursion
  4842. (org-back-to-heading t)
  4843. (if (looking-at
  4844. (if no-tags
  4845. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4846. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4847. (match-string 1) "")))
  4848. (defun org-heading-components ()
  4849. "Return the components of the current heading.
  4850. This is a list with the following elements:
  4851. - the level as an integer
  4852. - the reduced level, different if `org-odd-levels-only' is set.
  4853. - the TODO keyword, or nil
  4854. - the priority character, like ?A, or nil if no priority is given
  4855. - the headline text itself, or the tags string if no headline text
  4856. - the tags string, or nil."
  4857. (save-excursion
  4858. (org-back-to-heading t)
  4859. (if (looking-at org-complex-heading-regexp)
  4860. (list (length (match-string 1))
  4861. (org-reduced-level (length (match-string 1)))
  4862. (org-match-string-no-properties 2)
  4863. (and (match-end 3) (aref (match-string 3) 2))
  4864. (org-match-string-no-properties 4)
  4865. (org-match-string-no-properties 5)))))
  4866. (defun org-get-entry ()
  4867. "Get the entry text, after heading, entire subtree."
  4868. (save-excursion
  4869. (org-back-to-heading t)
  4870. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  4871. (defun org-insert-heading-after-current ()
  4872. "Insert a new heading with same level as current, after current subtree."
  4873. (interactive)
  4874. (org-back-to-heading)
  4875. (org-insert-heading)
  4876. (org-move-subtree-down)
  4877. (end-of-line 1))
  4878. (defun org-insert-heading-respect-content ()
  4879. (interactive)
  4880. (let ((org-insert-heading-respect-content t))
  4881. (org-insert-heading t)))
  4882. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4883. (interactive "P")
  4884. (let ((org-insert-heading-respect-content t))
  4885. (org-insert-todo-heading force-state t)))
  4886. (defun org-insert-todo-heading (arg &optional force-heading)
  4887. "Insert a new heading with the same level and TODO state as current heading.
  4888. If the heading has no TODO state, or if the state is DONE, use the first
  4889. state (TODO by default). Also with prefix arg, force first state."
  4890. (interactive "P")
  4891. (when (or force-heading (not (org-insert-item 'checkbox)))
  4892. (org-insert-heading force-heading)
  4893. (save-excursion
  4894. (org-back-to-heading)
  4895. (outline-previous-heading)
  4896. (looking-at org-todo-line-regexp))
  4897. (let*
  4898. ((new-mark-x
  4899. (if (or arg
  4900. (not (match-beginning 2))
  4901. (member (match-string 2) org-done-keywords))
  4902. (car org-todo-keywords-1)
  4903. (match-string 2)))
  4904. (new-mark
  4905. (or
  4906. (run-hook-with-args-until-success
  4907. 'org-todo-get-default-hook new-mark-x nil)
  4908. new-mark-x)))
  4909. (beginning-of-line 1)
  4910. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4911. (insert new-mark " ")))
  4912. (when org-provide-todo-statistics
  4913. (org-update-parent-todo-statistics))))
  4914. (defun org-insert-subheading (arg)
  4915. "Insert a new subheading and demote it.
  4916. Works for outline headings and for plain lists alike."
  4917. (interactive "P")
  4918. (org-insert-heading arg)
  4919. (cond
  4920. ((org-on-heading-p) (org-do-demote))
  4921. ((org-at-item-p) (org-indent-item 1))))
  4922. (defun org-insert-todo-subheading (arg)
  4923. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4924. Works for outline headings and for plain lists alike."
  4925. (interactive "P")
  4926. (org-insert-todo-heading arg)
  4927. (cond
  4928. ((org-on-heading-p) (org-do-demote))
  4929. ((org-at-item-p) (org-indent-item 1))))
  4930. ;;; Promotion and Demotion
  4931. (defun org-promote-subtree ()
  4932. "Promote the entire subtree.
  4933. See also `org-promote'."
  4934. (interactive)
  4935. (save-excursion
  4936. (org-map-tree 'org-promote))
  4937. (org-fix-position-after-promote))
  4938. (defun org-demote-subtree ()
  4939. "Demote the entire subtree. See `org-demote'.
  4940. See also `org-promote'."
  4941. (interactive)
  4942. (save-excursion
  4943. (org-map-tree 'org-demote))
  4944. (org-fix-position-after-promote))
  4945. (defun org-do-promote ()
  4946. "Promote the current heading higher up the tree.
  4947. If the region is active in `transient-mark-mode', promote all headings
  4948. in the region."
  4949. (interactive)
  4950. (save-excursion
  4951. (if (org-region-active-p)
  4952. (org-map-region 'org-promote (region-beginning) (region-end))
  4953. (org-promote)))
  4954. (org-fix-position-after-promote))
  4955. (defun org-do-demote ()
  4956. "Demote the current heading lower down the tree.
  4957. If the region is active in `transient-mark-mode', demote all headings
  4958. in the region."
  4959. (interactive)
  4960. (save-excursion
  4961. (if (org-region-active-p)
  4962. (org-map-region 'org-demote (region-beginning) (region-end))
  4963. (org-demote)))
  4964. (org-fix-position-after-promote))
  4965. (defun org-fix-position-after-promote ()
  4966. "Make sure that after pro/demotion cursor position is right."
  4967. (let ((pos (point)))
  4968. (when (save-excursion
  4969. (beginning-of-line 1)
  4970. (looking-at org-todo-line-regexp)
  4971. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4972. (cond ((eobp) (insert " "))
  4973. ((eolp) (insert " "))
  4974. ((equal (char-after) ?\ ) (forward-char 1))))))
  4975. (defun org-reduced-level (l)
  4976. "Compute the effective level of a heading.
  4977. This takes into account the setting of `org-odd-levels-only'."
  4978. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4979. (defun org-get-valid-level (level &optional change)
  4980. "Rectify a level change under the influence of `org-odd-levels-only'
  4981. LEVEL is a current level, CHANGE is by how much the level should be
  4982. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4983. even level numbers will become the next higher odd number."
  4984. (if org-odd-levels-only
  4985. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4986. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4987. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4988. (max 1 (+ level (or change 0)))))
  4989. (if (boundp 'define-obsolete-function-alias)
  4990. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4991. (define-obsolete-function-alias 'org-get-legal-level
  4992. 'org-get-valid-level)
  4993. (define-obsolete-function-alias 'org-get-legal-level
  4994. 'org-get-valid-level "23.1")))
  4995. (defun org-promote ()
  4996. "Promote the current heading higher up the tree.
  4997. If the region is active in `transient-mark-mode', promote all headings
  4998. in the region."
  4999. (org-back-to-heading t)
  5000. (let* ((level (save-match-data (funcall outline-level)))
  5001. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5002. (diff (abs (- level (length up-head) -1))))
  5003. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5004. (replace-match up-head nil t)
  5005. ;; Fixup tag positioning
  5006. (and org-auto-align-tags (org-set-tags nil t))
  5007. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5008. (defun org-demote ()
  5009. "Demote the current heading lower down the tree.
  5010. If the region is active in `transient-mark-mode', demote all headings
  5011. in the region."
  5012. (org-back-to-heading t)
  5013. (let* ((level (save-match-data (funcall outline-level)))
  5014. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5015. (diff (abs (- level (length down-head) -1))))
  5016. (replace-match down-head nil t)
  5017. ;; Fixup tag positioning
  5018. (and org-auto-align-tags (org-set-tags nil t))
  5019. (if org-adapt-indentation (org-fixup-indentation diff))))
  5020. (defun org-map-tree (fun)
  5021. "Call FUN for every heading underneath the current one."
  5022. (org-back-to-heading)
  5023. (let ((level (funcall outline-level)))
  5024. (save-excursion
  5025. (funcall fun)
  5026. (while (and (progn
  5027. (outline-next-heading)
  5028. (> (funcall outline-level) level))
  5029. (not (eobp)))
  5030. (funcall fun)))))
  5031. (defun org-map-region (fun beg end)
  5032. "Call FUN for every heading between BEG and END."
  5033. (let ((org-ignore-region t))
  5034. (save-excursion
  5035. (setq end (copy-marker end))
  5036. (goto-char beg)
  5037. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5038. (< (point) end))
  5039. (funcall fun))
  5040. (while (and (progn
  5041. (outline-next-heading)
  5042. (< (point) end))
  5043. (not (eobp)))
  5044. (funcall fun)))))
  5045. (defun org-fixup-indentation (diff)
  5046. "Change the indentation in the current entry by DIFF
  5047. However, if any line in the current entry has no indentation, or if it
  5048. would end up with no indentation after the change, nothing at all is done."
  5049. (save-excursion
  5050. (let ((end (save-excursion (outline-next-heading)
  5051. (point-marker)))
  5052. (prohibit (if (> diff 0)
  5053. "^\\S-"
  5054. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5055. col)
  5056. (unless (save-excursion (end-of-line 1)
  5057. (re-search-forward prohibit end t))
  5058. (while (and (< (point) end)
  5059. (re-search-forward "^[ \t]+" end t))
  5060. (goto-char (match-end 0))
  5061. (setq col (current-column))
  5062. (if (< diff 0) (replace-match ""))
  5063. (org-indent-to-column (+ diff col))))
  5064. (move-marker end nil))))
  5065. (defun org-convert-to-odd-levels ()
  5066. "Convert an org-mode file with all levels allowed to one with odd levels.
  5067. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5068. level 5 etc."
  5069. (interactive)
  5070. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5071. (let ((org-odd-levels-only nil) n)
  5072. (save-excursion
  5073. (goto-char (point-min))
  5074. (while (re-search-forward "^\\*\\*+ " nil t)
  5075. (setq n (- (length (match-string 0)) 2))
  5076. (while (>= (setq n (1- n)) 0)
  5077. (org-demote))
  5078. (end-of-line 1))))))
  5079. (defun org-convert-to-oddeven-levels ()
  5080. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5081. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5082. section with an even level, conversion would destroy the structure of the file. An error
  5083. is signaled in this case."
  5084. (interactive)
  5085. (goto-char (point-min))
  5086. ;; First check if there are no even levels
  5087. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5088. (org-show-context t)
  5089. (error "Not all levels are odd in this file. Conversion not possible."))
  5090. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5091. (let ((org-odd-levels-only nil) n)
  5092. (save-excursion
  5093. (goto-char (point-min))
  5094. (while (re-search-forward "^\\*\\*+ " nil t)
  5095. (setq n (/ (1- (length (match-string 0))) 2))
  5096. (while (>= (setq n (1- n)) 0)
  5097. (org-promote))
  5098. (end-of-line 1))))))
  5099. (defun org-tr-level (n)
  5100. "Make N odd if required."
  5101. (if org-odd-levels-only (1+ (/ n 2)) n))
  5102. ;;; Vertical tree motion, cutting and pasting of subtrees
  5103. (defun org-move-subtree-up (&optional arg)
  5104. "Move the current subtree up past ARG headlines of the same level."
  5105. (interactive "p")
  5106. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5107. (defun org-move-subtree-down (&optional arg)
  5108. "Move the current subtree down past ARG headlines of the same level."
  5109. (interactive "p")
  5110. (setq arg (prefix-numeric-value arg))
  5111. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5112. 'outline-get-last-sibling))
  5113. (ins-point (make-marker))
  5114. (cnt (abs arg))
  5115. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5116. ;; Select the tree
  5117. (org-back-to-heading)
  5118. (setq beg0 (point))
  5119. (save-excursion
  5120. (setq ne-beg (org-back-over-empty-lines))
  5121. (setq beg (point)))
  5122. (save-match-data
  5123. (save-excursion (outline-end-of-heading)
  5124. (setq folded (org-invisible-p)))
  5125. (outline-end-of-subtree))
  5126. (outline-next-heading)
  5127. (setq ne-end (org-back-over-empty-lines))
  5128. (setq end (point))
  5129. (goto-char beg0)
  5130. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5131. ;; include less whitespace
  5132. (save-excursion
  5133. (goto-char beg)
  5134. (forward-line (- ne-beg ne-end))
  5135. (setq beg (point))))
  5136. ;; Find insertion point, with error handling
  5137. (while (> cnt 0)
  5138. (or (and (funcall movfunc) (looking-at outline-regexp))
  5139. (progn (goto-char beg0)
  5140. (error "Cannot move past superior level or buffer limit")))
  5141. (setq cnt (1- cnt)))
  5142. (if (> arg 0)
  5143. ;; Moving forward - still need to move over subtree
  5144. (progn (org-end-of-subtree t t)
  5145. (save-excursion
  5146. (org-back-over-empty-lines)
  5147. (or (bolp) (newline)))))
  5148. (setq ne-ins (org-back-over-empty-lines))
  5149. (move-marker ins-point (point))
  5150. (setq txt (buffer-substring beg end))
  5151. (org-save-markers-in-region beg end)
  5152. (delete-region beg end)
  5153. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5154. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5155. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5156. (let ((bbb (point)))
  5157. (insert-before-markers txt)
  5158. (org-reinstall-markers-in-region bbb)
  5159. (move-marker ins-point bbb))
  5160. (or (bolp) (insert "\n"))
  5161. (setq ins-end (point))
  5162. (goto-char ins-point)
  5163. (org-skip-whitespace)
  5164. (when (and (< arg 0)
  5165. (org-first-sibling-p)
  5166. (> ne-ins ne-beg))
  5167. ;; Move whitespace back to beginning
  5168. (save-excursion
  5169. (goto-char ins-end)
  5170. (let ((kill-whole-line t))
  5171. (kill-line (- ne-ins ne-beg)) (point)))
  5172. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5173. (move-marker ins-point nil)
  5174. (org-compact-display-after-subtree-move)
  5175. (org-show-empty-lines-in-parent)
  5176. (unless folded
  5177. (org-show-entry)
  5178. (show-children)
  5179. (org-cycle-hide-drawers 'children))))
  5180. (defvar org-subtree-clip ""
  5181. "Clipboard for cut and paste of subtrees.
  5182. This is actually only a copy of the kill, because we use the normal kill
  5183. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5184. (defvar org-subtree-clip-folded nil
  5185. "Was the last copied subtree folded?
  5186. This is used to fold the tree back after pasting.")
  5187. (defun org-cut-subtree (&optional n)
  5188. "Cut the current subtree into the clipboard.
  5189. With prefix arg N, cut this many sequential subtrees.
  5190. This is a short-hand for marking the subtree and then cutting it."
  5191. (interactive "p")
  5192. (org-copy-subtree n 'cut))
  5193. (defun org-copy-subtree (&optional n cut force-store-markers)
  5194. "Cut the current subtree into the clipboard.
  5195. With prefix arg N, cut this many sequential subtrees.
  5196. This is a short-hand for marking the subtree and then copying it.
  5197. If CUT is non-nil, actually cut the subtree.
  5198. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5199. of some markers in the region, even if CUT is non-nil. This is
  5200. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5201. (interactive "p")
  5202. (let (beg end folded (beg0 (point)))
  5203. (if (interactive-p)
  5204. (org-back-to-heading nil) ; take what looks like a subtree
  5205. (org-back-to-heading t)) ; take what is really there
  5206. (org-back-over-empty-lines)
  5207. (setq beg (point))
  5208. (skip-chars-forward " \t\r\n")
  5209. (save-match-data
  5210. (save-excursion (outline-end-of-heading)
  5211. (setq folded (org-invisible-p)))
  5212. (condition-case nil
  5213. (outline-forward-same-level (1- n))
  5214. (error nil))
  5215. (org-end-of-subtree t t))
  5216. (org-back-over-empty-lines)
  5217. (setq end (point))
  5218. (goto-char beg0)
  5219. (when (> end beg)
  5220. (setq org-subtree-clip-folded folded)
  5221. (when (or cut force-store-markers)
  5222. (org-save-markers-in-region beg end))
  5223. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5224. (setq org-subtree-clip (current-kill 0))
  5225. (message "%s: Subtree(s) with %d characters"
  5226. (if cut "Cut" "Copied")
  5227. (length org-subtree-clip)))))
  5228. (defun org-paste-subtree (&optional level tree for-yank)
  5229. "Paste the clipboard as a subtree, with modification of headline level.
  5230. The entire subtree is promoted or demoted in order to match a new headline
  5231. level.
  5232. If the cursor is at the beginning of a headline, the same level as
  5233. that headline is used to paste the tree
  5234. If not, the new level is derived from the *visible* headings
  5235. before and after the insertion point, and taken to be the inferior headline
  5236. level of the two. So if the previous visible heading is level 3 and the
  5237. next is level 4 (or vice versa), level 4 will be used for insertion.
  5238. This makes sure that the subtree remains an independent subtree and does
  5239. not swallow low level entries.
  5240. You can also force a different level, either by using a numeric prefix
  5241. argument, or by inserting the heading marker by hand. For example, if the
  5242. cursor is after \"*****\", then the tree will be shifted to level 5.
  5243. If optional TREE is given, use this text instead of the kill ring.
  5244. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5245. move back over whitespace before inserting, and move point to the end of
  5246. the inserted text when done."
  5247. (interactive "P")
  5248. (unless (org-kill-is-subtree-p tree)
  5249. (error "%s"
  5250. (substitute-command-keys
  5251. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5252. (let* ((visp (not (org-invisible-p)))
  5253. (txt (or tree (and kill-ring (current-kill 0))))
  5254. (^re (concat "^\\(" outline-regexp "\\)"))
  5255. (re (concat "\\(" outline-regexp "\\)"))
  5256. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5257. (old-level (if (string-match ^re txt)
  5258. (- (match-end 0) (match-beginning 0) 1)
  5259. -1))
  5260. (force-level (cond (level (prefix-numeric-value level))
  5261. ((and (looking-at "[ \t]*$")
  5262. (string-match
  5263. ^re_ (buffer-substring
  5264. (point-at-bol) (point))))
  5265. (- (match-end 1) (match-beginning 1)))
  5266. ((and (bolp)
  5267. (looking-at org-outline-regexp))
  5268. (- (match-end 0) (point) 1))
  5269. (t nil)))
  5270. (previous-level (save-excursion
  5271. (condition-case nil
  5272. (progn
  5273. (outline-previous-visible-heading 1)
  5274. (if (looking-at re)
  5275. (- (match-end 0) (match-beginning 0) 1)
  5276. 1))
  5277. (error 1))))
  5278. (next-level (save-excursion
  5279. (condition-case nil
  5280. (progn
  5281. (or (looking-at outline-regexp)
  5282. (outline-next-visible-heading 1))
  5283. (if (looking-at re)
  5284. (- (match-end 0) (match-beginning 0) 1)
  5285. 1))
  5286. (error 1))))
  5287. (new-level (or force-level (max previous-level next-level)))
  5288. (shift (if (or (= old-level -1)
  5289. (= new-level -1)
  5290. (= old-level new-level))
  5291. 0
  5292. (- new-level old-level)))
  5293. (delta (if (> shift 0) -1 1))
  5294. (func (if (> shift 0) 'org-demote 'org-promote))
  5295. (org-odd-levels-only nil)
  5296. beg end newend)
  5297. ;; Remove the forced level indicator
  5298. (if force-level
  5299. (delete-region (point-at-bol) (point)))
  5300. ;; Paste
  5301. (beginning-of-line 1)
  5302. (unless for-yank (org-back-over-empty-lines))
  5303. (setq beg (point))
  5304. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5305. (insert-before-markers txt)
  5306. (unless (string-match "\n\\'" txt) (insert "\n"))
  5307. (setq newend (point))
  5308. (org-reinstall-markers-in-region beg)
  5309. (setq end (point))
  5310. (goto-char beg)
  5311. (skip-chars-forward " \t\n\r")
  5312. (setq beg (point))
  5313. (if (and (org-invisible-p) visp)
  5314. (save-excursion (outline-show-heading)))
  5315. ;; Shift if necessary
  5316. (unless (= shift 0)
  5317. (save-restriction
  5318. (narrow-to-region beg end)
  5319. (while (not (= shift 0))
  5320. (org-map-region func (point-min) (point-max))
  5321. (setq shift (+ delta shift)))
  5322. (goto-char (point-min))
  5323. (setq newend (point-max))))
  5324. (when (or (interactive-p) for-yank)
  5325. (message "Clipboard pasted as level %d subtree" new-level))
  5326. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5327. kill-ring
  5328. (eq org-subtree-clip (current-kill 0))
  5329. org-subtree-clip-folded)
  5330. ;; The tree was folded before it was killed/copied
  5331. (hide-subtree))
  5332. (and for-yank (goto-char newend))))
  5333. (defun org-kill-is-subtree-p (&optional txt)
  5334. "Check if the current kill is an outline subtree, or a set of trees.
  5335. Returns nil if kill does not start with a headline, or if the first
  5336. headline level is not the largest headline level in the tree.
  5337. So this will actually accept several entries of equal levels as well,
  5338. which is OK for `org-paste-subtree'.
  5339. If optional TXT is given, check this string instead of the current kill."
  5340. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5341. (start-level (and kill
  5342. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5343. org-outline-regexp "\\)")
  5344. kill)
  5345. (- (match-end 2) (match-beginning 2) 1)))
  5346. (re (concat "^" org-outline-regexp))
  5347. (start (1+ (or (match-beginning 2) -1))))
  5348. (if (not start-level)
  5349. (progn
  5350. nil) ;; does not even start with a heading
  5351. (catch 'exit
  5352. (while (setq start (string-match re kill (1+ start)))
  5353. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5354. (throw 'exit nil)))
  5355. t))))
  5356. (defvar org-markers-to-move nil
  5357. "Markers that should be moved with a cut-and-paste operation.
  5358. Those markers are stored together with their positions relative to
  5359. the start of the region.")
  5360. (defun org-save-markers-in-region (beg end)
  5361. "Check markers in region.
  5362. If these markers are between BEG and END, record their position relative
  5363. to BEG, so that after moving the block of text, we can put the markers back
  5364. into place.
  5365. This function gets called just before an entry or tree gets cut from the
  5366. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5367. called immediately, to move the markers with the entries."
  5368. (setq org-markers-to-move nil)
  5369. (when (featurep 'org-clock)
  5370. (org-clock-save-markers-for-cut-and-paste beg end))
  5371. (when (featurep 'org-agenda)
  5372. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5373. (defun org-check-and-save-marker (marker beg end)
  5374. "Check if MARKER is between BEG and END.
  5375. If yes, remember the marker and the distance to BEG."
  5376. (when (and (marker-buffer marker)
  5377. (equal (marker-buffer marker) (current-buffer)))
  5378. (if (and (>= marker beg) (< marker end))
  5379. (push (cons marker (- marker beg)) org-markers-to-move))))
  5380. (defun org-reinstall-markers-in-region (beg)
  5381. "Move all remembered markers to their position relative to BEG."
  5382. (mapc (lambda (x)
  5383. (move-marker (car x) (+ beg (cdr x))))
  5384. org-markers-to-move)
  5385. (setq org-markers-to-move nil))
  5386. (defun org-narrow-to-subtree ()
  5387. "Narrow buffer to the current subtree."
  5388. (interactive)
  5389. (save-excursion
  5390. (save-match-data
  5391. (narrow-to-region
  5392. (progn (org-back-to-heading) (point))
  5393. (progn (org-end-of-subtree t) (point))))))
  5394. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5395. "Clone the task (subtree) at point N times.
  5396. The clones will be inserted as siblings.
  5397. In interactive use, the user will be prompted for the number of clones
  5398. to be produced, and for a time SHIFT, which may be a repeater as used
  5399. in time stamps, for example `+3d'.
  5400. When a valid repeater is given and the entry contains any time stamps,
  5401. the clones will become a sequence in time, with time stamps in the
  5402. subtree shifted for each clone produced. If SHIFT is nil or the
  5403. empty string, time stamps will be left alone.
  5404. If the original subtree did contain time stamps with a repeater,
  5405. the following will happen:
  5406. - the repeater will be removed in each clone
  5407. - an additional clone will be produced, with the current, unshifted
  5408. date(s) in the entry.
  5409. - the original entry will be placed *after* all the clones, with
  5410. repeater intact.
  5411. - the start days in the repeater in the original entry will be shifted
  5412. to past the last clone.
  5413. I this way you can spell out a number of instances of a repeating task,
  5414. and still retain the repeater to cover future instances of the task."
  5415. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5416. (let (beg end template task
  5417. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5418. (if (not (and (integerp n) (> n 0)))
  5419. (error "Invalid number of replications %s" n))
  5420. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5421. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5422. shift)))
  5423. (error "Invalid shift specification %s" shift))
  5424. (when doshift
  5425. (setq shift-n (string-to-number (match-string 1 shift))
  5426. shift-what (cdr (assoc (match-string 2 shift)
  5427. '(("d" . day) ("w" . week)
  5428. ("m" . month) ("y" . year))))))
  5429. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5430. (setq nmin 1 nmax n)
  5431. (org-back-to-heading t)
  5432. (setq beg (point))
  5433. (org-end-of-subtree t t)
  5434. (setq end (point))
  5435. (setq template (buffer-substring beg end))
  5436. (when (and doshift
  5437. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5438. (delete-region beg end)
  5439. (setq end beg)
  5440. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5441. (goto-char end)
  5442. (loop for n from nmin to nmax do
  5443. (if (not doshift)
  5444. (setq task template)
  5445. (with-temp-buffer
  5446. (insert template)
  5447. (org-mode)
  5448. (goto-char (point-min))
  5449. (while (re-search-forward org-ts-regexp nil t)
  5450. (org-timestamp-change (* n shift-n) shift-what))
  5451. (unless (= n n-no-remove)
  5452. (goto-char (point-min))
  5453. (while (re-search-forward org-ts-regexp nil t)
  5454. (save-excursion
  5455. (goto-char (match-beginning 0))
  5456. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5457. (delete-region (match-beginning 1) (match-end 1))))))
  5458. (setq task (buffer-string))))
  5459. (insert task))
  5460. (goto-char beg)))
  5461. ;;; Outline Sorting
  5462. (defun org-sort (with-case)
  5463. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5464. Optional argument WITH-CASE means sort case-sensitively.
  5465. With a double prefix argument, also remove duplicate entries."
  5466. (interactive "P")
  5467. (if (org-at-table-p)
  5468. (org-call-with-arg 'org-table-sort-lines with-case)
  5469. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5470. (defun org-sort-remove-invisible (s)
  5471. (remove-text-properties 0 (length s) org-rm-props s)
  5472. (while (string-match org-bracket-link-regexp s)
  5473. (setq s (replace-match (if (match-end 2)
  5474. (match-string 3 s)
  5475. (match-string 1 s)) t t s)))
  5476. s)
  5477. (defvar org-priority-regexp) ; defined later in the file
  5478. (defvar org-after-sorting-entries-or-items-hook nil
  5479. "Hook that is run after a bunch of entries or items have been sorted.
  5480. When children are sorted, the cursor is in the parent line when this
  5481. hook gets called. When a region or a plain list is sorted, the cursor
  5482. will be in the first entry of the sorted region/list.")
  5483. (defun org-sort-entries-or-items
  5484. (&optional with-case sorting-type getkey-func compare-func property)
  5485. "Sort entries on a certain level of an outline tree, or plain list items.
  5486. If there is an active region, the entries in the region are sorted.
  5487. Else, if the cursor is before the first entry, sort the top-level items.
  5488. Else, the children of the entry at point are sorted.
  5489. If the cursor is at the first item in a plain list, the list items will be
  5490. sorted.
  5491. Sorting can be alphabetically, numerically, by date/time as given by
  5492. a time stamp, by a property or by priority.
  5493. The command prompts for the sorting type unless it has been given to the
  5494. function through the SORTING-TYPE argument, which needs to a character,
  5495. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5496. precise meaning of each character:
  5497. n Numerically, by converting the beginning of the entry/item to a number.
  5498. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5499. t By date/time, either the first active time stamp in the entry, or, if
  5500. none exist, by the first inactive one.
  5501. In items, only the first line will be chekced.
  5502. s By the scheduled date/time.
  5503. d By deadline date/time.
  5504. c By creation time, which is assumed to be the first inactive time stamp
  5505. at the beginning of a line.
  5506. p By priority according to the cookie.
  5507. r By the value of a property.
  5508. Capital letters will reverse the sort order.
  5509. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5510. called with point at the beginning of the record. It must return either
  5511. a string or a number that should serve as the sorting key for that record.
  5512. Comparing entries ignores case by default. However, with an optional argument
  5513. WITH-CASE, the sorting considers case as well."
  5514. (interactive "P")
  5515. (let ((case-func (if with-case 'identity 'downcase))
  5516. start beg end stars re re2
  5517. txt what tmp plain-list-p)
  5518. ;; Find beginning and end of region to sort
  5519. (cond
  5520. ((org-region-active-p)
  5521. ;; we will sort the region
  5522. (setq end (region-end)
  5523. what "region")
  5524. (goto-char (region-beginning))
  5525. (if (not (org-on-heading-p)) (outline-next-heading))
  5526. (setq start (point)))
  5527. ((org-at-item-p)
  5528. ;; we will sort this plain list
  5529. (org-beginning-of-item-list) (setq start (point))
  5530. (org-end-of-item-list) (setq end (point))
  5531. (goto-char start)
  5532. (setq plain-list-p t
  5533. what "plain list"))
  5534. ((or (org-on-heading-p)
  5535. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5536. ;; we will sort the children of the current headline
  5537. (org-back-to-heading)
  5538. (setq start (point)
  5539. end (progn (org-end-of-subtree t t)
  5540. (org-back-over-empty-lines)
  5541. (point))
  5542. what "children")
  5543. (goto-char start)
  5544. (show-subtree)
  5545. (outline-next-heading))
  5546. (t
  5547. ;; we will sort the top-level entries in this file
  5548. (goto-char (point-min))
  5549. (or (org-on-heading-p) (outline-next-heading))
  5550. (setq start (point) end (point-max) what "top-level")
  5551. (goto-char start)
  5552. (show-all)))
  5553. (setq beg (point))
  5554. (if (>= beg end) (error "Nothing to sort"))
  5555. (unless plain-list-p
  5556. (looking-at "\\(\\*+\\)")
  5557. (setq stars (match-string 1)
  5558. re (concat "^" (regexp-quote stars) " +")
  5559. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5560. txt (buffer-substring beg end))
  5561. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5562. (if (and (not (equal stars "*")) (string-match re2 txt))
  5563. (error "Region to sort contains a level above the first entry")))
  5564. (unless sorting-type
  5565. (message
  5566. (if plain-list-p
  5567. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5568. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5569. [t]ime [s]cheduled [d]eadline [c]reated
  5570. A/N/T/S/D/C/P/O/F means reversed:")
  5571. what)
  5572. (setq sorting-type (read-char-exclusive))
  5573. (and (= (downcase sorting-type) ?f)
  5574. (setq getkey-func
  5575. (org-ido-completing-read "Sort using function: "
  5576. obarray 'fboundp t nil nil))
  5577. (setq getkey-func (intern getkey-func)))
  5578. (and (= (downcase sorting-type) ?r)
  5579. (setq property
  5580. (org-ido-completing-read "Property: "
  5581. (mapcar 'list (org-buffer-property-keys t))
  5582. nil t))))
  5583. (message "Sorting entries...")
  5584. (save-restriction
  5585. (narrow-to-region start end)
  5586. (let ((dcst (downcase sorting-type))
  5587. (case-fold-search nil)
  5588. (now (current-time)))
  5589. (sort-subr
  5590. (/= dcst sorting-type)
  5591. ;; This function moves to the beginning character of the "record" to
  5592. ;; be sorted.
  5593. (if plain-list-p
  5594. (lambda nil
  5595. (if (org-at-item-p) t (goto-char (point-max))))
  5596. (lambda nil
  5597. (if (re-search-forward re nil t)
  5598. (goto-char (match-beginning 0))
  5599. (goto-char (point-max)))))
  5600. ;; This function moves to the last character of the "record" being
  5601. ;; sorted.
  5602. (if plain-list-p
  5603. 'org-end-of-item
  5604. (lambda nil
  5605. (save-match-data
  5606. (condition-case nil
  5607. (outline-forward-same-level 1)
  5608. (error
  5609. (goto-char (point-max)))))))
  5610. ;; This function returns the value that gets sorted against.
  5611. (if plain-list-p
  5612. (lambda nil
  5613. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5614. (cond
  5615. ((= dcst ?n)
  5616. (string-to-number (buffer-substring (match-end 0)
  5617. (point-at-eol))))
  5618. ((= dcst ?a)
  5619. (buffer-substring (match-end 0) (point-at-eol)))
  5620. ((= dcst ?t)
  5621. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5622. (re-search-forward org-ts-regexp-both
  5623. (point-at-eol) t))
  5624. (org-time-string-to-seconds (match-string 0))
  5625. (time-to-seconds now)))
  5626. ((= dcst ?f)
  5627. (if getkey-func
  5628. (progn
  5629. (setq tmp (funcall getkey-func))
  5630. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5631. tmp)
  5632. (error "Invalid key function `%s'" getkey-func)))
  5633. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5634. (lambda nil
  5635. (cond
  5636. ((= dcst ?n)
  5637. (if (looking-at org-complex-heading-regexp)
  5638. (string-to-number (match-string 4))
  5639. nil))
  5640. ((= dcst ?a)
  5641. (if (looking-at org-complex-heading-regexp)
  5642. (funcall case-func (match-string 4))
  5643. nil))
  5644. ((= dcst ?t)
  5645. (let ((end (save-excursion (outline-next-heading) (point))))
  5646. (if (or (re-search-forward org-ts-regexp end t)
  5647. (re-search-forward org-ts-regexp-both end t))
  5648. (org-time-string-to-seconds (match-string 0))
  5649. (time-to-seconds now))))
  5650. ((= dcst ?c)
  5651. (let ((end (save-excursion (outline-next-heading) (point))))
  5652. (if (re-search-forward
  5653. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5654. end t)
  5655. (org-time-string-to-seconds (match-string 0))
  5656. (time-to-seconds now))))
  5657. ((= dcst ?s)
  5658. (let ((end (save-excursion (outline-next-heading) (point))))
  5659. (if (re-search-forward org-scheduled-time-regexp end t)
  5660. (org-time-string-to-seconds (match-string 1))
  5661. (time-to-seconds now))))
  5662. ((= dcst ?d)
  5663. (let ((end (save-excursion (outline-next-heading) (point))))
  5664. (if (re-search-forward org-deadline-time-regexp end t)
  5665. (org-time-string-to-seconds (match-string 1))
  5666. (time-to-seconds now))))
  5667. ((= dcst ?p)
  5668. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5669. (string-to-char (match-string 2))
  5670. org-default-priority))
  5671. ((= dcst ?r)
  5672. (or (org-entry-get nil property) ""))
  5673. ((= dcst ?o)
  5674. (if (looking-at org-complex-heading-regexp)
  5675. (- 9999 (length (member (match-string 2)
  5676. org-todo-keywords-1)))))
  5677. ((= dcst ?f)
  5678. (if getkey-func
  5679. (progn
  5680. (setq tmp (funcall getkey-func))
  5681. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5682. tmp)
  5683. (error "Invalid key function `%s'" getkey-func)))
  5684. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5685. nil
  5686. (cond
  5687. ((= dcst ?a) 'string<)
  5688. ((= dcst ?t) 'time-less-p)
  5689. ((= dcst ?f) compare-func)
  5690. ((= dcst ?p) '<)
  5691. (t nil)))))
  5692. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5693. (message "Sorting entries...done")))
  5694. (defun org-do-sort (table what &optional with-case sorting-type)
  5695. "Sort TABLE of WHAT according to SORTING-TYPE.
  5696. The user will be prompted for the SORTING-TYPE if the call to this
  5697. function does not specify it. WHAT is only for the prompt, to indicate
  5698. what is being sorted. The sorting key will be extracted from
  5699. the car of the elements of the table.
  5700. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5701. (unless sorting-type
  5702. (message
  5703. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5704. what)
  5705. (setq sorting-type (read-char-exclusive)))
  5706. (let ((dcst (downcase sorting-type))
  5707. extractfun comparefun)
  5708. ;; Define the appropriate functions
  5709. (cond
  5710. ((= dcst ?n)
  5711. (setq extractfun 'string-to-number
  5712. comparefun (if (= dcst sorting-type) '< '>)))
  5713. ((= dcst ?a)
  5714. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5715. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5716. comparefun (if (= dcst sorting-type)
  5717. 'string<
  5718. (lambda (a b) (and (not (string< a b))
  5719. (not (string= a b)))))))
  5720. ((= dcst ?t)
  5721. (setq extractfun
  5722. (lambda (x)
  5723. (if (or (string-match org-ts-regexp x)
  5724. (string-match org-ts-regexp-both x))
  5725. (time-to-seconds
  5726. (org-time-string-to-time (match-string 0 x)))
  5727. 0))
  5728. comparefun (if (= dcst sorting-type) '< '>)))
  5729. (t (error "Invalid sorting type `%c'" sorting-type)))
  5730. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5731. table)
  5732. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5733. ;;; Editing source examples
  5734. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5735. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5736. (defvar org-edit-src-force-single-line nil)
  5737. (defvar org-edit-src-from-org-mode nil)
  5738. (defvar org-edit-src-picture nil)
  5739. (defvar org-edit-src-beg-marker nil)
  5740. (defvar org-edit-src-end-marker nil)
  5741. (defvar org-edit-src-overlay nil)
  5742. (define-minor-mode org-exit-edit-mode
  5743. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5744. (defun org-edit-src-code ()
  5745. "Edit the source code example at point.
  5746. The example is copied to a separate buffer, and that buffer is switched
  5747. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  5748. This will remove the original code in the Org buffer, and replace it with
  5749. the edited version."
  5750. (interactive)
  5751. (let ((line (org-current-line))
  5752. (case-fold-search t)
  5753. (msg (substitute-command-keys
  5754. "Edit, then exit with C-c ' (C-c and single quote)"))
  5755. (info (org-edit-src-find-region-and-lang))
  5756. (org-mode-p (eq major-mode 'org-mode))
  5757. (beg (make-marker))
  5758. (end (make-marker))
  5759. ovl lang lang-f single lfmt code begline buffer)
  5760. (if (not info)
  5761. nil
  5762. (setq beg (move-marker beg (nth 0 info))
  5763. end (move-marker end (nth 1 info))
  5764. code (buffer-substring-no-properties beg end)
  5765. lang (nth 2 info)
  5766. single (nth 3 info)
  5767. lfmt (nth 4 info)
  5768. lang-f (intern (concat lang "-mode"))
  5769. begline (save-excursion (goto-char beg) (org-current-line)))
  5770. (unless (functionp lang-f)
  5771. (error "No such language mode: %s" lang-f))
  5772. (goto-line line)
  5773. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  5774. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  5775. (switch-to-buffer buffer)
  5776. (when buffer
  5777. (with-current-buffer buffer
  5778. (if (boundp 'org-edit-src-overlay)
  5779. (org-delete-overlay org-edit-src-overlay)))
  5780. (kill-buffer buffer))
  5781. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  5782. (setq ovl (org-make-overlay beg end))
  5783. (org-overlay-put ovl 'face 'secondary-selection)
  5784. (org-overlay-put ovl 'edit-buffer buffer)
  5785. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  5786. (org-overlay-put ovl 'face 'secondary-selection)
  5787. (org-overlay-put ovl
  5788. 'keymap
  5789. (let ((map (make-sparse-keymap)))
  5790. (define-key map [mouse-1] 'org-edit-src-continue)
  5791. map))
  5792. (org-overlay-put ovl :read-only "Leave me alone")
  5793. (switch-to-buffer buffer)
  5794. (insert code)
  5795. (remove-text-properties (point-min) (point-max)
  5796. '(display nil invisible nil intangible nil))
  5797. (let ((org-inhibit-startup t))
  5798. (funcall lang-f))
  5799. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5800. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5801. (when lfmt
  5802. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5803. (when org-mode-p
  5804. (goto-char (point-min))
  5805. (while (re-search-forward "^," nil t)
  5806. (replace-match "")))
  5807. (goto-line (1+ (- line begline)))
  5808. (org-exit-edit-mode)
  5809. (org-set-local 'org-edit-src-beg-marker beg)
  5810. (org-set-local 'org-edit-src-end-marker end)
  5811. (org-set-local 'org-edit-src-overlay ovl)
  5812. (and org-edit-src-persistent-message
  5813. (org-set-local 'header-line-format msg)))
  5814. (message "%s" msg)
  5815. t)))
  5816. (defun org-edit-src-continue (e)
  5817. (interactive "e")
  5818. (mouse-set-point e)
  5819. (let ((buf (get-char-property (point) 'edit-buffer)))
  5820. (if buf (switch-to-buffer buf)
  5821. (error "Something is wrong here"))))
  5822. (defun org-edit-src-find-buffer (beg end)
  5823. "Find a source editing buffer that is already editing the region BEG to END."
  5824. (catch 'exit
  5825. (mapc
  5826. (lambda (b)
  5827. (with-current-buffer b
  5828. (if (and (string-match "\\`*Org Edit " (buffer-name))
  5829. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  5830. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  5831. (equal beg org-edit-src-beg-marker)
  5832. (equal end org-edit-src-end-marker))
  5833. (throw 'exit (current-buffer)))))
  5834. (buffer-list))
  5835. nil))
  5836. (defun org-edit-fixed-width-region ()
  5837. "Edit the fixed-width ascii drawing at point.
  5838. This must be a region where each line starts with a colon followed by
  5839. a space character.
  5840. An new buffer is created and the fixed-width region is copied into it,
  5841. and the buffer is switched into `artist-mode' for editing. When done,
  5842. exit with \\[org-edit-src-exit]. The edited text will then replace
  5843. the fragment in the Org-mode buffer."
  5844. (interactive)
  5845. (let ((line (org-current-line))
  5846. (case-fold-search t)
  5847. (msg (substitute-command-keys
  5848. "Edit, then exit with C-c ' (C-c and single quote)"))
  5849. (org-mode-p (eq major-mode 'org-mode))
  5850. (beg (make-marker))
  5851. (end (make-marker))
  5852. ovl beg1 end1 code begline buffer)
  5853. (beginning-of-line 1)
  5854. (if (looking-at "[ \t]*[^:\n \t]")
  5855. nil
  5856. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5857. (setq beg1 (point) end1 beg1)
  5858. (save-excursion
  5859. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5860. (setq beg1 (point-at-bol 2))
  5861. (setq beg1 (point))))
  5862. (save-excursion
  5863. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5864. (setq end1 (1- (match-beginning 0)))
  5865. (setq end1 (point))))
  5866. (goto-line line))
  5867. (setq beg (move-marker beg beg1)
  5868. end (move-marker end end1)
  5869. code (buffer-substring-no-properties beg end)
  5870. begline (save-excursion (goto-char beg) (org-current-line)))
  5871. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  5872. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  5873. (switch-to-buffer buffer)
  5874. (when buffer
  5875. (with-current-buffer buffer
  5876. (if (boundp 'org-edit-src-overlay)
  5877. (org-delete-overlay org-edit-src-overlay)))
  5878. (kill-buffer buffer))
  5879. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  5880. (setq ovl (org-make-overlay beg end))
  5881. (org-overlay-put ovl 'face 'secondary-selection)
  5882. (org-overlay-put ovl 'edit-buffer buffer)
  5883. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  5884. (org-overlay-put ovl 'face 'secondary-selection)
  5885. (org-overlay-put ovl
  5886. 'keymap
  5887. (let ((map (make-sparse-keymap)))
  5888. (define-key map [mouse-1] 'org-edit-src-continue)
  5889. map))
  5890. (org-overlay-put ovl :read-only "Leave me alone")
  5891. (switch-to-buffer buffer)
  5892. (insert code)
  5893. (remove-text-properties (point-min) (point-max)
  5894. '(display nil invisible nil intangible nil))
  5895. (cond
  5896. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5897. (fundamental-mode)
  5898. (artist-mode 1))
  5899. (t (funcall org-edit-fixed-width-region-mode)))
  5900. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5901. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5902. (set (make-local-variable 'org-edit-src-picture) t)
  5903. (goto-char (point-min))
  5904. (while (re-search-forward "^[ \t]*: ?" nil t)
  5905. (replace-match ""))
  5906. (goto-line (1+ (- line begline)))
  5907. (org-exit-edit-mode)
  5908. (org-set-local 'org-edit-src-beg-marker beg)
  5909. (org-set-local 'org-edit-src-end-marker end)
  5910. (org-set-local 'org-edit-src-overlay ovl)
  5911. (and org-edit-src-persistent-message
  5912. (org-set-local 'header-line-format msg)))
  5913. (message "%s" msg)
  5914. t)))
  5915. (defun org-edit-src-find-region-and-lang ()
  5916. "Find the region and language for a local edit.
  5917. Return a list with beginning and end of the region, a string representing
  5918. the language, a switch telling of the content should be in a single line."
  5919. (let ((re-list
  5920. (append
  5921. org-edit-src-region-extra
  5922. '(
  5923. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5924. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5925. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5926. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5927. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5928. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5929. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5930. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5931. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5932. ("^#\\+html:" "\n" "html" single-line)
  5933. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5934. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5935. ("^#\\+latex:" "\n" "latex" single-line)
  5936. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5937. ("^#\\+ascii:" "\n" "ascii" single-line)
  5938. )))
  5939. (pos (point))
  5940. re1 re2 single beg end lang lfmt match-re1)
  5941. (catch 'exit
  5942. (while (setq entry (pop re-list))
  5943. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5944. single (nth 3 entry))
  5945. (save-excursion
  5946. (if (or (looking-at re1)
  5947. (re-search-backward re1 nil t))
  5948. (progn
  5949. (setq match-re1 (match-string 0))
  5950. (setq beg (match-end 0)
  5951. lang (org-edit-src-get-lang lang)
  5952. lfmt (org-edit-src-get-label-format match-re1))
  5953. (if (and (re-search-forward re2 nil t)
  5954. (>= (match-end 0) pos))
  5955. (throw 'exit (list beg (match-beginning 0)
  5956. lang single lfmt))))
  5957. (if (or (looking-at re2)
  5958. (re-search-forward re2 nil t))
  5959. (progn
  5960. (setq end (match-beginning 0))
  5961. (if (and (re-search-backward re1 nil t)
  5962. (<= (match-beginning 0) pos))
  5963. (progn
  5964. (setq lfmt (org-edit-src-get-label-format
  5965. (match-string 0)))
  5966. (throw 'exit
  5967. (list (match-end 0) end
  5968. (org-edit-src-get-lang lang)
  5969. single lfmt))))))))))))
  5970. (defun org-edit-src-get-lang (lang)
  5971. "Extract the src language."
  5972. (let ((m (match-string 0)))
  5973. (cond
  5974. ((stringp lang) lang)
  5975. ((integerp lang) (match-string lang))
  5976. ((and (eq lang 'lang)
  5977. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5978. (match-string 1 m))
  5979. ((and (eq lang 'style)
  5980. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5981. (match-string 1 m))
  5982. (t "fundamental"))))
  5983. (defun org-edit-src-get-label-format (s)
  5984. "Extract the label format."
  5985. (save-match-data
  5986. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5987. (match-string 1 s))))
  5988. (defun org-edit-src-exit ()
  5989. "Exit special edit and protect problematic lines."
  5990. (interactive)
  5991. (unless (string-match "\\`*Org Edit " (buffer-name (current-buffer)))
  5992. (error "This is not an sub-editing buffer, something is wrong..."))
  5993. (let ((line (if (org-bound-and-true-p org-edit-src-force-single-line)
  5994. 1
  5995. (org-current-line)))
  5996. (beg org-edit-src-beg-marker)
  5997. (end org-edit-src-end-marker)
  5998. (ovl org-edit-src-overlay)
  5999. (buffer (current-buffer))
  6000. code)
  6001. (goto-char (point-min))
  6002. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  6003. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  6004. (when (org-bound-and-true-p org-edit-src-force-single-line)
  6005. (goto-char (point-min))
  6006. (while (re-search-forward "\n" nil t)
  6007. (replace-match " "))
  6008. (goto-char (point-min))
  6009. (if (looking-at "\\s-*") (replace-match " "))
  6010. (if (re-search-forward "\\s-+\\'" nil t)
  6011. (replace-match "")))
  6012. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  6013. (goto-char (point-min))
  6014. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  6015. (replace-match ",\\1"))
  6016. (when font-lock-mode
  6017. (font-lock-unfontify-region (point-min) (point-max)))
  6018. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  6019. (when (org-bound-and-true-p org-edit-src-picture)
  6020. (untabify (point-min) (point-max))
  6021. (goto-char (point-min))
  6022. (while (re-search-forward "^" nil t)
  6023. (replace-match ": "))
  6024. (when font-lock-mode
  6025. (font-lock-unfontify-region (point-min) (point-max)))
  6026. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  6027. (setq code (buffer-string))
  6028. (switch-to-buffer (marker-buffer beg))
  6029. (kill-buffer buffer)
  6030. (goto-char beg)
  6031. (org-delete-overlay ovl)
  6032. (delete-region beg end)
  6033. (insert code)
  6034. (goto-char beg)
  6035. (goto-line (1- (+ (org-current-line) line)))
  6036. (move-marker beg nil)
  6037. (move-marker end nil)))
  6038. ;;; The orgstruct minor mode
  6039. ;; Define a minor mode which can be used in other modes in order to
  6040. ;; integrate the org-mode structure editing commands.
  6041. ;; This is really a hack, because the org-mode structure commands use
  6042. ;; keys which normally belong to the major mode. Here is how it
  6043. ;; works: The minor mode defines all the keys necessary to operate the
  6044. ;; structure commands, but wraps the commands into a function which
  6045. ;; tests if the cursor is currently at a headline or a plain list
  6046. ;; item. If that is the case, the structure command is used,
  6047. ;; temporarily setting many Org-mode variables like regular
  6048. ;; expressions for filling etc. However, when any of those keys is
  6049. ;; used at a different location, function uses `key-binding' to look
  6050. ;; up if the key has an associated command in another currently active
  6051. ;; keymap (minor modes, major mode, global), and executes that
  6052. ;; command. There might be problems if any of the keys is otherwise
  6053. ;; used as a prefix key.
  6054. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6055. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6056. ;; addresses this by checking explicitly for both bindings.
  6057. (defvar orgstruct-mode-map (make-sparse-keymap)
  6058. "Keymap for the minor `orgstruct-mode'.")
  6059. (defvar org-local-vars nil
  6060. "List of local variables, for use by `orgstruct-mode'")
  6061. ;;;###autoload
  6062. (define-minor-mode orgstruct-mode
  6063. "Toggle the minor more `orgstruct-mode'.
  6064. This mode is for using Org-mode structure commands in other modes.
  6065. The following key behave as if Org-mode was active, if the cursor
  6066. is on a headline, or on a plain list item (both in the definition
  6067. of Org-mode).
  6068. M-up Move entry/item up
  6069. M-down Move entry/item down
  6070. M-left Promote
  6071. M-right Demote
  6072. M-S-up Move entry/item up
  6073. M-S-down Move entry/item down
  6074. M-S-left Promote subtree
  6075. M-S-right Demote subtree
  6076. M-q Fill paragraph and items like in Org-mode
  6077. C-c ^ Sort entries
  6078. C-c - Cycle list bullet
  6079. TAB Cycle item visibility
  6080. M-RET Insert new heading/item
  6081. S-M-RET Insert new TODO heading / Checkbox item
  6082. C-c C-c Set tags / toggle checkbox"
  6083. nil " OrgStruct" nil
  6084. (org-load-modules-maybe)
  6085. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6086. ;;;###autoload
  6087. (defun turn-on-orgstruct ()
  6088. "Unconditionally turn on `orgstruct-mode'."
  6089. (orgstruct-mode 1))
  6090. (defun orgstruct++-mode (&optional arg)
  6091. "Toggle `orgstruct-mode', the enhanced version of it.
  6092. In addition to setting orgstruct-mode, this also exports all indentation and
  6093. autofilling variables from org-mode into the buffer. It will also
  6094. recognize item context in multiline items.
  6095. Note that turning off orgstruct-mode will *not* remove the
  6096. indentation/paragraph settings. This can only be done by refreshing the
  6097. major mode, for example with \[normal-mode]."
  6098. (interactive "P")
  6099. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6100. (if (< arg 1)
  6101. (orgstruct-mode -1)
  6102. (orgstruct-mode 1)
  6103. (let (var val)
  6104. (mapc
  6105. (lambda (x)
  6106. (when (string-match
  6107. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6108. (symbol-name (car x)))
  6109. (setq var (car x) val (nth 1 x))
  6110. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6111. org-local-vars)
  6112. (org-set-local 'orgstruct-is-++ t))))
  6113. (defvar orgstruct-is-++ nil
  6114. "Is orgstruct-mode in ++ version in the current-buffer?")
  6115. (make-variable-buffer-local 'orgstruct-is-++)
  6116. ;;;###autoload
  6117. (defun turn-on-orgstruct++ ()
  6118. "Unconditionally turn on `orgstruct++-mode'."
  6119. (orgstruct++-mode 1))
  6120. (defun orgstruct-error ()
  6121. "Error when there is no default binding for a structure key."
  6122. (interactive)
  6123. (error "This key has no function outside structure elements"))
  6124. (defun orgstruct-setup ()
  6125. "Setup orgstruct keymaps."
  6126. (let ((nfunc 0)
  6127. (bindings
  6128. (list
  6129. '([(meta up)] org-metaup)
  6130. '([(meta down)] org-metadown)
  6131. '([(meta left)] org-metaleft)
  6132. '([(meta right)] org-metaright)
  6133. '([(meta shift up)] org-shiftmetaup)
  6134. '([(meta shift down)] org-shiftmetadown)
  6135. '([(meta shift left)] org-shiftmetaleft)
  6136. '([(meta shift right)] org-shiftmetaright)
  6137. '([(shift up)] org-shiftup)
  6138. '([(shift down)] org-shiftdown)
  6139. '([(shift left)] org-shiftleft)
  6140. '([(shift right)] org-shiftright)
  6141. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6142. '("\M-q" fill-paragraph)
  6143. '("\C-c^" org-sort)
  6144. '("\C-c-" org-cycle-list-bullet)))
  6145. elt key fun cmd)
  6146. (while (setq elt (pop bindings))
  6147. (setq nfunc (1+ nfunc))
  6148. (setq key (org-key (car elt))
  6149. fun (nth 1 elt)
  6150. cmd (orgstruct-make-binding fun nfunc key))
  6151. (org-defkey orgstruct-mode-map key cmd))
  6152. ;; Special treatment needed for TAB and RET
  6153. (org-defkey orgstruct-mode-map [(tab)]
  6154. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6155. (org-defkey orgstruct-mode-map "\C-i"
  6156. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6157. (org-defkey orgstruct-mode-map "\M-\C-m"
  6158. (orgstruct-make-binding 'org-insert-heading 105
  6159. "\M-\C-m" [(meta return)]))
  6160. (org-defkey orgstruct-mode-map [(meta return)]
  6161. (orgstruct-make-binding 'org-insert-heading 106
  6162. [(meta return)] "\M-\C-m"))
  6163. (org-defkey orgstruct-mode-map [(shift meta return)]
  6164. (orgstruct-make-binding 'org-insert-todo-heading 107
  6165. [(meta return)] "\M-\C-m"))
  6166. (unless org-local-vars
  6167. (setq org-local-vars (org-get-local-variables)))
  6168. t))
  6169. (defun orgstruct-make-binding (fun n &rest keys)
  6170. "Create a function for binding in the structure minor mode.
  6171. FUN is the command to call inside a table. N is used to create a unique
  6172. command name. KEYS are keys that should be checked in for a command
  6173. to execute outside of tables."
  6174. (eval
  6175. (list 'defun
  6176. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6177. '(arg)
  6178. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6179. "Outside of structure, run the binding of `"
  6180. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6181. "'.")
  6182. '(interactive "p")
  6183. (list 'if
  6184. `(org-context-p 'headline 'item
  6185. (and orgstruct-is-++
  6186. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6187. 'item-body))
  6188. (list 'org-run-like-in-org-mode (list 'quote fun))
  6189. (list 'let '(orgstruct-mode)
  6190. (list 'call-interactively
  6191. (append '(or)
  6192. (mapcar (lambda (k)
  6193. (list 'key-binding k))
  6194. keys)
  6195. '('orgstruct-error))))))))
  6196. (defun org-context-p (&rest contexts)
  6197. "Check if local context is any of CONTEXTS.
  6198. Possible values in the list of contexts are `table', `headline', and `item'."
  6199. (let ((pos (point)))
  6200. (goto-char (point-at-bol))
  6201. (prog1 (or (and (memq 'table contexts)
  6202. (looking-at "[ \t]*|"))
  6203. (and (memq 'headline contexts)
  6204. ;;????????? (looking-at "\\*+"))
  6205. (looking-at outline-regexp))
  6206. (and (memq 'item contexts)
  6207. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6208. (and (memq 'item-body contexts)
  6209. (org-in-item-p)))
  6210. (goto-char pos))))
  6211. (defun org-get-local-variables ()
  6212. "Return a list of all local variables in an org-mode buffer."
  6213. (let (varlist)
  6214. (with-current-buffer (get-buffer-create "*Org tmp*")
  6215. (erase-buffer)
  6216. (org-mode)
  6217. (setq varlist (buffer-local-variables)))
  6218. (kill-buffer "*Org tmp*")
  6219. (delq nil
  6220. (mapcar
  6221. (lambda (x)
  6222. (setq x
  6223. (if (symbolp x)
  6224. (list x)
  6225. (list (car x) (list 'quote (cdr x)))))
  6226. (if (string-match
  6227. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6228. (symbol-name (car x)))
  6229. x nil))
  6230. varlist))))
  6231. ;;;###autoload
  6232. (defun org-run-like-in-org-mode (cmd)
  6233. "Run a command, pretending that the current buffer is in Org-mode.
  6234. This will temporarily bind local variables that are typically bound in
  6235. Org-mode to the values they have in Org-mode, and then interactively
  6236. call CMD."
  6237. (org-load-modules-maybe)
  6238. (unless org-local-vars
  6239. (setq org-local-vars (org-get-local-variables)))
  6240. (eval (list 'let org-local-vars
  6241. (list 'call-interactively (list 'quote cmd)))))
  6242. ;;;; Archiving
  6243. (defun org-get-category (&optional pos)
  6244. "Get the category applying to position POS."
  6245. (get-text-property (or pos (point)) 'org-category))
  6246. (defun org-refresh-category-properties ()
  6247. "Refresh category text properties in the buffer."
  6248. (let ((def-cat (cond
  6249. ((null org-category)
  6250. (if buffer-file-name
  6251. (file-name-sans-extension
  6252. (file-name-nondirectory buffer-file-name))
  6253. "???"))
  6254. ((symbolp org-category) (symbol-name org-category))
  6255. (t org-category)))
  6256. beg end cat pos optionp)
  6257. (org-unmodified
  6258. (save-excursion
  6259. (save-restriction
  6260. (widen)
  6261. (goto-char (point-min))
  6262. (put-text-property (point) (point-max) 'org-category def-cat)
  6263. (while (re-search-forward
  6264. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6265. (setq pos (match-end 0)
  6266. optionp (equal (char-after (match-beginning 0)) ?#)
  6267. cat (org-trim (match-string 2)))
  6268. (if optionp
  6269. (setq beg (point-at-bol) end (point-max))
  6270. (org-back-to-heading t)
  6271. (setq beg (point) end (org-end-of-subtree t t)))
  6272. (put-text-property beg end 'org-category cat)
  6273. (goto-char pos)))))))
  6274. ;;;; Link Stuff
  6275. ;;; Link abbreviations
  6276. (defun org-link-expand-abbrev (link)
  6277. "Apply replacements as defined in `org-link-abbrev-alist."
  6278. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6279. (let* ((key (match-string 1 link))
  6280. (as (or (assoc key org-link-abbrev-alist-local)
  6281. (assoc key org-link-abbrev-alist)))
  6282. (tag (and (match-end 2) (match-string 3 link)))
  6283. rpl)
  6284. (if (not as)
  6285. link
  6286. (setq rpl (cdr as))
  6287. (cond
  6288. ((symbolp rpl) (funcall rpl tag))
  6289. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6290. ((string-match "%h" rpl)
  6291. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6292. (t (concat rpl tag)))))
  6293. link))
  6294. ;;; Storing and inserting links
  6295. (defvar org-insert-link-history nil
  6296. "Minibuffer history for links inserted with `org-insert-link'.")
  6297. (defvar org-stored-links nil
  6298. "Contains the links stored with `org-store-link'.")
  6299. (defvar org-store-link-plist nil
  6300. "Plist with info about the most recently link created with `org-store-link'.")
  6301. (defvar org-link-protocols nil
  6302. "Link protocols added to Org-mode using `org-add-link-type'.")
  6303. (defvar org-store-link-functions nil
  6304. "List of functions that are called to create and store a link.
  6305. Each function will be called in turn until one returns a non-nil
  6306. value. Each function should check if it is responsible for creating
  6307. this link (for example by looking at the major mode).
  6308. If not, it must exit and return nil.
  6309. If yes, it should return a non-nil value after a calling
  6310. `org-store-link-props' with a list of properties and values.
  6311. Special properties are:
  6312. :type The link prefix. like \"http\". This must be given.
  6313. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6314. This is obligatory as well.
  6315. :description Optional default description for the second pair
  6316. of brackets in an Org-mode link. The user can still change
  6317. this when inserting this link into an Org-mode buffer.
  6318. In addition to these, any additional properties can be specified
  6319. and then used in remember templates.")
  6320. (defun org-add-link-type (type &optional follow export)
  6321. "Add TYPE to the list of `org-link-types'.
  6322. Re-compute all regular expressions depending on `org-link-types'
  6323. FOLLOW and EXPORT are two functions.
  6324. FOLLOW should take the link path as the single argument and do whatever
  6325. is necessary to follow the link, for example find a file or display
  6326. a mail message.
  6327. EXPORT should format the link path for export to one of the export formats.
  6328. It should be a function accepting three arguments:
  6329. path the path of the link, the text after the prefix (like \"http:\")
  6330. desc the description of the link, if any, nil if there was no description
  6331. format the export format, a symbol like `html' or `latex'.
  6332. The function may use the FORMAT information to return different values
  6333. depending on the format. The return value will be put literally into
  6334. the exported file.
  6335. Org-mode has a built-in default for exporting links. If you are happy with
  6336. this default, there is no need to define an export function for the link
  6337. type. For a simple example of an export function, see `org-bbdb.el'."
  6338. (add-to-list 'org-link-types type t)
  6339. (org-make-link-regexps)
  6340. (if (assoc type org-link-protocols)
  6341. (setcdr (assoc type org-link-protocols) (list follow export))
  6342. (push (list type follow export) org-link-protocols)))
  6343. ;;;###autoload
  6344. (defun org-store-link (arg)
  6345. "\\<org-mode-map>Store an org-link to the current location.
  6346. This link is added to `org-stored-links' and can later be inserted
  6347. into an org-buffer with \\[org-insert-link].
  6348. For some link types, a prefix arg is interpreted:
  6349. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6350. For file links, arg negates `org-context-in-file-links'."
  6351. (interactive "P")
  6352. (org-load-modules-maybe)
  6353. (setq org-store-link-plist nil) ; reset
  6354. (let (link cpltxt desc description search txt custom-id)
  6355. (cond
  6356. ((run-hook-with-args-until-success 'org-store-link-functions)
  6357. (setq link (plist-get org-store-link-plist :link)
  6358. desc (or (plist-get org-store-link-plist :description) link)))
  6359. ((equal (buffer-name) "*Org Edit Src Example*")
  6360. (let (label gc)
  6361. (while (or (not label)
  6362. (save-excursion
  6363. (save-restriction
  6364. (widen)
  6365. (goto-char (point-min))
  6366. (re-search-forward
  6367. (regexp-quote (format org-coderef-label-format label))
  6368. nil t))))
  6369. (when label (message "Label exists already") (sit-for 2))
  6370. (setq label (read-string "Code line label: " label)))
  6371. (end-of-line 1)
  6372. (setq link (format org-coderef-label-format label))
  6373. (setq gc (- 79 (length link)))
  6374. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6375. (insert link)
  6376. (setq link (concat "(" label ")") desc nil)))
  6377. ((eq major-mode 'calendar-mode)
  6378. (let ((cd (calendar-cursor-to-date)))
  6379. (setq link
  6380. (format-time-string
  6381. (car org-time-stamp-formats)
  6382. (apply 'encode-time
  6383. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6384. nil nil nil))))
  6385. (org-store-link-props :type "calendar" :date cd)))
  6386. ((eq major-mode 'w3-mode)
  6387. (setq cpltxt (if (and (buffer-name)
  6388. (not (string-match "Untitled" (buffer-name))))
  6389. (buffer-name)
  6390. (url-view-url t))
  6391. link (org-make-link (url-view-url t)))
  6392. (org-store-link-props :type "w3" :url (url-view-url t)))
  6393. ((eq major-mode 'w3m-mode)
  6394. (setq cpltxt (or w3m-current-title w3m-current-url)
  6395. link (org-make-link w3m-current-url))
  6396. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6397. ((setq search (run-hook-with-args-until-success
  6398. 'org-create-file-search-functions))
  6399. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6400. "::" search))
  6401. (setq cpltxt (or description link)))
  6402. ((eq major-mode 'image-mode)
  6403. (setq cpltxt (concat "file:"
  6404. (abbreviate-file-name buffer-file-name))
  6405. link (org-make-link cpltxt))
  6406. (org-store-link-props :type "image" :file buffer-file-name))
  6407. ((eq major-mode 'dired-mode)
  6408. ;; link to the file in the current line
  6409. (setq cpltxt (concat "file:"
  6410. (abbreviate-file-name
  6411. (expand-file-name
  6412. (dired-get-filename nil t))))
  6413. link (org-make-link cpltxt)))
  6414. ((and buffer-file-name (org-mode-p))
  6415. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6416. (cond
  6417. ((org-in-regexp "<<\\(.*?\\)>>")
  6418. (setq cpltxt
  6419. (concat "file:"
  6420. (abbreviate-file-name buffer-file-name)
  6421. "::" (match-string 1))
  6422. link (org-make-link cpltxt)))
  6423. ((and (featurep 'org-id)
  6424. (or (eq org-link-to-org-use-id t)
  6425. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6426. (interactive-p))
  6427. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6428. (interactive-p)
  6429. (not custom-id))
  6430. (and org-link-to-org-use-id
  6431. (condition-case nil
  6432. (org-entry-get nil "ID")
  6433. (error nil)))))
  6434. ;; We can make a link using the ID.
  6435. (setq link (condition-case nil
  6436. (prog1 (org-id-store-link)
  6437. (setq desc (plist-get org-store-link-plist
  6438. :description)))
  6439. (error
  6440. ;; probably before first headline, link to file only
  6441. (concat "file:"
  6442. (abbreviate-file-name buffer-file-name))))))
  6443. (t
  6444. ;; Just link to current headline
  6445. (setq cpltxt (concat "file:"
  6446. (abbreviate-file-name buffer-file-name)))
  6447. ;; Add a context search string
  6448. (when (org-xor org-context-in-file-links arg)
  6449. (setq txt (cond
  6450. ((org-on-heading-p) nil)
  6451. ((org-region-active-p)
  6452. (buffer-substring (region-beginning) (region-end)))
  6453. (t nil)))
  6454. (when (or (null txt) (string-match "\\S-" txt))
  6455. (setq cpltxt
  6456. (concat cpltxt "::"
  6457. (condition-case nil
  6458. (org-make-org-heading-search-string txt)
  6459. (error "")))
  6460. desc "NONE")))
  6461. (if (string-match "::\\'" cpltxt)
  6462. (setq cpltxt (substring cpltxt 0 -2)))
  6463. (setq link (org-make-link cpltxt)))))
  6464. ((buffer-file-name (buffer-base-buffer))
  6465. ;; Just link to this file here.
  6466. (setq cpltxt (concat "file:"
  6467. (abbreviate-file-name
  6468. (buffer-file-name (buffer-base-buffer)))))
  6469. ;; Add a context string
  6470. (when (org-xor org-context-in-file-links arg)
  6471. (setq txt (if (org-region-active-p)
  6472. (buffer-substring (region-beginning) (region-end))
  6473. (buffer-substring (point-at-bol) (point-at-eol))))
  6474. ;; Only use search option if there is some text.
  6475. (when (string-match "\\S-" txt)
  6476. (setq cpltxt
  6477. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6478. desc "NONE")))
  6479. (setq link (org-make-link cpltxt)))
  6480. ((interactive-p)
  6481. (error "Cannot link to a buffer which is not visiting a file"))
  6482. (t (setq link nil)))
  6483. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6484. (setq link (or link cpltxt)
  6485. desc (or desc cpltxt))
  6486. (if (equal desc "NONE") (setq desc nil))
  6487. (if (and (interactive-p) link)
  6488. (progn
  6489. (setq org-stored-links
  6490. (cons (list link desc) org-stored-links))
  6491. (message "Stored: %s" (or desc link))
  6492. (when custom-id
  6493. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6494. "::#" custom-id))
  6495. (setq org-stored-links
  6496. (cons (list link desc) org-stored-links))))
  6497. (and link (org-make-link-string link desc)))))
  6498. (defun org-store-link-props (&rest plist)
  6499. "Store link properties, extract names and addresses."
  6500. (let (x adr)
  6501. (when (setq x (plist-get plist :from))
  6502. (setq adr (mail-extract-address-components x))
  6503. (setq plist (plist-put plist :fromname (car adr)))
  6504. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6505. (when (setq x (plist-get plist :to))
  6506. (setq adr (mail-extract-address-components x))
  6507. (setq plist (plist-put plist :toname (car adr)))
  6508. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6509. (let ((from (plist-get plist :from))
  6510. (to (plist-get plist :to)))
  6511. (when (and from to org-from-is-user-regexp)
  6512. (setq plist
  6513. (plist-put plist :fromto
  6514. (if (string-match org-from-is-user-regexp from)
  6515. (concat "to %t")
  6516. (concat "from %f"))))))
  6517. (setq org-store-link-plist plist))
  6518. (defun org-add-link-props (&rest plist)
  6519. "Add these properties to the link property list."
  6520. (let (key value)
  6521. (while plist
  6522. (setq key (pop plist) value (pop plist))
  6523. (setq org-store-link-plist
  6524. (plist-put org-store-link-plist key value)))))
  6525. (defun org-email-link-description (&optional fmt)
  6526. "Return the description part of an email link.
  6527. This takes information from `org-store-link-plist' and formats it
  6528. according to FMT (default from `org-email-link-description-format')."
  6529. (setq fmt (or fmt org-email-link-description-format))
  6530. (let* ((p org-store-link-plist)
  6531. (to (plist-get p :toaddress))
  6532. (from (plist-get p :fromaddress))
  6533. (table
  6534. (list
  6535. (cons "%c" (plist-get p :fromto))
  6536. (cons "%F" (plist-get p :from))
  6537. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6538. (cons "%T" (plist-get p :to))
  6539. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6540. (cons "%s" (plist-get p :subject))
  6541. (cons "%m" (plist-get p :message-id)))))
  6542. (when (string-match "%c" fmt)
  6543. ;; Check if the user wrote this message
  6544. (if (and org-from-is-user-regexp from to
  6545. (save-match-data (string-match org-from-is-user-regexp from)))
  6546. (setq fmt (replace-match "to %t" t t fmt))
  6547. (setq fmt (replace-match "from %f" t t fmt))))
  6548. (org-replace-escapes fmt table)))
  6549. (defun org-make-org-heading-search-string (&optional string heading)
  6550. "Make search string for STRING or current headline."
  6551. (interactive)
  6552. (let ((s (or string (org-get-heading))))
  6553. (unless (and string (not heading))
  6554. ;; We are using a headline, clean up garbage in there.
  6555. (if (string-match org-todo-regexp s)
  6556. (setq s (replace-match "" t t s)))
  6557. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6558. (setq s (replace-match "" t t s)))
  6559. (setq s (org-trim s))
  6560. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6561. org-comment-string "\\)") s)
  6562. (setq s (replace-match "" t t s)))
  6563. (while (string-match org-ts-regexp s)
  6564. (setq s (replace-match "" t t s))))
  6565. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6566. (setq s (replace-match " " t t s)))
  6567. (or string (setq s (concat "*" s))) ; Add * for headlines
  6568. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6569. (defun org-make-link (&rest strings)
  6570. "Concatenate STRINGS."
  6571. (apply 'concat strings))
  6572. (defun org-make-link-string (link &optional description)
  6573. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6574. (unless (string-match "\\S-" link)
  6575. (error "Empty link"))
  6576. (when (stringp description)
  6577. ;; Remove brackets from the description, they are fatal.
  6578. (while (string-match "\\[" description)
  6579. (setq description (replace-match "{" t t description)))
  6580. (while (string-match "\\]" description)
  6581. (setq description (replace-match "}" t t description))))
  6582. (when (equal (org-link-escape link) description)
  6583. ;; No description needed, it is identical
  6584. (setq description nil))
  6585. (when (and (not description)
  6586. (not (equal link (org-link-escape link))))
  6587. (setq description (org-extract-attributes link)))
  6588. (concat "[[" (org-link-escape link) "]"
  6589. (if description (concat "[" description "]") "")
  6590. "]"))
  6591. (defconst org-link-escape-chars
  6592. '((?\ . "%20")
  6593. (?\[ . "%5B")
  6594. (?\] . "%5D")
  6595. (?\340 . "%E0") ; `a
  6596. (?\342 . "%E2") ; ^a
  6597. (?\347 . "%E7") ; ,c
  6598. (?\350 . "%E8") ; `e
  6599. (?\351 . "%E9") ; 'e
  6600. (?\352 . "%EA") ; ^e
  6601. (?\356 . "%EE") ; ^i
  6602. (?\364 . "%F4") ; ^o
  6603. (?\371 . "%F9") ; `u
  6604. (?\373 . "%FB") ; ^u
  6605. (?\; . "%3B")
  6606. (?? . "%3F")
  6607. (?= . "%3D")
  6608. (?+ . "%2B")
  6609. )
  6610. "Association list of escapes for some characters problematic in links.
  6611. This is the list that is used for internal purposes.")
  6612. (defvar org-url-encoding-use-url-hexify nil)
  6613. (defconst org-link-escape-chars-browser
  6614. '((?\ . "%20")) ; 32 for the SPC char
  6615. "Association list of escapes for some characters problematic in links.
  6616. This is the list that is used before handing over to the browser.")
  6617. (defun org-link-escape (text &optional table)
  6618. "Escape characters in TEXT that are problematic for links."
  6619. (if org-url-encoding-use-url-hexify
  6620. (url-hexify-string text)
  6621. (setq table (or table org-link-escape-chars))
  6622. (when text
  6623. (let ((re (mapconcat (lambda (x) (regexp-quote
  6624. (char-to-string (car x))))
  6625. table "\\|")))
  6626. (while (string-match re text)
  6627. (setq text
  6628. (replace-match
  6629. (cdr (assoc (string-to-char (match-string 0 text))
  6630. table))
  6631. t t text)))
  6632. text))))
  6633. (defun org-link-unescape (text &optional table)
  6634. "Reverse the action of `org-link-escape'."
  6635. (if org-url-encoding-use-url-hexify
  6636. (url-unhex-string text)
  6637. (setq table (or table org-link-escape-chars))
  6638. (when text
  6639. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6640. table "\\|")))
  6641. (while (string-match re text)
  6642. (setq text
  6643. (replace-match
  6644. (char-to-string (car (rassoc (match-string 0 text) table)))
  6645. t t text)))
  6646. text))))
  6647. (defun org-xor (a b)
  6648. "Exclusive or."
  6649. (if a (not b) b))
  6650. (defun org-fixup-message-id-for-http (s)
  6651. "Replace special characters in a message id, so it can be used in an http query."
  6652. (while (string-match "<" s)
  6653. (setq s (replace-match "%3C" t t s)))
  6654. (while (string-match ">" s)
  6655. (setq s (replace-match "%3E" t t s)))
  6656. (while (string-match "@" s)
  6657. (setq s (replace-match "%40" t t s)))
  6658. s)
  6659. ;;;###autoload
  6660. (defun org-insert-link-global ()
  6661. "Insert a link like Org-mode does.
  6662. This command can be called in any mode to insert a link in Org-mode syntax."
  6663. (interactive)
  6664. (org-load-modules-maybe)
  6665. (org-run-like-in-org-mode 'org-insert-link))
  6666. (defun org-insert-link (&optional complete-file link-location)
  6667. "Insert a link. At the prompt, enter the link.
  6668. Completion can be used to insert any of the link protocol prefixes like
  6669. http or ftp in use.
  6670. The history can be used to select a link previously stored with
  6671. `org-store-link'. When the empty string is entered (i.e. if you just
  6672. press RET at the prompt), the link defaults to the most recently
  6673. stored link. As SPC triggers completion in the minibuffer, you need to
  6674. use M-SPC or C-q SPC to force the insertion of a space character.
  6675. You will also be prompted for a description, and if one is given, it will
  6676. be displayed in the buffer instead of the link.
  6677. If there is already a link at point, this command will allow you to edit link
  6678. and description parts.
  6679. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6680. be selected using completion. The path to the file will be relative to the
  6681. current directory if the file is in the current directory or a subdirectory.
  6682. Otherwise, the link will be the absolute path as completed in the minibuffer
  6683. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6684. option `org-link-file-path-type'.
  6685. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6686. the current directory or below.
  6687. With three \\[universal-argument] prefixes, negate the meaning of
  6688. `org-keep-stored-link-after-insertion'.
  6689. If `org-make-link-description-function' is non-nil, this function will be
  6690. called with the link target, and the result will be the default
  6691. link description.
  6692. If the LINK-LOCATION parameter is non-nil, this value will be
  6693. used as the link location instead of reading one interactively."
  6694. (interactive "P")
  6695. (let* ((wcf (current-window-configuration))
  6696. (region (if (org-region-active-p)
  6697. (buffer-substring (region-beginning) (region-end))))
  6698. (remove (and region (list (region-beginning) (region-end))))
  6699. (desc region)
  6700. tmphist ; byte-compile incorrectly complains about this
  6701. (link link-location)
  6702. entry file all-prefixes)
  6703. (cond
  6704. (link-location) ; specified by arg, just use it.
  6705. ((org-in-regexp org-bracket-link-regexp 1)
  6706. ;; We do have a link at point, and we are going to edit it.
  6707. (setq remove (list (match-beginning 0) (match-end 0)))
  6708. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6709. (setq link (read-string "Link: "
  6710. (org-link-unescape
  6711. (org-match-string-no-properties 1)))))
  6712. ((or (org-in-regexp org-angle-link-re)
  6713. (org-in-regexp org-plain-link-re))
  6714. ;; Convert to bracket link
  6715. (setq remove (list (match-beginning 0) (match-end 0))
  6716. link (read-string "Link: "
  6717. (org-remove-angle-brackets (match-string 0)))))
  6718. ((member complete-file '((4) (16)))
  6719. ;; Completing read for file names.
  6720. (setq link (org-file-complete-link complete-file)))
  6721. (t
  6722. ;; Read link, with completion for stored links.
  6723. (with-output-to-temp-buffer "*Org Links*"
  6724. (princ "Insert a link.
  6725. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6726. (when org-stored-links
  6727. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6728. (princ (mapconcat
  6729. (lambda (x)
  6730. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6731. (reverse org-stored-links) "\n"))))
  6732. (let ((cw (selected-window)))
  6733. (select-window (get-buffer-window "*Org Links*"))
  6734. (setq truncate-lines t)
  6735. (unless (pos-visible-in-window-p (point-max))
  6736. (org-fit-window-to-buffer))
  6737. (and (window-live-p cw) (select-window cw)))
  6738. ;; Fake a link history, containing the stored links.
  6739. (setq tmphist (append (mapcar 'car org-stored-links)
  6740. org-insert-link-history))
  6741. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6742. (mapcar 'car org-link-abbrev-alist)
  6743. org-link-types))
  6744. (unwind-protect
  6745. (progn
  6746. (setq link
  6747. (let ((org-completion-use-ido nil))
  6748. (org-completing-read
  6749. "Link: "
  6750. (mapcar (lambda (x) (list (concat x ":")))
  6751. all-prefixes)
  6752. nil nil nil
  6753. 'tmphist
  6754. (or (car (car org-stored-links))))))
  6755. (if (or (member link all-prefixes)
  6756. (and (equal ":" (substring link -1))
  6757. (member (substring link 0 -1) all-prefixes)
  6758. (setq link (substring link 0 -1))))
  6759. (setq link (org-link-try-special-completion link))))
  6760. (set-window-configuration wcf)
  6761. (kill-buffer "*Org Links*"))
  6762. (setq entry (assoc link org-stored-links))
  6763. (or entry (push link org-insert-link-history))
  6764. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6765. (not org-keep-stored-link-after-insertion))
  6766. (setq org-stored-links (delq (assoc link org-stored-links)
  6767. org-stored-links)))
  6768. (setq desc (or desc (nth 1 entry)))))
  6769. (if (string-match org-plain-link-re link)
  6770. ;; URL-like link, normalize the use of angular brackets.
  6771. (setq link (org-make-link (org-remove-angle-brackets link))))
  6772. ;; Check if we are linking to the current file with a search option
  6773. ;; If yes, simplify the link by using only the search option.
  6774. (when (and buffer-file-name
  6775. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6776. (let* ((path (match-string 1 link))
  6777. (case-fold-search nil)
  6778. (search (match-string 2 link)))
  6779. (save-match-data
  6780. (if (equal (file-truename buffer-file-name) (file-truename path))
  6781. ;; We are linking to this same file, with a search option
  6782. (setq link search)))))
  6783. ;; Check if we can/should use a relative path. If yes, simplify the link
  6784. (when (string-match "^file:\\(.*\\)" link)
  6785. (let* ((path (match-string 1 link))
  6786. (origpath path)
  6787. (case-fold-search nil))
  6788. (cond
  6789. ((or (eq org-link-file-path-type 'absolute)
  6790. (equal complete-file '(16)))
  6791. (setq path (abbreviate-file-name (expand-file-name path))))
  6792. ((eq org-link-file-path-type 'noabbrev)
  6793. (setq path (expand-file-name path)))
  6794. ((eq org-link-file-path-type 'relative)
  6795. (setq path (file-relative-name path)))
  6796. (t
  6797. (save-match-data
  6798. (if (string-match (concat "^" (regexp-quote
  6799. (file-name-as-directory
  6800. (expand-file-name "."))))
  6801. (expand-file-name path))
  6802. ;; We are linking a file with relative path name.
  6803. (setq path (substring (expand-file-name path)
  6804. (match-end 0)))
  6805. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6806. (setq link (concat "file:" path))
  6807. (if (equal desc origpath)
  6808. (setq desc path))))
  6809. (if org-make-link-description-function
  6810. (setq desc (funcall org-make-link-description-function link desc)))
  6811. (setq desc (read-string "Description: " desc))
  6812. (unless (string-match "\\S-" desc) (setq desc nil))
  6813. (if remove (apply 'delete-region remove))
  6814. (insert (org-make-link-string link desc))))
  6815. (defun org-link-try-special-completion (type)
  6816. "If there is completion support for link type TAPE, offer it."
  6817. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6818. (if (functionp fun)
  6819. (funcall fun)
  6820. (read-string "Link (no completion support): " (concat type ":")))))
  6821. (defun org-file-complete-link (&optional arg)
  6822. "Create a file link using completion."
  6823. (let (file link)
  6824. (setq file (read-file-name "File: "))
  6825. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6826. (pwd1 (file-name-as-directory (abbreviate-file-name
  6827. (expand-file-name ".")))))
  6828. (cond
  6829. ((equal arg '(16))
  6830. (setq link (org-make-link
  6831. "file:"
  6832. (abbreviate-file-name (expand-file-name file)))))
  6833. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6834. (setq link (org-make-link "file:" (match-string 1 file))))
  6835. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6836. (expand-file-name file))
  6837. (setq link (org-make-link
  6838. "file:" (match-string 1 (expand-file-name file)))))
  6839. (t (setq link (org-make-link "file:" file)))))
  6840. link))
  6841. (defun org-completing-read (&rest args)
  6842. "Completing-read with SPACE being a normal character."
  6843. (let ((minibuffer-local-completion-map
  6844. (copy-keymap minibuffer-local-completion-map)))
  6845. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6846. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6847. (apply 'org-ido-completing-read args)))
  6848. (defun org-completing-read-no-ido (&rest args)
  6849. (let (org-completion-use-ido)
  6850. (apply 'org-completing-read args)))
  6851. (defun org-ido-completing-read (&rest args)
  6852. "Completing-read using `ido-mode' speedups if available"
  6853. (if (and org-completion-use-ido
  6854. (fboundp 'ido-completing-read)
  6855. (boundp 'ido-mode) ido-mode
  6856. (listp (second args)))
  6857. (let ((ido-enter-matching-directory nil))
  6858. (apply 'ido-completing-read (concat (car args))
  6859. (mapcar (lambda (x) (car x)) (nth 1 args))
  6860. (cddr args)))
  6861. (apply 'completing-read args)))
  6862. (defun org-extract-attributes (s)
  6863. "Extract the attributes cookie from a string and set as text property."
  6864. (let (a attr (start 0) key value)
  6865. (save-match-data
  6866. (when (string-match "{{\\([^}]+\\)}}$" s)
  6867. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6868. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6869. (setq key (match-string 1 a) value (match-string 2 a)
  6870. start (match-end 0)
  6871. attr (plist-put attr (intern key) value))))
  6872. (org-add-props s nil 'org-attr attr))
  6873. s))
  6874. (defun org-extract-attributes-from-string (tag)
  6875. (let (key value attr)
  6876. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6877. (setq key (match-string 1 tag) value (match-string 2 tag)
  6878. tag (replace-match "" t t tag)
  6879. attr (plist-put attr (intern key) value)))
  6880. (cons tag attr)))
  6881. (defun org-attributes-to-string (plist)
  6882. "Format a property list into an HTML attribute list."
  6883. (let ((s "") key value)
  6884. (while plist
  6885. (setq key (pop plist) value (pop plist))
  6886. (and value
  6887. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6888. s))
  6889. ;;; Opening/following a link
  6890. (defvar org-link-search-failed nil)
  6891. (defun org-next-link ()
  6892. "Move forward to the next link.
  6893. If the link is in hidden text, expose it."
  6894. (interactive)
  6895. (when (and org-link-search-failed (eq this-command last-command))
  6896. (goto-char (point-min))
  6897. (message "Link search wrapped back to beginning of buffer"))
  6898. (setq org-link-search-failed nil)
  6899. (let* ((pos (point))
  6900. (ct (org-context))
  6901. (a (assoc :link ct)))
  6902. (if a (goto-char (nth 2 a)))
  6903. (if (re-search-forward org-any-link-re nil t)
  6904. (progn
  6905. (goto-char (match-beginning 0))
  6906. (if (org-invisible-p) (org-show-context)))
  6907. (goto-char pos)
  6908. (setq org-link-search-failed t)
  6909. (error "No further link found"))))
  6910. (defun org-previous-link ()
  6911. "Move backward to the previous link.
  6912. If the link is in hidden text, expose it."
  6913. (interactive)
  6914. (when (and org-link-search-failed (eq this-command last-command))
  6915. (goto-char (point-max))
  6916. (message "Link search wrapped back to end of buffer"))
  6917. (setq org-link-search-failed nil)
  6918. (let* ((pos (point))
  6919. (ct (org-context))
  6920. (a (assoc :link ct)))
  6921. (if a (goto-char (nth 1 a)))
  6922. (if (re-search-backward org-any-link-re nil t)
  6923. (progn
  6924. (goto-char (match-beginning 0))
  6925. (if (org-invisible-p) (org-show-context)))
  6926. (goto-char pos)
  6927. (setq org-link-search-failed t)
  6928. (error "No further link found"))))
  6929. (defun org-translate-link (s)
  6930. "Translate a link string if a translation function has been defined."
  6931. (if (and org-link-translation-function
  6932. (fboundp org-link-translation-function)
  6933. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6934. (progn
  6935. (setq s (funcall org-link-translation-function
  6936. (match-string 1) (match-string 2)))
  6937. (concat (car s) ":" (cdr s)))
  6938. s))
  6939. (defun org-translate-link-from-planner (type path)
  6940. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6941. This is still an experimental function, your mileage may vary."
  6942. (cond
  6943. ((member type '("http" "https" "news" "ftp"))
  6944. ;; standard Internet links are the same.
  6945. nil)
  6946. ((and (equal type "irc") (string-match "^//" path))
  6947. ;; Planner has two / at the beginning of an irc link, we have 1.
  6948. ;; We should have zero, actually....
  6949. (setq path (substring path 1)))
  6950. ((and (equal type "lisp") (string-match "^/" path))
  6951. ;; Planner has a slash, we do not.
  6952. (setq type "elisp" path (substring path 1)))
  6953. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6954. ;; A typical message link. Planner has the id after the fina slash,
  6955. ;; we separate it with a hash mark
  6956. (setq path (concat (match-string 1 path) "#"
  6957. (org-remove-angle-brackets (match-string 2 path)))))
  6958. )
  6959. (cons type path))
  6960. (defun org-find-file-at-mouse (ev)
  6961. "Open file link or URL at mouse."
  6962. (interactive "e")
  6963. (mouse-set-point ev)
  6964. (org-open-at-point 'in-emacs))
  6965. (defun org-open-at-mouse (ev)
  6966. "Open file link or URL at mouse."
  6967. (interactive "e")
  6968. (mouse-set-point ev)
  6969. (if (eq major-mode 'org-agenda-mode)
  6970. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6971. (org-open-at-point))
  6972. (defvar org-window-config-before-follow-link nil
  6973. "The window configuration before following a link.
  6974. This is saved in case the need arises to restore it.")
  6975. (defvar org-open-link-marker (make-marker)
  6976. "Marker pointing to the location where `org-open-at-point; was called.")
  6977. ;;;###autoload
  6978. (defun org-open-at-point-global ()
  6979. "Follow a link like Org-mode does.
  6980. This command can be called in any mode to follow a link that has
  6981. Org-mode syntax."
  6982. (interactive)
  6983. (org-run-like-in-org-mode 'org-open-at-point))
  6984. ;;;###autoload
  6985. (defun org-open-link-from-string (s &optional arg)
  6986. "Open a link in the string S, as if it was in Org-mode."
  6987. (interactive "sLink: \nP")
  6988. (let ((reference-buffer (current-buffer)))
  6989. (with-temp-buffer
  6990. (let ((org-inhibit-startup t))
  6991. (org-mode)
  6992. (insert s)
  6993. (goto-char (point-min))
  6994. (org-open-at-point arg reference-buffer)))))
  6995. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6996. "Open link at or after point.
  6997. If there is no link at point, this function will search forward up to
  6998. the end of the current line.
  6999. Normally, files will be opened by an appropriate application. If the
  7000. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7001. With a double prefix argument, try to open outside of Emacs, in the
  7002. application the system uses for this file type."
  7003. (interactive "P")
  7004. (org-load-modules-maybe)
  7005. (move-marker org-open-link-marker (point))
  7006. (setq org-window-config-before-follow-link (current-window-configuration))
  7007. (org-remove-occur-highlights nil nil t)
  7008. (cond
  7009. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7010. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7011. (org-footnote-action))
  7012. (t
  7013. (let (type path link line search (pos (point)))
  7014. (catch 'match
  7015. (save-excursion
  7016. (skip-chars-forward "^]\n\r")
  7017. (when (org-in-regexp org-bracket-link-regexp)
  7018. (setq link (org-extract-attributes
  7019. (org-link-unescape (org-match-string-no-properties 1))))
  7020. (while (string-match " *\n *" link)
  7021. (setq link (replace-match " " t t link)))
  7022. (setq link (org-link-expand-abbrev link))
  7023. (cond
  7024. ((or (file-name-absolute-p link)
  7025. (string-match "^\\.\\.?/" link))
  7026. (setq type "file" path link))
  7027. ((string-match org-link-re-with-space3 link)
  7028. (setq type (match-string 1 link) path (match-string 2 link)))
  7029. (t (setq type "thisfile" path link)))
  7030. (throw 'match t)))
  7031. (when (get-text-property (point) 'org-linked-text)
  7032. (setq type "thisfile"
  7033. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7034. (1+ (point)) (point))
  7035. path (buffer-substring
  7036. (previous-single-property-change pos 'org-linked-text)
  7037. (next-single-property-change pos 'org-linked-text)))
  7038. (throw 'match t))
  7039. (save-excursion
  7040. (when (or (org-in-regexp org-angle-link-re)
  7041. (org-in-regexp org-plain-link-re))
  7042. (setq type (match-string 1) path (match-string 2))
  7043. (throw 'match t)))
  7044. (save-excursion
  7045. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7046. (setq type "tags"
  7047. path (match-string 1))
  7048. (while (string-match ":" path)
  7049. (setq path (replace-match "+" t t path)))
  7050. (throw 'match t)))
  7051. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7052. (setq type "tree-match"
  7053. path (match-string 1))
  7054. (throw 'match t)))
  7055. (unless path
  7056. (error "No link found"))
  7057. ;; switch back to reference buffer
  7058. ;; needed when if called in a temporary buffer through
  7059. ;; org-open-link-from-string
  7060. (and reference-buffer (switch-to-buffer reference-buffer))
  7061. ;; Remove any trailing spaces in path
  7062. (if (string-match " +\\'" path)
  7063. (setq path (replace-match "" t t path)))
  7064. (if (and org-link-translation-function
  7065. (fboundp org-link-translation-function))
  7066. ;; Check if we need to translate the link
  7067. (let ((tmp (funcall org-link-translation-function type path)))
  7068. (setq type (car tmp) path (cdr tmp))))
  7069. (cond
  7070. ((assoc type org-link-protocols)
  7071. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7072. ((equal type "mailto")
  7073. (let ((cmd (car org-link-mailto-program))
  7074. (args (cdr org-link-mailto-program)) args1
  7075. (address path) (subject "") a)
  7076. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7077. (setq address (match-string 1 path)
  7078. subject (org-link-escape (match-string 2 path))))
  7079. (while args
  7080. (cond
  7081. ((not (stringp (car args))) (push (pop args) args1))
  7082. (t (setq a (pop args))
  7083. (if (string-match "%a" a)
  7084. (setq a (replace-match address t t a)))
  7085. (if (string-match "%s" a)
  7086. (setq a (replace-match subject t t a)))
  7087. (push a args1))))
  7088. (apply cmd (nreverse args1))))
  7089. ((member type '("http" "https" "ftp" "news"))
  7090. (browse-url (concat type ":" (org-link-escape
  7091. path org-link-escape-chars-browser))))
  7092. ((member type '("message"))
  7093. (browse-url (concat type ":" path)))
  7094. ((string= type "tags")
  7095. (org-tags-view in-emacs path))
  7096. ((string= type "thisfile")
  7097. (if in-emacs
  7098. (switch-to-buffer-other-window
  7099. (org-get-buffer-for-internal-link (current-buffer)))
  7100. (org-mark-ring-push))
  7101. (let ((cmd `(org-link-search
  7102. ,path
  7103. ,(cond ((equal in-emacs '(4)) 'occur)
  7104. ((equal in-emacs '(16)) 'org-occur)
  7105. (t nil))
  7106. ,pos)))
  7107. (condition-case nil (eval cmd)
  7108. (error (progn (widen) (eval cmd))))))
  7109. ((string= type "tree-match")
  7110. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7111. ((string= type "file")
  7112. (if (string-match "::\\([0-9]+\\)\\'" path)
  7113. (setq line (string-to-number (match-string 1 path))
  7114. path (substring path 0 (match-beginning 0)))
  7115. (if (string-match "::\\(.+\\)\\'" path)
  7116. (setq search (match-string 1 path)
  7117. path (substring path 0 (match-beginning 0)))))
  7118. (if (string-match "[*?{]" (file-name-nondirectory path))
  7119. (dired path)
  7120. (org-open-file path in-emacs line search)))
  7121. ((string= type "news")
  7122. (require 'org-gnus)
  7123. (org-gnus-follow-link path))
  7124. ((string= type "shell")
  7125. (let ((cmd path))
  7126. (if (or (not org-confirm-shell-link-function)
  7127. (funcall org-confirm-shell-link-function
  7128. (format "Execute \"%s\" in shell? "
  7129. (org-add-props cmd nil
  7130. 'face 'org-warning))))
  7131. (progn
  7132. (message "Executing %s" cmd)
  7133. (shell-command cmd))
  7134. (error "Abort"))))
  7135. ((string= type "elisp")
  7136. (let ((cmd path))
  7137. (if (or (not org-confirm-elisp-link-function)
  7138. (funcall org-confirm-elisp-link-function
  7139. (format "Execute \"%s\" as elisp? "
  7140. (org-add-props cmd nil
  7141. 'face 'org-warning))))
  7142. (message "%s => %s" cmd
  7143. (if (equal (string-to-char cmd) ?\()
  7144. (eval (read cmd))
  7145. (call-interactively (read cmd))))
  7146. (error "Abort"))))
  7147. (t
  7148. (browse-url-at-point))))))
  7149. (move-marker org-open-link-marker nil)
  7150. (run-hook-with-args 'org-follow-link-hook))
  7151. ;;;; Time estimates
  7152. (defun org-get-effort (&optional pom)
  7153. "Get the effort estimate for the current entry."
  7154. (org-entry-get pom org-effort-property))
  7155. ;;; File search
  7156. (defvar org-create-file-search-functions nil
  7157. "List of functions to construct the right search string for a file link.
  7158. These functions are called in turn with point at the location to
  7159. which the link should point.
  7160. A function in the hook should first test if it would like to
  7161. handle this file type, for example by checking the major-mode or
  7162. the file extension. If it decides not to handle this file, it
  7163. should just return nil to give other functions a chance. If it
  7164. does handle the file, it must return the search string to be used
  7165. when following the link. The search string will be part of the
  7166. file link, given after a double colon, and `org-open-at-point'
  7167. will automatically search for it. If special measures must be
  7168. taken to make the search successful, another function should be
  7169. added to the companion hook `org-execute-file-search-functions',
  7170. which see.
  7171. A function in this hook may also use `setq' to set the variable
  7172. `description' to provide a suggestion for the descriptive text to
  7173. be used for this link when it gets inserted into an Org-mode
  7174. buffer with \\[org-insert-link].")
  7175. (defvar org-execute-file-search-functions nil
  7176. "List of functions to execute a file search triggered by a link.
  7177. Functions added to this hook must accept a single argument, the
  7178. search string that was part of the file link, the part after the
  7179. double colon. The function must first check if it would like to
  7180. handle this search, for example by checking the major-mode or the
  7181. file extension. If it decides not to handle this search, it
  7182. should just return nil to give other functions a chance. If it
  7183. does handle the search, it must return a non-nil value to keep
  7184. other functions from trying.
  7185. Each function can access the current prefix argument through the
  7186. variable `current-prefix-argument'. Note that a single prefix is
  7187. used to force opening a link in Emacs, so it may be good to only
  7188. use a numeric or double prefix to guide the search function.
  7189. In case this is needed, a function in this hook can also restore
  7190. the window configuration before `org-open-at-point' was called using:
  7191. (set-window-configuration org-window-config-before-follow-link)")
  7192. (defun org-link-search (s &optional type avoid-pos)
  7193. "Search for a link search option.
  7194. If S is surrounded by forward slashes, it is interpreted as a
  7195. regular expression. In org-mode files, this will create an `org-occur'
  7196. sparse tree. In ordinary files, `occur' will be used to list matches.
  7197. If the current buffer is in `dired-mode', grep will be used to search
  7198. in all files. If AVOID-POS is given, ignore matches near that position."
  7199. (let ((case-fold-search t)
  7200. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7201. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7202. (append '(("") (" ") ("\t") ("\n"))
  7203. org-emphasis-alist)
  7204. "\\|") "\\)"))
  7205. (pos (point))
  7206. (pre nil) (post nil)
  7207. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7208. (cond
  7209. ;; First check if there are any special
  7210. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7211. ;; Now try the builtin stuff
  7212. ((and (equal (string-to-char s0) ?#)
  7213. (> (length s0) 1)
  7214. (save-excursion
  7215. (goto-char (point-min))
  7216. (and
  7217. (re-search-forward
  7218. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7219. (setq type 'dedicated
  7220. pos (match-beginning 0))))
  7221. ;; There is an exact target for this
  7222. (goto-char pos)
  7223. (org-back-to-heading t)))
  7224. ((save-excursion
  7225. (goto-char (point-min))
  7226. (and
  7227. (re-search-forward
  7228. (concat "<<" (regexp-quote s0) ">>") nil t)
  7229. (setq type 'dedicated
  7230. pos (match-beginning 0))))
  7231. ;; There is an exact target for this
  7232. (goto-char pos))
  7233. ((and (string-match "^(\\(.*\\))$" s0)
  7234. (save-excursion
  7235. (goto-char (point-min))
  7236. (and
  7237. (re-search-forward
  7238. (concat "[^[]" (regexp-quote
  7239. (format org-coderef-label-format
  7240. (match-string 1 s0))))
  7241. nil t)
  7242. (setq type 'dedicated
  7243. pos (1+ (match-beginning 0))))))
  7244. ;; There is a coderef target for this
  7245. (goto-char pos))
  7246. ((string-match "^/\\(.*\\)/$" s)
  7247. ;; A regular expression
  7248. (cond
  7249. ((org-mode-p)
  7250. (org-occur (match-string 1 s)))
  7251. ;;((eq major-mode 'dired-mode)
  7252. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7253. (t (org-do-occur (match-string 1 s)))))
  7254. (t
  7255. ;; A normal search strings
  7256. (when (equal (string-to-char s) ?*)
  7257. ;; Anchor on headlines, post may include tags.
  7258. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7259. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7260. s (substring s 1)))
  7261. (remove-text-properties
  7262. 0 (length s)
  7263. '(face nil mouse-face nil keymap nil fontified nil) s)
  7264. ;; Make a series of regular expressions to find a match
  7265. (setq words (org-split-string s "[ \n\r\t]+")
  7266. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7267. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7268. "\\)" markers)
  7269. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7270. re2a (concat "[ \t\r\n]" re2a_)
  7271. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7272. re4 (concat "[^a-zA-Z_]" re4_)
  7273. re1 (concat pre re2 post)
  7274. re3 (concat pre (if pre re4_ re4) post)
  7275. re5 (concat pre ".*" re4)
  7276. re2 (concat pre re2)
  7277. re2a (concat pre (if pre re2a_ re2a))
  7278. re4 (concat pre (if pre re4_ re4))
  7279. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7280. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7281. re5 "\\)"
  7282. ))
  7283. (cond
  7284. ((eq type 'org-occur) (org-occur reall))
  7285. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7286. (t (goto-char (point-min))
  7287. (setq type 'fuzzy)
  7288. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7289. (org-search-not-self 1 re1 nil t)
  7290. (org-search-not-self 1 re2 nil t)
  7291. (org-search-not-self 1 re2a nil t)
  7292. (org-search-not-self 1 re3 nil t)
  7293. (org-search-not-self 1 re4 nil t)
  7294. (org-search-not-self 1 re5 nil t)
  7295. )
  7296. (goto-char (match-beginning 1))
  7297. (goto-char pos)
  7298. (error "No match")))))
  7299. (t
  7300. ;; Normal string-search
  7301. (goto-char (point-min))
  7302. (if (search-forward s nil t)
  7303. (goto-char (match-beginning 0))
  7304. (error "No match"))))
  7305. (and (org-mode-p) (org-show-context 'link-search))
  7306. type))
  7307. (defun org-search-not-self (group &rest args)
  7308. "Execute `re-search-forward', but only accept matches that do not
  7309. enclose the position of `org-open-link-marker'."
  7310. (let ((m org-open-link-marker))
  7311. (catch 'exit
  7312. (while (apply 're-search-forward args)
  7313. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7314. (goto-char (match-end group))
  7315. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7316. (> (match-beginning 0) (marker-position m))
  7317. (< (match-end 0) (marker-position m)))
  7318. (save-match-data
  7319. (or (not (org-in-regexp
  7320. org-bracket-link-analytic-regexp 1))
  7321. (not (match-end 4)) ; no description
  7322. (and (<= (match-beginning 4) (point))
  7323. (>= (match-end 4) (point))))))
  7324. (throw 'exit (point))))))))
  7325. (defun org-get-buffer-for-internal-link (buffer)
  7326. "Return a buffer to be used for displaying the link target of internal links."
  7327. (cond
  7328. ((not org-display-internal-link-with-indirect-buffer)
  7329. buffer)
  7330. ((string-match "(Clone)$" (buffer-name buffer))
  7331. (message "Buffer is already a clone, not making another one")
  7332. ;; we also do not modify visibility in this case
  7333. buffer)
  7334. (t ; make a new indirect buffer for displaying the link
  7335. (let* ((bn (buffer-name buffer))
  7336. (ibn (concat bn "(Clone)"))
  7337. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7338. (with-current-buffer ib (org-overview))
  7339. ib))))
  7340. (defun org-do-occur (regexp &optional cleanup)
  7341. "Call the Emacs command `occur'.
  7342. If CLEANUP is non-nil, remove the printout of the regular expression
  7343. in the *Occur* buffer. This is useful if the regex is long and not useful
  7344. to read."
  7345. (occur regexp)
  7346. (when cleanup
  7347. (let ((cwin (selected-window)) win beg end)
  7348. (when (setq win (get-buffer-window "*Occur*"))
  7349. (select-window win))
  7350. (goto-char (point-min))
  7351. (when (re-search-forward "match[a-z]+" nil t)
  7352. (setq beg (match-end 0))
  7353. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7354. (setq end (1- (match-beginning 0)))))
  7355. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7356. (goto-char (point-min))
  7357. (select-window cwin))))
  7358. ;;; The mark ring for links jumps
  7359. (defvar org-mark-ring nil
  7360. "Mark ring for positions before jumps in Org-mode.")
  7361. (defvar org-mark-ring-last-goto nil
  7362. "Last position in the mark ring used to go back.")
  7363. ;; Fill and close the ring
  7364. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7365. (loop for i from 1 to org-mark-ring-length do
  7366. (push (make-marker) org-mark-ring))
  7367. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7368. org-mark-ring)
  7369. (defun org-mark-ring-push (&optional pos buffer)
  7370. "Put the current position or POS into the mark ring and rotate it."
  7371. (interactive)
  7372. (setq pos (or pos (point)))
  7373. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7374. (move-marker (car org-mark-ring)
  7375. (or pos (point))
  7376. (or buffer (current-buffer)))
  7377. (message "%s"
  7378. (substitute-command-keys
  7379. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7380. (defun org-mark-ring-goto (&optional n)
  7381. "Jump to the previous position in the mark ring.
  7382. With prefix arg N, jump back that many stored positions. When
  7383. called several times in succession, walk through the entire ring.
  7384. Org-mode commands jumping to a different position in the current file,
  7385. or to another Org-mode file, automatically push the old position
  7386. onto the ring."
  7387. (interactive "p")
  7388. (let (p m)
  7389. (if (eq last-command this-command)
  7390. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7391. (setq p org-mark-ring))
  7392. (setq org-mark-ring-last-goto p)
  7393. (setq m (car p))
  7394. (switch-to-buffer (marker-buffer m))
  7395. (goto-char m)
  7396. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7397. (defun org-remove-angle-brackets (s)
  7398. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7399. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7400. s)
  7401. (defun org-add-angle-brackets (s)
  7402. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7403. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7404. s)
  7405. (defun org-remove-double-quotes (s)
  7406. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7407. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7408. s)
  7409. ;;; Following specific links
  7410. (defun org-follow-timestamp-link ()
  7411. (cond
  7412. ((org-at-date-range-p t)
  7413. (let ((org-agenda-start-on-weekday)
  7414. (t1 (match-string 1))
  7415. (t2 (match-string 2)))
  7416. (setq t1 (time-to-days (org-time-string-to-time t1))
  7417. t2 (time-to-days (org-time-string-to-time t2)))
  7418. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7419. ((org-at-timestamp-p t)
  7420. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7421. (substring (match-string 1) 0 10)))
  7422. 1))
  7423. (t (error "This should not happen"))))
  7424. ;;; Following file links
  7425. (defvar org-wait nil)
  7426. (defun org-open-file (path &optional in-emacs line search)
  7427. "Open the file at PATH.
  7428. First, this expands any special file name abbreviations. Then the
  7429. configuration variable `org-file-apps' is checked if it contains an
  7430. entry for this file type, and if yes, the corresponding command is launched.
  7431. If no application is found, Emacs simply visits the file.
  7432. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7433. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7434. and o use an external application to visit the file.
  7435. Optional LINE specifies a line to go to, optional SEARCH a string to
  7436. search for. If LINE or SEARCH is given, the file will always be
  7437. opened in Emacs.
  7438. If the file does not exist, an error is thrown."
  7439. (setq in-emacs (or in-emacs line search))
  7440. (let* ((file (if (equal path "")
  7441. buffer-file-name
  7442. (substitute-in-file-name (expand-file-name path))))
  7443. (apps (append org-file-apps (org-default-apps)))
  7444. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7445. (dirp (if remp nil (file-directory-p file)))
  7446. (file (if (and dirp org-open-directory-means-index-dot-org)
  7447. (concat (file-name-as-directory file) "index.org")
  7448. file))
  7449. (a-m-a-p (assq 'auto-mode apps))
  7450. (dfile (downcase file))
  7451. (old-buffer (current-buffer))
  7452. (old-pos (point))
  7453. (old-mode major-mode)
  7454. ext cmd)
  7455. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7456. (setq ext (match-string 1 dfile))
  7457. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7458. (setq ext (match-string 1 dfile))))
  7459. (cond
  7460. ((equal in-emacs '(16))
  7461. (setq cmd (cdr (assoc 'system apps))))
  7462. (in-emacs (setq cmd 'emacs))
  7463. (t
  7464. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7465. (and dirp (cdr (assoc 'directory apps)))
  7466. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7467. 'string-match)
  7468. (cdr (assoc ext apps))
  7469. (cdr (assoc t apps))))))
  7470. (when (eq cmd 'system)
  7471. (setq cmd (cdr (assoc 'system apps))))
  7472. (when (eq cmd 'default)
  7473. (setq cmd (cdr (assoc t apps))))
  7474. (when (eq cmd 'mailcap)
  7475. (require 'mailcap)
  7476. (mailcap-parse-mailcaps)
  7477. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7478. (command (mailcap-mime-info mime-type)))
  7479. (if (stringp command)
  7480. (setq cmd command)
  7481. (setq cmd 'emacs))))
  7482. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7483. (not (file-exists-p file))
  7484. (not org-open-non-existing-files))
  7485. (error "No such file: %s" file))
  7486. (cond
  7487. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7488. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7489. (while (string-match "['\"]%s['\"]" cmd)
  7490. (setq cmd (replace-match "%s" t t cmd)))
  7491. (while (string-match "%s" cmd)
  7492. (setq cmd (replace-match
  7493. (save-match-data
  7494. (shell-quote-argument
  7495. (convert-standard-filename file)))
  7496. t t cmd)))
  7497. (save-window-excursion
  7498. (start-process-shell-command cmd nil cmd)
  7499. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7500. ))
  7501. ((or (stringp cmd)
  7502. (eq cmd 'emacs))
  7503. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7504. (widen)
  7505. (if line (goto-line line)
  7506. (if search (org-link-search search))))
  7507. ((consp cmd)
  7508. (let ((file (convert-standard-filename file)))
  7509. (eval cmd)))
  7510. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7511. (and (org-mode-p) (eq old-mode 'org-mode)
  7512. (or (not (equal old-buffer (current-buffer)))
  7513. (not (equal old-pos (point))))
  7514. (org-mark-ring-push old-pos old-buffer))))
  7515. (defun org-default-apps ()
  7516. "Return the default applications for this operating system."
  7517. (cond
  7518. ((eq system-type 'darwin)
  7519. org-file-apps-defaults-macosx)
  7520. ((eq system-type 'windows-nt)
  7521. org-file-apps-defaults-windowsnt)
  7522. (t org-file-apps-defaults-gnu)))
  7523. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7524. "Convert extensions to regular expressions in the cars of LIST.
  7525. Also, weed out any non-string entries, because the return value is used
  7526. only for regexp matching.
  7527. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7528. point to the symbol `emacs', indicating that the file should
  7529. be opened in Emacs."
  7530. (append
  7531. (delq nil
  7532. (mapcar (lambda (x)
  7533. (if (not (stringp (car x)))
  7534. nil
  7535. (if (string-match "\\W" (car x))
  7536. x
  7537. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7538. list))
  7539. (if add-auto-mode
  7540. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7541. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7542. (defun org-file-remote-p (file)
  7543. "Test whether FILE specifies a location on a remote system.
  7544. Return non-nil if the location is indeed remote.
  7545. For example, the filename \"/user@host:/foo\" specifies a location
  7546. on the system \"/user@host:\"."
  7547. (cond ((fboundp 'file-remote-p)
  7548. (file-remote-p file))
  7549. ((fboundp 'tramp-handle-file-remote-p)
  7550. (tramp-handle-file-remote-p file))
  7551. ((and (boundp 'ange-ftp-name-format)
  7552. (string-match (car ange-ftp-name-format) file))
  7553. t)
  7554. (t nil)))
  7555. ;;;; Refiling
  7556. (defun org-get-org-file ()
  7557. "Read a filename, with default directory `org-directory'."
  7558. (let ((default (or org-default-notes-file remember-data-file)))
  7559. (read-file-name (format "File name [%s]: " default)
  7560. (file-name-as-directory org-directory)
  7561. default)))
  7562. (defun org-notes-order-reversed-p ()
  7563. "Check if the current file should receive notes in reversed order."
  7564. (cond
  7565. ((not org-reverse-note-order) nil)
  7566. ((eq t org-reverse-note-order) t)
  7567. ((not (listp org-reverse-note-order)) nil)
  7568. (t (catch 'exit
  7569. (let ((all org-reverse-note-order)
  7570. entry)
  7571. (while (setq entry (pop all))
  7572. (if (string-match (car entry) buffer-file-name)
  7573. (throw 'exit (cdr entry))))
  7574. nil)))))
  7575. (defvar org-refile-target-table nil
  7576. "The list of refile targets, created by `org-refile'.")
  7577. (defvar org-agenda-new-buffers nil
  7578. "Buffers created to visit agenda files.")
  7579. (defun org-get-refile-targets (&optional default-buffer)
  7580. "Produce a table with refile targets."
  7581. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7582. targets txt re files f desc descre fast-path-p level pos0)
  7583. (message "Getting targets...")
  7584. (with-current-buffer (or default-buffer (current-buffer))
  7585. (while (setq entry (pop entries))
  7586. (setq files (car entry) desc (cdr entry))
  7587. (setq fast-path-p nil)
  7588. (cond
  7589. ((null files) (setq files (list (current-buffer))))
  7590. ((eq files 'org-agenda-files)
  7591. (setq files (org-agenda-files 'unrestricted)))
  7592. ((and (symbolp files) (fboundp files))
  7593. (setq files (funcall files)))
  7594. ((and (symbolp files) (boundp files))
  7595. (setq files (symbol-value files))))
  7596. (if (stringp files) (setq files (list files)))
  7597. (cond
  7598. ((eq (car desc) :tag)
  7599. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7600. ((eq (car desc) :todo)
  7601. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7602. ((eq (car desc) :regexp)
  7603. (setq descre (cdr desc)))
  7604. ((eq (car desc) :level)
  7605. (setq descre (concat "^\\*\\{" (number-to-string
  7606. (if org-odd-levels-only
  7607. (1- (* 2 (cdr desc)))
  7608. (cdr desc)))
  7609. "\\}[ \t]")))
  7610. ((eq (car desc) :maxlevel)
  7611. (setq fast-path-p t)
  7612. (setq descre (concat "^\\*\\{1," (number-to-string
  7613. (if org-odd-levels-only
  7614. (1- (* 2 (cdr desc)))
  7615. (cdr desc)))
  7616. "\\}[ \t]")))
  7617. (t (error "Bad refiling target description %s" desc)))
  7618. (while (setq f (pop files))
  7619. (save-excursion
  7620. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7621. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7622. (setq f (expand-file-name f))
  7623. (if (eq org-refile-use-outline-path 'file)
  7624. (push (list (file-name-nondirectory f) f nil nil) targets))
  7625. (save-excursion
  7626. (save-restriction
  7627. (widen)
  7628. (goto-char (point-min))
  7629. (while (re-search-forward descre nil t)
  7630. (goto-char (setq pos0 (point-at-bol)))
  7631. (catch 'next
  7632. (when org-refile-target-verify-function
  7633. (save-match-data
  7634. (or (funcall org-refile-target-verify-function)
  7635. (throw 'next t))))
  7636. (when (looking-at org-complex-heading-regexp)
  7637. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7638. txt (org-link-display-format (match-string 4))
  7639. re (concat "^" (regexp-quote
  7640. (buffer-substring (match-beginning 1)
  7641. (match-end 4)))))
  7642. (if (match-end 5) (setq re (concat re "[ \t]+"
  7643. (regexp-quote
  7644. (match-string 5)))))
  7645. (setq re (concat re "[ \t]*$"))
  7646. (when org-refile-use-outline-path
  7647. (setq txt (mapconcat 'org-protect-slash
  7648. (append
  7649. (if (eq org-refile-use-outline-path 'file)
  7650. (list (file-name-nondirectory
  7651. (buffer-file-name (buffer-base-buffer))))
  7652. (if (eq org-refile-use-outline-path 'full-file-path)
  7653. (list (buffer-file-name (buffer-base-buffer)))))
  7654. (org-get-outline-path fast-path-p level txt)
  7655. (list txt))
  7656. "/")))
  7657. (push (list txt f re (point)) targets)))
  7658. (when (= (point) pos0)
  7659. ;; verification function has not moved point
  7660. (goto-char (point-at-eol))))))))))
  7661. (message "Getting targets...done")
  7662. (nreverse targets)))
  7663. (defun org-protect-slash (s)
  7664. (while (string-match "/" s)
  7665. (setq s (replace-match "\\" t t s)))
  7666. s)
  7667. (defvar org-olpa (make-vector 20 nil))
  7668. (defun org-get-outline-path (&optional fastp level heading)
  7669. "Return the outline path to the current entry, as a list."
  7670. (if fastp
  7671. (progn
  7672. (if (> level 19)
  7673. (error "Outline path failure, more than 19 levels."))
  7674. (loop for i from level upto 19 do
  7675. (aset org-olpa i nil))
  7676. (prog1
  7677. (delq nil (append org-olpa nil))
  7678. (aset org-olpa level heading)))
  7679. (let (rtn)
  7680. (save-excursion
  7681. (while (org-up-heading-safe)
  7682. (when (looking-at org-complex-heading-regexp)
  7683. (push (org-match-string-no-properties 4) rtn)))
  7684. rtn))))
  7685. (defvar org-refile-history nil
  7686. "History for refiling operations.")
  7687. (defvar org-after-refile-insert-hook nil
  7688. "Hook run after `org-refile' has inserted its stuff at the new location.
  7689. Note that this is still *before* the stuff will be removed from
  7690. the *old* location.")
  7691. (defun org-refile (&optional goto default-buffer)
  7692. "Move the entry at point to another heading.
  7693. The list of target headings is compiled using the information in
  7694. `org-refile-targets', which see. This list is created before each use
  7695. and will therefore always be up-to-date.
  7696. At the target location, the entry is filed as a subitem of the target heading.
  7697. Depending on `org-reverse-note-order', the new subitem will either be the
  7698. first or the last subitem.
  7699. If there is an active region, all entries in that region will be moved.
  7700. However, the region must fulfil the requirement that the first heading
  7701. is the first one sets the top-level of the moved text - at most siblings
  7702. below it are allowed.
  7703. With prefix arg GOTO, the command will only visit the target location,
  7704. not actually move anything.
  7705. With a double prefix `C-u C-u', go to the location where the last refiling
  7706. operation has put the subtree.
  7707. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7708. (interactive "P")
  7709. (let* ((cbuf (current-buffer))
  7710. (regionp (org-region-active-p))
  7711. (region-start (and regionp (region-beginning)))
  7712. (region-end (and regionp (region-end)))
  7713. (region-length (and regionp (- region-end region-start)))
  7714. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7715. pos it nbuf file re level reversed)
  7716. (when regionp (goto-char region-start)
  7717. (unless (org-kill-is-subtree-p
  7718. (buffer-substring region-start region-end))
  7719. (error "The region is not a (sequence of) subtree(s)")))
  7720. (if (equal goto '(16))
  7721. (org-refile-goto-last-stored)
  7722. (when (setq it (org-refile-get-location
  7723. (if goto "Goto: " "Refile to: ") default-buffer
  7724. org-refile-allow-creating-parent-nodes))
  7725. (setq file (nth 1 it)
  7726. re (nth 2 it)
  7727. pos (nth 3 it))
  7728. (if (and (not goto)
  7729. pos
  7730. (equal (buffer-file-name) file)
  7731. (if regionp
  7732. (and (>= pos region-start)
  7733. (<= pos region-end))
  7734. (and (>= pos (point))
  7735. (< pos (save-excursion
  7736. (org-end-of-subtree t t))))))
  7737. (error "Cannot refile to position inside the tree or region"))
  7738. (setq nbuf (or (find-buffer-visiting file)
  7739. (find-file-noselect file)))
  7740. (if goto
  7741. (progn
  7742. (switch-to-buffer nbuf)
  7743. (goto-char pos)
  7744. (org-show-context 'org-goto))
  7745. (if regionp
  7746. (progn
  7747. (kill-new (buffer-substring region-start region-end))
  7748. (org-save-markers-in-region region-start region-end))
  7749. (org-copy-subtree 1 nil t))
  7750. (save-excursion
  7751. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7752. (find-file-noselect file))))
  7753. (setq reversed (org-notes-order-reversed-p))
  7754. (save-excursion
  7755. (save-restriction
  7756. (widen)
  7757. (if pos
  7758. (progn
  7759. (goto-char pos)
  7760. (looking-at outline-regexp)
  7761. (setq level (org-get-valid-level (funcall outline-level) 1))
  7762. (goto-char
  7763. (if reversed
  7764. (or (outline-next-heading) (point-max))
  7765. (or (save-excursion (outline-get-next-sibling))
  7766. (org-end-of-subtree t t)
  7767. (point-max)))))
  7768. (setq level 1)
  7769. (if (not reversed)
  7770. (goto-char (point-max))
  7771. (goto-char (point-min))
  7772. (or (outline-next-heading) (goto-char (point-max)))))
  7773. (if (not (bolp)) (newline))
  7774. (bookmark-set "org-refile-last-stored")
  7775. (org-paste-subtree level)
  7776. (run-hooks 'org-after-refile-insert-hook))))
  7777. (if regionp
  7778. (delete-region (point) (+ (point) region-length))
  7779. (org-cut-subtree))
  7780. (setq org-markers-to-move nil)
  7781. (message "Refiled to \"%s\"" (car it))))))
  7782. (org-reveal))
  7783. (defun org-refile-goto-last-stored ()
  7784. "Go to the location where the last refile was stored."
  7785. (interactive)
  7786. (bookmark-jump "org-refile-last-stored")
  7787. (message "This is the location of the last refile"))
  7788. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7789. "Prompt the user for a refile location, using PROMPT."
  7790. (let ((org-refile-targets org-refile-targets)
  7791. (org-refile-use-outline-path org-refile-use-outline-path))
  7792. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7793. (unless org-refile-target-table
  7794. (error "No refile targets"))
  7795. (let* ((cbuf (current-buffer))
  7796. (partial-completion-mode nil)
  7797. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7798. (cfunc (if (and org-refile-use-outline-path
  7799. org-outline-path-complete-in-steps)
  7800. 'org-olpath-completing-read
  7801. 'org-ido-completing-read))
  7802. (extra (if org-refile-use-outline-path "/" ""))
  7803. (filename (and cfn (expand-file-name cfn)))
  7804. (tbl (mapcar
  7805. (lambda (x)
  7806. (if (and (not (member org-refile-use-outline-path
  7807. '(file full-file-path)))
  7808. (not (equal filename (nth 1 x))))
  7809. (cons (concat (car x) extra " ("
  7810. (file-name-nondirectory (nth 1 x)) ")")
  7811. (cdr x))
  7812. (cons (concat (car x) extra) (cdr x))))
  7813. org-refile-target-table))
  7814. (completion-ignore-case t)
  7815. pa answ parent-target child parent)
  7816. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7817. nil 'org-refile-history))
  7818. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7819. (if pa
  7820. pa
  7821. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7822. (setq parent (match-string 1 answ)
  7823. child (match-string 2 answ))
  7824. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7825. (when (and parent-target
  7826. (or (eq new-nodes t)
  7827. (and (eq new-nodes 'confirm)
  7828. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7829. (org-refile-new-child parent-target child))))))
  7830. (defun org-refile-new-child (parent-target child)
  7831. "Use refile target PARENT-TARGET to add new CHILD below it."
  7832. (unless parent-target
  7833. (error "Cannot find parent for new node"))
  7834. (let ((file (nth 1 parent-target))
  7835. (pos (nth 3 parent-target))
  7836. level)
  7837. (with-current-buffer (or (find-buffer-visiting file)
  7838. (find-file-noselect file))
  7839. (save-excursion
  7840. (save-restriction
  7841. (widen)
  7842. (if pos
  7843. (goto-char pos)
  7844. (goto-char (point-max))
  7845. (if (not (bolp)) (newline)))
  7846. (when (looking-at outline-regexp)
  7847. (setq level (funcall outline-level))
  7848. (org-end-of-subtree t t))
  7849. (org-back-over-empty-lines)
  7850. (insert "\n" (make-string
  7851. (if pos (org-get-valid-level level 1) 1) ?*)
  7852. " " child "\n")
  7853. (beginning-of-line 0)
  7854. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7855. (defun org-olpath-completing-read (prompt collection &rest args)
  7856. "Read an outline path like a file name."
  7857. (let ((thetable collection)
  7858. (org-completion-use-ido nil)) ; does not work with ido.
  7859. (apply
  7860. 'org-ido-completing-read prompt
  7861. (lambda (string predicate &optional flag)
  7862. (let (rtn r f (l (length string)))
  7863. (cond
  7864. ((eq flag nil)
  7865. ;; try completion
  7866. (try-completion string thetable))
  7867. ((eq flag t)
  7868. ;; all-completions
  7869. (setq rtn (all-completions string thetable predicate))
  7870. (mapcar
  7871. (lambda (x)
  7872. (setq r (substring x l))
  7873. (if (string-match " ([^)]*)$" x)
  7874. (setq f (match-string 0 x))
  7875. (setq f ""))
  7876. (if (string-match "/" r)
  7877. (concat string (substring r 0 (match-end 0)) f)
  7878. x))
  7879. rtn))
  7880. ((eq flag 'lambda)
  7881. ;; exact match?
  7882. (assoc string thetable)))
  7883. ))
  7884. args)))
  7885. ;;;; Dynamic blocks
  7886. (defun org-find-dblock (name)
  7887. "Find the first dynamic block with name NAME in the buffer.
  7888. If not found, stay at current position and return nil."
  7889. (let (pos)
  7890. (save-excursion
  7891. (goto-char (point-min))
  7892. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7893. nil t)
  7894. (match-beginning 0))))
  7895. (if pos (goto-char pos))
  7896. pos))
  7897. (defconst org-dblock-start-re
  7898. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7899. "Matches the startline of a dynamic block, with parameters.")
  7900. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7901. "Matches the end of a dynamic block.")
  7902. (defun org-create-dblock (plist)
  7903. "Create a dynamic block section, with parameters taken from PLIST.
  7904. PLIST must contain a :name entry which is used as name of the block."
  7905. (unless (bolp) (newline))
  7906. (let ((name (plist-get plist :name)))
  7907. (insert "#+BEGIN: " name)
  7908. (while plist
  7909. (if (eq (car plist) :name)
  7910. (setq plist (cddr plist))
  7911. (insert " " (prin1-to-string (pop plist)))))
  7912. (insert "\n\n#+END:\n")
  7913. (beginning-of-line -2)))
  7914. (defun org-prepare-dblock ()
  7915. "Prepare dynamic block for refresh.
  7916. This empties the block, puts the cursor at the insert position and returns
  7917. the property list including an extra property :name with the block name."
  7918. (unless (looking-at org-dblock-start-re)
  7919. (error "Not at a dynamic block"))
  7920. (let* ((begdel (1+ (match-end 0)))
  7921. (name (org-no-properties (match-string 1)))
  7922. (params (append (list :name name)
  7923. (read (concat "(" (match-string 3) ")")))))
  7924. (unless (re-search-forward org-dblock-end-re nil t)
  7925. (error "Dynamic block not terminated"))
  7926. (setq params
  7927. (append params
  7928. (list :content (buffer-substring
  7929. begdel (match-beginning 0)))))
  7930. (delete-region begdel (match-beginning 0))
  7931. (goto-char begdel)
  7932. (open-line 1)
  7933. params))
  7934. (defun org-map-dblocks (&optional command)
  7935. "Apply COMMAND to all dynamic blocks in the current buffer.
  7936. If COMMAND is not given, use `org-update-dblock'."
  7937. (let ((cmd (or command 'org-update-dblock))
  7938. pos)
  7939. (save-excursion
  7940. (goto-char (point-min))
  7941. (while (re-search-forward org-dblock-start-re nil t)
  7942. (goto-char (setq pos (match-beginning 0)))
  7943. (condition-case nil
  7944. (funcall cmd)
  7945. (error (message "Error during update of dynamic block")))
  7946. (goto-char pos)
  7947. (unless (re-search-forward org-dblock-end-re nil t)
  7948. (error "Dynamic block not terminated"))))))
  7949. (defun org-dblock-update (&optional arg)
  7950. "User command for updating dynamic blocks.
  7951. Update the dynamic block at point. With prefix ARG, update all dynamic
  7952. blocks in the buffer."
  7953. (interactive "P")
  7954. (if arg
  7955. (org-update-all-dblocks)
  7956. (or (looking-at org-dblock-start-re)
  7957. (org-beginning-of-dblock))
  7958. (org-update-dblock)))
  7959. (defun org-update-dblock ()
  7960. "Update the dynamic block at point
  7961. This means to empty the block, parse for parameters and then call
  7962. the correct writing function."
  7963. (save-window-excursion
  7964. (let* ((pos (point))
  7965. (line (org-current-line))
  7966. (params (org-prepare-dblock))
  7967. (name (plist-get params :name))
  7968. (cmd (intern (concat "org-dblock-write:" name))))
  7969. (message "Updating dynamic block `%s' at line %d..." name line)
  7970. (funcall cmd params)
  7971. (message "Updating dynamic block `%s' at line %d...done" name line)
  7972. (goto-char pos))))
  7973. (defun org-beginning-of-dblock ()
  7974. "Find the beginning of the dynamic block at point.
  7975. Error if there is no such block at point."
  7976. (let ((pos (point))
  7977. beg)
  7978. (end-of-line 1)
  7979. (if (and (re-search-backward org-dblock-start-re nil t)
  7980. (setq beg (match-beginning 0))
  7981. (re-search-forward org-dblock-end-re nil t)
  7982. (> (match-end 0) pos))
  7983. (goto-char beg)
  7984. (goto-char pos)
  7985. (error "Not in a dynamic block"))))
  7986. (defun org-update-all-dblocks ()
  7987. "Update all dynamic blocks in the buffer.
  7988. This function can be used in a hook."
  7989. (when (org-mode-p)
  7990. (org-map-dblocks 'org-update-dblock)))
  7991. ;;;; Completion
  7992. (defconst org-additional-option-like-keywords
  7993. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7994. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7995. "LATEX_HEADER:"
  7996. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7997. "BEGIN_QUOTE" "END_QUOTE"
  7998. "BEGIN_VERSE" "END_VERSE"
  7999. "BEGIN_CENTER" "END_CENTER"
  8000. "BEGIN_SRC" "END_SRC"
  8001. "CATEGORY" "COLUMNS"
  8002. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  8003. (defcustom org-structure-template-alist
  8004. '(
  8005. ("s" "#+begin_src ?\n\n#+end_src"
  8006. "<src lang=\"?\">\n\n</src>")
  8007. ("e" "#+begin_example\n?\n#+end_example"
  8008. "<example>\n?\n</example>")
  8009. ("q" "#+begin_quote\n?\n#+end_quote"
  8010. "<quote>\n?\n</quote>")
  8011. ("v" "#+begin_verse\n?\n#+end_verse"
  8012. "<verse>\n?\n/verse>")
  8013. ("c" "#+begin_center\n?\n#+end_center"
  8014. "<center>\n?\n/center>")
  8015. ("l" "#+begin_latex\n?\n#+end_latex"
  8016. "<literal style=\"latex\">\n?\n</literal>")
  8017. ("L" "#+latex: "
  8018. "<literal style=\"latex\">?</literal>")
  8019. ("h" "#+begin_html\n?\n#+end_html"
  8020. "<literal style=\"html\">\n?\n</literal>")
  8021. ("H" "#+html: "
  8022. "<literal style=\"html\">?</literal>")
  8023. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8024. ("A" "#+ascii: ")
  8025. ("i" "#+include %file ?"
  8026. "<include file=%file markup=\"?\">")
  8027. )
  8028. "Structure completion elements.
  8029. This is a list of abbreviation keys and values. The value gets inserted
  8030. it you type @samp{.} followed by the key and then the completion key,
  8031. usually `M-TAB'. %file will be replaced by a file name after prompting
  8032. for the file using completion.
  8033. There are two templates for each key, the first uses the original Org syntax,
  8034. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8035. the default when the /org-mtags.el/ module has been loaded. See also the
  8036. variable `org-mtags-prefer-muse-templates'.
  8037. This is an experimental feature, it is undecided if it is going to stay in."
  8038. :group 'org-completion
  8039. :type '(repeat
  8040. (string :tag "Key")
  8041. (string :tag "Template")
  8042. (string :tag "Muse Template")))
  8043. (defun org-try-structure-completion ()
  8044. "Try to complete a structure template before point.
  8045. This looks for strings like \"<e\" on an otherwise empty line and
  8046. expands them."
  8047. (let ((l (buffer-substring (point-at-bol) (point)))
  8048. a)
  8049. (when (and (looking-at "[ \t]*$")
  8050. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8051. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8052. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8053. (match-beginning 1)) a)
  8054. t)))
  8055. (defun org-complete-expand-structure-template (start cell)
  8056. "Expand a structure template."
  8057. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8058. (rpl (nth (if musep 2 1) cell)))
  8059. (delete-region start (point))
  8060. (when (string-match "\\`#\\+" rpl)
  8061. (cond
  8062. ((bolp))
  8063. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8064. (delete-region (point-at-bol) (point)))
  8065. (t (newline))))
  8066. (setq start (point))
  8067. (if (string-match "%file" rpl)
  8068. (setq rpl (replace-match
  8069. (concat
  8070. "\""
  8071. (save-match-data
  8072. (abbreviate-file-name (read-file-name "Include file: ")))
  8073. "\"")
  8074. t t rpl)))
  8075. (insert rpl)
  8076. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8077. (defun org-complete (&optional arg)
  8078. "Perform completion on word at point.
  8079. At the beginning of a headline, this completes TODO keywords as given in
  8080. `org-todo-keywords'.
  8081. If the current word is preceded by a backslash, completes the TeX symbols
  8082. that are supported for HTML support.
  8083. If the current word is preceded by \"#+\", completes special words for
  8084. setting file options.
  8085. In the line after \"#+STARTUP:, complete valid keywords.\"
  8086. At all other locations, this simply calls the value of
  8087. `org-completion-fallback-command'."
  8088. (interactive "P")
  8089. (org-without-partial-completion
  8090. (catch 'exit
  8091. (let* ((a nil)
  8092. (end (point))
  8093. (beg1 (save-excursion
  8094. (skip-chars-backward (org-re "[:alnum:]_@"))
  8095. (point)))
  8096. (beg (save-excursion
  8097. (skip-chars-backward "a-zA-Z0-9_:$")
  8098. (point)))
  8099. (confirm (lambda (x) (stringp (car x))))
  8100. (searchhead (equal (char-before beg) ?*))
  8101. (struct
  8102. (when (and (member (char-before beg1) '(?. ?<))
  8103. (setq a (assoc (buffer-substring beg1 (point))
  8104. org-structure-template-alist)))
  8105. (org-complete-expand-structure-template (1- beg1) a)
  8106. (throw 'exit t)))
  8107. (tag (and (equal (char-before beg1) ?:)
  8108. (equal (char-after (point-at-bol)) ?*)))
  8109. (prop (and (equal (char-before beg1) ?:)
  8110. (not (equal (char-after (point-at-bol)) ?*))))
  8111. (texp (equal (char-before beg) ?\\))
  8112. (link (equal (char-before beg) ?\[))
  8113. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8114. beg)
  8115. "#+"))
  8116. (startup (string-match "^#\\+STARTUP:.*"
  8117. (buffer-substring (point-at-bol) (point))))
  8118. (completion-ignore-case opt)
  8119. (type nil)
  8120. (tbl nil)
  8121. (table (cond
  8122. (opt
  8123. (setq type :opt)
  8124. (require 'org-exp)
  8125. (append
  8126. (mapcar
  8127. (lambda (x)
  8128. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8129. (cons (match-string 2 x) (match-string 1 x)))
  8130. (org-split-string (org-get-current-options) "\n"))
  8131. (mapcar 'list org-additional-option-like-keywords)))
  8132. (startup
  8133. (setq type :startup)
  8134. org-startup-options)
  8135. (link (append org-link-abbrev-alist-local
  8136. org-link-abbrev-alist))
  8137. (texp
  8138. (setq type :tex)
  8139. org-html-entities)
  8140. ((string-match "\\`\\*+[ \t]+\\'"
  8141. (buffer-substring (point-at-bol) beg))
  8142. (setq type :todo)
  8143. (mapcar 'list org-todo-keywords-1))
  8144. (searchhead
  8145. (setq type :searchhead)
  8146. (save-excursion
  8147. (goto-char (point-min))
  8148. (while (re-search-forward org-todo-line-regexp nil t)
  8149. (push (list
  8150. (org-make-org-heading-search-string
  8151. (match-string 3) t))
  8152. tbl)))
  8153. tbl)
  8154. (tag (setq type :tag beg beg1)
  8155. (or org-tag-alist (org-get-buffer-tags)))
  8156. (prop (setq type :prop beg beg1)
  8157. (mapcar 'list (org-buffer-property-keys nil t t)))
  8158. (t (progn
  8159. (call-interactively org-completion-fallback-command)
  8160. (throw 'exit nil)))))
  8161. (pattern (buffer-substring-no-properties beg end))
  8162. (completion (try-completion pattern table confirm)))
  8163. (cond ((eq completion t)
  8164. (if (not (assoc (upcase pattern) table))
  8165. (message "Already complete")
  8166. (if (and (equal type :opt)
  8167. (not (member (car (assoc (upcase pattern) table))
  8168. org-additional-option-like-keywords)))
  8169. (insert (substring (cdr (assoc (upcase pattern) table))
  8170. (length pattern)))
  8171. (if (memq type '(:tag :prop)) (insert ":")))))
  8172. ((null completion)
  8173. (message "Can't find completion for \"%s\"" pattern)
  8174. (ding))
  8175. ((not (string= pattern completion))
  8176. (delete-region beg end)
  8177. (if (string-match " +$" completion)
  8178. (setq completion (replace-match "" t t completion)))
  8179. (insert completion)
  8180. (if (get-buffer-window "*Completions*")
  8181. (delete-window (get-buffer-window "*Completions*")))
  8182. (if (assoc completion table)
  8183. (if (eq type :todo) (insert " ")
  8184. (if (memq type '(:tag :prop)) (insert ":"))))
  8185. (if (and (equal type :opt) (assoc completion table))
  8186. (message "%s" (substitute-command-keys
  8187. "Press \\[org-complete] again to insert example settings"))))
  8188. (t
  8189. (message "Making completion list...")
  8190. (let ((list (sort (all-completions pattern table confirm)
  8191. 'string<)))
  8192. (with-output-to-temp-buffer "*Completions*"
  8193. (condition-case nil
  8194. ;; Protection needed for XEmacs and emacs 21
  8195. (display-completion-list list pattern)
  8196. (error (display-completion-list list)))))
  8197. (message "Making completion list...%s" "done")))))))
  8198. ;;;; TODO, DEADLINE, Comments
  8199. (defun org-toggle-comment ()
  8200. "Change the COMMENT state of an entry."
  8201. (interactive)
  8202. (save-excursion
  8203. (org-back-to-heading)
  8204. (let (case-fold-search)
  8205. (if (looking-at (concat outline-regexp
  8206. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8207. (replace-match "" t t nil 1)
  8208. (if (looking-at outline-regexp)
  8209. (progn
  8210. (goto-char (match-end 0))
  8211. (insert org-comment-string " ")))))))
  8212. (defvar org-last-todo-state-is-todo nil
  8213. "This is non-nil when the last TODO state change led to a TODO state.
  8214. If the last change removed the TODO tag or switched to DONE, then
  8215. this is nil.")
  8216. (defvar org-setting-tags nil) ; dynamically skipped
  8217. (defun org-parse-local-options (string var)
  8218. "Parse STRING for startup setting relevant for variable VAR."
  8219. (let ((rtn (symbol-value var))
  8220. e opts)
  8221. (save-match-data
  8222. (if (or (not string) (not (string-match "\\S-" string)))
  8223. rtn
  8224. (setq opts (delq nil (mapcar (lambda (x)
  8225. (setq e (assoc x org-startup-options))
  8226. (if (eq (nth 1 e) var) e nil))
  8227. (org-split-string string "[ \t]+"))))
  8228. (if (not opts)
  8229. rtn
  8230. (setq rtn nil)
  8231. (while (setq e (pop opts))
  8232. (if (not (nth 3 e))
  8233. (setq rtn (nth 2 e))
  8234. (if (not (listp rtn)) (setq rtn nil))
  8235. (push (nth 2 e) rtn)))
  8236. rtn)))))
  8237. (defvar org-todo-setup-filter-hook nil
  8238. "Hook for functions that pre-filter todo specs.
  8239. Each function takes a todo spec and returns either `nil' or the spec
  8240. transformed into canonical form." )
  8241. (defvar org-todo-get-default-hook nil
  8242. "Hook for functions that get a default item for todo.
  8243. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8244. `nil' or a string to be used for the todo mark." )
  8245. (defvar org-agenda-headline-snapshot-before-repeat)
  8246. (defun org-todo (&optional arg)
  8247. "Change the TODO state of an item.
  8248. The state of an item is given by a keyword at the start of the heading,
  8249. like
  8250. *** TODO Write paper
  8251. *** DONE Call mom
  8252. The different keywords are specified in the variable `org-todo-keywords'.
  8253. By default the available states are \"TODO\" and \"DONE\".
  8254. So for this example: when the item starts with TODO, it is changed to DONE.
  8255. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8256. DONE are present, add TODO at the beginning of the heading.
  8257. With C-u prefix arg, use completion to determine the new state.
  8258. With numeric prefix arg, switch to that state.
  8259. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8260. With a tripple C-u prefix, circumvent any state blocking.
  8261. For calling through lisp, arg is also interpreted in the following way:
  8262. 'none -> empty state
  8263. \"\"(empty string) -> switch to empty state
  8264. 'done -> switch to DONE
  8265. 'nextset -> switch to the next set of keywords
  8266. 'previousset -> switch to the previous set of keywords
  8267. \"WAITING\" -> switch to the specified keyword, but only if it
  8268. really is a member of `org-todo-keywords'."
  8269. (interactive "P")
  8270. (if (equal arg '(16)) (setq arg 'nextset))
  8271. (let ((org-blocker-hook org-blocker-hook)
  8272. (case-fold-search nil))
  8273. (when (equal arg '(64))
  8274. (setq arg nil org-blocker-hook nil))
  8275. (when (and org-blocker-hook
  8276. (org-entry-get nil "NOBLOCKING"))
  8277. (setq org-blocker-hook nil))
  8278. (save-excursion
  8279. (catch 'exit
  8280. (org-back-to-heading)
  8281. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8282. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8283. (looking-at " *"))
  8284. (let* ((match-data (match-data))
  8285. (startpos (point-at-bol))
  8286. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8287. (org-log-done org-log-done)
  8288. (org-log-repeat org-log-repeat)
  8289. (org-todo-log-states org-todo-log-states)
  8290. (this (match-string 1))
  8291. (hl-pos (match-beginning 0))
  8292. (head (org-get-todo-sequence-head this))
  8293. (ass (assoc head org-todo-kwd-alist))
  8294. (interpret (nth 1 ass))
  8295. (done-word (nth 3 ass))
  8296. (final-done-word (nth 4 ass))
  8297. (last-state (or this ""))
  8298. (completion-ignore-case t)
  8299. (member (member this org-todo-keywords-1))
  8300. (tail (cdr member))
  8301. (state (cond
  8302. ((and org-todo-key-trigger
  8303. (or (and (equal arg '(4))
  8304. (eq org-use-fast-todo-selection 'prefix))
  8305. (and (not arg) org-use-fast-todo-selection
  8306. (not (eq org-use-fast-todo-selection
  8307. 'prefix)))))
  8308. ;; Use fast selection
  8309. (org-fast-todo-selection))
  8310. ((and (equal arg '(4))
  8311. (or (not org-use-fast-todo-selection)
  8312. (not org-todo-key-trigger)))
  8313. ;; Read a state with completion
  8314. (org-ido-completing-read
  8315. "State: " (mapcar (lambda(x) (list x))
  8316. org-todo-keywords-1)
  8317. nil t))
  8318. ((eq arg 'right)
  8319. (if this
  8320. (if tail (car tail) nil)
  8321. (car org-todo-keywords-1)))
  8322. ((eq arg 'left)
  8323. (if (equal member org-todo-keywords-1)
  8324. nil
  8325. (if this
  8326. (nth (- (length org-todo-keywords-1)
  8327. (length tail) 2)
  8328. org-todo-keywords-1)
  8329. (org-last org-todo-keywords-1))))
  8330. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8331. (setq arg nil))) ; hack to fall back to cycling
  8332. (arg
  8333. ;; user or caller requests a specific state
  8334. (cond
  8335. ((equal arg "") nil)
  8336. ((eq arg 'none) nil)
  8337. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8338. ((eq arg 'nextset)
  8339. (or (car (cdr (member head org-todo-heads)))
  8340. (car org-todo-heads)))
  8341. ((eq arg 'previousset)
  8342. (let ((org-todo-heads (reverse org-todo-heads)))
  8343. (or (car (cdr (member head org-todo-heads)))
  8344. (car org-todo-heads))))
  8345. ((car (member arg org-todo-keywords-1)))
  8346. ((nth (1- (prefix-numeric-value arg))
  8347. org-todo-keywords-1))))
  8348. ((null member) (or head (car org-todo-keywords-1)))
  8349. ((equal this final-done-word) nil) ;; -> make empty
  8350. ((null tail) nil) ;; -> first entry
  8351. ((memq interpret '(type priority))
  8352. (if (eq this-command last-command)
  8353. (car tail)
  8354. (if (> (length tail) 0)
  8355. (or done-word (car org-done-keywords))
  8356. nil)))
  8357. (t
  8358. (car tail))))
  8359. (state (or
  8360. (run-hook-with-args-until-success
  8361. 'org-todo-get-default-hook state last-state)
  8362. state))
  8363. (next (if state (concat " " state " ") " "))
  8364. (change-plist (list :type 'todo-state-change :from this :to state
  8365. :position startpos))
  8366. dolog now-done-p)
  8367. (when org-blocker-hook
  8368. (setq org-last-todo-state-is-todo
  8369. (not (member this org-done-keywords)))
  8370. (unless (save-excursion
  8371. (save-match-data
  8372. (run-hook-with-args-until-failure
  8373. 'org-blocker-hook change-plist)))
  8374. (if (interactive-p)
  8375. (error "TODO state change from %s to %s blocked" this state)
  8376. ;; fail silently
  8377. (message "TODO state change from %s to %s blocked" this state)
  8378. (throw 'exit nil))))
  8379. (store-match-data match-data)
  8380. (replace-match next t t)
  8381. (unless (pos-visible-in-window-p hl-pos)
  8382. (message "TODO state changed to %s" (org-trim next)))
  8383. (unless head
  8384. (setq head (org-get-todo-sequence-head state)
  8385. ass (assoc head org-todo-kwd-alist)
  8386. interpret (nth 1 ass)
  8387. done-word (nth 3 ass)
  8388. final-done-word (nth 4 ass)))
  8389. (when (memq arg '(nextset previousset))
  8390. (message "Keyword-Set %d/%d: %s"
  8391. (- (length org-todo-sets) -1
  8392. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8393. (length org-todo-sets)
  8394. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8395. (setq org-last-todo-state-is-todo
  8396. (not (member state org-done-keywords)))
  8397. (setq now-done-p (and (member state org-done-keywords)
  8398. (not (member this org-done-keywords))))
  8399. (and logging (org-local-logging logging))
  8400. (when (and (or org-todo-log-states org-log-done)
  8401. (not (memq arg '(nextset previousset))))
  8402. ;; we need to look at recording a time and note
  8403. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8404. (nth 2 (assoc this org-todo-log-states))))
  8405. (when (and state
  8406. (member state org-not-done-keywords)
  8407. (not (member this org-not-done-keywords)))
  8408. ;; This is now a todo state and was not one before
  8409. ;; If there was a CLOSED time stamp, get rid of it.
  8410. (org-add-planning-info nil nil 'closed))
  8411. (when (and now-done-p org-log-done)
  8412. ;; It is now done, and it was not done before
  8413. (org-add-planning-info 'closed (org-current-time))
  8414. (if (and (not dolog) (eq 'note org-log-done))
  8415. (org-add-log-setup 'done state this 'findpos 'note)))
  8416. (when (and state dolog)
  8417. ;; This is a non-nil state, and we need to log it
  8418. (org-add-log-setup 'state state this 'findpos dolog)))
  8419. ;; Fixup tag positioning
  8420. (org-todo-trigger-tag-changes state)
  8421. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8422. (when org-provide-todo-statistics
  8423. (org-update-parent-todo-statistics))
  8424. (run-hooks 'org-after-todo-state-change-hook)
  8425. (if (and arg (not (member state org-done-keywords)))
  8426. (setq head (org-get-todo-sequence-head state)))
  8427. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8428. ;; Do we need to trigger a repeat?
  8429. (when now-done-p
  8430. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8431. ;; This is for the agenda, take a snapshot of the headline.
  8432. (save-match-data
  8433. (setq org-agenda-headline-snapshot-before-repeat
  8434. (org-get-heading))))
  8435. (org-auto-repeat-maybe state))
  8436. ;; Fixup cursor location if close to the keyword
  8437. (if (and (outline-on-heading-p)
  8438. (not (bolp))
  8439. (save-excursion (beginning-of-line 1)
  8440. (looking-at org-todo-line-regexp))
  8441. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8442. (progn
  8443. (goto-char (or (match-end 2) (match-end 1)))
  8444. (and (looking-at " ") (just-one-space))))
  8445. (when org-trigger-hook
  8446. (save-excursion
  8447. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8448. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8449. "Block turning an entry into a TODO, using the hierarchy.
  8450. This checks whether the current task should be blocked from state
  8451. changes. Such blocking occurs when:
  8452. 1. The task has children which are not all in a completed state.
  8453. 2. A task has a parent with the property :ORDERED:, and there
  8454. are siblings prior to the current task with incomplete
  8455. status.
  8456. 3. The parent of the task is blocked because it has siblings that should
  8457. be done first, or is child of a block grandparent TODO entry."
  8458. (catch 'dont-block
  8459. ;; If this is not a todo state change, or if this entry is already DONE,
  8460. ;; do not block
  8461. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8462. (member (plist-get change-plist :from)
  8463. (cons 'done org-done-keywords))
  8464. (member (plist-get change-plist :to)
  8465. (cons 'todo org-not-done-keywords)))
  8466. (throw 'dont-block t))
  8467. ;; If this task has children, and any are undone, it's blocked
  8468. (save-excursion
  8469. (org-back-to-heading t)
  8470. (let ((this-level (funcall outline-level)))
  8471. (outline-next-heading)
  8472. (let ((child-level (funcall outline-level)))
  8473. (while (and (not (eobp))
  8474. (> child-level this-level))
  8475. ;; this todo has children, check whether they are all
  8476. ;; completed
  8477. (if (and (not (org-entry-is-done-p))
  8478. (org-entry-is-todo-p))
  8479. (throw 'dont-block nil))
  8480. (outline-next-heading)
  8481. (setq child-level (funcall outline-level))))))
  8482. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8483. ;; any previous siblings are undone, it's blocked
  8484. (save-excursion
  8485. (org-back-to-heading t)
  8486. (let* ((pos (point))
  8487. (parent-pos (and (org-up-heading-safe) (point))))
  8488. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8489. (when (and (org-entry-get (point) "ORDERED")
  8490. (forward-line 1)
  8491. (re-search-forward org-not-done-heading-regexp pos t))
  8492. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8493. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8494. (while t
  8495. (goto-char parent-pos)
  8496. (if (not (looking-at org-not-done-heading-regexp))
  8497. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8498. (setq pos (point))
  8499. (setq parent-pos (and (org-up-heading-safe) (point)))
  8500. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8501. (when (and (org-entry-get (point) "ORDERED")
  8502. (forward-line 1)
  8503. (re-search-forward org-not-done-heading-regexp pos t))
  8504. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8505. (defcustom org-track-ordered-property-with-tag nil
  8506. "Should the ORDERED property also be shown as a tag?
  8507. The ORDERED property decides if an entry should require subtasks to be
  8508. completed in sequence. Since a property is not very visible, setting
  8509. this option means that toggling the ORDERED property with the command
  8510. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8511. not relevant for the behavior, but it makes things more visible.
  8512. Note that toggling the tag with tags commands will not change the property
  8513. and therefore not influence behavior!
  8514. This can be t, meaning the tag ORDERED should be used, It can also be a
  8515. string to select a different tag for this task."
  8516. :group 'org-todo
  8517. :type '(choice
  8518. (const :tag "No tracking" nil)
  8519. (const :tag "Track with ORDERED tag" t)
  8520. (string :tag "Use other tag")))
  8521. (defun org-toggle-ordered-property ()
  8522. "Toggle the ORDERED property of the current entry.
  8523. For better visibility, you can track the value of this property with a tag.
  8524. See variable `org-track-ordered-property-with-tag'."
  8525. (interactive)
  8526. (let* ((t1 org-track-ordered-property-with-tag)
  8527. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8528. (save-excursion
  8529. (org-back-to-heading)
  8530. (if (org-entry-get nil "ORDERED")
  8531. (progn
  8532. (org-delete-property "ORDERED")
  8533. (and tag (org-toggle-tag tag 'off))
  8534. (message "Subtasks can be completed in arbitrary order"))
  8535. (org-entry-put nil "ORDERED" "t")
  8536. (and tag (org-toggle-tag tag 'on))
  8537. (message "Subtasks must be completed in sequence")))))
  8538. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8539. (defun org-block-todo-from-checkboxes (change-plist)
  8540. "Block turning an entry into a TODO, using checkboxes.
  8541. This checks whether the current task should be blocked from state
  8542. changes because there are uncheckd boxes in this entry."
  8543. (catch 'dont-block
  8544. ;; If this is not a todo state change, or if this entry is already DONE,
  8545. ;; do not block
  8546. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8547. (member (plist-get change-plist :from)
  8548. (cons 'done org-done-keywords))
  8549. (member (plist-get change-plist :to)
  8550. (cons 'todo org-not-done-keywords)))
  8551. (throw 'dont-block t))
  8552. ;; If this task has checkboxes that are not checked, it's blocked
  8553. (save-excursion
  8554. (org-back-to-heading t)
  8555. (let ((beg (point)) end)
  8556. (outline-next-heading)
  8557. (setq end (point))
  8558. (goto-char beg)
  8559. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8560. end t)
  8561. (progn
  8562. (if (boundp 'org-blocked-by-checkboxes)
  8563. (setq org-blocked-by-checkboxes t))
  8564. (throw 'dont-block nil)))))
  8565. t)) ; do not block
  8566. (defvar org-entry-property-inherited-from) ;; defined below
  8567. (defun org-update-parent-todo-statistics ()
  8568. "Update any statistics cookie in the parent of the current headline.
  8569. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8570. the entire subtree and this will travel up the hierarchy and update
  8571. statistics everywhere."
  8572. (interactive)
  8573. (let* ((lim 0) prop
  8574. (recursive (or (not org-hierarchical-todo-statistics)
  8575. (string-match
  8576. "\\<recursive\\>"
  8577. (or (setq prop (org-entry-get
  8578. nil "COOKIE_DATA" 'inherit)) ""))))
  8579. (lim (or (and prop (marker-position
  8580. org-entry-property-inherited-from))
  8581. lim))
  8582. (first t)
  8583. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8584. level ltoggle l1
  8585. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8586. (catch 'exit
  8587. (save-excursion
  8588. (beginning-of-line 1)
  8589. (if (org-at-heading-p)
  8590. (setq ltoggle (funcall outline-level))
  8591. (error "This should not happen"))
  8592. (while (and (setq level (org-up-heading-safe))
  8593. (or recursive first)
  8594. (>= (point) lim))
  8595. (setq first nil)
  8596. (unless (and level
  8597. (not (string-match
  8598. "\\<checkbox\\>"
  8599. (downcase
  8600. (or (org-entry-get
  8601. nil "COOKIE_DATA")
  8602. "")))))
  8603. (throw 'exit nil))
  8604. (while (re-search-forward box-re (point-at-eol) t)
  8605. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8606. (setq is-percent (match-end 2))
  8607. (save-match-data
  8608. (unless (outline-next-heading) (throw 'exit nil))
  8609. (while (and (looking-at org-complex-heading-regexp)
  8610. (> (setq l1 (length (match-string 1))) level))
  8611. (setq kwd (and (or recursive (= l1 ltoggle))
  8612. (match-string 2)))
  8613. (and kwd (setq cnt-all (1+ cnt-all)))
  8614. (and (member kwd org-done-keywords)
  8615. (setq cnt-done (1+ cnt-done)))
  8616. (outline-next-heading)))
  8617. (replace-match
  8618. (if is-percent
  8619. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8620. (format "[%d/%d]" cnt-done cnt-all)))))
  8621. (when cookie-present
  8622. (run-hook-with-args 'org-after-todo-statistics-hook
  8623. cnt-done (- cnt-all cnt-done)))))
  8624. (run-hooks 'org-todo-statistics-hook)))
  8625. (defvar org-after-todo-statistics-hook nil
  8626. "Hook that is called after a TODO statistics cookie has been updated.
  8627. Each function is called with two arguments: the number of not-done entries
  8628. and the number of done entries.
  8629. For example, the following function, when added to this hook, will switch
  8630. an entry to DONE when all children are done, and back to TODO when new
  8631. entries are set to a TODO status. Note that this hook is only called
  8632. when there is a statistics cookie in the headline!
  8633. (defun org-summary-todo (n-done n-not-done)
  8634. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8635. (let (org-log-done org-log-states) ; turn off logging
  8636. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8637. ")
  8638. (defvar org-todo-statistics-hook nil
  8639. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8640. This hook runs even if there is no statisics cookie present, in which case
  8641. `org-after-todo-statistics-hook' would not run.")
  8642. (defun org-todo-trigger-tag-changes (state)
  8643. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8644. (let ((l org-todo-state-tags-triggers)
  8645. changes)
  8646. (when (or (not state) (equal state ""))
  8647. (setq changes (append changes (cdr (assoc "" l)))))
  8648. (when (and (stringp state) (> (length state) 0))
  8649. (setq changes (append changes (cdr (assoc state l)))))
  8650. (when (member state org-not-done-keywords)
  8651. (setq changes (append changes (cdr (assoc 'todo l)))))
  8652. (when (member state org-done-keywords)
  8653. (setq changes (append changes (cdr (assoc 'done l)))))
  8654. (dolist (c changes)
  8655. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8656. (defun org-local-logging (value)
  8657. "Get logging settings from a property VALUE."
  8658. (let* (words w a)
  8659. ;; directly set the variables, they are already local.
  8660. (setq org-log-done nil
  8661. org-log-repeat nil
  8662. org-todo-log-states nil)
  8663. (setq words (org-split-string value))
  8664. (while (setq w (pop words))
  8665. (cond
  8666. ((setq a (assoc w org-startup-options))
  8667. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8668. (set (nth 1 a) (nth 2 a))))
  8669. ((setq a (org-extract-log-state-settings w))
  8670. (and (member (car a) org-todo-keywords-1)
  8671. (push a org-todo-log-states)))))))
  8672. (defun org-get-todo-sequence-head (kwd)
  8673. "Return the head of the TODO sequence to which KWD belongs.
  8674. If KWD is not set, check if there is a text property remembering the
  8675. right sequence."
  8676. (let (p)
  8677. (cond
  8678. ((not kwd)
  8679. (or (get-text-property (point-at-bol) 'org-todo-head)
  8680. (progn
  8681. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8682. nil (point-at-eol)))
  8683. (get-text-property p 'org-todo-head))))
  8684. ((not (member kwd org-todo-keywords-1))
  8685. (car org-todo-keywords-1))
  8686. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8687. (defun org-fast-todo-selection ()
  8688. "Fast TODO keyword selection with single keys.
  8689. Returns the new TODO keyword, or nil if no state change should occur."
  8690. (let* ((fulltable org-todo-key-alist)
  8691. (done-keywords org-done-keywords) ;; needed for the faces.
  8692. (maxlen (apply 'max (mapcar
  8693. (lambda (x)
  8694. (if (stringp (car x)) (string-width (car x)) 0))
  8695. fulltable)))
  8696. (expert nil)
  8697. (fwidth (+ maxlen 3 1 3))
  8698. (ncol (/ (- (window-width) 4) fwidth))
  8699. tg cnt e c tbl
  8700. groups ingroup)
  8701. (save-excursion
  8702. (save-window-excursion
  8703. (if expert
  8704. (set-buffer (get-buffer-create " *Org todo*"))
  8705. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8706. (erase-buffer)
  8707. (org-set-local 'org-done-keywords done-keywords)
  8708. (setq tbl fulltable cnt 0)
  8709. (while (setq e (pop tbl))
  8710. (cond
  8711. ((equal e '(:startgroup))
  8712. (push '() groups) (setq ingroup t)
  8713. (when (not (= cnt 0))
  8714. (setq cnt 0)
  8715. (insert "\n"))
  8716. (insert "{ "))
  8717. ((equal e '(:endgroup))
  8718. (setq ingroup nil cnt 0)
  8719. (insert "}\n"))
  8720. ((equal e '(:newline))
  8721. (when (not (= cnt 0))
  8722. (setq cnt 0)
  8723. (insert "\n")
  8724. (setq e (car tbl))
  8725. (while (equal (car tbl) '(:newline))
  8726. (insert "\n")
  8727. (setq tbl (cdr tbl)))))
  8728. (t
  8729. (setq tg (car e) c (cdr e))
  8730. (if ingroup (push tg (car groups)))
  8731. (setq tg (org-add-props tg nil 'face
  8732. (org-get-todo-face tg)))
  8733. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8734. (insert "[" c "] " tg (make-string
  8735. (- fwidth 4 (length tg)) ?\ ))
  8736. (when (= (setq cnt (1+ cnt)) ncol)
  8737. (insert "\n")
  8738. (if ingroup (insert " "))
  8739. (setq cnt 0)))))
  8740. (insert "\n")
  8741. (goto-char (point-min))
  8742. (if (not expert) (org-fit-window-to-buffer))
  8743. (message "[a-z..]:Set [SPC]:clear")
  8744. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8745. (cond
  8746. ((or (= c ?\C-g)
  8747. (and (= c ?q) (not (rassoc c fulltable))))
  8748. (setq quit-flag t))
  8749. ((= c ?\ ) nil)
  8750. ((setq e (rassoc c fulltable) tg (car e))
  8751. tg)
  8752. (t (setq quit-flag t)))))))
  8753. (defun org-entry-is-todo-p ()
  8754. (member (org-get-todo-state) org-not-done-keywords))
  8755. (defun org-entry-is-done-p ()
  8756. (member (org-get-todo-state) org-done-keywords))
  8757. (defun org-get-todo-state ()
  8758. (save-excursion
  8759. (org-back-to-heading t)
  8760. (and (looking-at org-todo-line-regexp)
  8761. (match-end 2)
  8762. (match-string 2))))
  8763. (defun org-at-date-range-p (&optional inactive-ok)
  8764. "Is the cursor inside a date range?"
  8765. (interactive)
  8766. (save-excursion
  8767. (catch 'exit
  8768. (let ((pos (point)))
  8769. (skip-chars-backward "^[<\r\n")
  8770. (skip-chars-backward "<[")
  8771. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8772. (>= (match-end 0) pos)
  8773. (throw 'exit t))
  8774. (skip-chars-backward "^<[\r\n")
  8775. (skip-chars-backward "<[")
  8776. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8777. (>= (match-end 0) pos)
  8778. (throw 'exit t)))
  8779. nil)))
  8780. (defun org-get-repeat ()
  8781. "Check if there is a deadline/schedule with repeater in this entry."
  8782. (save-match-data
  8783. (save-excursion
  8784. (org-back-to-heading t)
  8785. (if (re-search-forward
  8786. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8787. (match-string 1)))))
  8788. (defvar org-last-changed-timestamp)
  8789. (defvar org-last-inserted-timestamp)
  8790. (defvar org-log-post-message)
  8791. (defvar org-log-note-purpose)
  8792. (defvar org-log-note-how)
  8793. (defvar org-log-note-extra)
  8794. (defun org-auto-repeat-maybe (done-word)
  8795. "Check if the current headline contains a repeated deadline/schedule.
  8796. If yes, set TODO state back to what it was and change the base date
  8797. of repeating deadline/scheduled time stamps to new date.
  8798. This function is run automatically after each state change to a DONE state."
  8799. ;; last-state is dynamically scoped into this function
  8800. (let* ((repeat (org-get-repeat))
  8801. (aa (assoc last-state org-todo-kwd-alist))
  8802. (interpret (nth 1 aa))
  8803. (head (nth 2 aa))
  8804. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8805. (msg "Entry repeats: ")
  8806. (org-log-done nil)
  8807. (org-todo-log-states nil)
  8808. (nshiftmax 10) (nshift 0)
  8809. re type n what ts time)
  8810. (when repeat
  8811. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8812. (org-todo (if (eq interpret 'type) last-state head))
  8813. (when org-log-repeat
  8814. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8815. (memq 'org-add-log-note post-command-hook))
  8816. ;; OK, we are already setup for some record
  8817. (if (eq org-log-repeat 'note)
  8818. ;; make sure we take a note, not only a time stamp
  8819. (setq org-log-note-how 'note))
  8820. ;; Set up for taking a record
  8821. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8822. last-state
  8823. 'findpos org-log-repeat)))
  8824. (org-back-to-heading t)
  8825. (org-add-planning-info nil nil 'closed)
  8826. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8827. org-deadline-time-regexp "\\)\\|\\("
  8828. org-ts-regexp "\\)"))
  8829. (while (re-search-forward
  8830. re (save-excursion (outline-next-heading) (point)) t)
  8831. (setq type (if (match-end 1) org-scheduled-string
  8832. (if (match-end 3) org-deadline-string "Plain:"))
  8833. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8834. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8835. (setq n (string-to-number (match-string 2 ts))
  8836. what (match-string 3 ts))
  8837. (if (equal what "w") (setq n (* n 7) what "d"))
  8838. ;; Preparation, see if we need to modify the start date for the change
  8839. (when (match-end 1)
  8840. (setq time (save-match-data (org-time-string-to-time ts)))
  8841. (cond
  8842. ((equal (match-string 1 ts) ".")
  8843. ;; Shift starting date to today
  8844. (org-timestamp-change
  8845. (- (time-to-days (current-time)) (time-to-days time))
  8846. 'day))
  8847. ((equal (match-string 1 ts) "+")
  8848. (while (or (= nshift 0)
  8849. (<= (time-to-days time) (time-to-days (current-time))))
  8850. (when (= (incf nshift) nshiftmax)
  8851. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8852. (error "Abort")))
  8853. (org-timestamp-change n (cdr (assoc what whata)))
  8854. (org-at-timestamp-p t)
  8855. (setq ts (match-string 1))
  8856. (setq time (save-match-data (org-time-string-to-time ts))))
  8857. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8858. ;; rematch, so that we have everything in place for the real shift
  8859. (org-at-timestamp-p t)
  8860. (setq ts (match-string 1))
  8861. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8862. (org-timestamp-change n (cdr (assoc what whata)))
  8863. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8864. (setq org-log-post-message msg)
  8865. (message "%s" msg))))
  8866. (defun org-show-todo-tree (arg)
  8867. "Make a compact tree which shows all headlines marked with TODO.
  8868. The tree will show the lines where the regexp matches, and all higher
  8869. headlines above the match.
  8870. With a \\[universal-argument] prefix, also show the DONE entries.
  8871. With a numeric prefix N, construct a sparse tree for the Nth element
  8872. of `org-todo-keywords-1'."
  8873. (interactive "P")
  8874. (let ((case-fold-search nil)
  8875. (kwd-re
  8876. (cond ((null arg) org-not-done-regexp)
  8877. ((equal arg '(4))
  8878. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8879. (mapcar 'list org-todo-keywords-1))))
  8880. (concat "\\("
  8881. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8882. "\\)\\>")))
  8883. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8884. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8885. org-todo-keywords-1)))
  8886. (t (error "Invalid prefix argument: %s" arg)))))
  8887. (message "%d TODO entries found"
  8888. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8889. (defun org-deadline (&optional remove time)
  8890. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8891. With argument REMOVE, remove any deadline from the item.
  8892. When TIME is set, it should be an internal time specification, and the
  8893. scheduling will use the corresponding date."
  8894. (interactive "P")
  8895. (if remove
  8896. (progn
  8897. (org-remove-timestamp-with-keyword org-deadline-string)
  8898. (message "Item no longer has a deadline."))
  8899. (if (org-get-repeat)
  8900. (error "Cannot change deadline on task with repeater, please do that by hand")
  8901. (org-add-planning-info 'deadline time 'closed)
  8902. (message "Deadline on %s" org-last-inserted-timestamp))))
  8903. (defun org-schedule (&optional remove time)
  8904. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8905. With argument REMOVE, remove any scheduling date from the item.
  8906. When TIME is set, it should be an internal time specification, and the
  8907. scheduling will use the corresponding date."
  8908. (interactive "P")
  8909. (if remove
  8910. (progn
  8911. (org-remove-timestamp-with-keyword org-scheduled-string)
  8912. (message "Item is no longer scheduled."))
  8913. (if (org-get-repeat)
  8914. (error "Cannot reschedule task with repeater, please do that by hand")
  8915. (org-add-planning-info 'scheduled time 'closed)
  8916. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8917. (defun org-get-scheduled-time (pom &optional inherit)
  8918. "Get the scheduled time as a time tuple, of a format suitable
  8919. for calling org-schedule with, or if there is no scheduling,
  8920. returns nil."
  8921. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8922. (when time
  8923. (apply 'encode-time (org-parse-time-string time)))))
  8924. (defun org-get-deadline-time (pom &optional inherit)
  8925. "Get the deadine as a time tuple, of a format suitable for
  8926. calling org-deadlin with, or if there is no scheduling, returns
  8927. nil."
  8928. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8929. (when time
  8930. (apply 'encode-time (org-parse-time-string time)))))
  8931. (defun org-remove-timestamp-with-keyword (keyword)
  8932. "Remove all time stamps with KEYWORD in the current entry."
  8933. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8934. beg)
  8935. (save-excursion
  8936. (org-back-to-heading t)
  8937. (setq beg (point))
  8938. (org-end-of-subtree t t)
  8939. (while (re-search-backward re beg t)
  8940. (replace-match "")
  8941. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8942. (equal (char-before) ?\ ))
  8943. (backward-delete-char 1)
  8944. (if (string-match "^[ \t]*$" (buffer-substring
  8945. (point-at-bol) (point-at-eol)))
  8946. (delete-region (point-at-bol)
  8947. (min (point-max) (1+ (point-at-eol))))))))))
  8948. (defun org-add-planning-info (what &optional time &rest remove)
  8949. "Insert new timestamp with keyword in the line directly after the headline.
  8950. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8951. If non is given, the user is prompted for a date.
  8952. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8953. be removed."
  8954. (interactive)
  8955. (let (org-time-was-given org-end-time-was-given ts
  8956. end default-time default-input)
  8957. (catch 'exit
  8958. (when (and (not time) (memq what '(scheduled deadline)))
  8959. ;; Try to get a default date/time from existing timestamp
  8960. (save-excursion
  8961. (org-back-to-heading t)
  8962. (setq end (save-excursion (outline-next-heading) (point)))
  8963. (when (re-search-forward (if (eq what 'scheduled)
  8964. org-scheduled-time-regexp
  8965. org-deadline-time-regexp)
  8966. end t)
  8967. (setq ts (match-string 1)
  8968. default-time
  8969. (apply 'encode-time (org-parse-time-string ts))
  8970. default-input (and ts (org-get-compact-tod ts))))))
  8971. (when what
  8972. ;; If necessary, get the time from the user
  8973. (setq time (or time (org-read-date nil 'to-time nil nil
  8974. default-time default-input))))
  8975. (when (and org-insert-labeled-timestamps-at-point
  8976. (member what '(scheduled deadline)))
  8977. (insert
  8978. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8979. (org-insert-time-stamp time org-time-was-given
  8980. nil nil nil (list org-end-time-was-given))
  8981. (setq what nil))
  8982. (save-excursion
  8983. (save-restriction
  8984. (let (col list elt ts buffer-invisibility-spec)
  8985. (org-back-to-heading t)
  8986. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8987. (goto-char (match-end 1))
  8988. (setq col (current-column))
  8989. (goto-char (match-end 0))
  8990. (if (eobp) (insert "\n") (forward-char 1))
  8991. (when (and (not what)
  8992. (not (looking-at
  8993. (concat "[ \t]*"
  8994. org-keyword-time-not-clock-regexp))))
  8995. ;; Nothing to add, nothing to remove...... :-)
  8996. (throw 'exit nil))
  8997. (if (and (not (looking-at outline-regexp))
  8998. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8999. "[^\r\n]*"))
  9000. (not (equal (match-string 1) org-clock-string)))
  9001. (narrow-to-region (match-beginning 0) (match-end 0))
  9002. (insert-before-markers "\n")
  9003. (backward-char 1)
  9004. (narrow-to-region (point) (point))
  9005. (and org-adapt-indentation (org-indent-to-column col)))
  9006. ;; Check if we have to remove something.
  9007. (setq list (cons what remove))
  9008. (while list
  9009. (setq elt (pop list))
  9010. (goto-char (point-min))
  9011. (when (or (and (eq elt 'scheduled)
  9012. (re-search-forward org-scheduled-time-regexp nil t))
  9013. (and (eq elt 'deadline)
  9014. (re-search-forward org-deadline-time-regexp nil t))
  9015. (and (eq elt 'closed)
  9016. (re-search-forward org-closed-time-regexp nil t)))
  9017. (replace-match "")
  9018. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9019. (if (looking-at " +") (replace-match ""))))
  9020. (goto-char (point-max))
  9021. (when what
  9022. (insert
  9023. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9024. (cond ((eq what 'scheduled) org-scheduled-string)
  9025. ((eq what 'deadline) org-deadline-string)
  9026. ((eq what 'closed) org-closed-string))
  9027. " ")
  9028. (setq ts (org-insert-time-stamp
  9029. time
  9030. (or org-time-was-given
  9031. (and (eq what 'closed) org-log-done-with-time))
  9032. (eq what 'closed)
  9033. nil nil (list org-end-time-was-given)))
  9034. (end-of-line 1))
  9035. (goto-char (point-min))
  9036. (widen)
  9037. (if (and (looking-at "[ \t]+\n")
  9038. (equal (char-before) ?\n))
  9039. (delete-region (1- (point)) (point-at-eol)))
  9040. ts))))))
  9041. (defvar org-log-note-marker (make-marker))
  9042. (defvar org-log-note-purpose nil)
  9043. (defvar org-log-note-state nil)
  9044. (defvar org-log-note-previous-state nil)
  9045. (defvar org-log-note-how nil)
  9046. (defvar org-log-note-extra nil)
  9047. (defvar org-log-note-window-configuration nil)
  9048. (defvar org-log-note-return-to (make-marker))
  9049. (defvar org-log-post-message nil
  9050. "Message to be displayed after a log note has been stored.
  9051. The auto-repeater uses this.")
  9052. (defun org-add-note ()
  9053. "Add a note to the current entry.
  9054. This is done in the same way as adding a state change note."
  9055. (interactive)
  9056. (org-add-log-setup 'note nil nil 'findpos nil))
  9057. (defvar org-property-end-re)
  9058. (defun org-add-log-setup (&optional purpose state prev-state
  9059. findpos how &optional extra)
  9060. "Set up the post command hook to take a note.
  9061. If this is about to TODO state change, the new state is expected in STATE.
  9062. When FINDPOS is non-nil, find the correct position for the note in
  9063. the current entry. If not, assume that it can be inserted at point.
  9064. HOW is an indicator what kind of note should be created.
  9065. EXTRA is additional text that will be inserted into the notes buffer."
  9066. (let* ((org-log-into-drawer (org-log-into-drawer))
  9067. (drawer (cond ((stringp org-log-into-drawer)
  9068. org-log-into-drawer)
  9069. (org-log-into-drawer "LOGBOOK")
  9070. (t nil))))
  9071. (save-restriction
  9072. (save-excursion
  9073. (when findpos
  9074. (org-back-to-heading t)
  9075. (narrow-to-region (point) (save-excursion
  9076. (outline-next-heading) (point)))
  9077. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9078. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9079. "[^\r\n]*\\)?"))
  9080. (goto-char (match-end 0))
  9081. (cond
  9082. (drawer
  9083. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9084. nil t)
  9085. (progn
  9086. (goto-char (match-end 0))
  9087. (or org-log-states-order-reversed
  9088. (and (re-search-forward org-property-end-re nil t)
  9089. (goto-char (1- (match-beginning 0))))))
  9090. (insert "\n:" drawer ":\n:END:")
  9091. (beginning-of-line 0)
  9092. (org-indent-line-function)
  9093. (beginning-of-line 2)
  9094. (org-indent-line-function)
  9095. (end-of-line 0)))
  9096. ((and org-log-state-notes-insert-after-drawers
  9097. (save-excursion
  9098. (forward-line) (looking-at org-drawer-regexp)))
  9099. (forward-line)
  9100. (while (looking-at org-drawer-regexp)
  9101. (goto-char (match-end 0))
  9102. (re-search-forward org-property-end-re (point-max) t)
  9103. (forward-line))
  9104. (forward-line -1)))
  9105. (unless org-log-states-order-reversed
  9106. (and (= (char-after) ?\n) (forward-char 1))
  9107. (org-skip-over-state-notes)
  9108. (skip-chars-backward " \t\n\r")))
  9109. (move-marker org-log-note-marker (point))
  9110. (setq org-log-note-purpose purpose
  9111. org-log-note-state state
  9112. org-log-note-previous-state prev-state
  9113. org-log-note-how how
  9114. org-log-note-extra extra)
  9115. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9116. (defun org-skip-over-state-notes ()
  9117. "Skip past the list of State notes in an entry."
  9118. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9119. (while (looking-at "[ \t]*- State")
  9120. (condition-case nil
  9121. (org-next-item)
  9122. (error (org-end-of-item)))))
  9123. (defun org-add-log-note (&optional purpose)
  9124. "Pop up a window for taking a note, and add this note later at point."
  9125. (remove-hook 'post-command-hook 'org-add-log-note)
  9126. (setq org-log-note-window-configuration (current-window-configuration))
  9127. (delete-other-windows)
  9128. (move-marker org-log-note-return-to (point))
  9129. (switch-to-buffer (marker-buffer org-log-note-marker))
  9130. (goto-char org-log-note-marker)
  9131. (org-switch-to-buffer-other-window "*Org Note*")
  9132. (erase-buffer)
  9133. (if (memq org-log-note-how '(time state))
  9134. (let (current-prefix-arg) (org-store-log-note))
  9135. (let ((org-inhibit-startup t)) (org-mode))
  9136. (insert (format "# Insert note for %s.
  9137. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9138. (cond
  9139. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9140. ((eq org-log-note-purpose 'done) "closed todo item")
  9141. ((eq org-log-note-purpose 'state)
  9142. (format "state change from \"%s\" to \"%s\""
  9143. (or org-log-note-previous-state "")
  9144. (or org-log-note-state "")))
  9145. ((eq org-log-note-purpose 'note)
  9146. "this entry")
  9147. (t (error "This should not happen")))))
  9148. (if org-log-note-extra (insert org-log-note-extra))
  9149. (org-set-local 'org-finish-function 'org-store-log-note)))
  9150. (defvar org-note-abort nil) ; dynamically scoped
  9151. (defun org-store-log-note ()
  9152. "Finish taking a log note, and insert it to where it belongs."
  9153. (let ((txt (buffer-string))
  9154. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9155. lines ind)
  9156. (kill-buffer (current-buffer))
  9157. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9158. (setq txt (replace-match "" t t txt)))
  9159. (if (string-match "\\s-+\\'" txt)
  9160. (setq txt (replace-match "" t t txt)))
  9161. (setq lines (org-split-string txt "\n"))
  9162. (when (and note (string-match "\\S-" note))
  9163. (setq note
  9164. (org-replace-escapes
  9165. note
  9166. (list (cons "%u" (user-login-name))
  9167. (cons "%U" user-full-name)
  9168. (cons "%t" (format-time-string
  9169. (org-time-stamp-format 'long 'inactive)
  9170. (current-time)))
  9171. (cons "%s" (if org-log-note-state
  9172. (concat "\"" org-log-note-state "\"")
  9173. ""))
  9174. (cons "%S" (if org-log-note-previous-state
  9175. (concat "\"" org-log-note-previous-state "\"")
  9176. "\"\"")))))
  9177. (if lines (setq note (concat note " \\\\")))
  9178. (push note lines))
  9179. (when (or current-prefix-arg org-note-abort)
  9180. (when org-log-into-drawer
  9181. (org-remove-empty-drawer-at
  9182. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9183. org-log-note-marker))
  9184. (setq lines nil))
  9185. (when lines
  9186. (save-excursion
  9187. (set-buffer (marker-buffer org-log-note-marker))
  9188. (save-excursion
  9189. (goto-char org-log-note-marker)
  9190. (move-marker org-log-note-marker nil)
  9191. (end-of-line 1)
  9192. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9193. (insert "- " (pop lines))
  9194. (org-indent-line-function)
  9195. (beginning-of-line 1)
  9196. (looking-at "[ \t]*")
  9197. (setq ind (concat (match-string 0) " "))
  9198. (end-of-line 1)
  9199. (while lines (insert "\n" ind (pop lines)))
  9200. (message "Note stored")
  9201. (org-back-to-heading t)
  9202. (org-cycle-hide-drawers 'children)))))
  9203. (set-window-configuration org-log-note-window-configuration)
  9204. (with-current-buffer (marker-buffer org-log-note-return-to)
  9205. (goto-char org-log-note-return-to))
  9206. (move-marker org-log-note-return-to nil)
  9207. (and org-log-post-message (message "%s" org-log-post-message)))
  9208. (defun org-remove-empty-drawer-at (drawer pos)
  9209. "Remove an emptyr DARWER drawer at position POS.
  9210. POS may also be a marker."
  9211. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9212. (save-excursion
  9213. (save-restriction
  9214. (widen)
  9215. (goto-char pos)
  9216. (if (org-in-regexp
  9217. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9218. (replace-match ""))))))
  9219. (defun org-sparse-tree (&optional arg)
  9220. "Create a sparse tree, prompt for the details.
  9221. This command can create sparse trees. You first need to select the type
  9222. of match used to create the tree:
  9223. t Show entries with a specific TODO keyword.
  9224. m Show entries selected by a tags/property match.
  9225. p Enter a property name and its value (both with completion on existing
  9226. names/values) and show entries with that property.
  9227. r Show entries matching a regular expression.
  9228. d Show deadlines due within `org-deadline-warning-days'.
  9229. b Show deadlines and scheduled items before a date.
  9230. a Show deadlines and scheduled items after a date."
  9231. (interactive "P")
  9232. (let (ans kwd value)
  9233. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9234. (setq ans (read-char-exclusive))
  9235. (cond
  9236. ((equal ans ?d)
  9237. (call-interactively 'org-check-deadlines))
  9238. ((equal ans ?b)
  9239. (call-interactively 'org-check-before-date))
  9240. ((equal ans ?a)
  9241. (call-interactively 'org-check-after-date))
  9242. ((equal ans ?t)
  9243. (org-show-todo-tree '(4)))
  9244. ((member ans '(?T ?m))
  9245. (call-interactively 'org-match-sparse-tree))
  9246. ((member ans '(?p ?P))
  9247. (setq kwd (org-ido-completing-read "Property: "
  9248. (mapcar 'list (org-buffer-property-keys))))
  9249. (setq value (org-ido-completing-read "Value: "
  9250. (mapcar 'list (org-property-values kwd))))
  9251. (unless (string-match "\\`{.*}\\'" value)
  9252. (setq value (concat "\"" value "\"")))
  9253. (org-match-sparse-tree arg (concat kwd "=" value)))
  9254. ((member ans '(?r ?R ?/))
  9255. (call-interactively 'org-occur))
  9256. (t (error "No such sparse tree command \"%c\"" ans)))))
  9257. (defvar org-occur-highlights nil
  9258. "List of overlays used for occur matches.")
  9259. (make-variable-buffer-local 'org-occur-highlights)
  9260. (defvar org-occur-parameters nil
  9261. "Parameters of the active org-occur calls.
  9262. This is a list, each call to org-occur pushes as cons cell,
  9263. containing the regular expression and the callback, onto the list.
  9264. The list can contain several entries if `org-occur' has been called
  9265. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9266. will only contain one set of parameters. When the highlights are
  9267. removed (for example with `C-c C-c', or with the next edit (depending
  9268. on `org-remove-highlights-with-change'), this variable is emptied
  9269. as well.")
  9270. (make-variable-buffer-local 'org-occur-parameters)
  9271. (defun org-occur (regexp &optional keep-previous callback)
  9272. "Make a compact tree which shows all matches of REGEXP.
  9273. The tree will show the lines where the regexp matches, and all higher
  9274. headlines above the match. It will also show the heading after the match,
  9275. to make sure editing the matching entry is easy.
  9276. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9277. call to `org-occur' will be kept, to allow stacking of calls to this
  9278. command.
  9279. If CALLBACK is non-nil, it is a function which is called to confirm
  9280. that the match should indeed be shown."
  9281. (interactive "sRegexp: \nP")
  9282. (unless keep-previous
  9283. (org-remove-occur-highlights nil nil t))
  9284. (push (cons regexp callback) org-occur-parameters)
  9285. (let ((cnt 0))
  9286. (save-excursion
  9287. (goto-char (point-min))
  9288. (if (or (not keep-previous) ; do not want to keep
  9289. (not org-occur-highlights)) ; no previous matches
  9290. ;; hide everything
  9291. (org-overview))
  9292. (while (re-search-forward regexp nil t)
  9293. (when (or (not callback)
  9294. (save-match-data (funcall callback)))
  9295. (setq cnt (1+ cnt))
  9296. (when org-highlight-sparse-tree-matches
  9297. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9298. (org-show-context 'occur-tree))))
  9299. (when org-remove-highlights-with-change
  9300. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9301. nil 'local))
  9302. (unless org-sparse-tree-open-archived-trees
  9303. (org-hide-archived-subtrees (point-min) (point-max)))
  9304. (run-hooks 'org-occur-hook)
  9305. (if (interactive-p)
  9306. (message "%d match(es) for regexp %s" cnt regexp))
  9307. cnt))
  9308. (defun org-show-context (&optional key)
  9309. "Make sure point and context and visible.
  9310. How much context is shown depends upon the variables
  9311. `org-show-hierarchy-above', `org-show-following-heading'. and
  9312. `org-show-siblings'."
  9313. (let ((heading-p (org-on-heading-p t))
  9314. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9315. (following-p (org-get-alist-option org-show-following-heading key))
  9316. (entry-p (org-get-alist-option org-show-entry-below key))
  9317. (siblings-p (org-get-alist-option org-show-siblings key)))
  9318. (catch 'exit
  9319. ;; Show heading or entry text
  9320. (if (and heading-p (not entry-p))
  9321. (org-flag-heading nil) ; only show the heading
  9322. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9323. (org-show-hidden-entry))) ; show entire entry
  9324. (when following-p
  9325. ;; Show next sibling, or heading below text
  9326. (save-excursion
  9327. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9328. (org-flag-heading nil))))
  9329. (when siblings-p (org-show-siblings))
  9330. (when hierarchy-p
  9331. ;; show all higher headings, possibly with siblings
  9332. (save-excursion
  9333. (while (and (condition-case nil
  9334. (progn (org-up-heading-all 1) t)
  9335. (error nil))
  9336. (not (bobp)))
  9337. (org-flag-heading nil)
  9338. (when siblings-p (org-show-siblings))))))))
  9339. (defun org-reveal (&optional siblings)
  9340. "Show current entry, hierarchy above it, and the following headline.
  9341. This can be used to show a consistent set of context around locations
  9342. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9343. not t for the search context.
  9344. With optional argument SIBLINGS, on each level of the hierarchy all
  9345. siblings are shown. This repairs the tree structure to what it would
  9346. look like when opened with hierarchical calls to `org-cycle'."
  9347. (interactive "P")
  9348. (let ((org-show-hierarchy-above t)
  9349. (org-show-following-heading t)
  9350. (org-show-siblings (if siblings t org-show-siblings)))
  9351. (org-show-context nil)))
  9352. (defun org-highlight-new-match (beg end)
  9353. "Highlight from BEG to END and mark the highlight is an occur headline."
  9354. (let ((ov (org-make-overlay beg end)))
  9355. (org-overlay-put ov 'face 'secondary-selection)
  9356. (push ov org-occur-highlights)))
  9357. (defun org-remove-occur-highlights (&optional beg end noremove)
  9358. "Remove the occur highlights from the buffer.
  9359. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9360. from the `before-change-functions' in the current buffer."
  9361. (interactive)
  9362. (unless org-inhibit-highlight-removal
  9363. (mapc 'org-delete-overlay org-occur-highlights)
  9364. (setq org-occur-highlights nil)
  9365. (setq org-occur-parameters nil)
  9366. (unless noremove
  9367. (remove-hook 'before-change-functions
  9368. 'org-remove-occur-highlights 'local))))
  9369. ;;;; Priorities
  9370. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9371. "Regular expression matching the priority indicator.")
  9372. (defvar org-remove-priority-next-time nil)
  9373. (defun org-priority-up ()
  9374. "Increase the priority of the current item."
  9375. (interactive)
  9376. (org-priority 'up))
  9377. (defun org-priority-down ()
  9378. "Decrease the priority of the current item."
  9379. (interactive)
  9380. (org-priority 'down))
  9381. (defun org-priority (&optional action)
  9382. "Change the priority of an item by ARG.
  9383. ACTION can be `set', `up', `down', or a character."
  9384. (interactive)
  9385. (unless org-enable-priority-commands
  9386. (error "Priority commands are disabled"))
  9387. (setq action (or action 'set))
  9388. (let (current new news have remove)
  9389. (save-excursion
  9390. (org-back-to-heading t)
  9391. (if (looking-at org-priority-regexp)
  9392. (setq current (string-to-char (match-string 2))
  9393. have t)
  9394. (setq current org-default-priority))
  9395. (cond
  9396. ((or (eq action 'set)
  9397. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9398. (if (not (eq action 'set))
  9399. (setq new action)
  9400. (message "Priority %c-%c, SPC to remove: "
  9401. org-highest-priority org-lowest-priority)
  9402. (setq new (read-char-exclusive)))
  9403. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9404. (= (upcase org-lowest-priority) org-lowest-priority))
  9405. (setq new (upcase new)))
  9406. (cond ((equal new ?\ ) (setq remove t))
  9407. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9408. (error "Priority must be between `%c' and `%c'"
  9409. org-highest-priority org-lowest-priority))))
  9410. ((eq action 'up)
  9411. (if (and (not have) (eq last-command this-command))
  9412. (setq new org-lowest-priority)
  9413. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9414. org-default-priority (1- current)))))
  9415. ((eq action 'down)
  9416. (if (and (not have) (eq last-command this-command))
  9417. (setq new org-highest-priority)
  9418. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9419. org-default-priority (1+ current)))))
  9420. (t (error "Invalid action")))
  9421. (if (or (< (upcase new) org-highest-priority)
  9422. (> (upcase new) org-lowest-priority))
  9423. (setq remove t))
  9424. (setq news (format "%c" new))
  9425. (if have
  9426. (if remove
  9427. (replace-match "" t t nil 1)
  9428. (replace-match news t t nil 2))
  9429. (if remove
  9430. (error "No priority cookie found in line")
  9431. (let ((case-fold-search nil))
  9432. (looking-at org-todo-line-regexp))
  9433. (if (match-end 2)
  9434. (progn
  9435. (goto-char (match-end 2))
  9436. (insert " [#" news "]"))
  9437. (goto-char (match-beginning 3))
  9438. (insert "[#" news "] ")))))
  9439. (org-preserve-lc (org-set-tags nil 'align))
  9440. (if remove
  9441. (message "Priority removed")
  9442. (message "Priority of current item set to %s" news))))
  9443. (defun org-get-priority (s)
  9444. "Find priority cookie and return priority."
  9445. (save-match-data
  9446. (if (not (string-match org-priority-regexp s))
  9447. (* 1000 (- org-lowest-priority org-default-priority))
  9448. (* 1000 (- org-lowest-priority
  9449. (string-to-char (match-string 2 s)))))))
  9450. ;;;; Tags
  9451. (defvar org-agenda-archives-mode)
  9452. (defvar org-map-continue-from nil
  9453. "Position from where mapping should continue.
  9454. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9455. (defvar org-scanner-tags nil
  9456. "The current tag list while the tags scanner is running.")
  9457. (defvar org-trust-scanner-tags nil
  9458. "Should `org-get-tags-at' use the tags fro the scanner.
  9459. This is for internal dynamical scoping only.
  9460. When this is non-nil, the function `org-get-tags-at' will return the value
  9461. of `org-scanner-tags' instead of building the list by itself. This
  9462. can lead to large speed-ups when the tags scanner is used in a file with
  9463. many entries, and when the list of tags is retrieved, for example to
  9464. obtain a list of properties. Building the tags list for each entry in such
  9465. a file becomes an N^2 operation - but with this variable set, it scales
  9466. as N.")
  9467. (defun org-scan-tags (action matcher &optional todo-only)
  9468. "Scan headline tags with inheritance and produce output ACTION.
  9469. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9470. or `agenda' to produce an entry list for an agenda view. It can also be
  9471. a Lisp form or a function that should be called at each matched headline, in
  9472. this case the return value is a list of all return values from these calls.
  9473. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9474. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9475. only lines with a TODO keyword are included in the output."
  9476. (require 'org-agenda)
  9477. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9478. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9479. (org-re
  9480. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9481. (props (list 'face 'default
  9482. 'done-face 'org-agenda-done
  9483. 'undone-face 'default
  9484. 'mouse-face 'highlight
  9485. 'org-not-done-regexp org-not-done-regexp
  9486. 'org-todo-regexp org-todo-regexp
  9487. 'keymap org-agenda-keymap
  9488. 'help-echo
  9489. (format "mouse-2 or RET jump to org file %s"
  9490. (abbreviate-file-name
  9491. (or (buffer-file-name (buffer-base-buffer))
  9492. (buffer-name (buffer-base-buffer)))))))
  9493. (case-fold-search nil)
  9494. (org-map-continue-from nil)
  9495. lspos tags tags-list
  9496. (tags-alist (list (cons 0 org-file-tags)))
  9497. (llast 0) rtn rtn1 level category i txt
  9498. todo marker entry priority)
  9499. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9500. (setq action (list 'lambda nil action)))
  9501. (save-excursion
  9502. (goto-char (point-min))
  9503. (when (eq action 'sparse-tree)
  9504. (org-overview)
  9505. (org-remove-occur-highlights))
  9506. (while (re-search-forward re nil t)
  9507. (catch :skip
  9508. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9509. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9510. (goto-char (setq lspos (match-beginning 0)))
  9511. (setq level (org-reduced-level (funcall outline-level))
  9512. category (org-get-category))
  9513. (setq i llast llast level)
  9514. ;; remove tag lists from same and sublevels
  9515. (while (>= i level)
  9516. (when (setq entry (assoc i tags-alist))
  9517. (setq tags-alist (delete entry tags-alist)))
  9518. (setq i (1- i)))
  9519. ;; add the next tags
  9520. (when tags
  9521. (setq tags (org-split-string tags ":")
  9522. tags-alist
  9523. (cons (cons level tags) tags-alist)))
  9524. ;; compile tags for current headline
  9525. (setq tags-list
  9526. (if org-use-tag-inheritance
  9527. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9528. tags)
  9529. org-scanner-tags tags-list)
  9530. (when org-use-tag-inheritance
  9531. (setcdr (car tags-alist)
  9532. (mapcar (lambda (x)
  9533. (setq x (copy-sequence x))
  9534. (org-add-prop-inherited x))
  9535. (cdar tags-alist))))
  9536. (when (and tags org-use-tag-inheritance
  9537. (or (not (eq t org-use-tag-inheritance))
  9538. org-tags-exclude-from-inheritance))
  9539. ;; selective inheritance, remove uninherited ones
  9540. (setcdr (car tags-alist)
  9541. (org-remove-uniherited-tags (cdar tags-alist))))
  9542. (when (and (or (not todo-only)
  9543. (and (member todo org-not-done-keywords)
  9544. (or (not org-agenda-tags-todo-honor-ignore-options)
  9545. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9546. (let ((case-fold-search t)) (eval matcher))
  9547. (or
  9548. (not (member org-archive-tag tags-list))
  9549. ;; we have an archive tag, should we use this anyway?
  9550. (or (not org-agenda-skip-archived-trees)
  9551. (and (eq action 'agenda) org-agenda-archives-mode))))
  9552. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9553. ;; select this headline
  9554. (cond
  9555. ((eq action 'sparse-tree)
  9556. (and org-highlight-sparse-tree-matches
  9557. (org-get-heading) (match-end 0)
  9558. (org-highlight-new-match
  9559. (match-beginning 0) (match-beginning 1)))
  9560. (org-show-context 'tags-tree))
  9561. ((eq action 'agenda)
  9562. (setq txt (org-format-agenda-item
  9563. ""
  9564. (concat
  9565. (if (eq org-tags-match-list-sublevels 'indented)
  9566. (make-string (1- level) ?.) "")
  9567. (org-get-heading))
  9568. category
  9569. tags-list
  9570. )
  9571. priority (org-get-priority txt))
  9572. (goto-char lspos)
  9573. (setq marker (org-agenda-new-marker))
  9574. (org-add-props txt props
  9575. 'org-marker marker 'org-hd-marker marker 'org-category category
  9576. 'todo-state todo
  9577. 'priority priority 'type "tagsmatch")
  9578. (push txt rtn))
  9579. ((functionp action)
  9580. (setq org-map-continue-from nil)
  9581. (save-excursion
  9582. (setq rtn1 (funcall action))
  9583. (push rtn1 rtn)))
  9584. (t (error "Invalid action")))
  9585. ;; if we are to skip sublevels, jump to end of subtree
  9586. (unless org-tags-match-list-sublevels
  9587. (org-end-of-subtree t)
  9588. (backward-char 1))))
  9589. ;; Get the correct position from where to continue
  9590. (if org-map-continue-from
  9591. (goto-char org-map-continue-from)
  9592. (and (= (point) lspos) (end-of-line 1)))))
  9593. (when (and (eq action 'sparse-tree)
  9594. (not org-sparse-tree-open-archived-trees))
  9595. (org-hide-archived-subtrees (point-min) (point-max)))
  9596. (nreverse rtn)))
  9597. (defun org-remove-uniherited-tags (tags)
  9598. "Remove all tags that are not inherited from the list TAGS."
  9599. (cond
  9600. ((eq org-use-tag-inheritance t)
  9601. (if org-tags-exclude-from-inheritance
  9602. (org-delete-all org-tags-exclude-from-inheritance tags)
  9603. tags))
  9604. ((not org-use-tag-inheritance) nil)
  9605. ((stringp org-use-tag-inheritance)
  9606. (delq nil (mapcar
  9607. (lambda (x)
  9608. (if (and (string-match org-use-tag-inheritance x)
  9609. (not (member x org-tags-exclude-from-inheritance)))
  9610. x nil))
  9611. tags)))
  9612. ((listp org-use-tag-inheritance)
  9613. (delq nil (mapcar
  9614. (lambda (x)
  9615. (if (member x org-use-tag-inheritance) x nil))
  9616. tags)))))
  9617. (defvar todo-only) ;; dynamically scoped
  9618. (defun org-match-sparse-tree (&optional todo-only match)
  9619. "Create a sparse tree according to tags string MATCH.
  9620. MATCH can contain positive and negative selection of tags, like
  9621. \"+WORK+URGENT-WITHBOSS\".
  9622. If optional argument TODO-ONLY is non-nil, only select lines that are
  9623. also TODO lines."
  9624. (interactive "P")
  9625. (org-prepare-agenda-buffers (list (current-buffer)))
  9626. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9627. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9628. (defvar org-cached-props nil)
  9629. (defun org-cached-entry-get (pom property)
  9630. (if (or (eq t org-use-property-inheritance)
  9631. (and (stringp org-use-property-inheritance)
  9632. (string-match org-use-property-inheritance property))
  9633. (and (listp org-use-property-inheritance)
  9634. (member property org-use-property-inheritance)))
  9635. ;; Caching is not possible, check it directly
  9636. (org-entry-get pom property 'inherit)
  9637. ;; Get all properties, so that we can do complicated checks easily
  9638. (cdr (assoc property (or org-cached-props
  9639. (setq org-cached-props
  9640. (org-entry-properties pom)))))))
  9641. (defun org-global-tags-completion-table (&optional files)
  9642. "Return the list of all tags in all agenda buffer/files."
  9643. (save-excursion
  9644. (org-uniquify
  9645. (delq nil
  9646. (apply 'append
  9647. (mapcar
  9648. (lambda (file)
  9649. (set-buffer (find-file-noselect file))
  9650. (append (org-get-buffer-tags)
  9651. (mapcar (lambda (x) (if (stringp (car-safe x))
  9652. (list (car-safe x)) nil))
  9653. org-tag-alist)))
  9654. (if (and files (car files))
  9655. files
  9656. (org-agenda-files))))))))
  9657. (defun org-make-tags-matcher (match)
  9658. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9659. ;; todo-only is scoped dynamically into this function, and the function
  9660. ;; may change it if the matcher asks for it.
  9661. (unless match
  9662. ;; Get a new match request, with completion
  9663. (let ((org-last-tags-completion-table
  9664. (org-global-tags-completion-table)))
  9665. (setq match (org-completing-read-no-ido
  9666. "Match: " 'org-tags-completion-function nil nil nil
  9667. 'org-tags-history))))
  9668. ;; Parse the string and create a lisp form
  9669. (let ((match0 match)
  9670. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9671. minus tag mm
  9672. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9673. orterms term orlist re-p str-p level-p level-op time-p
  9674. prop-p pn pv po cat-p gv rest)
  9675. (if (string-match "/+" match)
  9676. ;; match contains also a todo-matching request
  9677. (progn
  9678. (setq tagsmatch (substring match 0 (match-beginning 0))
  9679. todomatch (substring match (match-end 0)))
  9680. (if (string-match "^!" todomatch)
  9681. (setq todo-only t todomatch (substring todomatch 1)))
  9682. (if (string-match "^\\s-*$" todomatch)
  9683. (setq todomatch nil)))
  9684. ;; only matching tags
  9685. (setq tagsmatch match todomatch nil))
  9686. ;; Make the tags matcher
  9687. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9688. (setq tagsmatcher t)
  9689. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9690. (while (setq term (pop orterms))
  9691. (while (and (equal (substring term -1) "\\") orterms)
  9692. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9693. (while (string-match re term)
  9694. (setq rest (substring term (match-end 0))
  9695. minus (and (match-end 1)
  9696. (equal (match-string 1 term) "-"))
  9697. tag (match-string 2 term)
  9698. re-p (equal (string-to-char tag) ?{)
  9699. level-p (match-end 4)
  9700. prop-p (match-end 5)
  9701. mm (cond
  9702. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9703. (level-p
  9704. (setq level-op (org-op-to-function (match-string 3 term)))
  9705. `(,level-op level ,(string-to-number
  9706. (match-string 4 term))))
  9707. (prop-p
  9708. (setq pn (match-string 5 term)
  9709. po (match-string 6 term)
  9710. pv (match-string 7 term)
  9711. cat-p (equal pn "CATEGORY")
  9712. re-p (equal (string-to-char pv) ?{)
  9713. str-p (equal (string-to-char pv) ?\")
  9714. time-p (save-match-data
  9715. (string-match "^\"[[<].*[]>]\"$" pv))
  9716. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9717. (if time-p (setq pv (org-matcher-time pv)))
  9718. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9719. (cond
  9720. ((equal pn "CATEGORY")
  9721. (setq gv '(get-text-property (point) 'org-category)))
  9722. ((equal pn "TODO")
  9723. (setq gv 'todo))
  9724. (t
  9725. (setq gv `(org-cached-entry-get nil ,pn))))
  9726. (if re-p
  9727. (if (eq po 'org<>)
  9728. `(not (string-match ,pv (or ,gv "")))
  9729. `(string-match ,pv (or ,gv "")))
  9730. (if str-p
  9731. `(,po (or ,gv "") ,pv)
  9732. `(,po (string-to-number (or ,gv ""))
  9733. ,(string-to-number pv) ))))
  9734. (t `(member ,tag tags-list)))
  9735. mm (if minus (list 'not mm) mm)
  9736. term rest)
  9737. (push mm tagsmatcher))
  9738. (push (if (> (length tagsmatcher) 1)
  9739. (cons 'and tagsmatcher)
  9740. (car tagsmatcher))
  9741. orlist)
  9742. (setq tagsmatcher nil))
  9743. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9744. (setq tagsmatcher
  9745. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9746. ;; Make the todo matcher
  9747. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9748. (setq todomatcher t)
  9749. (setq orterms (org-split-string todomatch "|") orlist nil)
  9750. (while (setq term (pop orterms))
  9751. (while (string-match re term)
  9752. (setq minus (and (match-end 1)
  9753. (equal (match-string 1 term) "-"))
  9754. kwd (match-string 2 term)
  9755. re-p (equal (string-to-char kwd) ?{)
  9756. term (substring term (match-end 0))
  9757. mm (if re-p
  9758. `(string-match ,(substring kwd 1 -1) todo)
  9759. (list 'equal 'todo kwd))
  9760. mm (if minus (list 'not mm) mm))
  9761. (push mm todomatcher))
  9762. (push (if (> (length todomatcher) 1)
  9763. (cons 'and todomatcher)
  9764. (car todomatcher))
  9765. orlist)
  9766. (setq todomatcher nil))
  9767. (setq todomatcher (if (> (length orlist) 1)
  9768. (cons 'or orlist) (car orlist))))
  9769. ;; Return the string and lisp forms of the matcher
  9770. (setq matcher (if todomatcher
  9771. (list 'and tagsmatcher todomatcher)
  9772. tagsmatcher))
  9773. (cons match0 matcher)))
  9774. (defun org-op-to-function (op &optional stringp)
  9775. "Turn an operator into the appropriate function."
  9776. (setq op
  9777. (cond
  9778. ((equal op "<" ) '(< string< org-time<))
  9779. ((equal op ">" ) '(> org-string> org-time>))
  9780. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9781. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9782. ((member op '("=" "==")) '(= string= org-time=))
  9783. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9784. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9785. (defun org<> (a b) (not (= a b)))
  9786. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9787. (defun org-string>= (a b) (not (string< a b)))
  9788. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9789. (defun org-string<> (a b) (not (string= a b)))
  9790. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9791. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9792. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9793. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9794. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9795. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9796. (defun org-2ft (s)
  9797. "Convert S to a floating point time.
  9798. If S is already a number, just return it. If it is a string, parse
  9799. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9800. (cond
  9801. ((numberp s) s)
  9802. ((stringp s)
  9803. (condition-case nil
  9804. (float-time (apply 'encode-time (org-parse-time-string s)))
  9805. (error 0.)))
  9806. (t 0.)))
  9807. (defun org-time-today ()
  9808. "Time in seconds today at 0:00.
  9809. Returns the float number of seconds since the beginning of the
  9810. epoch to the beginning of today (00:00)."
  9811. (float-time (apply 'encode-time
  9812. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9813. (defun org-matcher-time (s)
  9814. "Interpret a time comparison value."
  9815. (save-match-data
  9816. (cond
  9817. ((string= s "<now>") (float-time))
  9818. ((string= s "<today>") (org-time-today))
  9819. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9820. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9821. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9822. (+ (org-time-today)
  9823. (* (string-to-number (match-string 1 s))
  9824. (cdr (assoc (match-string 2 s)
  9825. '(("d" . 86400.0) ("w" . 604800.0)
  9826. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9827. (t (org-2ft s)))))
  9828. (defun org-match-any-p (re list)
  9829. "Does re match any element of list?"
  9830. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9831. (delq nil list))
  9832. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9833. (defvar org-tags-overlay (org-make-overlay 1 1))
  9834. (org-detach-overlay org-tags-overlay)
  9835. (defun org-get-local-tags-at (&optional pos)
  9836. "Get a list of tags defined in the current headline."
  9837. (org-get-tags-at pos 'local))
  9838. (defun org-get-local-tags ()
  9839. "Get a list of tags defined in the current headline."
  9840. (org-get-tags-at nil 'local))
  9841. (defun org-get-tags-at (&optional pos local)
  9842. "Get a list of all headline tags applicable at POS.
  9843. POS defaults to point. If tags are inherited, the list contains
  9844. the targets in the same sequence as the headlines appear, i.e.
  9845. the tags of the current headline come last.
  9846. When LOCAL is non-nil, only return tags from the current headline,
  9847. ignore inherited ones."
  9848. (interactive)
  9849. (if (and org-trust-scanner-tags
  9850. (or (not pos) (equal pos (point)))
  9851. (not local))
  9852. org-scanner-tags
  9853. (let (tags ltags lastpos parent)
  9854. (save-excursion
  9855. (save-restriction
  9856. (widen)
  9857. (goto-char (or pos (point)))
  9858. (save-match-data
  9859. (catch 'done
  9860. (condition-case nil
  9861. (progn
  9862. (org-back-to-heading t)
  9863. (while (not (equal lastpos (point)))
  9864. (setq lastpos (point))
  9865. (when (looking-at
  9866. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9867. (setq ltags (org-split-string
  9868. (org-match-string-no-properties 1) ":"))
  9869. (when parent
  9870. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9871. (setq tags (append
  9872. (if parent
  9873. (org-remove-uniherited-tags ltags)
  9874. ltags)
  9875. tags)))
  9876. (or org-use-tag-inheritance (throw 'done t))
  9877. (if local (throw 'done t))
  9878. (or (org-up-heading-safe) (error nil))
  9879. (setq parent t)))
  9880. (error nil)))))
  9881. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9882. (defun org-add-prop-inherited (s)
  9883. (add-text-properties 0 (length s) '(inherited t) s)
  9884. s)
  9885. (defun org-toggle-tag (tag &optional onoff)
  9886. "Toggle the tag TAG for the current line.
  9887. If ONOFF is `on' or `off', don't toggle but set to this state."
  9888. (let (res current)
  9889. (save-excursion
  9890. (org-back-to-heading t)
  9891. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9892. (point-at-eol) t)
  9893. (progn
  9894. (setq current (match-string 1))
  9895. (replace-match ""))
  9896. (setq current ""))
  9897. (setq current (nreverse (org-split-string current ":")))
  9898. (cond
  9899. ((eq onoff 'on)
  9900. (setq res t)
  9901. (or (member tag current) (push tag current)))
  9902. ((eq onoff 'off)
  9903. (or (not (member tag current)) (setq current (delete tag current))))
  9904. (t (if (member tag current)
  9905. (setq current (delete tag current))
  9906. (setq res t)
  9907. (push tag current))))
  9908. (end-of-line 1)
  9909. (if current
  9910. (progn
  9911. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9912. (org-set-tags nil t))
  9913. (delete-horizontal-space))
  9914. (run-hooks 'org-after-tags-change-hook))
  9915. res))
  9916. (defun org-align-tags-here (to-col)
  9917. ;; Assumes that this is a headline
  9918. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9919. (beginning-of-line 1)
  9920. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9921. (< pos (match-beginning 2)))
  9922. (progn
  9923. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9924. (goto-char (match-beginning 1))
  9925. (insert " ")
  9926. (delete-region (point) (1+ (match-beginning 2)))
  9927. (setq ncol (max (1+ (current-column))
  9928. (1+ col)
  9929. (if (> to-col 0)
  9930. to-col
  9931. (- (abs to-col) tags-l))))
  9932. (setq p (point))
  9933. (insert (make-string (- ncol (current-column)) ?\ ))
  9934. (setq ncol (current-column))
  9935. (when indent-tabs-mode (tabify p (point-at-eol)))
  9936. (org-move-to-column (min ncol col) t))
  9937. (goto-char pos))))
  9938. (defun org-set-tags-command (&optional arg just-align)
  9939. "Call the set-tags command for the current entry."
  9940. (interactive "P")
  9941. (if (org-on-heading-p)
  9942. (org-set-tags arg just-align)
  9943. (save-excursion
  9944. (org-back-to-heading t)
  9945. (org-set-tags arg just-align))))
  9946. (defun org-set-tags (&optional arg just-align)
  9947. "Set the tags for the current headline.
  9948. With prefix ARG, realign all tags in headings in the current buffer."
  9949. (interactive "P")
  9950. (let* ((re (concat "^" outline-regexp))
  9951. (current (org-get-tags-string))
  9952. (col (current-column))
  9953. (org-setting-tags t)
  9954. table current-tags inherited-tags ; computed below when needed
  9955. tags p0 c0 c1 rpl)
  9956. (if arg
  9957. (save-excursion
  9958. (goto-char (point-min))
  9959. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9960. (while (re-search-forward re nil t)
  9961. (org-set-tags nil t)
  9962. (end-of-line 1)))
  9963. (message "All tags realigned to column %d" org-tags-column))
  9964. (if just-align
  9965. (setq tags current)
  9966. ;; Get a new set of tags from the user
  9967. (save-excursion
  9968. (setq table (append org-tag-persistent-alist
  9969. (or org-tag-alist (org-get-buffer-tags)))
  9970. org-last-tags-completion-table table
  9971. current-tags (org-split-string current ":")
  9972. inherited-tags (nreverse
  9973. (nthcdr (length current-tags)
  9974. (nreverse (org-get-tags-at))))
  9975. tags
  9976. (if (or (eq t org-use-fast-tag-selection)
  9977. (and org-use-fast-tag-selection
  9978. (delq nil (mapcar 'cdr table))))
  9979. (org-fast-tag-selection
  9980. current-tags inherited-tags table
  9981. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9982. (let ((org-add-colon-after-tag-completion t))
  9983. (org-trim
  9984. (org-without-partial-completion
  9985. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9986. nil nil current 'org-tags-history)))))))
  9987. (while (string-match "[-+&]+" tags)
  9988. ;; No boolean logic, just a list
  9989. (setq tags (replace-match ":" t t tags))))
  9990. (if (string-match "\\`[\t ]*\\'" tags)
  9991. (setq tags "")
  9992. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9993. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9994. ;; Insert new tags at the correct column
  9995. (beginning-of-line 1)
  9996. (cond
  9997. ((and (equal current "") (equal tags "")))
  9998. ((re-search-forward
  9999. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10000. (point-at-eol) t)
  10001. (if (equal tags "")
  10002. (setq rpl "")
  10003. (goto-char (match-beginning 0))
  10004. (setq c0 (current-column) p0 (point)
  10005. c1 (max (1+ c0) (if (> org-tags-column 0)
  10006. org-tags-column
  10007. (- (- org-tags-column) (length tags))))
  10008. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10009. (replace-match rpl t t)
  10010. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10011. tags)
  10012. (t (error "Tags alignment failed")))
  10013. (org-move-to-column col)
  10014. (unless just-align
  10015. (run-hooks 'org-after-tags-change-hook)))))
  10016. (defun org-change-tag-in-region (beg end tag off)
  10017. "Add or remove TAG for each entry in the region.
  10018. This works in the agenda, and also in an org-mode buffer."
  10019. (interactive
  10020. (list (region-beginning) (region-end)
  10021. (let ((org-last-tags-completion-table
  10022. (if (org-mode-p)
  10023. (org-get-buffer-tags)
  10024. (org-global-tags-completion-table))))
  10025. (org-ido-completing-read
  10026. "Tag: " 'org-tags-completion-function nil nil nil
  10027. 'org-tags-history))
  10028. (progn
  10029. (message "[s]et or [r]emove? ")
  10030. (equal (read-char-exclusive) ?r))))
  10031. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10032. (let ((agendap (equal major-mode 'org-agenda-mode))
  10033. l1 l2 m buf pos newhead (cnt 0))
  10034. (goto-char end)
  10035. (setq l2 (1- (org-current-line)))
  10036. (goto-char beg)
  10037. (setq l1 (org-current-line))
  10038. (loop for l from l1 to l2 do
  10039. (goto-line l)
  10040. (setq m (get-text-property (point) 'org-hd-marker))
  10041. (when (or (and (org-mode-p) (org-on-heading-p))
  10042. (and agendap m))
  10043. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10044. pos (if agendap m (point)))
  10045. (with-current-buffer buf
  10046. (save-excursion
  10047. (save-restriction
  10048. (goto-char pos)
  10049. (setq cnt (1+ cnt))
  10050. (org-toggle-tag tag (if off 'off 'on))
  10051. (setq newhead (org-get-heading)))))
  10052. (and agendap (org-agenda-change-all-lines newhead m))))
  10053. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10054. (defun org-tags-completion-function (string predicate &optional flag)
  10055. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10056. (confirm (lambda (x) (stringp (car x)))))
  10057. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10058. (setq s1 (match-string 1 string)
  10059. s2 (match-string 2 string))
  10060. (setq s1 "" s2 string))
  10061. (cond
  10062. ((eq flag nil)
  10063. ;; try completion
  10064. (setq rtn (try-completion s2 ctable confirm))
  10065. (if (stringp rtn)
  10066. (setq rtn
  10067. (concat s1 s2 (substring rtn (length s2))
  10068. (if (and org-add-colon-after-tag-completion
  10069. (assoc rtn ctable))
  10070. ":" ""))))
  10071. rtn)
  10072. ((eq flag t)
  10073. ;; all-completions
  10074. (all-completions s2 ctable confirm)
  10075. )
  10076. ((eq flag 'lambda)
  10077. ;; exact match?
  10078. (assoc s2 ctable)))
  10079. ))
  10080. (defun org-fast-tag-insert (kwd tags face &optional end)
  10081. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10082. (insert (format "%-12s" (concat kwd ":"))
  10083. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10084. (or end "")))
  10085. (defun org-fast-tag-show-exit (flag)
  10086. (save-excursion
  10087. (goto-line 3)
  10088. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10089. (replace-match ""))
  10090. (when flag
  10091. (end-of-line 1)
  10092. (org-move-to-column (- (window-width) 19) t)
  10093. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10094. (defun org-set-current-tags-overlay (current prefix)
  10095. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10096. (if (featurep 'xemacs)
  10097. (org-overlay-display org-tags-overlay (concat prefix s)
  10098. 'secondary-selection)
  10099. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10100. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10101. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10102. "Fast tag selection with single keys.
  10103. CURRENT is the current list of tags in the headline, INHERITED is the
  10104. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10105. possibly with grouping information. TODO-TABLE is a similar table with
  10106. TODO keywords, should these have keys assigned to them.
  10107. If the keys are nil, a-z are automatically assigned.
  10108. Returns the new tags string, or nil to not change the current settings."
  10109. (let* ((fulltable (append table todo-table))
  10110. (maxlen (apply 'max (mapcar
  10111. (lambda (x)
  10112. (if (stringp (car x)) (string-width (car x)) 0))
  10113. fulltable)))
  10114. (buf (current-buffer))
  10115. (expert (eq org-fast-tag-selection-single-key 'expert))
  10116. (buffer-tags nil)
  10117. (fwidth (+ maxlen 3 1 3))
  10118. (ncol (/ (- (window-width) 4) fwidth))
  10119. (i-face 'org-done)
  10120. (c-face 'org-todo)
  10121. tg cnt e c char c1 c2 ntable tbl rtn
  10122. ov-start ov-end ov-prefix
  10123. (exit-after-next org-fast-tag-selection-single-key)
  10124. (done-keywords org-done-keywords)
  10125. groups ingroup)
  10126. (save-excursion
  10127. (beginning-of-line 1)
  10128. (if (looking-at
  10129. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10130. (setq ov-start (match-beginning 1)
  10131. ov-end (match-end 1)
  10132. ov-prefix "")
  10133. (setq ov-start (1- (point-at-eol))
  10134. ov-end (1+ ov-start))
  10135. (skip-chars-forward "^\n\r")
  10136. (setq ov-prefix
  10137. (concat
  10138. (buffer-substring (1- (point)) (point))
  10139. (if (> (current-column) org-tags-column)
  10140. " "
  10141. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10142. (org-move-overlay org-tags-overlay ov-start ov-end)
  10143. (save-window-excursion
  10144. (if expert
  10145. (set-buffer (get-buffer-create " *Org tags*"))
  10146. (delete-other-windows)
  10147. (split-window-vertically)
  10148. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10149. (erase-buffer)
  10150. (org-set-local 'org-done-keywords done-keywords)
  10151. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10152. (org-fast-tag-insert "Current" current c-face "\n\n")
  10153. (org-fast-tag-show-exit exit-after-next)
  10154. (org-set-current-tags-overlay current ov-prefix)
  10155. (setq tbl fulltable char ?a cnt 0)
  10156. (while (setq e (pop tbl))
  10157. (cond
  10158. ((equal e '(:startgroup))
  10159. (push '() groups) (setq ingroup t)
  10160. (when (not (= cnt 0))
  10161. (setq cnt 0)
  10162. (insert "\n"))
  10163. (insert "{ "))
  10164. ((equal e '(:endgroup))
  10165. (setq ingroup nil cnt 0)
  10166. (insert "}\n"))
  10167. ((equal e '(:newline))
  10168. (when (not (= cnt 0))
  10169. (setq cnt 0)
  10170. (insert "\n")
  10171. (setq e (car tbl))
  10172. (while (equal (car tbl) '(:newline))
  10173. (insert "\n")
  10174. (setq tbl (cdr tbl)))))
  10175. (t
  10176. (setq tg (car e) c2 nil)
  10177. (if (cdr e)
  10178. (setq c (cdr e))
  10179. ;; automatically assign a character.
  10180. (setq c1 (string-to-char
  10181. (downcase (substring
  10182. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10183. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10184. (while (or (rassoc char ntable) (rassoc char table))
  10185. (setq char (1+ char)))
  10186. (setq c2 c1))
  10187. (setq c (or c2 char)))
  10188. (if ingroup (push tg (car groups)))
  10189. (setq tg (org-add-props tg nil 'face
  10190. (cond
  10191. ((not (assoc tg table))
  10192. (org-get-todo-face tg))
  10193. ((member tg current) c-face)
  10194. ((member tg inherited) i-face)
  10195. (t nil))))
  10196. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10197. (insert "[" c "] " tg (make-string
  10198. (- fwidth 4 (length tg)) ?\ ))
  10199. (push (cons tg c) ntable)
  10200. (when (= (setq cnt (1+ cnt)) ncol)
  10201. (insert "\n")
  10202. (if ingroup (insert " "))
  10203. (setq cnt 0)))))
  10204. (setq ntable (nreverse ntable))
  10205. (insert "\n")
  10206. (goto-char (point-min))
  10207. (if (not expert) (org-fit-window-to-buffer))
  10208. (setq rtn
  10209. (catch 'exit
  10210. (while t
  10211. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10212. (if groups " [!] no groups" " [!]groups")
  10213. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10214. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10215. (cond
  10216. ((= c ?\r) (throw 'exit t))
  10217. ((= c ?!)
  10218. (setq groups (not groups))
  10219. (goto-char (point-min))
  10220. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10221. ((= c ?\C-c)
  10222. (if (not expert)
  10223. (org-fast-tag-show-exit
  10224. (setq exit-after-next (not exit-after-next)))
  10225. (setq expert nil)
  10226. (delete-other-windows)
  10227. (split-window-vertically)
  10228. (org-switch-to-buffer-other-window " *Org tags*")
  10229. (org-fit-window-to-buffer)))
  10230. ((or (= c ?\C-g)
  10231. (and (= c ?q) (not (rassoc c ntable))))
  10232. (org-detach-overlay org-tags-overlay)
  10233. (setq quit-flag t))
  10234. ((= c ?\ )
  10235. (setq current nil)
  10236. (if exit-after-next (setq exit-after-next 'now)))
  10237. ((= c ?\t)
  10238. (condition-case nil
  10239. (setq tg (org-ido-completing-read
  10240. "Tag: "
  10241. (or buffer-tags
  10242. (with-current-buffer buf
  10243. (org-get-buffer-tags)))))
  10244. (quit (setq tg "")))
  10245. (when (string-match "\\S-" tg)
  10246. (add-to-list 'buffer-tags (list tg))
  10247. (if (member tg current)
  10248. (setq current (delete tg current))
  10249. (push tg current)))
  10250. (if exit-after-next (setq exit-after-next 'now)))
  10251. ((setq e (rassoc c todo-table) tg (car e))
  10252. (with-current-buffer buf
  10253. (save-excursion (org-todo tg)))
  10254. (if exit-after-next (setq exit-after-next 'now)))
  10255. ((setq e (rassoc c ntable) tg (car e))
  10256. (if (member tg current)
  10257. (setq current (delete tg current))
  10258. (loop for g in groups do
  10259. (if (member tg g)
  10260. (mapc (lambda (x)
  10261. (setq current (delete x current)))
  10262. g)))
  10263. (push tg current))
  10264. (if exit-after-next (setq exit-after-next 'now))))
  10265. ;; Create a sorted list
  10266. (setq current
  10267. (sort current
  10268. (lambda (a b)
  10269. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10270. (if (eq exit-after-next 'now) (throw 'exit t))
  10271. (goto-char (point-min))
  10272. (beginning-of-line 2)
  10273. (delete-region (point) (point-at-eol))
  10274. (org-fast-tag-insert "Current" current c-face)
  10275. (org-set-current-tags-overlay current ov-prefix)
  10276. (while (re-search-forward
  10277. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10278. (setq tg (match-string 1))
  10279. (add-text-properties
  10280. (match-beginning 1) (match-end 1)
  10281. (list 'face
  10282. (cond
  10283. ((member tg current) c-face)
  10284. ((member tg inherited) i-face)
  10285. (t (get-text-property (match-beginning 1) 'face))))))
  10286. (goto-char (point-min)))))
  10287. (org-detach-overlay org-tags-overlay)
  10288. (if rtn
  10289. (mapconcat 'identity current ":")
  10290. nil))))
  10291. (defun org-get-tags-string ()
  10292. "Get the TAGS string in the current headline."
  10293. (unless (org-on-heading-p t)
  10294. (error "Not on a heading"))
  10295. (save-excursion
  10296. (beginning-of-line 1)
  10297. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10298. (org-match-string-no-properties 1)
  10299. "")))
  10300. (defun org-get-tags ()
  10301. "Get the list of tags specified in the current headline."
  10302. (org-split-string (org-get-tags-string) ":"))
  10303. (defun org-get-buffer-tags ()
  10304. "Get a table of all tags used in the buffer, for completion."
  10305. (let (tags)
  10306. (save-excursion
  10307. (goto-char (point-min))
  10308. (while (re-search-forward
  10309. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10310. (when (equal (char-after (point-at-bol 0)) ?*)
  10311. (mapc (lambda (x) (add-to-list 'tags x))
  10312. (org-split-string (org-match-string-no-properties 1) ":")))))
  10313. (mapcar 'list tags)))
  10314. ;;;; The mapping API
  10315. ;;;###autoload
  10316. (defun org-map-entries (func &optional match scope &rest skip)
  10317. "Call FUNC at each headline selected by MATCH in SCOPE.
  10318. FUNC is a function or a lisp form. The function will be called without
  10319. arguments, with the cursor positioned at the beginning of the headline.
  10320. The return values of all calls to the function will be collected and
  10321. returned as a list.
  10322. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10323. does not need to preserve point. After evaluaton, the cursor will be
  10324. moved to the end of the line (presumably of the headline of the
  10325. processed entry) and search continues from there. Under some
  10326. circumstances, this may not produce the wanted results. For example,
  10327. if you have removed (e.g. archived) the current (sub)tree it could
  10328. mean that the next entry will be skipped entirely. In such cases, you
  10329. can specify the position from where search should continue by making
  10330. FUNC set the variable `org-map-continue-from' to the desired buffer
  10331. position.
  10332. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10333. Only headlines that are matched by this query will be considered during
  10334. the iteration. When MATCH is nil or t, all headlines will be
  10335. visited by the iteration.
  10336. SCOPE determines the scope of this command. It can be any of:
  10337. nil The current buffer, respecting the restriction if any
  10338. tree The subtree started with the entry at point
  10339. file The current buffer, without restriction
  10340. file-with-archives
  10341. The current buffer, and any archives associated with it
  10342. agenda All agenda files
  10343. agenda-with-archives
  10344. All agenda files with any archive files associated with them
  10345. \(file1 file2 ...)
  10346. If this is a list, all files in the list will be scanned
  10347. The remaining args are treated as settings for the skipping facilities of
  10348. the scanner. The following items can be given here:
  10349. archive skip trees with the archive tag.
  10350. comment skip trees with the COMMENT keyword
  10351. function or Emacs Lisp form:
  10352. will be used as value for `org-agenda-skip-function', so whenever
  10353. the the function returns t, FUNC will not be called for that
  10354. entry and search will continue from the point where the
  10355. function leaves it.
  10356. If your function needs to retrieve the tags including inherited tags
  10357. at the *current* entry, you can use the value of the variable
  10358. `org-scanner-tags' which will be much faster than getting the value
  10359. with `org-get-tags-at'. If your function gets properties with
  10360. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10361. to t around the call to `org-entry-properties' to get the same speedup.
  10362. Note that if your function moves around to retrieve tags and properties at
  10363. a *different* entry, you cannot use these techniques."
  10364. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10365. (org-agenda-skip-archived-trees (memq 'archive skip))
  10366. (org-agenda-skip-comment-trees (memq 'comment skip))
  10367. (org-agenda-skip-function
  10368. (car (org-delete-all '(comment archive) skip)))
  10369. (org-tags-match-list-sublevels t)
  10370. matcher file res
  10371. org-todo-keywords-for-agenda
  10372. org-done-keywords-for-agenda
  10373. org-todo-keyword-alist-for-agenda
  10374. org-tag-alist-for-agenda)
  10375. (cond
  10376. ((eq match t) (setq matcher t))
  10377. ((eq match nil) (setq matcher t))
  10378. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10379. (save-excursion
  10380. (save-restriction
  10381. (when (eq scope 'tree)
  10382. (org-back-to-heading t)
  10383. (org-narrow-to-subtree)
  10384. (setq scope nil))
  10385. (if (not scope)
  10386. (progn
  10387. (org-prepare-agenda-buffers
  10388. (list (buffer-file-name (current-buffer))))
  10389. (setq res (org-scan-tags func matcher)))
  10390. ;; Get the right scope
  10391. (cond
  10392. ((and scope (listp scope) (symbolp (car scope)))
  10393. (setq scope (eval scope)))
  10394. ((eq scope 'agenda)
  10395. (setq scope (org-agenda-files t)))
  10396. ((eq scope 'agenda-with-archives)
  10397. (setq scope (org-agenda-files t))
  10398. (setq scope (org-add-archive-files scope)))
  10399. ((eq scope 'file)
  10400. (setq scope (list (buffer-file-name))))
  10401. ((eq scope 'file-with-archives)
  10402. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10403. (org-prepare-agenda-buffers scope)
  10404. (while (setq file (pop scope))
  10405. (with-current-buffer (org-find-base-buffer-visiting file)
  10406. (save-excursion
  10407. (save-restriction
  10408. (widen)
  10409. (goto-char (point-min))
  10410. (setq res (append res (org-scan-tags func matcher))))))))))
  10411. res))
  10412. ;;;; Properties
  10413. ;;; Setting and retrieving properties
  10414. (defconst org-special-properties
  10415. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10416. "TIMESTAMP" "TIMESTAMP_IA")
  10417. "The special properties valid in Org-mode.
  10418. These are properties that are not defined in the property drawer,
  10419. but in some other way.")
  10420. (defconst org-default-properties
  10421. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10422. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10423. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10424. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10425. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER")
  10426. "Some properties that are used by Org-mode for various purposes.
  10427. Being in this list makes sure that they are offered for completion.")
  10428. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10429. "Regular expression matching the first line of a property drawer.")
  10430. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10431. "Regular expression matching the first line of a property drawer.")
  10432. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10433. "Regular expression matching the first line of a property drawer.")
  10434. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10435. "Regular expression matching the first line of a property drawer.")
  10436. (defconst org-property-drawer-re
  10437. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10438. org-property-end-re "\\)\n?")
  10439. "Matches an entire property drawer.")
  10440. (defconst org-clock-drawer-re
  10441. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10442. org-property-end-re "\\)\n?")
  10443. "Matches an entire clock drawer.")
  10444. (defun org-property-action ()
  10445. "Do an action on properties."
  10446. (interactive)
  10447. (let (c)
  10448. (org-at-property-p)
  10449. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10450. (setq c (read-char-exclusive))
  10451. (cond
  10452. ((equal c ?s)
  10453. (call-interactively 'org-set-property))
  10454. ((equal c ?d)
  10455. (call-interactively 'org-delete-property))
  10456. ((equal c ?D)
  10457. (call-interactively 'org-delete-property-globally))
  10458. ((equal c ?c)
  10459. (call-interactively 'org-compute-property-at-point))
  10460. (t (error "No such property action %c" c)))))
  10461. (defun org-at-property-p ()
  10462. "Is the cursor in a property line?"
  10463. ;; FIXME: Does not check if we are actually in the drawer.
  10464. ;; FIXME: also returns true on any drawers.....
  10465. ;; This is used by C-c C-c for property action.
  10466. (save-excursion
  10467. (beginning-of-line 1)
  10468. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10469. (defun org-get-property-block (&optional beg end force)
  10470. "Return the (beg . end) range of the body of the property drawer.
  10471. BEG and END can be beginning and end of subtree, if not given
  10472. they will be found.
  10473. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10474. (catch 'exit
  10475. (save-excursion
  10476. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10477. (end (or end (progn (outline-next-heading) (point)))))
  10478. (goto-char beg)
  10479. (if (re-search-forward org-property-start-re end t)
  10480. (setq beg (1+ (match-end 0)))
  10481. (if force
  10482. (save-excursion
  10483. (org-insert-property-drawer)
  10484. (setq end (progn (outline-next-heading) (point))))
  10485. (throw 'exit nil))
  10486. (goto-char beg)
  10487. (if (re-search-forward org-property-start-re end t)
  10488. (setq beg (1+ (match-end 0)))))
  10489. (if (re-search-forward org-property-end-re end t)
  10490. (setq end (match-beginning 0))
  10491. (or force (throw 'exit nil))
  10492. (goto-char beg)
  10493. (setq end beg)
  10494. (org-indent-line-function)
  10495. (insert ":END:\n"))
  10496. (cons beg end)))))
  10497. (defun org-entry-properties (&optional pom which)
  10498. "Get all properties of the entry at point-or-marker POM.
  10499. This includes the TODO keyword, the tags, time strings for deadline,
  10500. scheduled, and clocking, and any additional properties defined in the
  10501. entry. The return value is an alist, keys may occur multiple times
  10502. if the property key was used several times.
  10503. POM may also be nil, in which case the current entry is used.
  10504. If WHICH is nil or `all', get all properties. If WHICH is
  10505. `special' or `standard', only get that subclass."
  10506. (setq which (or which 'all))
  10507. (org-with-point-at pom
  10508. (let ((clockstr (substring org-clock-string 0 -1))
  10509. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10510. beg end range props sum-props key value string clocksum)
  10511. (save-excursion
  10512. (when (condition-case nil
  10513. (and (org-mode-p) (org-back-to-heading t))
  10514. (error nil))
  10515. (setq beg (point))
  10516. (setq sum-props (get-text-property (point) 'org-summaries))
  10517. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10518. (outline-next-heading)
  10519. (setq end (point))
  10520. (when (memq which '(all special))
  10521. ;; Get the special properties, like TODO and tags
  10522. (goto-char beg)
  10523. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10524. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10525. (when (looking-at org-priority-regexp)
  10526. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10527. (when (and (setq value (org-get-tags-string))
  10528. (string-match "\\S-" value))
  10529. (push (cons "TAGS" value) props))
  10530. (when (setq value (org-get-tags-at))
  10531. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10532. props))
  10533. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10534. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10535. string (if (equal key clockstr)
  10536. (org-no-properties
  10537. (org-trim
  10538. (buffer-substring
  10539. (match-beginning 3) (goto-char (point-at-eol)))))
  10540. (substring (org-match-string-no-properties 3) 1 -1)))
  10541. (unless key
  10542. (if (= (char-after (match-beginning 3)) ?\[)
  10543. (setq key "TIMESTAMP_IA")
  10544. (setq key "TIMESTAMP")))
  10545. (when (or (equal key clockstr) (not (assoc key props)))
  10546. (push (cons key string) props)))
  10547. )
  10548. (when (memq which '(all standard))
  10549. ;; Get the standard properties, like :PROP: ...
  10550. (setq range (org-get-property-block beg end))
  10551. (when range
  10552. (goto-char (car range))
  10553. (while (re-search-forward
  10554. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10555. (cdr range) t)
  10556. (setq key (org-match-string-no-properties 1)
  10557. value (org-trim (or (org-match-string-no-properties 2) "")))
  10558. (unless (member key excluded)
  10559. (push (cons key (or value "")) props)))))
  10560. (if clocksum
  10561. (push (cons "CLOCKSUM"
  10562. (org-columns-number-to-string (/ (float clocksum) 60.)
  10563. 'add_times))
  10564. props))
  10565. (unless (assoc "CATEGORY" props)
  10566. (setq value (or (org-get-category)
  10567. (progn (org-refresh-category-properties)
  10568. (org-get-category))))
  10569. (push (cons "CATEGORY" value) props))
  10570. (append sum-props (nreverse props)))))))
  10571. (defun org-entry-get (pom property &optional inherit)
  10572. "Get value of PROPERTY for entry at point-or-marker POM.
  10573. If INHERIT is non-nil and the entry does not have the property,
  10574. then also check higher levels of the hierarchy.
  10575. If INHERIT is the symbol `selective', use inheritance only if the setting
  10576. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10577. If the property is present but empty, the return value is the empty string.
  10578. If the property is not present at all, nil is returned."
  10579. (org-with-point-at pom
  10580. (if (and inherit (if (eq inherit 'selective)
  10581. (org-property-inherit-p property)
  10582. t))
  10583. (org-entry-get-with-inheritance property)
  10584. (if (member property org-special-properties)
  10585. ;; We need a special property. Use brute force, get all properties.
  10586. (cdr (assoc property (org-entry-properties nil 'special)))
  10587. (let ((range (org-get-property-block)))
  10588. (if (and range
  10589. (goto-char (car range))
  10590. (re-search-forward
  10591. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10592. (cdr range) t))
  10593. ;; Found the property, return it.
  10594. (if (match-end 1)
  10595. (org-match-string-no-properties 1)
  10596. "")))))))
  10597. (defun org-property-or-variable-value (var &optional inherit)
  10598. "Check if there is a property fixing the value of VAR.
  10599. If yes, return this value. If not, return the current value of the variable."
  10600. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10601. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10602. (read prop)
  10603. (symbol-value var))))
  10604. (defun org-entry-delete (pom property)
  10605. "Delete the property PROPERTY from entry at point-or-marker POM."
  10606. (org-with-point-at pom
  10607. (if (member property org-special-properties)
  10608. nil ; cannot delete these properties.
  10609. (let ((range (org-get-property-block)))
  10610. (if (and range
  10611. (goto-char (car range))
  10612. (re-search-forward
  10613. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10614. (cdr range) t))
  10615. (progn
  10616. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10617. t)
  10618. nil)))))
  10619. ;; Multi-values properties are properties that contain multiple values
  10620. ;; These values are assumed to be single words, separated by whitespace.
  10621. (defun org-entry-add-to-multivalued-property (pom property value)
  10622. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10623. (let* ((old (org-entry-get pom property))
  10624. (values (and old (org-split-string old "[ \t]"))))
  10625. (setq value (org-entry-protect-space value))
  10626. (unless (member value values)
  10627. (setq values (cons value values))
  10628. (org-entry-put pom property
  10629. (mapconcat 'identity values " ")))))
  10630. (defun org-entry-remove-from-multivalued-property (pom property value)
  10631. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10632. (let* ((old (org-entry-get pom property))
  10633. (values (and old (org-split-string old "[ \t]"))))
  10634. (setq value (org-entry-protect-space value))
  10635. (when (member value values)
  10636. (setq values (delete value values))
  10637. (org-entry-put pom property
  10638. (mapconcat 'identity values " ")))))
  10639. (defun org-entry-member-in-multivalued-property (pom property value)
  10640. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10641. (let* ((old (org-entry-get pom property))
  10642. (values (and old (org-split-string old "[ \t]"))))
  10643. (setq value (org-entry-protect-space value))
  10644. (member value values)))
  10645. (defun org-entry-get-multivalued-property (pom property)
  10646. "Return a list of values in a multivalued property."
  10647. (let* ((value (org-entry-get pom property))
  10648. (values (and value (org-split-string value "[ \t]"))))
  10649. (mapcar 'org-entry-restore-space values)))
  10650. (defun org-entry-put-multivalued-property (pom property &rest values)
  10651. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10652. VALUES should be a list of strings. Spaces will be protected."
  10653. (org-entry-put pom property
  10654. (mapconcat 'org-entry-protect-space values " "))
  10655. (let* ((value (org-entry-get pom property))
  10656. (values (and value (org-split-string value "[ \t]"))))
  10657. (mapcar 'org-entry-restore-space values)))
  10658. (defun org-entry-protect-space (s)
  10659. "Protect spaces and newline in string S."
  10660. (while (string-match " " s)
  10661. (setq s (replace-match "%20" t t s)))
  10662. (while (string-match "\n" s)
  10663. (setq s (replace-match "%0A" t t s)))
  10664. s)
  10665. (defun org-entry-restore-space (s)
  10666. "Restore spaces and newline in string S."
  10667. (while (string-match "%20" s)
  10668. (setq s (replace-match " " t t s)))
  10669. (while (string-match "%0A" s)
  10670. (setq s (replace-match "\n" t t s)))
  10671. s)
  10672. (defvar org-entry-property-inherited-from (make-marker)
  10673. "Marker pointing to the entry from where a property was inherited.
  10674. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10675. location of the entry where the inheritance search matched. If there was
  10676. no match, the marker will point nowhere.
  10677. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10678. is set.")
  10679. (defun org-entry-get-with-inheritance (property)
  10680. "Get entry property, and search higher levels if not present."
  10681. (move-marker org-entry-property-inherited-from nil)
  10682. (let (tmp)
  10683. (save-excursion
  10684. (save-restriction
  10685. (widen)
  10686. (catch 'ex
  10687. (while t
  10688. (when (setq tmp (org-entry-get nil property))
  10689. (org-back-to-heading t)
  10690. (move-marker org-entry-property-inherited-from (point))
  10691. (throw 'ex tmp))
  10692. (or (org-up-heading-safe) (throw 'ex nil)))))
  10693. (or tmp
  10694. (cdr (assoc property org-file-properties))
  10695. (cdr (assoc property org-global-properties))
  10696. (cdr (assoc property org-global-properties-fixed))))))
  10697. (defun org-entry-put (pom property value)
  10698. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10699. (org-with-point-at pom
  10700. (org-back-to-heading t)
  10701. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10702. range)
  10703. (cond
  10704. ((equal property "TODO")
  10705. (when (and (stringp value) (string-match "\\S-" value)
  10706. (not (member value org-todo-keywords-1)))
  10707. (error "\"%s\" is not a valid TODO state" value))
  10708. (if (or (not value)
  10709. (not (string-match "\\S-" value)))
  10710. (setq value 'none))
  10711. (org-todo value)
  10712. (org-set-tags nil 'align))
  10713. ((equal property "PRIORITY")
  10714. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10715. (string-to-char value) ?\ ))
  10716. (org-set-tags nil 'align))
  10717. ((equal property "SCHEDULED")
  10718. (if (re-search-forward org-scheduled-time-regexp end t)
  10719. (cond
  10720. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10721. ((eq value 'later) (org-timestamp-change 1 'day))
  10722. (t (call-interactively 'org-schedule)))
  10723. (call-interactively 'org-schedule)))
  10724. ((equal property "DEADLINE")
  10725. (if (re-search-forward org-deadline-time-regexp end t)
  10726. (cond
  10727. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10728. ((eq value 'later) (org-timestamp-change 1 'day))
  10729. (t (call-interactively 'org-deadline)))
  10730. (call-interactively 'org-deadline)))
  10731. ((member property org-special-properties)
  10732. (error "The %s property can not yet be set with `org-entry-put'"
  10733. property))
  10734. (t ; a non-special property
  10735. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10736. (setq range (org-get-property-block beg end 'force))
  10737. (goto-char (car range))
  10738. (if (re-search-forward
  10739. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10740. (progn
  10741. (delete-region (match-beginning 1) (match-end 1))
  10742. (goto-char (match-beginning 1)))
  10743. (goto-char (cdr range))
  10744. (insert "\n")
  10745. (backward-char 1)
  10746. (org-indent-line-function)
  10747. (insert ":" property ":"))
  10748. (and value (insert " " value))
  10749. (org-indent-line-function)))))))
  10750. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10751. "Get all property keys in the current buffer.
  10752. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10753. like tags and TODO state.
  10754. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10755. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10756. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10757. formats in the current buffer."
  10758. (let (rtn range cfmt s p)
  10759. (save-excursion
  10760. (save-restriction
  10761. (widen)
  10762. (goto-char (point-min))
  10763. (while (re-search-forward org-property-start-re nil t)
  10764. (setq range (org-get-property-block))
  10765. (goto-char (car range))
  10766. (while (re-search-forward
  10767. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10768. (cdr range) t)
  10769. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10770. (outline-next-heading))))
  10771. (when include-specials
  10772. (setq rtn (append org-special-properties rtn)))
  10773. (when include-defaults
  10774. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10775. (when include-columns
  10776. (save-excursion
  10777. (save-restriction
  10778. (widen)
  10779. (goto-char (point-min))
  10780. (while (re-search-forward
  10781. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10782. nil t)
  10783. (setq cfmt (match-string 2) s 0)
  10784. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10785. cfmt s)
  10786. (setq s (match-end 0)
  10787. p (match-string 1 cfmt))
  10788. (unless (or (equal p "ITEM")
  10789. (member p org-special-properties))
  10790. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10791. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10792. (defun org-property-values (key)
  10793. "Return a list of all values of property KEY."
  10794. (save-excursion
  10795. (save-restriction
  10796. (widen)
  10797. (goto-char (point-min))
  10798. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10799. values)
  10800. (while (re-search-forward re nil t)
  10801. (add-to-list 'values (org-trim (match-string 1))))
  10802. (delete "" values)))))
  10803. (defun org-insert-property-drawer ()
  10804. "Insert a property drawer into the current entry."
  10805. (interactive)
  10806. (org-back-to-heading t)
  10807. (looking-at outline-regexp)
  10808. (let ((indent (- (match-end 0)(match-beginning 0)))
  10809. (beg (point))
  10810. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10811. end hiddenp)
  10812. (outline-next-heading)
  10813. (setq end (point))
  10814. (goto-char beg)
  10815. (while (re-search-forward re end t))
  10816. (setq hiddenp (org-invisible-p))
  10817. (end-of-line 1)
  10818. (and (equal (char-after) ?\n) (forward-char 1))
  10819. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10820. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10821. ;; just skip this line
  10822. (beginning-of-line 2)
  10823. ;; Drawer start, find the end
  10824. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10825. (beginning-of-line 1)))
  10826. (org-skip-over-state-notes)
  10827. (skip-chars-backward " \t\n\r")
  10828. (if (eq (char-before) ?*) (forward-char 1))
  10829. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10830. (beginning-of-line 0)
  10831. (org-indent-to-column indent)
  10832. (beginning-of-line 2)
  10833. (org-indent-to-column indent)
  10834. (beginning-of-line 0)
  10835. (if hiddenp
  10836. (save-excursion
  10837. (org-back-to-heading t)
  10838. (hide-entry))
  10839. (org-flag-drawer t))))
  10840. (defun org-set-property (property value)
  10841. "In the current entry, set PROPERTY to VALUE.
  10842. When called interactively, this will prompt for a property name, offering
  10843. completion on existing and default properties. And then it will prompt
  10844. for a value, offering completion either on allowed values (via an inherited
  10845. xxx_ALL property) or on existing values in other instances of this property
  10846. in the current file."
  10847. (interactive
  10848. (let* ((completion-ignore-case t)
  10849. (keys (org-buffer-property-keys nil t t))
  10850. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10851. (prop (if (member prop0 keys)
  10852. prop0
  10853. (or (cdr (assoc (downcase prop0)
  10854. (mapcar (lambda (x) (cons (downcase x) x))
  10855. keys)))
  10856. prop0)))
  10857. (cur (org-entry-get nil prop))
  10858. (allowed (org-property-get-allowed-values nil prop 'table))
  10859. (existing (mapcar 'list (org-property-values prop)))
  10860. (val (if allowed
  10861. (org-completing-read "Value: " allowed nil 'req-match)
  10862. (let (org-completion-use-ido)
  10863. (org-completing-read
  10864. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10865. (concat "[" cur "]") "")
  10866. ": ")
  10867. existing nil nil "" nil cur)))))
  10868. (list prop (if (equal val "") cur val))))
  10869. (unless (equal (org-entry-get nil property) value)
  10870. (org-entry-put nil property value)))
  10871. (defun org-delete-property (property)
  10872. "In the current entry, delete PROPERTY."
  10873. (interactive
  10874. (let* ((completion-ignore-case t)
  10875. (prop (org-ido-completing-read
  10876. "Property: " (org-entry-properties nil 'standard))))
  10877. (list prop)))
  10878. (message "Property %s %s" property
  10879. (if (org-entry-delete nil property)
  10880. "deleted"
  10881. "was not present in the entry")))
  10882. (defun org-delete-property-globally (property)
  10883. "Remove PROPERTY globally, from all entries."
  10884. (interactive
  10885. (let* ((completion-ignore-case t)
  10886. (prop (org-ido-completing-read
  10887. "Globally remove property: "
  10888. (mapcar 'list (org-buffer-property-keys)))))
  10889. (list prop)))
  10890. (save-excursion
  10891. (save-restriction
  10892. (widen)
  10893. (goto-char (point-min))
  10894. (let ((cnt 0))
  10895. (while (re-search-forward
  10896. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10897. nil t)
  10898. (setq cnt (1+ cnt))
  10899. (replace-match ""))
  10900. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10901. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10902. (defun org-compute-property-at-point ()
  10903. "Compute the property at point.
  10904. This looks for an enclosing column format, extracts the operator and
  10905. then applies it to the property in the column format's scope."
  10906. (interactive)
  10907. (unless (org-at-property-p)
  10908. (error "Not at a property"))
  10909. (let ((prop (org-match-string-no-properties 2)))
  10910. (org-columns-get-format-and-top-level)
  10911. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10912. (error "No operator defined for property %s" prop))
  10913. (org-columns-compute prop)))
  10914. (defun org-property-get-allowed-values (pom property &optional table)
  10915. "Get allowed values for the property PROPERTY.
  10916. When TABLE is non-nil, return an alist that can directly be used for
  10917. completion."
  10918. (let (vals)
  10919. (cond
  10920. ((equal property "TODO")
  10921. (setq vals (org-with-point-at pom
  10922. (append org-todo-keywords-1 '("")))))
  10923. ((equal property "PRIORITY")
  10924. (let ((n org-lowest-priority))
  10925. (while (>= n org-highest-priority)
  10926. (push (char-to-string n) vals)
  10927. (setq n (1- n)))))
  10928. ((member property org-special-properties))
  10929. (t
  10930. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10931. (when (and vals (string-match "\\S-" vals))
  10932. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10933. (setq vals (mapcar (lambda (x)
  10934. (cond ((stringp x) x)
  10935. ((numberp x) (number-to-string x))
  10936. ((symbolp x) (symbol-name x))
  10937. (t "???")))
  10938. vals)))))
  10939. (if table (mapcar 'list vals) vals)))
  10940. (defun org-property-previous-allowed-value (&optional previous)
  10941. "Switch to the next allowed value for this property."
  10942. (interactive)
  10943. (org-property-next-allowed-value t))
  10944. (defun org-property-next-allowed-value (&optional previous)
  10945. "Switch to the next allowed value for this property."
  10946. (interactive)
  10947. (unless (org-at-property-p)
  10948. (error "Not at a property"))
  10949. (let* ((key (match-string 2))
  10950. (value (match-string 3))
  10951. (allowed (or (org-property-get-allowed-values (point) key)
  10952. (and (member value '("[ ]" "[-]" "[X]"))
  10953. '("[ ]" "[X]"))))
  10954. nval)
  10955. (unless allowed
  10956. (error "Allowed values for this property have not been defined"))
  10957. (if previous (setq allowed (reverse allowed)))
  10958. (if (member value allowed)
  10959. (setq nval (car (cdr (member value allowed)))))
  10960. (setq nval (or nval (car allowed)))
  10961. (if (equal nval value)
  10962. (error "Only one allowed value for this property"))
  10963. (org-at-property-p)
  10964. (replace-match (concat " :" key ": " nval) t t)
  10965. (org-indent-line-function)
  10966. (beginning-of-line 1)
  10967. (skip-chars-forward " \t")))
  10968. (defun org-find-entry-with-id (ident)
  10969. "Locate the entry that contains the ID property with exact value IDENT.
  10970. IDENT can be a string, a symbol or a number, this function will search for
  10971. the string representation of it.
  10972. Return the position where this entry starts, or nil if there is no such entry."
  10973. (interactive "sID: ")
  10974. (let ((id (cond
  10975. ((stringp ident) ident)
  10976. ((symbol-name ident) (symbol-name ident))
  10977. ((numberp ident) (number-to-string ident))
  10978. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10979. (case-fold-search nil))
  10980. (save-excursion
  10981. (save-restriction
  10982. (widen)
  10983. (goto-char (point-min))
  10984. (when (re-search-forward
  10985. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10986. nil t)
  10987. (org-back-to-heading)
  10988. (point))))))
  10989. ;;;; Timestamps
  10990. (defvar org-last-changed-timestamp nil)
  10991. (defvar org-last-inserted-timestamp nil
  10992. "The last time stamp inserted with `org-insert-time-stamp'.")
  10993. (defvar org-time-was-given) ; dynamically scoped parameter
  10994. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10995. (defvar org-ts-what) ; dynamically scoped parameter
  10996. (defun org-time-stamp (arg &optional inactive)
  10997. "Prompt for a date/time and insert a time stamp.
  10998. If the user specifies a time like HH:MM, or if this command is called
  10999. with a prefix argument, the time stamp will contain date and time.
  11000. Otherwise, only the date will be included. All parts of a date not
  11001. specified by the user will be filled in from the current date/time.
  11002. So if you press just return without typing anything, the time stamp
  11003. will represent the current date/time. If there is already a timestamp
  11004. at the cursor, it will be modified."
  11005. (interactive "P")
  11006. (let* ((ts nil)
  11007. (default-time
  11008. ;; Default time is either today, or, when entering a range,
  11009. ;; the range start.
  11010. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11011. (save-excursion
  11012. (re-search-backward
  11013. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11014. (- (point) 20) t)))
  11015. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11016. (current-time)))
  11017. (default-input (and ts (org-get-compact-tod ts)))
  11018. org-time-was-given org-end-time-was-given time)
  11019. (cond
  11020. ((and (org-at-timestamp-p t)
  11021. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11022. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11023. (insert "--")
  11024. (setq time (let ((this-command this-command))
  11025. (org-read-date arg 'totime nil nil
  11026. default-time default-input)))
  11027. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11028. ((org-at-timestamp-p t)
  11029. (setq time (let ((this-command this-command))
  11030. (org-read-date arg 'totime nil nil default-time default-input)))
  11031. (when (org-at-timestamp-p t) ; just to get the match data
  11032. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11033. (replace-match "")
  11034. (setq org-last-changed-timestamp
  11035. (org-insert-time-stamp
  11036. time (or org-time-was-given arg)
  11037. inactive nil nil (list org-end-time-was-given))))
  11038. (message "Timestamp updated"))
  11039. (t
  11040. (setq time (let ((this-command this-command))
  11041. (org-read-date arg 'totime nil nil default-time default-input)))
  11042. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11043. nil nil (list org-end-time-was-given))))))
  11044. ;; FIXME: can we use this for something else, like computing time differences?
  11045. (defun org-get-compact-tod (s)
  11046. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11047. (let* ((t1 (match-string 1 s))
  11048. (h1 (string-to-number (match-string 2 s)))
  11049. (m1 (string-to-number (match-string 3 s)))
  11050. (t2 (and (match-end 4) (match-string 5 s)))
  11051. (h2 (and t2 (string-to-number (match-string 6 s))))
  11052. (m2 (and t2 (string-to-number (match-string 7 s))))
  11053. dh dm)
  11054. (if (not t2)
  11055. t1
  11056. (setq dh (- h2 h1) dm (- m2 m1))
  11057. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11058. (concat t1 "+" (number-to-string dh)
  11059. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11060. (defun org-time-stamp-inactive (&optional arg)
  11061. "Insert an inactive time stamp.
  11062. An inactive time stamp is enclosed in square brackets instead of angle
  11063. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11064. does not link to the calendar and cannot be changed with the S-cursor keys.
  11065. So these are more for recording a certain time/date."
  11066. (interactive "P")
  11067. (org-time-stamp arg 'inactive))
  11068. (defvar org-date-ovl (org-make-overlay 1 1))
  11069. (org-overlay-put org-date-ovl 'face 'org-warning)
  11070. (org-detach-overlay org-date-ovl)
  11071. (defvar org-ans1) ; dynamically scoped parameter
  11072. (defvar org-ans2) ; dynamically scoped parameter
  11073. (defvar org-plain-time-of-day-regexp) ; defined below
  11074. (defvar org-overriding-default-time nil) ; dynamically scoped
  11075. (defvar org-read-date-overlay nil)
  11076. (defvar org-dcst nil) ; dynamically scoped
  11077. (defvar org-read-date-history nil)
  11078. (defun org-read-date (&optional with-time to-time from-string prompt
  11079. default-time default-input)
  11080. "Read a date, possibly a time, and make things smooth for the user.
  11081. The prompt will suggest to enter an ISO date, but you can also enter anything
  11082. which will at least partially be understood by `parse-time-string'.
  11083. Unrecognized parts of the date will default to the current day, month, year,
  11084. hour and minute. If this command is called to replace a timestamp at point,
  11085. of to enter the second timestamp of a range, the default time is taken from the
  11086. existing stamp. For example,
  11087. 3-2-5 --> 2003-02-05
  11088. feb 15 --> currentyear-02-15
  11089. sep 12 9 --> 2009-09-12
  11090. 12:45 --> today 12:45
  11091. 22 sept 0:34 --> currentyear-09-22 0:34
  11092. 12 --> currentyear-currentmonth-12
  11093. Fri --> nearest Friday (today or later)
  11094. etc.
  11095. Furthermore you can specify a relative date by giving, as the *first* thing
  11096. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11097. change in days weeks, months, years.
  11098. With a single plus or minus, the date is relative to today. With a double
  11099. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11100. +4d --> four days from today
  11101. +4 --> same as above
  11102. +2w --> two weeks from today
  11103. ++5 --> five days from default date
  11104. The function understands only English month and weekday abbreviations,
  11105. but this can be configured with the variables `parse-time-months' and
  11106. `parse-time-weekdays'.
  11107. While prompting, a calendar is popped up - you can also select the
  11108. date with the mouse (button 1). The calendar shows a period of three
  11109. months. To scroll it to other months, use the keys `>' and `<'.
  11110. If you don't like the calendar, turn it off with
  11111. \(setq org-read-date-popup-calendar nil)
  11112. With optional argument TO-TIME, the date will immediately be converted
  11113. to an internal time.
  11114. With an optional argument WITH-TIME, the prompt will suggest to also
  11115. insert a time. Note that when WITH-TIME is not set, you can still
  11116. enter a time, and this function will inform the calling routine about
  11117. this change. The calling routine may then choose to change the format
  11118. used to insert the time stamp into the buffer to include the time.
  11119. With optional argument FROM-STRING, read from this string instead from
  11120. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11121. the time/date that is used for everything that is not specified by the
  11122. user."
  11123. (require 'parse-time)
  11124. (let* ((org-time-stamp-rounding-minutes
  11125. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11126. (org-dcst org-display-custom-times)
  11127. (ct (org-current-time))
  11128. (def (or org-overriding-default-time default-time ct))
  11129. (defdecode (decode-time def))
  11130. (dummy (progn
  11131. (when (< (nth 2 defdecode) org-extend-today-until)
  11132. (setcar (nthcdr 2 defdecode) -1)
  11133. (setcar (nthcdr 1 defdecode) 59)
  11134. (setq def (apply 'encode-time defdecode)
  11135. defdecode (decode-time def)))))
  11136. (calendar-move-hook nil)
  11137. (calendar-view-diary-initially-flag nil)
  11138. (view-diary-entries-initially nil)
  11139. (calendar-view-holidays-initially-flag nil)
  11140. (view-calendar-holidays-initially nil)
  11141. (timestr (format-time-string
  11142. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11143. (prompt (concat (if prompt (concat prompt " ") "")
  11144. (format "Date+time [%s]: " timestr)))
  11145. ans (org-ans0 "") org-ans1 org-ans2 final)
  11146. (cond
  11147. (from-string (setq ans from-string))
  11148. (org-read-date-popup-calendar
  11149. (save-excursion
  11150. (save-window-excursion
  11151. (calendar)
  11152. (calendar-forward-day (- (time-to-days def)
  11153. (calendar-absolute-from-gregorian
  11154. (calendar-current-date))))
  11155. (org-eval-in-calendar nil t)
  11156. (let* ((old-map (current-local-map))
  11157. (map (copy-keymap calendar-mode-map))
  11158. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11159. (org-defkey map (kbd "RET") 'org-calendar-select)
  11160. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11161. 'org-calendar-select-mouse)
  11162. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11163. 'org-calendar-select-mouse)
  11164. (org-defkey minibuffer-local-map [(meta shift left)]
  11165. (lambda () (interactive)
  11166. (org-eval-in-calendar '(calendar-backward-month 1))))
  11167. (org-defkey minibuffer-local-map [(meta shift right)]
  11168. (lambda () (interactive)
  11169. (org-eval-in-calendar '(calendar-forward-month 1))))
  11170. (org-defkey minibuffer-local-map [(meta shift up)]
  11171. (lambda () (interactive)
  11172. (org-eval-in-calendar '(calendar-backward-year 1))))
  11173. (org-defkey minibuffer-local-map [(meta shift down)]
  11174. (lambda () (interactive)
  11175. (org-eval-in-calendar '(calendar-forward-year 1))))
  11176. (org-defkey minibuffer-local-map [(shift up)]
  11177. (lambda () (interactive)
  11178. (org-eval-in-calendar '(calendar-backward-week 1))))
  11179. (org-defkey minibuffer-local-map [(shift down)]
  11180. (lambda () (interactive)
  11181. (org-eval-in-calendar '(calendar-forward-week 1))))
  11182. (org-defkey minibuffer-local-map [(shift left)]
  11183. (lambda () (interactive)
  11184. (org-eval-in-calendar '(calendar-backward-day 1))))
  11185. (org-defkey minibuffer-local-map [(shift right)]
  11186. (lambda () (interactive)
  11187. (org-eval-in-calendar '(calendar-forward-day 1))))
  11188. (org-defkey minibuffer-local-map ">"
  11189. (lambda () (interactive)
  11190. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11191. (org-defkey minibuffer-local-map "<"
  11192. (lambda () (interactive)
  11193. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11194. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11195. (unwind-protect
  11196. (progn
  11197. (use-local-map map)
  11198. (add-hook 'post-command-hook 'org-read-date-display)
  11199. (setq org-ans0 (read-string prompt default-input
  11200. 'org-read-date-history nil))
  11201. ;; org-ans0: from prompt
  11202. ;; org-ans1: from mouse click
  11203. ;; org-ans2: from calendar motion
  11204. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11205. (remove-hook 'post-command-hook 'org-read-date-display)
  11206. (use-local-map old-map)
  11207. (when org-read-date-overlay
  11208. (org-delete-overlay org-read-date-overlay)
  11209. (setq org-read-date-overlay nil)))))))
  11210. (t ; Naked prompt only
  11211. (unwind-protect
  11212. (setq ans (read-string prompt default-input
  11213. 'org-read-date-history timestr))
  11214. (when org-read-date-overlay
  11215. (org-delete-overlay org-read-date-overlay)
  11216. (setq org-read-date-overlay nil)))))
  11217. (setq final (org-read-date-analyze ans def defdecode))
  11218. (if to-time
  11219. (apply 'encode-time final)
  11220. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11221. (format "%04d-%02d-%02d %02d:%02d"
  11222. (nth 5 final) (nth 4 final) (nth 3 final)
  11223. (nth 2 final) (nth 1 final))
  11224. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11225. (defvar def)
  11226. (defvar defdecode)
  11227. (defvar with-time)
  11228. (defun org-read-date-display ()
  11229. "Display the current date prompt interpretation in the minibuffer."
  11230. (when org-read-date-display-live
  11231. (when org-read-date-overlay
  11232. (org-delete-overlay org-read-date-overlay))
  11233. (let ((p (point)))
  11234. (end-of-line 1)
  11235. (while (not (equal (buffer-substring
  11236. (max (point-min) (- (point) 4)) (point))
  11237. " "))
  11238. (insert " "))
  11239. (goto-char p))
  11240. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11241. " " (or org-ans1 org-ans2)))
  11242. (org-end-time-was-given nil)
  11243. (f (org-read-date-analyze ans def defdecode))
  11244. (fmts (if org-dcst
  11245. org-time-stamp-custom-formats
  11246. org-time-stamp-formats))
  11247. (fmt (if (or with-time
  11248. (and (boundp 'org-time-was-given) org-time-was-given))
  11249. (cdr fmts)
  11250. (car fmts)))
  11251. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11252. (when (and org-end-time-was-given
  11253. (string-match org-plain-time-of-day-regexp txt))
  11254. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11255. org-end-time-was-given
  11256. (substring txt (match-end 0)))))
  11257. (setq org-read-date-overlay
  11258. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11259. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11260. (defun org-read-date-analyze (ans def defdecode)
  11261. "Analyse the combined answer of the date prompt."
  11262. ;; FIXME: cleanup and comment
  11263. (let (delta deltan deltaw deltadef year month day
  11264. hour minute second wday pm h2 m2 tl wday1
  11265. iso-year iso-weekday iso-week iso-year iso-date)
  11266. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11267. (setq ans "+0"))
  11268. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11269. (setq ans (replace-match "" t t ans)
  11270. deltan (car delta)
  11271. deltaw (nth 1 delta)
  11272. deltadef (nth 2 delta)))
  11273. ;; Check if there is an iso week date in there
  11274. ;; If yes, sore the info and postpone interpreting it until the rest
  11275. ;; of the parsing is done
  11276. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11277. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11278. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11279. iso-week (string-to-number (match-string 2 ans)))
  11280. (setq ans (replace-match "" t t ans)))
  11281. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11282. (when (string-match
  11283. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11284. (setq year (if (match-end 2)
  11285. (string-to-number (match-string 2 ans))
  11286. (string-to-number (format-time-string "%Y")))
  11287. month (string-to-number (match-string 3 ans))
  11288. day (string-to-number (match-string 4 ans)))
  11289. (if (< year 100) (setq year (+ 2000 year)))
  11290. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11291. t nil ans)))
  11292. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11293. ;; If there is a time with am/pm, and *no* time without it, we convert
  11294. ;; so that matching will be successful.
  11295. (loop for i from 1 to 2 do ; twice, for end time as well
  11296. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11297. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11298. (setq hour (string-to-number (match-string 1 ans))
  11299. minute (if (match-end 3)
  11300. (string-to-number (match-string 3 ans))
  11301. 0)
  11302. pm (equal ?p
  11303. (string-to-char (downcase (match-string 4 ans)))))
  11304. (if (and (= hour 12) (not pm))
  11305. (setq hour 0)
  11306. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11307. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11308. t t ans))))
  11309. ;; Check if a time range is given as a duration
  11310. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11311. (setq hour (string-to-number (match-string 1 ans))
  11312. h2 (+ hour (string-to-number (match-string 3 ans)))
  11313. minute (string-to-number (match-string 2 ans))
  11314. m2 (+ minute (if (match-end 5) (string-to-number
  11315. (match-string 5 ans))0)))
  11316. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11317. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11318. t t ans)))
  11319. ;; Check if there is a time range
  11320. (when (boundp 'org-end-time-was-given)
  11321. (setq org-time-was-given nil)
  11322. (when (and (string-match org-plain-time-of-day-regexp ans)
  11323. (match-end 8))
  11324. (setq org-end-time-was-given (match-string 8 ans))
  11325. (setq ans (concat (substring ans 0 (match-beginning 7))
  11326. (substring ans (match-end 7))))))
  11327. (setq tl (parse-time-string ans)
  11328. day (or (nth 3 tl) (nth 3 defdecode))
  11329. month (or (nth 4 tl)
  11330. (if (and org-read-date-prefer-future
  11331. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11332. (1+ (nth 4 defdecode))
  11333. (nth 4 defdecode)))
  11334. year (or (nth 5 tl)
  11335. (if (and org-read-date-prefer-future
  11336. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11337. (1+ (nth 5 defdecode))
  11338. (nth 5 defdecode)))
  11339. hour (or (nth 2 tl) (nth 2 defdecode))
  11340. minute (or (nth 1 tl) (nth 1 defdecode))
  11341. second (or (nth 0 tl) 0)
  11342. wday (nth 6 tl))
  11343. ;; Special date definitions below
  11344. (cond
  11345. (iso-week
  11346. ;; There was an iso week
  11347. (setq year (or iso-year year)
  11348. day (or iso-weekday wday 1)
  11349. wday nil ; to make sure that the trigger below does not match
  11350. iso-date (calendar-gregorian-from-absolute
  11351. (calendar-absolute-from-iso
  11352. (list iso-week day year))))
  11353. ; FIXME: Should we also push ISO weeks into the future?
  11354. ; (when (and org-read-date-prefer-future
  11355. ; (not iso-year)
  11356. ; (< (calendar-absolute-from-gregorian iso-date)
  11357. ; (time-to-days (current-time))))
  11358. ; (setq year (1+ year)
  11359. ; iso-date (calendar-gregorian-from-absolute
  11360. ; (calendar-absolute-from-iso
  11361. ; (list iso-week day year)))))
  11362. (setq month (car iso-date)
  11363. year (nth 2 iso-date)
  11364. day (nth 1 iso-date)))
  11365. (deltan
  11366. (unless deltadef
  11367. (let ((now (decode-time (current-time))))
  11368. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11369. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11370. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11371. ((equal deltaw "m") (setq month (+ month deltan)))
  11372. ((equal deltaw "y") (setq year (+ year deltan)))))
  11373. ((and wday (not (nth 3 tl)))
  11374. ;; Weekday was given, but no day, so pick that day in the week
  11375. ;; on or after the derived date.
  11376. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11377. (unless (equal wday wday1)
  11378. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11379. (if (and (boundp 'org-time-was-given)
  11380. (nth 2 tl))
  11381. (setq org-time-was-given t))
  11382. (if (< year 100) (setq year (+ 2000 year)))
  11383. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11384. (list second minute hour day month year)))
  11385. (defvar parse-time-weekdays)
  11386. (defun org-read-date-get-relative (s today default)
  11387. "Check string S for special relative date string.
  11388. TODAY and DEFAULT are internal times, for today and for a default.
  11389. Return shift list (N what def-flag)
  11390. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11391. N is the number of WHATs to shift.
  11392. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11393. the DEFAULT date rather than TODAY."
  11394. (when (and
  11395. (string-match
  11396. (concat
  11397. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11398. "\\([0-9]+\\)?"
  11399. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11400. "\\([ \t]\\|$\\)") s)
  11401. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11402. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11403. (string-to-char (substring (match-string 1 s) -1))
  11404. ?+))
  11405. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11406. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11407. (what (if (match-end 3) (match-string 3 s) "d"))
  11408. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11409. (date (if rel default today))
  11410. (wday (nth 6 (decode-time date)))
  11411. delta)
  11412. (if wday1
  11413. (progn
  11414. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11415. (if (= dir ?-) (setq delta (- delta 7)))
  11416. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11417. (list delta "d" rel))
  11418. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11419. (defun org-eval-in-calendar (form &optional keepdate)
  11420. "Eval FORM in the calendar window and return to current window.
  11421. Also, store the cursor date in variable org-ans2."
  11422. (let ((sw (selected-window)))
  11423. (select-window (get-buffer-window "*Calendar*"))
  11424. (eval form)
  11425. (when (and (not keepdate) (calendar-cursor-to-date))
  11426. (let* ((date (calendar-cursor-to-date))
  11427. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11428. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11429. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11430. (select-window sw)))
  11431. (defun org-calendar-select ()
  11432. "Return to `org-read-date' with the date currently selected.
  11433. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11434. (interactive)
  11435. (when (calendar-cursor-to-date)
  11436. (let* ((date (calendar-cursor-to-date))
  11437. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11438. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11439. (if (active-minibuffer-window) (exit-minibuffer))))
  11440. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11441. "Insert a date stamp for the date given by the internal TIME.
  11442. WITH-HM means, use the stamp format that includes the time of the day.
  11443. INACTIVE means use square brackets instead of angular ones, so that the
  11444. stamp will not contribute to the agenda.
  11445. PRE and POST are optional strings to be inserted before and after the
  11446. stamp.
  11447. The command returns the inserted time stamp."
  11448. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11449. stamp)
  11450. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11451. (insert-before-markers (or pre ""))
  11452. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11453. (when (listp extra)
  11454. (setq extra (car extra))
  11455. (if (and (stringp extra)
  11456. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11457. (setq extra (format "-%02d:%02d"
  11458. (string-to-number (match-string 1 extra))
  11459. (string-to-number (match-string 2 extra))))
  11460. (setq extra nil)))
  11461. (when extra
  11462. (backward-char 1)
  11463. (insert-before-markers extra)
  11464. (forward-char 1))
  11465. (insert-before-markers (or post ""))
  11466. (setq org-last-inserted-timestamp stamp)))
  11467. (defun org-toggle-time-stamp-overlays ()
  11468. "Toggle the use of custom time stamp formats."
  11469. (interactive)
  11470. (setq org-display-custom-times (not org-display-custom-times))
  11471. (unless org-display-custom-times
  11472. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11473. (while (setq p (next-single-property-change p 'display))
  11474. (if (and (get-text-property p 'display)
  11475. (eq (get-text-property p 'face) 'org-date))
  11476. (remove-text-properties
  11477. p (setq p (next-single-property-change p 'display))
  11478. '(display t))))
  11479. (set-buffer-modified-p bmp)))
  11480. (if (featurep 'xemacs)
  11481. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11482. (org-restart-font-lock)
  11483. (setq org-table-may-need-update t)
  11484. (if org-display-custom-times
  11485. (message "Time stamps are overlayed with custom format")
  11486. (message "Time stamp overlays removed")))
  11487. (defun org-display-custom-time (beg end)
  11488. "Overlay modified time stamp format over timestamp between BEG and END."
  11489. (let* ((ts (buffer-substring beg end))
  11490. t1 w1 with-hm tf time str w2 (off 0))
  11491. (save-match-data
  11492. (setq t1 (org-parse-time-string ts t))
  11493. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11494. (setq off (- (match-end 0) (match-beginning 0)))))
  11495. (setq end (- end off))
  11496. (setq w1 (- end beg)
  11497. with-hm (and (nth 1 t1) (nth 2 t1))
  11498. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11499. time (org-fix-decoded-time t1)
  11500. str (org-add-props
  11501. (format-time-string
  11502. (substring tf 1 -1) (apply 'encode-time time))
  11503. nil 'mouse-face 'highlight)
  11504. w2 (length str))
  11505. (if (not (= w2 w1))
  11506. (add-text-properties (1+ beg) (+ 2 beg)
  11507. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11508. (if (featurep 'xemacs)
  11509. (progn
  11510. (put-text-property beg end 'invisible t)
  11511. (put-text-property beg end 'end-glyph (make-glyph str)))
  11512. (put-text-property beg end 'display str))))
  11513. (defun org-translate-time (string)
  11514. "Translate all timestamps in STRING to custom format.
  11515. But do this only if the variable `org-display-custom-times' is set."
  11516. (when org-display-custom-times
  11517. (save-match-data
  11518. (let* ((start 0)
  11519. (re org-ts-regexp-both)
  11520. t1 with-hm inactive tf time str beg end)
  11521. (while (setq start (string-match re string start))
  11522. (setq beg (match-beginning 0)
  11523. end (match-end 0)
  11524. t1 (save-match-data
  11525. (org-parse-time-string (substring string beg end) t))
  11526. with-hm (and (nth 1 t1) (nth 2 t1))
  11527. inactive (equal (substring string beg (1+ beg)) "[")
  11528. tf (funcall (if with-hm 'cdr 'car)
  11529. org-time-stamp-custom-formats)
  11530. time (org-fix-decoded-time t1)
  11531. str (format-time-string
  11532. (concat
  11533. (if inactive "[" "<") (substring tf 1 -1)
  11534. (if inactive "]" ">"))
  11535. (apply 'encode-time time))
  11536. string (replace-match str t t string)
  11537. start (+ start (length str)))))))
  11538. string)
  11539. (defun org-fix-decoded-time (time)
  11540. "Set 0 instead of nil for the first 6 elements of time.
  11541. Don't touch the rest."
  11542. (let ((n 0))
  11543. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11544. (defun org-days-to-time (timestamp-string)
  11545. "Difference between TIMESTAMP-STRING and now in days."
  11546. (- (time-to-days (org-time-string-to-time timestamp-string))
  11547. (time-to-days (current-time))))
  11548. (defun org-deadline-close (timestamp-string &optional ndays)
  11549. "Is the time in TIMESTAMP-STRING close to the current date?"
  11550. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11551. (and (< (org-days-to-time timestamp-string) ndays)
  11552. (not (org-entry-is-done-p))))
  11553. (defun org-get-wdays (ts)
  11554. "Get the deadline lead time appropriate for timestring TS."
  11555. (cond
  11556. ((<= org-deadline-warning-days 0)
  11557. ;; 0 or negative, enforce this value no matter what
  11558. (- org-deadline-warning-days))
  11559. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11560. ;; lead time is specified.
  11561. (floor (* (string-to-number (match-string 1 ts))
  11562. (cdr (assoc (match-string 2 ts)
  11563. '(("d" . 1) ("w" . 7)
  11564. ("m" . 30.4) ("y" . 365.25)))))))
  11565. ;; go for the default.
  11566. (t org-deadline-warning-days)))
  11567. (defun org-calendar-select-mouse (ev)
  11568. "Return to `org-read-date' with the date currently selected.
  11569. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11570. (interactive "e")
  11571. (mouse-set-point ev)
  11572. (when (calendar-cursor-to-date)
  11573. (let* ((date (calendar-cursor-to-date))
  11574. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11575. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11576. (if (active-minibuffer-window) (exit-minibuffer))))
  11577. (defun org-check-deadlines (ndays)
  11578. "Check if there are any deadlines due or past due.
  11579. A deadline is considered due if it happens within `org-deadline-warning-days'
  11580. days from today's date. If the deadline appears in an entry marked DONE,
  11581. it is not shown. The prefix arg NDAYS can be used to test that many
  11582. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11583. (interactive "P")
  11584. (let* ((org-warn-days
  11585. (cond
  11586. ((equal ndays '(4)) 100000)
  11587. (ndays (prefix-numeric-value ndays))
  11588. (t (abs org-deadline-warning-days))))
  11589. (case-fold-search nil)
  11590. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11591. (callback
  11592. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11593. (message "%d deadlines past-due or due within %d days"
  11594. (org-occur regexp nil callback)
  11595. org-warn-days)))
  11596. (defun org-check-before-date (date)
  11597. "Check if there are deadlines or scheduled entries before DATE."
  11598. (interactive (list (org-read-date)))
  11599. (let ((case-fold-search nil)
  11600. (regexp (concat "\\<\\(" org-deadline-string
  11601. "\\|" org-scheduled-string
  11602. "\\) *<\\([^>]+\\)>"))
  11603. (callback
  11604. (lambda () (time-less-p
  11605. (org-time-string-to-time (match-string 2))
  11606. (org-time-string-to-time date)))))
  11607. (message "%d entries before %s"
  11608. (org-occur regexp nil callback) date)))
  11609. (defun org-check-after-date (date)
  11610. "Check if there are deadlines or scheduled entries after DATE."
  11611. (interactive (list (org-read-date)))
  11612. (let ((case-fold-search nil)
  11613. (regexp (concat "\\<\\(" org-deadline-string
  11614. "\\|" org-scheduled-string
  11615. "\\) *<\\([^>]+\\)>"))
  11616. (callback
  11617. (lambda () (not
  11618. (time-less-p
  11619. (org-time-string-to-time (match-string 2))
  11620. (org-time-string-to-time date))))))
  11621. (message "%d entries after %s"
  11622. (org-occur regexp nil callback) date)))
  11623. (defun org-evaluate-time-range (&optional to-buffer)
  11624. "Evaluate a time range by computing the difference between start and end.
  11625. Normally the result is just printed in the echo area, but with prefix arg
  11626. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11627. If the time range is actually in a table, the result is inserted into the
  11628. next column.
  11629. For time difference computation, a year is assumed to be exactly 365
  11630. days in order to avoid rounding problems."
  11631. (interactive "P")
  11632. (or
  11633. (org-clock-update-time-maybe)
  11634. (save-excursion
  11635. (unless (org-at-date-range-p t)
  11636. (goto-char (point-at-bol))
  11637. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11638. (if (not (org-at-date-range-p t))
  11639. (error "Not at a time-stamp range, and none found in current line")))
  11640. (let* ((ts1 (match-string 1))
  11641. (ts2 (match-string 2))
  11642. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11643. (match-end (match-end 0))
  11644. (time1 (org-time-string-to-time ts1))
  11645. (time2 (org-time-string-to-time ts2))
  11646. (t1 (time-to-seconds time1))
  11647. (t2 (time-to-seconds time2))
  11648. (diff (abs (- t2 t1)))
  11649. (negative (< (- t2 t1) 0))
  11650. ;; (ys (floor (* 365 24 60 60)))
  11651. (ds (* 24 60 60))
  11652. (hs (* 60 60))
  11653. (fy "%dy %dd %02d:%02d")
  11654. (fy1 "%dy %dd")
  11655. (fd "%dd %02d:%02d")
  11656. (fd1 "%dd")
  11657. (fh "%02d:%02d")
  11658. y d h m align)
  11659. (if havetime
  11660. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11661. y 0
  11662. d (floor (/ diff ds)) diff (mod diff ds)
  11663. h (floor (/ diff hs)) diff (mod diff hs)
  11664. m (floor (/ diff 60)))
  11665. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11666. y 0
  11667. d (floor (+ (/ diff ds) 0.5))
  11668. h 0 m 0))
  11669. (if (not to-buffer)
  11670. (message "%s" (org-make-tdiff-string y d h m))
  11671. (if (org-at-table-p)
  11672. (progn
  11673. (goto-char match-end)
  11674. (setq align t)
  11675. (and (looking-at " *|") (goto-char (match-end 0))))
  11676. (goto-char match-end))
  11677. (if (looking-at
  11678. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11679. (replace-match ""))
  11680. (if negative (insert " -"))
  11681. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11682. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11683. (insert " " (format fh h m))))
  11684. (if align (org-table-align))
  11685. (message "Time difference inserted")))))
  11686. (defun org-make-tdiff-string (y d h m)
  11687. (let ((fmt "")
  11688. (l nil))
  11689. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11690. l (push y l)))
  11691. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11692. l (push d l)))
  11693. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11694. l (push h l)))
  11695. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11696. l (push m l)))
  11697. (apply 'format fmt (nreverse l))))
  11698. (defun org-time-string-to-time (s)
  11699. (apply 'encode-time (org-parse-time-string s)))
  11700. (defun org-time-string-to-seconds (s)
  11701. (time-to-seconds (org-time-string-to-time s)))
  11702. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11703. "Convert a time stamp to an absolute day number.
  11704. If there is a specifyer for a cyclic time stamp, get the closest date to
  11705. DAYNR.
  11706. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11707. (cond
  11708. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11709. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11710. daynr
  11711. (+ daynr 1000)))
  11712. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11713. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11714. (time-to-days (current-time))) (match-string 0 s)
  11715. prefer show-all))
  11716. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11717. (defun org-days-to-iso-week (days)
  11718. "Return the iso week number."
  11719. (require 'cal-iso)
  11720. (car (calendar-iso-from-absolute days)))
  11721. (defun org-small-year-to-year (year)
  11722. "Convert 2-digit years into 4-digit years.
  11723. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11724. The year 2000 cannot be abbreviated. Any year larger than 99
  11725. is returned unchanged."
  11726. (if (< year 38)
  11727. (setq year (+ 2000 year))
  11728. (if (< year 100)
  11729. (setq year (+ 1900 year))))
  11730. year)
  11731. (defun org-time-from-absolute (d)
  11732. "Return the time corresponding to date D.
  11733. D may be an absolute day number, or a calendar-type list (month day year)."
  11734. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11735. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11736. (defun org-calendar-holiday ()
  11737. "List of holidays, for Diary display in Org-mode."
  11738. (require 'holidays)
  11739. (let ((hl (funcall
  11740. (if (fboundp 'calendar-check-holidays)
  11741. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11742. (if hl (mapconcat 'identity hl "; "))))
  11743. (defun org-diary-sexp-entry (sexp entry date)
  11744. "Process a SEXP diary ENTRY for DATE."
  11745. (require 'diary-lib)
  11746. (let ((result (if calendar-debug-sexp
  11747. (let ((stack-trace-on-error t))
  11748. (eval (car (read-from-string sexp))))
  11749. (condition-case nil
  11750. (eval (car (read-from-string sexp)))
  11751. (error
  11752. (beep)
  11753. (message "Bad sexp at line %d in %s: %s"
  11754. (org-current-line)
  11755. (buffer-file-name) sexp)
  11756. (sleep-for 2))))))
  11757. (cond ((stringp result) result)
  11758. ((and (consp result)
  11759. (stringp (cdr result))) (cdr result))
  11760. (result entry)
  11761. (t nil))))
  11762. (defun org-diary-to-ical-string (frombuf)
  11763. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11764. This uses the icalendar.el library."
  11765. (let* ((tmpdir (if (featurep 'xemacs)
  11766. (temp-directory)
  11767. temporary-file-directory))
  11768. (tmpfile (make-temp-name
  11769. (expand-file-name "orgics" tmpdir)))
  11770. buf rtn b e)
  11771. (save-excursion
  11772. (set-buffer frombuf)
  11773. (icalendar-export-region (point-min) (point-max) tmpfile)
  11774. (setq buf (find-buffer-visiting tmpfile))
  11775. (set-buffer buf)
  11776. (goto-char (point-min))
  11777. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11778. (setq b (match-beginning 0)))
  11779. (goto-char (point-max))
  11780. (if (re-search-backward "^END:VEVENT" nil t)
  11781. (setq e (match-end 0)))
  11782. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11783. (kill-buffer buf)
  11784. (delete-file tmpfile)
  11785. rtn))
  11786. (defun org-closest-date (start current change prefer show-all)
  11787. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11788. When PREFER is `past' return a date that is either CURRENT or past.
  11789. When PREFER is `future', return a date that is either CURRENT or future.
  11790. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11791. ;; Make the proper lists from the dates
  11792. (catch 'exit
  11793. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11794. dn dw sday cday n1 n2 n0
  11795. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11796. (setq start (org-date-to-gregorian start)
  11797. current (org-date-to-gregorian
  11798. (if show-all
  11799. current
  11800. (time-to-days (current-time))))
  11801. sday (calendar-absolute-from-gregorian start)
  11802. cday (calendar-absolute-from-gregorian current))
  11803. (if (<= cday sday) (throw 'exit sday))
  11804. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11805. (setq dn (string-to-number (match-string 1 change))
  11806. dw (cdr (assoc (match-string 2 change) a1)))
  11807. (error "Invalid change specifyer: %s" change))
  11808. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11809. (cond
  11810. ((eq dw 'day)
  11811. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11812. n2 (+ n1 dn)))
  11813. ((eq dw 'year)
  11814. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11815. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11816. (setq date1 (list m d y1)
  11817. n1 (calendar-absolute-from-gregorian date1)
  11818. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11819. n2 (calendar-absolute-from-gregorian date2)))
  11820. ((eq dw 'month)
  11821. ;; approx number of month between the two dates
  11822. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11823. ;; How often does dn fit in there?
  11824. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11825. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11826. m (+ m nm)
  11827. ny (floor (/ m 12))
  11828. y (+ y ny)
  11829. m (- m (* ny 12)))
  11830. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11831. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11832. (setq m2 (+ m dn) y2 y)
  11833. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11834. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11835. (while (<= n2 cday)
  11836. (setq n1 n2 m m2 y y2)
  11837. (setq m2 (+ m dn) y2 y)
  11838. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11839. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11840. ;; Make sure n1 is the earlier date
  11841. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11842. (if show-all
  11843. (cond
  11844. ((eq prefer 'past) n1)
  11845. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11846. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11847. (cond
  11848. ((eq prefer 'past) n1)
  11849. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11850. (t (if (= cday n1) n1 n2)))))))
  11851. (defun org-date-to-gregorian (date)
  11852. "Turn any specification of DATE into a gregorian date for the calendar."
  11853. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11854. ((and (listp date) (= (length date) 3)) date)
  11855. ((stringp date)
  11856. (setq date (org-parse-time-string date))
  11857. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11858. ((listp date)
  11859. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11860. (defun org-parse-time-string (s &optional nodefault)
  11861. "Parse the standard Org-mode time string.
  11862. This should be a lot faster than the normal `parse-time-string'.
  11863. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11864. hour and minute fields will be nil if not given."
  11865. (if (string-match org-ts-regexp0 s)
  11866. (list 0
  11867. (if (or (match-beginning 8) (not nodefault))
  11868. (string-to-number (or (match-string 8 s) "0")))
  11869. (if (or (match-beginning 7) (not nodefault))
  11870. (string-to-number (or (match-string 7 s) "0")))
  11871. (string-to-number (match-string 4 s))
  11872. (string-to-number (match-string 3 s))
  11873. (string-to-number (match-string 2 s))
  11874. nil nil nil)
  11875. (make-list 9 0)))
  11876. (defun org-timestamp-up (&optional arg)
  11877. "Increase the date item at the cursor by one.
  11878. If the cursor is on the year, change the year. If it is on the month or
  11879. the day, change that.
  11880. With prefix ARG, change by that many units."
  11881. (interactive "p")
  11882. (org-timestamp-change (prefix-numeric-value arg)))
  11883. (defun org-timestamp-down (&optional arg)
  11884. "Decrease the date item at the cursor by one.
  11885. If the cursor is on the year, change the year. If it is on the month or
  11886. the day, change that.
  11887. With prefix ARG, change by that many units."
  11888. (interactive "p")
  11889. (org-timestamp-change (- (prefix-numeric-value arg))))
  11890. (defun org-timestamp-up-day (&optional arg)
  11891. "Increase the date in the time stamp by one day.
  11892. With prefix ARG, change that many days."
  11893. (interactive "p")
  11894. (if (and (not (org-at-timestamp-p t))
  11895. (org-on-heading-p))
  11896. (org-todo 'up)
  11897. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11898. (defun org-timestamp-down-day (&optional arg)
  11899. "Decrease the date in the time stamp by one day.
  11900. With prefix ARG, change that many days."
  11901. (interactive "p")
  11902. (if (and (not (org-at-timestamp-p t))
  11903. (org-on-heading-p))
  11904. (org-todo 'down)
  11905. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11906. (defun org-at-timestamp-p (&optional inactive-ok)
  11907. "Determine if the cursor is in or at a timestamp."
  11908. (interactive)
  11909. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11910. (pos (point))
  11911. (ans (or (looking-at tsr)
  11912. (save-excursion
  11913. (skip-chars-backward "^[<\n\r\t")
  11914. (if (> (point) (point-min)) (backward-char 1))
  11915. (and (looking-at tsr)
  11916. (> (- (match-end 0) pos) -1))))))
  11917. (and ans
  11918. (boundp 'org-ts-what)
  11919. (setq org-ts-what
  11920. (cond
  11921. ((= pos (match-beginning 0)) 'bracket)
  11922. ((= pos (1- (match-end 0))) 'bracket)
  11923. ((org-pos-in-match-range pos 2) 'year)
  11924. ((org-pos-in-match-range pos 3) 'month)
  11925. ((org-pos-in-match-range pos 7) 'hour)
  11926. ((org-pos-in-match-range pos 8) 'minute)
  11927. ((or (org-pos-in-match-range pos 4)
  11928. (org-pos-in-match-range pos 5)) 'day)
  11929. ((and (> pos (or (match-end 8) (match-end 5)))
  11930. (< pos (match-end 0)))
  11931. (- pos (or (match-end 8) (match-end 5))))
  11932. (t 'day))))
  11933. ans))
  11934. (defun org-toggle-timestamp-type ()
  11935. "Toggle the type (<active> or [inactive]) of a time stamp."
  11936. (interactive)
  11937. (when (org-at-timestamp-p t)
  11938. (let ((beg (match-beginning 0)) (end (match-end 0))
  11939. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11940. (save-excursion
  11941. (goto-char beg)
  11942. (while (re-search-forward "[][<>]" end t)
  11943. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11944. t t)))
  11945. (message "Timestamp is now %sactive"
  11946. (if (equal (char-after beg) ?<) "" "in")))))
  11947. (defun org-timestamp-change (n &optional what)
  11948. "Change the date in the time stamp at point.
  11949. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11950. `year', `minute', `second'. If WHAT is not given, the cursor position
  11951. in the timestamp determines what will be changed."
  11952. (let ((pos (point))
  11953. with-hm inactive
  11954. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11955. org-ts-what
  11956. extra rem
  11957. ts time time0)
  11958. (if (not (org-at-timestamp-p t))
  11959. (error "Not at a timestamp"))
  11960. (if (and (not what) (eq org-ts-what 'bracket))
  11961. (org-toggle-timestamp-type)
  11962. (if (and (not what) (not (eq org-ts-what 'day))
  11963. org-display-custom-times
  11964. (get-text-property (point) 'display)
  11965. (not (get-text-property (1- (point)) 'display)))
  11966. (setq org-ts-what 'day))
  11967. (setq org-ts-what (or what org-ts-what)
  11968. inactive (= (char-after (match-beginning 0)) ?\[)
  11969. ts (match-string 0))
  11970. (replace-match "")
  11971. (if (string-match
  11972. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11973. ts)
  11974. (setq extra (match-string 1 ts)))
  11975. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11976. (setq with-hm t))
  11977. (setq time0 (org-parse-time-string ts))
  11978. (when (and (eq org-ts-what 'minute)
  11979. (eq current-prefix-arg nil))
  11980. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11981. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11982. (setcar (cdr time0) (+ (nth 1 time0)
  11983. (if (> n 0) (- rem) (- dm rem))))))
  11984. (setq time
  11985. (encode-time (or (car time0) 0)
  11986. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11987. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11988. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11989. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11990. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11991. (nthcdr 6 time0)))
  11992. (when (and (member org-ts-what '(hour minute))
  11993. extra
  11994. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11995. (setq extra (org-modify-ts-extra
  11996. extra
  11997. (if (eq org-ts-what 'hour) 2 5)
  11998. n dm)))
  11999. (when (integerp org-ts-what)
  12000. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12001. (if (eq what 'calendar)
  12002. (let ((cal-date (org-get-date-from-calendar)))
  12003. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12004. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12005. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12006. (setcar time0 (or (car time0) 0))
  12007. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12008. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12009. (setq time (apply 'encode-time time0))))
  12010. (setq org-last-changed-timestamp
  12011. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12012. (org-clock-update-time-maybe)
  12013. (goto-char pos)
  12014. ;; Try to recenter the calendar window, if any
  12015. (if (and org-calendar-follow-timestamp-change
  12016. (get-buffer-window "*Calendar*" t)
  12017. (memq org-ts-what '(day month year)))
  12018. (org-recenter-calendar (time-to-days time))))))
  12019. (defun org-modify-ts-extra (s pos n dm)
  12020. "Change the different parts of the lead-time and repeat fields in timestamp."
  12021. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12022. ng h m new rem)
  12023. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12024. (cond
  12025. ((or (org-pos-in-match-range pos 2)
  12026. (org-pos-in-match-range pos 3))
  12027. (setq m (string-to-number (match-string 3 s))
  12028. h (string-to-number (match-string 2 s)))
  12029. (if (org-pos-in-match-range pos 2)
  12030. (setq h (+ h n))
  12031. (setq n (* dm (org-no-warnings (signum n))))
  12032. (when (not (= 0 (setq rem (% m dm))))
  12033. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12034. (setq m (+ m n)))
  12035. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12036. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12037. (setq h (min 24 (max 0 h)))
  12038. (setq ng 1 new (format "-%02d:%02d" h m)))
  12039. ((org-pos-in-match-range pos 6)
  12040. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12041. ((org-pos-in-match-range pos 5)
  12042. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12043. ((org-pos-in-match-range pos 9)
  12044. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12045. ((org-pos-in-match-range pos 8)
  12046. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12047. (when ng
  12048. (setq s (concat
  12049. (substring s 0 (match-beginning ng))
  12050. new
  12051. (substring s (match-end ng))))))
  12052. s))
  12053. (defun org-recenter-calendar (date)
  12054. "If the calendar is visible, recenter it to DATE."
  12055. (let* ((win (selected-window))
  12056. (cwin (get-buffer-window "*Calendar*" t))
  12057. (calendar-move-hook nil))
  12058. (when cwin
  12059. (select-window cwin)
  12060. (calendar-goto-date (if (listp date) date
  12061. (calendar-gregorian-from-absolute date)))
  12062. (select-window win))))
  12063. (defun org-goto-calendar (&optional arg)
  12064. "Go to the Emacs calendar at the current date.
  12065. If there is a time stamp in the current line, go to that date.
  12066. A prefix ARG can be used to force the current date."
  12067. (interactive "P")
  12068. (let ((tsr org-ts-regexp) diff
  12069. (calendar-move-hook nil)
  12070. (calendar-view-holidays-initially-flag nil)
  12071. (view-calendar-holidays-initially nil)
  12072. (calendar-view-diary-initially-flag nil)
  12073. (view-diary-entries-initially nil))
  12074. (if (or (org-at-timestamp-p)
  12075. (save-excursion
  12076. (beginning-of-line 1)
  12077. (looking-at (concat ".*" tsr))))
  12078. (let ((d1 (time-to-days (current-time)))
  12079. (d2 (time-to-days
  12080. (org-time-string-to-time (match-string 1)))))
  12081. (setq diff (- d2 d1))))
  12082. (calendar)
  12083. (calendar-goto-today)
  12084. (if (and diff (not arg)) (calendar-forward-day diff))))
  12085. (defun org-get-date-from-calendar ()
  12086. "Return a list (month day year) of date at point in calendar."
  12087. (with-current-buffer "*Calendar*"
  12088. (save-match-data
  12089. (calendar-cursor-to-date))))
  12090. (defun org-date-from-calendar ()
  12091. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12092. If there is already a time stamp at the cursor position, update it."
  12093. (interactive)
  12094. (if (org-at-timestamp-p t)
  12095. (org-timestamp-change 0 'calendar)
  12096. (let ((cal-date (org-get-date-from-calendar)))
  12097. (org-insert-time-stamp
  12098. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12099. (defun org-minutes-to-hh:mm-string (m)
  12100. "Compute H:MM from a number of minutes."
  12101. (let ((h (/ m 60)))
  12102. (setq m (- m (* 60 h)))
  12103. (format org-time-clocksum-format h m)))
  12104. (defun org-hh:mm-string-to-minutes (s)
  12105. "Convert a string H:MM to a number of minutes."
  12106. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12107. (+ (* (string-to-number (match-string 1 s)) 60)
  12108. (string-to-number (match-string 2 s)))
  12109. 0))
  12110. ;;;; Files
  12111. (defun org-save-all-org-buffers ()
  12112. "Save all Org-mode buffers without user confirmation."
  12113. (interactive)
  12114. (message "Saving all Org-mode buffers...")
  12115. (save-some-buffers t 'org-mode-p)
  12116. (when (featurep 'org-id) (org-id-locations-save))
  12117. (message "Saving all Org-mode buffers... done"))
  12118. (defun org-revert-all-org-buffers ()
  12119. "Revert all Org-mode buffers.
  12120. Prompt for confirmation when there are unsaved changes.
  12121. Be sure you know what you are doing before letting this function
  12122. overwrite your changes.
  12123. This function is useful in a setup where one tracks org files
  12124. with a version control system, to revert on one machine after pulling
  12125. changes from another. I believe the procedure must be like this:
  12126. 1. M-x org-save-all-org-buffers
  12127. 2. Pull changes from the other machine, resolve conflicts
  12128. 3. M-x org-revert-all-org-buffers"
  12129. (interactive)
  12130. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12131. (error "Abort"))
  12132. (save-excursion
  12133. (save-window-excursion
  12134. (mapc
  12135. (lambda (b)
  12136. (when (and (with-current-buffer b (org-mode-p))
  12137. (with-current-buffer b buffer-file-name))
  12138. (switch-to-buffer b)
  12139. (revert-buffer t 'no-confirm)))
  12140. (buffer-list))
  12141. (when (and (featurep 'org-id) org-id-track-globally)
  12142. (org-id-locations-load)))))
  12143. ;;;; Agenda files
  12144. ;;;###autoload
  12145. (defun org-iswitchb (&optional arg)
  12146. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12147. With a prefix argument, restrict available to files.
  12148. With two prefix arguments, restrict available buffers to agenda files.
  12149. Due to some yet unresolved reason, the global function
  12150. `iswitchb-mode' needs to be active for this function to work."
  12151. (interactive "P")
  12152. (require 'iswitchb)
  12153. (let ((enabled iswitchb-mode) blist)
  12154. (or enabled (iswitchb-mode 1))
  12155. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12156. ((equal arg '(16)) (org-buffer-list 'agenda))
  12157. (t (org-buffer-list))))
  12158. (unwind-protect
  12159. (let ((iswitchb-make-buflist-hook
  12160. (lambda ()
  12161. (setq iswitchb-temp-buflist
  12162. (mapcar 'buffer-name blist)))))
  12163. (switch-to-buffer
  12164. (iswitchb-read-buffer
  12165. "Switch-to: " nil t))
  12166. (or enabled (iswitchb-mode -1))))))
  12167. ;;;###autoload
  12168. (defun org-ido-switchb (&optional arg)
  12169. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12170. With a prefix argument, restrict available to files.
  12171. With two prefix arguments, restrict available buffers to agenda files."
  12172. (interactive "P")
  12173. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12174. ((equal arg '(16)) (org-buffer-list 'agenda))
  12175. (t (org-buffer-list)))))
  12176. (switch-to-buffer
  12177. (org-ido-completing-read "Org buffer: "
  12178. (mapcar 'list (mapcar 'buffer-name blist))
  12179. nil t))))
  12180. (defun org-buffer-list (&optional predicate exclude-tmp)
  12181. "Return a list of Org buffers.
  12182. PREDICATE can be `export', `files' or `agenda'.
  12183. export restrict the list to Export buffers.
  12184. files restrict the list to buffers visiting Org files.
  12185. agenda restrict the list to buffers visiting agenda files.
  12186. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12187. (let* ((bfn nil)
  12188. (agenda-files (and (eq predicate 'agenda)
  12189. (mapcar 'file-truename (org-agenda-files t))))
  12190. (filter
  12191. (cond
  12192. ((eq predicate 'files)
  12193. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12194. ((eq predicate 'export)
  12195. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12196. ((eq predicate 'agenda)
  12197. (lambda (b)
  12198. (with-current-buffer b
  12199. (and (eq major-mode 'org-mode)
  12200. (setq bfn (buffer-file-name b))
  12201. (member (file-truename bfn) agenda-files)))))
  12202. (t (lambda (b) (with-current-buffer b
  12203. (or (eq major-mode 'org-mode)
  12204. (string-match "\*Org .*Export"
  12205. (buffer-name b)))))))))
  12206. (delq nil
  12207. (mapcar
  12208. (lambda(b)
  12209. (if (and (funcall filter b)
  12210. (or (not exclude-tmp)
  12211. (not (string-match "tmp" (buffer-name b)))))
  12212. b
  12213. nil))
  12214. (buffer-list)))))
  12215. (defun org-agenda-files (&optional unrestricted archives)
  12216. "Get the list of agenda files.
  12217. Optional UNRESTRICTED means return the full list even if a restriction
  12218. is currently in place.
  12219. When ARCHIVES is t, include all archive files hat are really being
  12220. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12221. `org-agenda-archives-mode' is t."
  12222. (let ((files
  12223. (cond
  12224. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12225. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12226. ((listp org-agenda-files) org-agenda-files)
  12227. (t (error "Invalid value of `org-agenda-files'")))))
  12228. (setq files (apply 'append
  12229. (mapcar (lambda (f)
  12230. (if (file-directory-p f)
  12231. (directory-files
  12232. f t org-agenda-file-regexp)
  12233. (list f)))
  12234. files)))
  12235. (when org-agenda-skip-unavailable-files
  12236. (setq files (delq nil
  12237. (mapcar (function
  12238. (lambda (file)
  12239. (and (file-readable-p file) file)))
  12240. files))))
  12241. (when (or (eq archives t)
  12242. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12243. (setq files (org-add-archive-files files)))
  12244. files))
  12245. (defun org-edit-agenda-file-list ()
  12246. "Edit the list of agenda files.
  12247. Depending on setup, this either uses customize to edit the variable
  12248. `org-agenda-files', or it visits the file that is holding the list. In the
  12249. latter case, the buffer is set up in a way that saving it automatically kills
  12250. the buffer and restores the previous window configuration."
  12251. (interactive)
  12252. (if (stringp org-agenda-files)
  12253. (let ((cw (current-window-configuration)))
  12254. (find-file org-agenda-files)
  12255. (org-set-local 'org-window-configuration cw)
  12256. (org-add-hook 'after-save-hook
  12257. (lambda ()
  12258. (set-window-configuration
  12259. (prog1 org-window-configuration
  12260. (kill-buffer (current-buffer))))
  12261. (org-install-agenda-files-menu)
  12262. (message "New agenda file list installed"))
  12263. nil 'local)
  12264. (message "%s" (substitute-command-keys
  12265. "Edit list and finish with \\[save-buffer]")))
  12266. (customize-variable 'org-agenda-files)))
  12267. (defun org-store-new-agenda-file-list (list)
  12268. "Set new value for the agenda file list and save it correctly."
  12269. (if (stringp org-agenda-files)
  12270. (let ((f org-agenda-files) b)
  12271. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12272. (with-temp-file f
  12273. (insert (mapconcat 'identity list "\n") "\n")))
  12274. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12275. (setq org-agenda-files list)
  12276. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12277. (defun org-read-agenda-file-list ()
  12278. "Read the list of agenda files from a file."
  12279. (when (file-directory-p org-agenda-files)
  12280. (error "`org-agenda-files' cannot be a single directory"))
  12281. (when (stringp org-agenda-files)
  12282. (with-temp-buffer
  12283. (insert-file-contents org-agenda-files)
  12284. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12285. ;;;###autoload
  12286. (defun org-cycle-agenda-files ()
  12287. "Cycle through the files in `org-agenda-files'.
  12288. If the current buffer visits an agenda file, find the next one in the list.
  12289. If the current buffer does not, find the first agenda file."
  12290. (interactive)
  12291. (let* ((fs (org-agenda-files t))
  12292. (files (append fs (list (car fs))))
  12293. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12294. file)
  12295. (unless files (error "No agenda files"))
  12296. (catch 'exit
  12297. (while (setq file (pop files))
  12298. (if (equal (file-truename file) tcf)
  12299. (when (car files)
  12300. (find-file (car files))
  12301. (throw 'exit t))))
  12302. (find-file (car fs)))
  12303. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12304. (defun org-agenda-file-to-front (&optional to-end)
  12305. "Move/add the current file to the top of the agenda file list.
  12306. If the file is not present in the list, it is added to the front. If it is
  12307. present, it is moved there. With optional argument TO-END, add/move to the
  12308. end of the list."
  12309. (interactive "P")
  12310. (let ((org-agenda-skip-unavailable-files nil)
  12311. (file-alist (mapcar (lambda (x)
  12312. (cons (file-truename x) x))
  12313. (org-agenda-files t)))
  12314. (ctf (file-truename buffer-file-name))
  12315. x had)
  12316. (setq x (assoc ctf file-alist) had x)
  12317. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12318. (if to-end
  12319. (setq file-alist (append (delq x file-alist) (list x)))
  12320. (setq file-alist (cons x (delq x file-alist))))
  12321. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12322. (org-install-agenda-files-menu)
  12323. (message "File %s to %s of agenda file list"
  12324. (if had "moved" "added") (if to-end "end" "front"))))
  12325. (defun org-remove-file (&optional file)
  12326. "Remove current file from the list of files in variable `org-agenda-files'.
  12327. These are the files which are being checked for agenda entries.
  12328. Optional argument FILE means, use this file instead of the current."
  12329. (interactive)
  12330. (let* ((org-agenda-skip-unavailable-files nil)
  12331. (file (or file buffer-file-name))
  12332. (true-file (file-truename file))
  12333. (afile (abbreviate-file-name file))
  12334. (files (delq nil (mapcar
  12335. (lambda (x)
  12336. (if (equal true-file
  12337. (file-truename x))
  12338. nil x))
  12339. (org-agenda-files t)))))
  12340. (if (not (= (length files) (length (org-agenda-files t))))
  12341. (progn
  12342. (org-store-new-agenda-file-list files)
  12343. (org-install-agenda-files-menu)
  12344. (message "Removed file: %s" afile))
  12345. (message "File was not in list: %s (not removed)" afile))))
  12346. (defun org-file-menu-entry (file)
  12347. (vector file (list 'find-file file) t))
  12348. (defun org-check-agenda-file (file)
  12349. "Make sure FILE exists. If not, ask user what to do."
  12350. (when (not (file-exists-p file))
  12351. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12352. (abbreviate-file-name file))
  12353. (let ((r (downcase (read-char-exclusive))))
  12354. (cond
  12355. ((equal r ?r)
  12356. (org-remove-file file)
  12357. (throw 'nextfile t))
  12358. (t (error "Abort"))))))
  12359. (defun org-get-agenda-file-buffer (file)
  12360. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12361. it to the list of buffers which might be released later."
  12362. (let ((buf (org-find-base-buffer-visiting file)))
  12363. (if buf
  12364. buf ; just return it
  12365. ;; Make a new buffer and remember it
  12366. (setq buf (find-file-noselect file))
  12367. (if buf (push buf org-agenda-new-buffers))
  12368. buf)))
  12369. (defun org-release-buffers (blist)
  12370. "Release all buffers in list, asking the user for confirmation when needed.
  12371. When a buffer is unmodified, it is just killed. When modified, it is saved
  12372. \(if the user agrees) and then killed."
  12373. (let (buf file)
  12374. (while (setq buf (pop blist))
  12375. (setq file (buffer-file-name buf))
  12376. (when (and (buffer-modified-p buf)
  12377. file
  12378. (y-or-n-p (format "Save file %s? " file)))
  12379. (with-current-buffer buf (save-buffer)))
  12380. (kill-buffer buf))))
  12381. (defun org-prepare-agenda-buffers (files)
  12382. "Create buffers for all agenda files, protect archived trees and comments."
  12383. (interactive)
  12384. (let ((pa '(:org-archived t))
  12385. (pc '(:org-comment t))
  12386. (pall '(:org-archived t :org-comment t))
  12387. (inhibit-read-only t)
  12388. (rea (concat ":" org-archive-tag ":"))
  12389. bmp file re)
  12390. (save-excursion
  12391. (save-restriction
  12392. (while (setq file (pop files))
  12393. (catch 'nextfile
  12394. (if (bufferp file)
  12395. (set-buffer file)
  12396. (org-check-agenda-file file)
  12397. (set-buffer (org-get-agenda-file-buffer file)))
  12398. (widen)
  12399. (setq bmp (buffer-modified-p))
  12400. (org-refresh-category-properties)
  12401. (setq org-todo-keywords-for-agenda
  12402. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12403. (setq org-done-keywords-for-agenda
  12404. (append org-done-keywords-for-agenda org-done-keywords))
  12405. (setq org-todo-keyword-alist-for-agenda
  12406. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12407. (setq org-tag-alist-for-agenda
  12408. (append org-tag-alist-for-agenda org-tag-alist))
  12409. (save-excursion
  12410. (remove-text-properties (point-min) (point-max) pall)
  12411. (when org-agenda-skip-archived-trees
  12412. (goto-char (point-min))
  12413. (while (re-search-forward rea nil t)
  12414. (if (org-on-heading-p t)
  12415. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12416. (goto-char (point-min))
  12417. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12418. (while (re-search-forward re nil t)
  12419. (add-text-properties
  12420. (match-beginning 0) (org-end-of-subtree t) pc)))
  12421. (set-buffer-modified-p bmp)))))
  12422. (setq org-todo-keyword-alist-for-agenda
  12423. (org-uniquify org-todo-keyword-alist-for-agenda)
  12424. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12425. ;;;; Embedded LaTeX
  12426. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12427. "Keymap for the minor `org-cdlatex-mode'.")
  12428. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12429. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12430. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12431. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12432. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12433. (defvar org-cdlatex-texmathp-advice-is-done nil
  12434. "Flag remembering if we have applied the advice to texmathp already.")
  12435. (define-minor-mode org-cdlatex-mode
  12436. "Toggle the minor `org-cdlatex-mode'.
  12437. This mode supports entering LaTeX environment and math in LaTeX fragments
  12438. in Org-mode.
  12439. \\{org-cdlatex-mode-map}"
  12440. nil " OCDL" nil
  12441. (when org-cdlatex-mode (require 'cdlatex))
  12442. (unless org-cdlatex-texmathp-advice-is-done
  12443. (setq org-cdlatex-texmathp-advice-is-done t)
  12444. (defadvice texmathp (around org-math-always-on activate)
  12445. "Always return t in org-mode buffers.
  12446. This is because we want to insert math symbols without dollars even outside
  12447. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12448. an embedded LaTeX fragment, let texmathp do its job.
  12449. \\[org-cdlatex-mode-map]"
  12450. (interactive)
  12451. (let (p)
  12452. (cond
  12453. ((not (org-mode-p)) ad-do-it)
  12454. ((eq this-command 'cdlatex-math-symbol)
  12455. (setq ad-return-value t
  12456. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12457. (t
  12458. (let ((p (org-inside-LaTeX-fragment-p)))
  12459. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12460. (setq ad-return-value t
  12461. texmathp-why '("Org-mode embedded math" . 0))
  12462. (if p ad-do-it)))))))))
  12463. (defun turn-on-org-cdlatex ()
  12464. "Unconditionally turn on `org-cdlatex-mode'."
  12465. (org-cdlatex-mode 1))
  12466. (defun org-inside-LaTeX-fragment-p ()
  12467. "Test if point is inside a LaTeX fragment.
  12468. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12469. sequence appearing also before point.
  12470. Even though the matchers for math are configurable, this function assumes
  12471. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12472. delimiters are skipped when they have been removed by customization.
  12473. The return value is nil, or a cons cell with the delimiter and
  12474. and the position of this delimiter.
  12475. This function does a reasonably good job, but can locally be fooled by
  12476. for example currency specifications. For example it will assume being in
  12477. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12478. fragments that are properly closed, but during editing, we have to live
  12479. with the uncertainty caused by missing closing delimiters. This function
  12480. looks only before point, not after."
  12481. (catch 'exit
  12482. (let ((pos (point))
  12483. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12484. (lim (progn
  12485. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12486. (point)))
  12487. dd-on str (start 0) m re)
  12488. (goto-char pos)
  12489. (when dodollar
  12490. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12491. re (nth 1 (assoc "$" org-latex-regexps)))
  12492. (while (string-match re str start)
  12493. (cond
  12494. ((= (match-end 0) (length str))
  12495. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12496. ((= (match-end 0) (- (length str) 5))
  12497. (throw 'exit nil))
  12498. (t (setq start (match-end 0))))))
  12499. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12500. (goto-char pos)
  12501. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12502. (and (match-beginning 2) (throw 'exit nil))
  12503. ;; count $$
  12504. (while (re-search-backward "\\$\\$" lim t)
  12505. (setq dd-on (not dd-on)))
  12506. (goto-char pos)
  12507. (if dd-on (cons "$$" m))))))
  12508. (defun org-try-cdlatex-tab ()
  12509. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12510. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12511. - inside a LaTeX fragment, or
  12512. - after the first word in a line, where an abbreviation expansion could
  12513. insert a LaTeX environment."
  12514. (when org-cdlatex-mode
  12515. (cond
  12516. ((save-excursion
  12517. (skip-chars-backward "a-zA-Z0-9*")
  12518. (skip-chars-backward " \t")
  12519. (bolp))
  12520. (cdlatex-tab) t)
  12521. ((org-inside-LaTeX-fragment-p)
  12522. (cdlatex-tab) t)
  12523. (t nil))))
  12524. (defun org-cdlatex-underscore-caret (&optional arg)
  12525. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12526. Revert to the normal definition outside of these fragments."
  12527. (interactive "P")
  12528. (if (org-inside-LaTeX-fragment-p)
  12529. (call-interactively 'cdlatex-sub-superscript)
  12530. (let (org-cdlatex-mode)
  12531. (call-interactively (key-binding (vector last-input-event))))))
  12532. (defun org-cdlatex-math-modify (&optional arg)
  12533. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12534. Revert to the normal definition outside of these fragments."
  12535. (interactive "P")
  12536. (if (org-inside-LaTeX-fragment-p)
  12537. (call-interactively 'cdlatex-math-modify)
  12538. (let (org-cdlatex-mode)
  12539. (call-interactively (key-binding (vector last-input-event))))))
  12540. (defvar org-latex-fragment-image-overlays nil
  12541. "List of overlays carrying the images of latex fragments.")
  12542. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12543. (defun org-remove-latex-fragment-image-overlays ()
  12544. "Remove all overlays with LaTeX fragment images in current buffer."
  12545. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12546. (setq org-latex-fragment-image-overlays nil))
  12547. (defun org-preview-latex-fragment (&optional subtree)
  12548. "Preview the LaTeX fragment at point, or all locally or globally.
  12549. If the cursor is in a LaTeX fragment, create the image and overlay
  12550. it over the source code. If there is no fragment at point, display
  12551. all fragments in the current text, from one headline to the next. With
  12552. prefix SUBTREE, display all fragments in the current subtree. With a
  12553. double prefix `C-u C-u', or when the cursor is before the first headline,
  12554. display all fragments in the buffer.
  12555. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12556. (interactive "P")
  12557. (org-remove-latex-fragment-image-overlays)
  12558. (save-excursion
  12559. (save-restriction
  12560. (let (beg end at msg)
  12561. (cond
  12562. ((or (equal subtree '(16))
  12563. (not (save-excursion
  12564. (re-search-backward (concat "^" outline-regexp) nil t))))
  12565. (setq beg (point-min) end (point-max)
  12566. msg "Creating images for buffer...%s"))
  12567. ((equal subtree '(4))
  12568. (org-back-to-heading)
  12569. (setq beg (point) end (org-end-of-subtree t)
  12570. msg "Creating images for subtree...%s"))
  12571. (t
  12572. (if (setq at (org-inside-LaTeX-fragment-p))
  12573. (goto-char (max (point-min) (- (cdr at) 2)))
  12574. (org-back-to-heading))
  12575. (setq beg (point) end (progn (outline-next-heading) (point))
  12576. msg (if at "Creating image...%s"
  12577. "Creating images for entry...%s"))))
  12578. (message msg "")
  12579. (narrow-to-region beg end)
  12580. (goto-char beg)
  12581. (org-format-latex
  12582. (concat "ltxpng/" (file-name-sans-extension
  12583. (file-name-nondirectory
  12584. buffer-file-name)))
  12585. default-directory 'overlays msg at 'forbuffer)
  12586. (message msg "done. Use `C-c C-c' to remove images.")))))
  12587. (defvar org-latex-regexps
  12588. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12589. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12590. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12591. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12592. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12593. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12594. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12595. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12596. "Regular expressions for matching embedded LaTeX.")
  12597. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12598. "Replace LaTeX fragments with links to an image, and produce images."
  12599. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12600. (let* ((prefixnodir (file-name-nondirectory prefix))
  12601. (absprefix (expand-file-name prefix dir))
  12602. (todir (file-name-directory absprefix))
  12603. (opt org-format-latex-options)
  12604. (matchers (plist-get opt :matchers))
  12605. (re-list org-latex-regexps)
  12606. (cnt 0) txt link beg end re e checkdir
  12607. executables-checked
  12608. m n block linkfile movefile ov)
  12609. ;; Check if there are old images files with this prefix, and remove them
  12610. (when (file-directory-p todir)
  12611. (mapc 'delete-file
  12612. (directory-files
  12613. todir 'full
  12614. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12615. ;; Check the different regular expressions
  12616. (while (setq e (pop re-list))
  12617. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12618. block (if (nth 3 e) "\n\n" ""))
  12619. (when (member m matchers)
  12620. (goto-char (point-min))
  12621. (while (re-search-forward re nil t)
  12622. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12623. (not (get-text-property (match-beginning n)
  12624. 'org-protected)))
  12625. (setq txt (match-string n)
  12626. beg (match-beginning n) end (match-end n)
  12627. cnt (1+ cnt)
  12628. linkfile (format "%s_%04d.png" prefix cnt)
  12629. movefile (format "%s_%04d.png" absprefix cnt)
  12630. link (concat block "[[file:" linkfile "]]" block))
  12631. (if msg (message msg cnt))
  12632. (goto-char beg)
  12633. (unless checkdir ; make sure the directory exists
  12634. (setq checkdir t)
  12635. (or (file-directory-p todir) (make-directory todir)))
  12636. (unless executables-checked
  12637. (org-check-external-command
  12638. "latex" "needed to convert LaTeX fragments to images")
  12639. (org-check-external-command
  12640. "dvipng" "needed to convert LaTeX fragments to images")
  12641. (setq executables-checked t))
  12642. (org-create-formula-image
  12643. txt movefile opt forbuffer)
  12644. (if overlays
  12645. (progn
  12646. (setq ov (org-make-overlay beg end))
  12647. (if (featurep 'xemacs)
  12648. (progn
  12649. (org-overlay-put ov 'invisible t)
  12650. (org-overlay-put
  12651. ov 'end-glyph
  12652. (make-glyph (vector 'png :file movefile))))
  12653. (org-overlay-put
  12654. ov 'display
  12655. (list 'image :type 'png :file movefile :ascent 'center)))
  12656. (push ov org-latex-fragment-image-overlays)
  12657. (goto-char end))
  12658. (delete-region beg end)
  12659. (insert link))))))))
  12660. ;; This function borrows from Ganesh Swami's latex2png.el
  12661. (defun org-create-formula-image (string tofile options buffer)
  12662. (let* ((tmpdir (if (featurep 'xemacs)
  12663. (temp-directory)
  12664. temporary-file-directory))
  12665. (texfilebase (make-temp-name
  12666. (expand-file-name "orgtex" tmpdir)))
  12667. (texfile (concat texfilebase ".tex"))
  12668. (dvifile (concat texfilebase ".dvi"))
  12669. (pngfile (concat texfilebase ".png"))
  12670. (fnh (if (featurep 'xemacs)
  12671. (font-height (get-face-font 'default))
  12672. (face-attribute 'default :height nil)))
  12673. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12674. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12675. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12676. "Black"))
  12677. (bg (or (plist-get options (if buffer :background :html-background))
  12678. "Transparent")))
  12679. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12680. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12681. (with-temp-file texfile
  12682. (insert org-format-latex-header
  12683. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12684. (let ((dir default-directory))
  12685. (condition-case nil
  12686. (progn
  12687. (cd tmpdir)
  12688. (call-process "latex" nil nil nil texfile))
  12689. (error nil))
  12690. (cd dir))
  12691. (if (not (file-exists-p dvifile))
  12692. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12693. (condition-case nil
  12694. (call-process "dvipng" nil nil nil
  12695. "-E" "-fg" fg "-bg" bg
  12696. "-D" dpi
  12697. ;;"-x" scale "-y" scale
  12698. "-T" "tight"
  12699. "-o" pngfile
  12700. dvifile)
  12701. (error nil))
  12702. (if (not (file-exists-p pngfile))
  12703. (progn (message "Failed to create png file from %s" texfile) nil)
  12704. ;; Use the requested file name and clean up
  12705. (copy-file pngfile tofile 'replace)
  12706. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12707. (delete-file (concat texfilebase e)))
  12708. pngfile))))
  12709. (defun org-dvipng-color (attr)
  12710. "Return an rgb color specification for dvipng."
  12711. (apply 'format "rgb %s %s %s"
  12712. (mapcar 'org-normalize-color
  12713. (color-values (face-attribute 'default attr nil)))))
  12714. (defun org-normalize-color (value)
  12715. "Return string to be used as color value for an RGB component."
  12716. (format "%g" (/ value 65535.0)))
  12717. ;;;; Key bindings
  12718. ;; Make `C-c C-x' a prefix key
  12719. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12720. ;; TAB key with modifiers
  12721. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12722. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12723. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12724. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12725. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12726. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12727. ;; The following line is necessary under Suse GNU/Linux
  12728. (unless (featurep 'xemacs)
  12729. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12730. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12731. (define-key org-mode-map [backtab] 'org-shifttab)
  12732. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12733. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12734. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12735. ;; Cursor keys with modifiers
  12736. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12737. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12738. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12739. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12740. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12741. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12742. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12743. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12744. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12745. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12746. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12747. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12748. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12749. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12750. ;;; Extra keys for tty access.
  12751. ;; We only set them when really needed because otherwise the
  12752. ;; menus don't show the simple keys
  12753. (when (or org-use-extra-keys
  12754. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12755. (not window-system))
  12756. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12757. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12758. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12759. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12760. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12761. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12762. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12763. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12764. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12765. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12766. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12767. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12768. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12769. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12770. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12771. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12772. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12773. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12774. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12775. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12776. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12777. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12778. ;; All the other keys
  12779. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12780. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12781. (if (boundp 'narrow-map)
  12782. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12783. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12784. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12785. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12786. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12787. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12788. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12789. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12790. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12791. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12792. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12793. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12794. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12795. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12796. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12797. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12798. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12799. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12800. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12801. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12802. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12803. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12804. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12805. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12806. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12807. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12808. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12809. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12810. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12811. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12812. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12813. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12814. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12815. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12816. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12817. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12818. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12819. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12820. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12821. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12822. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12823. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12824. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12825. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12826. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12827. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12828. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12829. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12830. (org-defkey org-mode-map "\C-m" 'org-return)
  12831. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12832. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12833. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12834. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12835. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12836. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12837. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12838. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12839. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12840. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12841. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12842. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12843. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12844. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12845. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12846. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12847. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12848. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12849. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12850. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12851. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12852. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12853. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12854. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12855. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12856. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12857. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12858. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12859. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12860. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12861. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12862. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12863. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12864. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12865. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12866. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12867. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12868. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12869. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12870. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  12871. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12872. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12873. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12874. (when (featurep 'xemacs)
  12875. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12876. (defvar org-self-insert-command-undo-counter 0)
  12877. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12878. (defun org-self-insert-command (N)
  12879. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12880. If the cursor is in a table looking at whitespace, the whitespace is
  12881. overwritten, and the table is not marked as requiring realignment."
  12882. (interactive "p")
  12883. (if (and
  12884. (org-table-p)
  12885. (progn
  12886. ;; check if we blank the field, and if that triggers align
  12887. (and (featurep 'org-table) org-table-auto-blank-field
  12888. (member last-command
  12889. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12890. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12891. ;; got extra space, this field does not determine column width
  12892. (let (org-table-may-need-update) (org-table-blank-field))
  12893. ;; no extra space, this field may determine column width
  12894. (org-table-blank-field)))
  12895. t)
  12896. (eq N 1)
  12897. (looking-at "[^|\n]* |"))
  12898. (let (org-table-may-need-update)
  12899. (goto-char (1- (match-end 0)))
  12900. (delete-backward-char 1)
  12901. (goto-char (match-beginning 0))
  12902. (self-insert-command N))
  12903. (setq org-table-may-need-update t)
  12904. (self-insert-command N)
  12905. (org-fix-tags-on-the-fly)
  12906. (if org-self-insert-cluster-for-undo
  12907. (if (not (eq last-command 'org-self-insert-command))
  12908. (setq org-self-insert-command-undo-counter 1)
  12909. (if (>= org-self-insert-command-undo-counter 20)
  12910. (setq org-self-insert-command-undo-counter 1)
  12911. (and (> org-self-insert-command-undo-counter 0)
  12912. buffer-undo-list
  12913. (not (cadr buffer-undo-list)) ; remove nil entry
  12914. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12915. (setq org-self-insert-command-undo-counter
  12916. (1+ org-self-insert-command-undo-counter)))))))
  12917. (defun org-fix-tags-on-the-fly ()
  12918. (when (and (equal (char-after (point-at-bol)) ?*)
  12919. (org-on-heading-p))
  12920. (org-align-tags-here org-tags-column)))
  12921. (defun org-delete-backward-char (N)
  12922. "Like `delete-backward-char', insert whitespace at field end in tables.
  12923. When deleting backwards, in tables this function will insert whitespace in
  12924. front of the next \"|\" separator, to keep the table aligned. The table will
  12925. still be marked for re-alignment if the field did fill the entire column,
  12926. because, in this case the deletion might narrow the column."
  12927. (interactive "p")
  12928. (if (and (org-table-p)
  12929. (eq N 1)
  12930. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12931. (looking-at ".*?|"))
  12932. (let ((pos (point))
  12933. (noalign (looking-at "[^|\n\r]* |"))
  12934. (c org-table-may-need-update))
  12935. (backward-delete-char N)
  12936. (skip-chars-forward "^|")
  12937. (insert " ")
  12938. (goto-char (1- pos))
  12939. ;; noalign: if there were two spaces at the end, this field
  12940. ;; does not determine the width of the column.
  12941. (if noalign (setq org-table-may-need-update c)))
  12942. (backward-delete-char N)
  12943. (org-fix-tags-on-the-fly)))
  12944. (defun org-delete-char (N)
  12945. "Like `delete-char', but insert whitespace at field end in tables.
  12946. When deleting characters, in tables this function will insert whitespace in
  12947. front of the next \"|\" separator, to keep the table aligned. The table will
  12948. still be marked for re-alignment if the field did fill the entire column,
  12949. because, in this case the deletion might narrow the column."
  12950. (interactive "p")
  12951. (if (and (org-table-p)
  12952. (not (bolp))
  12953. (not (= (char-after) ?|))
  12954. (eq N 1))
  12955. (if (looking-at ".*?|")
  12956. (let ((pos (point))
  12957. (noalign (looking-at "[^|\n\r]* |"))
  12958. (c org-table-may-need-update))
  12959. (replace-match (concat
  12960. (substring (match-string 0) 1 -1)
  12961. " |"))
  12962. (goto-char pos)
  12963. ;; noalign: if there were two spaces at the end, this field
  12964. ;; does not determine the width of the column.
  12965. (if noalign (setq org-table-may-need-update c)))
  12966. (delete-char N))
  12967. (delete-char N)
  12968. (org-fix-tags-on-the-fly)))
  12969. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12970. (put 'org-self-insert-command 'delete-selection t)
  12971. (put 'orgtbl-self-insert-command 'delete-selection t)
  12972. (put 'org-delete-char 'delete-selection 'supersede)
  12973. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12974. (put 'org-yank 'delete-selection 'yank)
  12975. ;; Make `flyspell-mode' delay after some commands
  12976. (put 'org-self-insert-command 'flyspell-delayed t)
  12977. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12978. (put 'org-delete-char 'flyspell-delayed t)
  12979. (put 'org-delete-backward-char 'flyspell-delayed t)
  12980. ;; Make pabbrev-mode expand after org-mode commands
  12981. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12982. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12983. ;; How to do this: Measure non-white length of current string
  12984. ;; If equal to column width, we should realign.
  12985. (defun org-remap (map &rest commands)
  12986. "In MAP, remap the functions given in COMMANDS.
  12987. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12988. (let (new old)
  12989. (while commands
  12990. (setq old (pop commands) new (pop commands))
  12991. (if (fboundp 'command-remapping)
  12992. (org-defkey map (vector 'remap old) new)
  12993. (substitute-key-definition old new map global-map)))))
  12994. (when (eq org-enable-table-editor 'optimized)
  12995. ;; If the user wants maximum table support, we need to hijack
  12996. ;; some standard editing functions
  12997. (org-remap org-mode-map
  12998. 'self-insert-command 'org-self-insert-command
  12999. 'delete-char 'org-delete-char
  13000. 'delete-backward-char 'org-delete-backward-char)
  13001. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13002. (defvar org-ctrl-c-ctrl-c-hook nil
  13003. "Hook for functions attaching themselves to `C-c C-c'.
  13004. This can be used to add additional functionality to the C-c C-c key which
  13005. executes context-dependent commands.
  13006. Each function will be called with no arguments. The function must check
  13007. if the context is appropriate for it to act. If yes, it should do its
  13008. thing and then return a non-nil value. If the context is wrong,
  13009. just do nothing and return nil.")
  13010. (defvar org-tab-first-hook nil
  13011. "Hook for functions to attach themselves to TAB.
  13012. See `org-ctrl-c-ctrl-c-hook' for more information.
  13013. This hook runs as the first action when TAB is pressed, even before
  13014. `org-cycle' messes around with the `outline-regexp' to cater for
  13015. inline tasks and plain list item folding.
  13016. If any function in this hook returns t, not other actions like table
  13017. field motion visibility cycling will be done.")
  13018. (defvar org-tab-after-check-for-table-hook nil
  13019. "Hook for functions to attach themselves to TAB.
  13020. See `org-ctrl-c-ctrl-c-hook' for more information.
  13021. This hook runs after it has been established that the cursor is not in a
  13022. table, but before checking if the cursor is in a headline or if global cycling
  13023. should be done.
  13024. If any function in this hook returns t, not other actions like visibility
  13025. cycling will be done.")
  13026. (defvar org-tab-after-check-for-cycling-hook nil
  13027. "Hook for functions to attach themselves to TAB.
  13028. See `org-ctrl-c-ctrl-c-hook' for more information.
  13029. This hook runs after it has been established that not table field motion and
  13030. not visibility should be done because of current context. This is probably
  13031. the place where a package like yasnippets can hook in.")
  13032. (defvar org-metaleft-hook nil
  13033. "Hook for functions attaching themselves to `M-left'.
  13034. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13035. (defvar org-metaright-hook nil
  13036. "Hook for functions attaching themselves to `M-right'.
  13037. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13038. (defvar org-metaup-hook nil
  13039. "Hook for functions attaching themselves to `M-up'.
  13040. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13041. (defvar org-metadown-hook nil
  13042. "Hook for functions attaching themselves to `M-down'.
  13043. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13044. (defvar org-shiftmetaleft-hook nil
  13045. "Hook for functions attaching themselves to `M-S-left'.
  13046. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13047. (defvar org-shiftmetaright-hook nil
  13048. "Hook for functions attaching themselves to `M-S-right'.
  13049. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13050. (defvar org-shiftmetaup-hook nil
  13051. "Hook for functions attaching themselves to `M-S-up'.
  13052. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13053. (defvar org-shiftmetadown-hook nil
  13054. "Hook for functions attaching themselves to `M-S-down'.
  13055. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13056. (defvar org-metareturn-hook nil
  13057. "Hook for functions attaching themselves to `M-RET'.
  13058. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13059. (defun org-modifier-cursor-error ()
  13060. "Throw an error, a modified cursor command was applied in wrong context."
  13061. (error "This command is active in special context like tables, headlines or items"))
  13062. (defun org-shiftselect-error ()
  13063. "Throw an error because Shift-Cursor command was applied in wrong context."
  13064. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13065. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13066. (error "This command works only in special context like headlines or timestamps.")))
  13067. (defun org-call-for-shift-select (cmd)
  13068. (let ((this-command-keys-shift-translated t))
  13069. (call-interactively cmd)))
  13070. (defun org-shifttab (&optional arg)
  13071. "Global visibility cycling or move to previous table field.
  13072. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13073. on context.
  13074. See the individual commands for more information."
  13075. (interactive "P")
  13076. (cond
  13077. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13078. ((integerp arg)
  13079. (message "Content view to level: %d" arg)
  13080. (org-content (prefix-numeric-value arg))
  13081. (setq org-cycle-global-status 'overview))
  13082. (t (call-interactively 'org-global-cycle))))
  13083. (defun org-shiftmetaleft ()
  13084. "Promote subtree or delete table column.
  13085. Calls `org-promote-subtree', `org-outdent-item',
  13086. or `org-table-delete-column', depending on context.
  13087. See the individual commands for more information."
  13088. (interactive)
  13089. (cond
  13090. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13091. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13092. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13093. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13094. (t (org-modifier-cursor-error))))
  13095. (defun org-shiftmetaright ()
  13096. "Demote subtree or insert table column.
  13097. Calls `org-demote-subtree', `org-indent-item',
  13098. or `org-table-insert-column', depending on context.
  13099. See the individual commands for more information."
  13100. (interactive)
  13101. (cond
  13102. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13103. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13104. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13105. ((org-at-item-p) (call-interactively 'org-indent-item))
  13106. (t (org-modifier-cursor-error))))
  13107. (defun org-shiftmetaup (&optional arg)
  13108. "Move subtree up or kill table row.
  13109. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13110. `org-move-item-up' depending on context. See the individual commands
  13111. for more information."
  13112. (interactive "P")
  13113. (cond
  13114. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13115. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13116. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13117. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13118. (t (org-modifier-cursor-error))))
  13119. (defun org-shiftmetadown (&optional arg)
  13120. "Move subtree down or insert table row.
  13121. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13122. `org-move-item-down', depending on context. See the individual
  13123. commands for more information."
  13124. (interactive "P")
  13125. (cond
  13126. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13127. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13128. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13129. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13130. (t (org-modifier-cursor-error))))
  13131. (defun org-metaleft (&optional arg)
  13132. "Promote heading or move table column to left.
  13133. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13134. With no specific context, calls the Emacs default `backward-word'.
  13135. See the individual commands for more information."
  13136. (interactive "P")
  13137. (cond
  13138. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13139. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13140. ((or (org-on-heading-p)
  13141. (and (org-region-active-p)
  13142. (save-excursion
  13143. (goto-char (region-beginning))
  13144. (org-on-heading-p))))
  13145. (call-interactively 'org-do-promote))
  13146. ((or (org-at-item-p)
  13147. (and (org-region-active-p)
  13148. (save-excursion
  13149. (goto-char (region-beginning))
  13150. (org-at-item-p))))
  13151. (call-interactively 'org-outdent-item))
  13152. (t (call-interactively 'backward-word))))
  13153. (defun org-metaright (&optional arg)
  13154. "Demote subtree or move table column to right.
  13155. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13156. With no specific context, calls the Emacs default `forward-word'.
  13157. See the individual commands for more information."
  13158. (interactive "P")
  13159. (cond
  13160. ((run-hook-with-args-until-success 'org-metaright-hook))
  13161. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13162. ((or (org-on-heading-p)
  13163. (and (org-region-active-p)
  13164. (save-excursion
  13165. (goto-char (region-beginning))
  13166. (org-on-heading-p))))
  13167. (call-interactively 'org-do-demote))
  13168. ((or (org-at-item-p)
  13169. (and (org-region-active-p)
  13170. (save-excursion
  13171. (goto-char (region-beginning))
  13172. (org-at-item-p))))
  13173. (call-interactively 'org-indent-item))
  13174. (t (call-interactively 'forward-word))))
  13175. (defun org-metaup (&optional arg)
  13176. "Move subtree up or move table row up.
  13177. Calls `org-move-subtree-up' or `org-table-move-row' or
  13178. `org-move-item-up', depending on context. See the individual commands
  13179. for more information."
  13180. (interactive "P")
  13181. (cond
  13182. ((run-hook-with-args-until-success 'org-metaup-hook))
  13183. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13184. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13185. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13186. (t (transpose-lines 1) (beginning-of-line -1))))
  13187. (defun org-metadown (&optional arg)
  13188. "Move subtree down or move table row down.
  13189. Calls `org-move-subtree-down' or `org-table-move-row' or
  13190. `org-move-item-down', depending on context. See the individual
  13191. commands for more information."
  13192. (interactive "P")
  13193. (cond
  13194. ((run-hook-with-args-until-success 'org-metadown-hook))
  13195. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13196. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13197. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13198. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13199. (defun org-shiftup (&optional arg)
  13200. "Increase item in timestamp or increase priority of current headline.
  13201. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13202. depending on context. See the individual commands for more information."
  13203. (interactive "P")
  13204. (cond
  13205. ((and org-support-shift-select (org-region-active-p))
  13206. (org-call-for-shift-select 'previous-line))
  13207. ((org-at-timestamp-p t)
  13208. (call-interactively (if org-edit-timestamp-down-means-later
  13209. 'org-timestamp-down 'org-timestamp-up)))
  13210. ((and (not (eq org-support-shift-select 'always))
  13211. org-enable-priority-commands
  13212. (org-on-heading-p))
  13213. (call-interactively 'org-priority-up))
  13214. ((and (not org-support-shift-select) (org-at-item-p))
  13215. (call-interactively 'org-previous-item))
  13216. ((org-clocktable-try-shift 'up arg))
  13217. (org-support-shift-select
  13218. (org-call-for-shift-select 'previous-line))
  13219. (t (org-shiftselect-error))))
  13220. (defun org-shiftdown (&optional arg)
  13221. "Decrease item in timestamp or decrease priority of current headline.
  13222. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13223. depending on context. See the individual commands for more information."
  13224. (interactive "P")
  13225. (cond
  13226. ((and org-support-shift-select (org-region-active-p))
  13227. (org-call-for-shift-select 'next-line))
  13228. ((org-at-timestamp-p t)
  13229. (call-interactively (if org-edit-timestamp-down-means-later
  13230. 'org-timestamp-up 'org-timestamp-down)))
  13231. ((and (not (eq org-support-shift-select 'always))
  13232. org-enable-priority-commands
  13233. (org-on-heading-p))
  13234. (call-interactively 'org-priority-down))
  13235. ((and (not org-support-shift-select) (org-at-item-p))
  13236. (call-interactively 'org-next-item))
  13237. ((org-clocktable-try-shift 'down arg))
  13238. (org-support-shift-select
  13239. (org-call-for-shift-select 'next-line))
  13240. (t (org-shiftselect-error))))
  13241. (defun org-shiftright (&optional arg)
  13242. "Cycle the thing at point or in the current line, depending on context.
  13243. Depending on context, this does one of the following:
  13244. - switch a timestamp at point one day into the future
  13245. - on a headline, switch to the next TODO keyword.
  13246. - on an item, switch entire list to the next bullet type
  13247. - on a property line, switch to the next allowed value
  13248. - on a clocktable definition line, move time block into the future"
  13249. (interactive "P")
  13250. (cond
  13251. ((and org-support-shift-select (org-region-active-p))
  13252. (org-call-for-shift-select 'forward-char))
  13253. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13254. ((and (not (eq org-support-shift-select 'always))
  13255. (org-on-heading-p))
  13256. (org-call-with-arg 'org-todo 'right))
  13257. ((or (and org-support-shift-select
  13258. (not (eq org-support-shift-select 'always))
  13259. (org-at-item-bullet-p))
  13260. (and (not org-support-shift-select) (org-at-item-p)))
  13261. (org-call-with-arg 'org-cycle-list-bullet nil))
  13262. ((and (not (eq org-support-shift-select 'always))
  13263. (org-at-property-p))
  13264. (call-interactively 'org-property-next-allowed-value))
  13265. ((org-clocktable-try-shift 'right arg))
  13266. (org-support-shift-select
  13267. (org-call-for-shift-select 'forward-char))
  13268. (t (org-shiftselect-error))))
  13269. (defun org-shiftleft (&optional arg)
  13270. "Cycle the thing at point or in the current line, depending on context.
  13271. Depending on context, this does one of the following:
  13272. - switch a timestamp at point one day into the past
  13273. - on a headline, switch to the previous TODO keyword.
  13274. - on an item, switch entire list to the previous bullet type
  13275. - on a property line, switch to the previous allowed value
  13276. - on a clocktable definition line, move time block into the past"
  13277. (interactive "P")
  13278. (cond
  13279. ((and org-support-shift-select (org-region-active-p))
  13280. (org-call-for-shift-select 'backward-char))
  13281. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13282. ((and (not (eq org-support-shift-select 'always))
  13283. (org-on-heading-p))
  13284. (org-call-with-arg 'org-todo 'left))
  13285. ((or (and org-support-shift-select
  13286. (not (eq org-support-shift-select 'always))
  13287. (org-at-item-bullet-p))
  13288. (and (not org-support-shift-select) (org-at-item-p)))
  13289. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13290. ((and (not (eq org-support-shift-select 'always))
  13291. (org-at-property-p))
  13292. (call-interactively 'org-property-previous-allowed-value))
  13293. ((org-clocktable-try-shift 'left arg))
  13294. (org-support-shift-select
  13295. (org-call-for-shift-select 'backward-char))
  13296. (t (org-shiftselect-error))))
  13297. (defun org-shiftcontrolright ()
  13298. "Switch to next TODO set."
  13299. (interactive)
  13300. (cond
  13301. ((and org-support-shift-select (org-region-active-p))
  13302. (org-call-for-shift-select 'forward-word))
  13303. ((and (not (eq org-support-shift-select 'always))
  13304. (org-on-heading-p))
  13305. (org-call-with-arg 'org-todo 'nextset))
  13306. (org-support-shift-select
  13307. (org-call-for-shift-select 'forward-word))
  13308. (t (org-shiftselect-error))))
  13309. (defun org-shiftcontrolleft ()
  13310. "Switch to previous TODO set."
  13311. (interactive)
  13312. (cond
  13313. ((and org-support-shift-select (org-region-active-p))
  13314. (org-call-for-shift-select 'backward-word))
  13315. ((and (not (eq org-support-shift-select 'always))
  13316. (org-on-heading-p))
  13317. (org-call-with-arg 'org-todo 'previousset))
  13318. (org-support-shift-select
  13319. (org-call-for-shift-select 'backward-word))
  13320. (t (org-shiftselect-error))))
  13321. (defun org-ctrl-c-ret ()
  13322. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13323. (interactive)
  13324. (cond
  13325. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13326. (t (call-interactively 'org-insert-heading))))
  13327. (defun org-copy-special ()
  13328. "Copy region in table or copy current subtree.
  13329. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13330. See the individual commands for more information."
  13331. (interactive)
  13332. (call-interactively
  13333. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13334. (defun org-cut-special ()
  13335. "Cut region in table or cut current subtree.
  13336. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13337. See the individual commands for more information."
  13338. (interactive)
  13339. (call-interactively
  13340. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13341. (defun org-paste-special (arg)
  13342. "Paste rectangular region into table, or past subtree relative to level.
  13343. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13344. See the individual commands for more information."
  13345. (interactive "P")
  13346. (if (org-at-table-p)
  13347. (org-table-paste-rectangle)
  13348. (org-paste-subtree arg)))
  13349. (defun org-edit-special ()
  13350. "Call a special editor for the stuff at point.
  13351. When at a table, call the formula editor with `org-table-edit-formulas'.
  13352. When at the first line of an src example, call `org-edit-src-code'.
  13353. When in an #+include line, visit the include file. Otherwise call
  13354. `ffap' to visit the file at point."
  13355. (interactive)
  13356. (cond
  13357. ((org-at-table-p)
  13358. (call-interactively 'org-table-edit-formulas))
  13359. ((save-excursion
  13360. (beginning-of-line 1)
  13361. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13362. (find-file (org-trim (match-string 1))))
  13363. ((org-edit-src-code))
  13364. ((org-edit-fixed-width-region))
  13365. (t (call-interactively 'ffap))))
  13366. (defun org-ctrl-c-ctrl-c (&optional arg)
  13367. "Set tags in headline, or update according to changed information at point.
  13368. This command does many different things, depending on context:
  13369. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13370. this is what we do.
  13371. - If the cursor is in a headline, prompt for tags and insert them
  13372. into the current line, aligned to `org-tags-column'. When called
  13373. with prefix arg, realign all tags in the current buffer.
  13374. - If the cursor is in one of the special #+KEYWORD lines, this
  13375. triggers scanning the buffer for these lines and updating the
  13376. information.
  13377. - If the cursor is inside a table, realign the table. This command
  13378. works even if the automatic table editor has been turned off.
  13379. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13380. the entire table.
  13381. - If the cursor is at a footnote reference or definition, jump to
  13382. the corresponding definition or references, respectively.
  13383. - If the cursor is a the beginning of a dynamic block, update it.
  13384. - If the cursor is inside a table created by the table.el package,
  13385. activate that table.
  13386. - If the current buffer is a remember buffer, close note and file
  13387. it. A prefix argument of 1 files to the default location
  13388. without further interaction. A prefix argument of 2 files to
  13389. the currently clocking task.
  13390. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13391. links in this buffer.
  13392. - If the cursor is on a numbered item in a plain list, renumber the
  13393. ordered list.
  13394. - If the cursor is on a checkbox, toggle it."
  13395. (interactive "P")
  13396. (let ((org-enable-table-editor t))
  13397. (cond
  13398. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13399. org-occur-highlights
  13400. org-latex-fragment-image-overlays)
  13401. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13402. (org-remove-occur-highlights)
  13403. (org-remove-latex-fragment-image-overlays)
  13404. (message "Temporary highlights/overlays removed from current buffer"))
  13405. ((and (local-variable-p 'org-finish-function (current-buffer))
  13406. (fboundp org-finish-function))
  13407. (funcall org-finish-function))
  13408. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13409. ((org-at-property-p)
  13410. (call-interactively 'org-property-action))
  13411. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13412. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13413. ((org-at-table.el-p)
  13414. (require 'table)
  13415. (beginning-of-line 1)
  13416. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13417. (call-interactively 'table-recognize-table))
  13418. ((org-at-table-p)
  13419. (org-table-maybe-eval-formula)
  13420. (if arg
  13421. (call-interactively 'org-table-recalculate)
  13422. (org-table-maybe-recalculate-line))
  13423. (call-interactively 'org-table-align))
  13424. ((or (org-footnote-at-reference-p)
  13425. (org-footnote-at-definition-p))
  13426. (call-interactively 'org-footnote-action))
  13427. ((org-at-item-checkbox-p)
  13428. (call-interactively 'org-toggle-checkbox))
  13429. ((org-at-item-p)
  13430. (if arg
  13431. (call-interactively 'org-toggle-checkbox)
  13432. (call-interactively 'org-maybe-renumber-ordered-list)))
  13433. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13434. ;; Dynamic block
  13435. (beginning-of-line 1)
  13436. (save-excursion (org-update-dblock)))
  13437. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13438. (cond
  13439. ((equal (match-string 1) "TBLFM")
  13440. ;; Recalculate the table before this line
  13441. (save-excursion
  13442. (beginning-of-line 1)
  13443. (skip-chars-backward " \r\n\t")
  13444. (if (org-at-table-p)
  13445. (org-call-with-arg 'org-table-recalculate t))))
  13446. (t
  13447. ; (org-set-regexps-and-options)
  13448. ; (org-restart-font-lock)
  13449. (let ((org-inhibit-startup t)) (org-mode-restart))
  13450. (message "Local setup has been refreshed"))))
  13451. (t (error "C-c C-c can do nothing useful at this location.")))))
  13452. (defun org-mode-restart ()
  13453. "Restart Org-mode, to scan again for special lines.
  13454. Also updates the keyword regular expressions."
  13455. (interactive)
  13456. (org-mode)
  13457. (message "Org-mode restarted"))
  13458. (defun org-kill-note-or-show-branches ()
  13459. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13460. (interactive)
  13461. (if (not org-finish-function)
  13462. (call-interactively 'show-branches)
  13463. (let ((org-note-abort t))
  13464. (funcall org-finish-function))))
  13465. (defun org-return (&optional indent)
  13466. "Goto next table row or insert a newline.
  13467. Calls `org-table-next-row' or `newline', depending on context.
  13468. See the individual commands for more information."
  13469. (interactive)
  13470. (cond
  13471. ((bobp) (if indent (newline-and-indent) (newline)))
  13472. ((org-at-table-p)
  13473. (org-table-justify-field-maybe)
  13474. (call-interactively 'org-table-next-row))
  13475. ((and org-return-follows-link
  13476. (eq (get-text-property (point) 'face) 'org-link))
  13477. (call-interactively 'org-open-at-point))
  13478. ((and (org-at-heading-p)
  13479. (looking-at
  13480. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13481. (org-show-entry)
  13482. (end-of-line 1)
  13483. (newline))
  13484. (t (if indent (newline-and-indent) (newline)))))
  13485. (defun org-return-indent ()
  13486. "Goto next table row or insert a newline and indent.
  13487. Calls `org-table-next-row' or `newline-and-indent', depending on
  13488. context. See the individual commands for more information."
  13489. (interactive)
  13490. (org-return t))
  13491. (defun org-ctrl-c-star ()
  13492. "Compute table, or change heading status of lines.
  13493. Calls `org-table-recalculate' or `org-toggle-heading',
  13494. depending on context."
  13495. (interactive)
  13496. (cond
  13497. ((org-at-table-p)
  13498. (call-interactively 'org-table-recalculate))
  13499. (t
  13500. ;; Convert all lines in region to list items
  13501. (call-interactively 'org-toggle-heading))))
  13502. (defun org-ctrl-c-minus ()
  13503. "Insert separator line in table or modify bullet status of line.
  13504. Also turns a plain line or a region of lines into list items.
  13505. Calls `org-table-insert-hline', `org-toggle-item', or
  13506. `org-cycle-list-bullet', depending on context."
  13507. (interactive)
  13508. (cond
  13509. ((org-at-table-p)
  13510. (call-interactively 'org-table-insert-hline))
  13511. ((org-region-active-p)
  13512. (call-interactively 'org-toggle-item))
  13513. ((org-in-item-p)
  13514. (call-interactively 'org-cycle-list-bullet))
  13515. (t
  13516. (call-interactively 'org-toggle-item))))
  13517. (defun org-toggle-item ()
  13518. "Convert headings or normal lines to items, items to normal lines.
  13519. If there is no active region, only the current line is considered.
  13520. If the first line in the region is a headline, convert all headlines to items.
  13521. If the first line in the region is an item, convert all items to normal lines.
  13522. If the first line is normal text, add an item bullet to each line."
  13523. (interactive)
  13524. (let (l2 l beg end)
  13525. (if (org-region-active-p)
  13526. (setq beg (region-beginning) end (region-end))
  13527. (setq beg (point-at-bol)
  13528. end (min (1+ (point-at-eol)) (point-max))))
  13529. (save-excursion
  13530. (goto-char end)
  13531. (setq l2 (org-current-line))
  13532. (goto-char beg)
  13533. (beginning-of-line 1)
  13534. (setq l (1- (org-current-line)))
  13535. (if (org-at-item-p)
  13536. ;; We already have items, de-itemize
  13537. (while (< (setq l (1+ l)) l2)
  13538. (when (org-at-item-p)
  13539. (goto-char (match-beginning 2))
  13540. (delete-region (match-beginning 2) (match-end 2))
  13541. (and (looking-at "[ \t]+") (replace-match "")))
  13542. (beginning-of-line 2))
  13543. (if (org-on-heading-p)
  13544. ;; Headings, convert to items
  13545. (while (< (setq l (1+ l)) l2)
  13546. (if (looking-at org-outline-regexp)
  13547. (replace-match "- " t t))
  13548. (beginning-of-line 2))
  13549. ;; normal lines, turn them into items
  13550. (while (< (setq l (1+ l)) l2)
  13551. (unless (org-at-item-p)
  13552. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13553. (replace-match "\\1- \\2")))
  13554. (beginning-of-line 2)))))))
  13555. (defun org-toggle-heading (&optional nstars)
  13556. "Convert headings to normal text, or items or text to headings.
  13557. If there is no active region, only the current line is considered.
  13558. If the first line is a heading, remove the stars from all headlines
  13559. in the region.
  13560. If the first line is a plain list item, turn all plain list items into
  13561. headings.
  13562. If the first line is a normal line, turn each and every line in the region
  13563. into a heading.
  13564. When converting a line into a heading, the number of stars is chosen
  13565. such that the lines become children of the current entry. However, when
  13566. a prefix argument is given, its value determines the number of stars to add."
  13567. (interactive "P")
  13568. (let (l2 l itemp beg end)
  13569. (if (org-region-active-p)
  13570. (setq beg (region-beginning) end (region-end))
  13571. (setq beg (point-at-bol)
  13572. end (min (1+ (point-at-eol)) (point-max))))
  13573. (save-excursion
  13574. (goto-char end)
  13575. (setq l2 (org-current-line))
  13576. (goto-char beg)
  13577. (beginning-of-line 1)
  13578. (setq l (1- (org-current-line)))
  13579. (if (org-on-heading-p)
  13580. ;; We already have headlines, de-star them
  13581. (while (< (setq l (1+ l)) l2)
  13582. (when (org-on-heading-p t)
  13583. (and (looking-at outline-regexp) (replace-match "")))
  13584. (beginning-of-line 2))
  13585. (setq itemp (org-at-item-p))
  13586. (let* ((stars
  13587. (if nstars
  13588. (make-string (prefix-numeric-value current-prefix-arg)
  13589. ?*)
  13590. (save-excursion
  13591. (re-search-backward org-complex-heading-regexp nil t)
  13592. (or (match-string 1) ""))))
  13593. (add-stars (cond (nstars "")
  13594. ((equal stars "") "*")
  13595. (org-odd-levels-only "**")
  13596. (t "*")))
  13597. (rpl (concat stars add-stars " ")))
  13598. (while (< (setq l (1+ l)) l2)
  13599. (if itemp
  13600. (and (org-at-item-p) (replace-match rpl t t))
  13601. (unless (org-on-heading-p)
  13602. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13603. (replace-match (concat rpl (match-string 2))))))
  13604. (beginning-of-line 2)))))))
  13605. (defun org-meta-return (&optional arg)
  13606. "Insert a new heading or wrap a region in a table.
  13607. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13608. See the individual commands for more information."
  13609. (interactive "P")
  13610. (cond
  13611. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13612. ((org-at-table-p)
  13613. (call-interactively 'org-table-wrap-region))
  13614. (t (call-interactively 'org-insert-heading))))
  13615. ;;; Menu entries
  13616. ;; Define the Org-mode menus
  13617. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13618. '("Tbl"
  13619. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13620. ["Next Field" org-cycle (org-at-table-p)]
  13621. ["Previous Field" org-shifttab (org-at-table-p)]
  13622. ["Next Row" org-return (org-at-table-p)]
  13623. "--"
  13624. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13625. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13626. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13627. "--"
  13628. ("Column"
  13629. ["Move Column Left" org-metaleft (org-at-table-p)]
  13630. ["Move Column Right" org-metaright (org-at-table-p)]
  13631. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13632. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13633. ("Row"
  13634. ["Move Row Up" org-metaup (org-at-table-p)]
  13635. ["Move Row Down" org-metadown (org-at-table-p)]
  13636. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13637. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13638. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13639. "--"
  13640. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13641. ("Rectangle"
  13642. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13643. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13644. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13645. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13646. "--"
  13647. ("Calculate"
  13648. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13649. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13650. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13651. "--"
  13652. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13653. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13654. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13655. "--"
  13656. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13657. "--"
  13658. ["Sum Column/Rectangle" org-table-sum
  13659. (or (org-at-table-p) (org-region-active-p))]
  13660. ["Which Column?" org-table-current-column (org-at-table-p)])
  13661. ["Debug Formulas"
  13662. org-table-toggle-formula-debugger
  13663. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13664. ["Show Col/Row Numbers"
  13665. org-table-toggle-coordinate-overlays
  13666. :style toggle
  13667. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13668. "--"
  13669. ["Create" org-table-create (and (not (org-at-table-p))
  13670. org-enable-table-editor)]
  13671. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13672. ["Import from File" org-table-import (not (org-at-table-p))]
  13673. ["Export to File" org-table-export (org-at-table-p)]
  13674. "--"
  13675. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13676. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13677. '("Org"
  13678. ("Show/Hide"
  13679. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13680. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13681. ["Sparse Tree..." org-sparse-tree t]
  13682. ["Reveal Context" org-reveal t]
  13683. ["Show All" show-all t]
  13684. "--"
  13685. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13686. "--"
  13687. ["New Heading" org-insert-heading t]
  13688. ("Navigate Headings"
  13689. ["Up" outline-up-heading t]
  13690. ["Next" outline-next-visible-heading t]
  13691. ["Previous" outline-previous-visible-heading t]
  13692. ["Next Same Level" outline-forward-same-level t]
  13693. ["Previous Same Level" outline-backward-same-level t]
  13694. "--"
  13695. ["Jump" org-goto t])
  13696. ("Edit Structure"
  13697. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13698. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13699. "--"
  13700. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13701. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13702. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13703. "--"
  13704. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13705. "--"
  13706. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13707. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13708. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13709. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13710. "--"
  13711. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13712. "--"
  13713. ["Convert to odd levels" org-convert-to-odd-levels t]
  13714. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13715. ("Editing"
  13716. ["Emphasis..." org-emphasize t]
  13717. ["Edit Source Example" org-edit-special t]
  13718. "--"
  13719. ["Footnote new/jump" org-footnote-action t]
  13720. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13721. ("Archive"
  13722. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13723. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13724. ; :active t :keys "C-u C-c C-x C-a"]
  13725. ["Sparse trees open ARCHIVE trees"
  13726. (setq org-sparse-tree-open-archived-trees
  13727. (not org-sparse-tree-open-archived-trees))
  13728. :style toggle :selected org-sparse-tree-open-archived-trees]
  13729. ["Cycling opens ARCHIVE trees"
  13730. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13731. :style toggle :selected org-cycle-open-archived-trees]
  13732. "--"
  13733. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13734. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13735. ; ["Check and Move Children" (org-archive-subtree '(4))
  13736. ; :active t :keys "C-u C-c C-x C-s"]
  13737. )
  13738. "--"
  13739. ("Hyperlinks"
  13740. ["Store Link (Global)" org-store-link t]
  13741. ["Insert Link" org-insert-link t]
  13742. ["Follow Link" org-open-at-point t]
  13743. "--"
  13744. ["Next link" org-next-link t]
  13745. ["Previous link" org-previous-link t]
  13746. "--"
  13747. ["Descriptive Links"
  13748. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13749. :style radio
  13750. :selected (member '(org-link) buffer-invisibility-spec)]
  13751. ["Literal Links"
  13752. (progn
  13753. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13754. :style radio
  13755. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13756. "--"
  13757. ("TODO Lists"
  13758. ["TODO/DONE/-" org-todo t]
  13759. ("Select keyword"
  13760. ["Next keyword" org-shiftright (org-on-heading-p)]
  13761. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13762. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13763. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13764. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13765. ["Show TODO Tree" org-show-todo-tree t]
  13766. ["Global TODO list" org-todo-list t]
  13767. "--"
  13768. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13769. :selected org-enforce-todo-dependencies :style toggle :active t]
  13770. "Settings for tree at point"
  13771. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13772. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13773. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13774. ["Do Children parallel" org-toggle-ordered-property :style radio
  13775. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13776. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13777. "--"
  13778. ["Set Priority" org-priority t]
  13779. ["Priority Up" org-shiftup t]
  13780. ["Priority Down" org-shiftdown t]
  13781. "--"
  13782. ["Get news from all feeds" org-feed-update-all t]
  13783. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13784. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13785. ("TAGS and Properties"
  13786. ["Set Tags" org-set-tags-command t]
  13787. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13788. "--"
  13789. ["Set property" org-set-property t]
  13790. ["Column view of properties" org-columns t]
  13791. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13792. ("Dates and Scheduling"
  13793. ["Timestamp" org-time-stamp t]
  13794. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13795. ("Change Date"
  13796. ["1 Day Later" org-shiftright t]
  13797. ["1 Day Earlier" org-shiftleft t]
  13798. ["1 ... Later" org-shiftup t]
  13799. ["1 ... Earlier" org-shiftdown t])
  13800. ["Compute Time Range" org-evaluate-time-range t]
  13801. ["Schedule Item" org-schedule t]
  13802. ["Deadline" org-deadline t]
  13803. "--"
  13804. ["Custom time format" org-toggle-time-stamp-overlays
  13805. :style radio :selected org-display-custom-times]
  13806. "--"
  13807. ["Goto Calendar" org-goto-calendar t]
  13808. ["Date from Calendar" org-date-from-calendar t]
  13809. "--"
  13810. ["Start/Restart Timer" org-timer-start t]
  13811. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13812. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13813. ["Insert Timer String" org-timer t]
  13814. ["Insert Timer Item" org-timer-item t])
  13815. ("Logging work"
  13816. ["Clock in" org-clock-in t]
  13817. ["Clock out" org-clock-out t]
  13818. ["Clock cancel" org-clock-cancel t]
  13819. ["Goto running clock" org-clock-goto t]
  13820. ["Display times" org-clock-display t]
  13821. ["Create clock table" org-clock-report t]
  13822. "--"
  13823. ["Record DONE time"
  13824. (progn (setq org-log-done (not org-log-done))
  13825. (message "Switching to %s will %s record a timestamp"
  13826. (car org-done-keywords)
  13827. (if org-log-done "automatically" "not")))
  13828. :style toggle :selected org-log-done])
  13829. "--"
  13830. ["Agenda Command..." org-agenda t]
  13831. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13832. ("File List for Agenda")
  13833. ("Special views current file"
  13834. ["TODO Tree" org-show-todo-tree t]
  13835. ["Check Deadlines" org-check-deadlines t]
  13836. ["Timeline" org-timeline t]
  13837. ["Tags/Property tree" org-match-sparse-tree t])
  13838. "--"
  13839. ["Export/Publish..." org-export t]
  13840. ("LaTeX"
  13841. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13842. :selected org-cdlatex-mode]
  13843. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13844. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13845. ["Modify math symbol" org-cdlatex-math-modify
  13846. (org-inside-LaTeX-fragment-p)]
  13847. ["Insert citation" org-reftex-citation t]
  13848. "--"
  13849. ["Export LaTeX fragments as images"
  13850. (if (featurep 'org-exp)
  13851. (setq org-export-with-LaTeX-fragments
  13852. (not org-export-with-LaTeX-fragments))
  13853. (require 'org-exp))
  13854. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13855. org-export-with-LaTeX-fragments)])
  13856. "--"
  13857. ("Documentation"
  13858. ["Show Version" org-version t]
  13859. ["Info Documentation" org-info t])
  13860. ("Customize"
  13861. ["Browse Org Group" org-customize t]
  13862. "--"
  13863. ["Expand This Menu" org-create-customize-menu
  13864. (fboundp 'customize-menu-create)])
  13865. "--"
  13866. ("Refresh/Reload"
  13867. ["Refresh setup current buffer" org-mode-restart t]
  13868. ["Reload Org (after update)" org-reload t]
  13869. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13870. ))
  13871. (defun org-info (&optional node)
  13872. "Read documentation for Org-mode in the info system.
  13873. With optional NODE, go directly to that node."
  13874. (interactive)
  13875. (info (format "(org)%s" (or node ""))))
  13876. (defun org-install-agenda-files-menu ()
  13877. (let ((bl (buffer-list)))
  13878. (save-excursion
  13879. (while bl
  13880. (set-buffer (pop bl))
  13881. (if (org-mode-p) (setq bl nil)))
  13882. (when (org-mode-p)
  13883. (easy-menu-change
  13884. '("Org") "File List for Agenda"
  13885. (append
  13886. (list
  13887. ["Edit File List" (org-edit-agenda-file-list) t]
  13888. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13889. ["Remove Current File from List" org-remove-file t]
  13890. ["Cycle through agenda files" org-cycle-agenda-files t]
  13891. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13892. "--")
  13893. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13894. ;;;; Documentation
  13895. ;;;###autoload
  13896. (defun org-require-autoloaded-modules ()
  13897. (interactive)
  13898. (mapc 'require
  13899. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13900. org-docbook org-exp org-html org-icalendar
  13901. org-id org-latex
  13902. org-publish org-remember org-table
  13903. org-timer org-xoxo)))
  13904. ;;;###autoload
  13905. (defun org-reload (&optional uncompiled)
  13906. "Reload all org lisp files.
  13907. With prefix arg UNCOMPILED, load the uncompiled versions."
  13908. (interactive "P")
  13909. (require 'find-func)
  13910. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13911. (dir-org (file-name-directory (org-find-library-name "org")))
  13912. (dir-org-contrib (ignore-errors
  13913. (file-name-directory
  13914. (org-find-library-name "org-contribdir"))))
  13915. (files
  13916. (append (directory-files dir-org t file-re)
  13917. (and dir-org-contrib
  13918. (directory-files dir-org-contrib t file-re))))
  13919. (remove-re (concat (if (featurep 'xemacs)
  13920. "org-colview" "org-colview-xemacs")
  13921. "\\'")))
  13922. (setq files (mapcar 'file-name-sans-extension files))
  13923. (setq files (mapcar
  13924. (lambda (x) (if (string-match remove-re x) nil x))
  13925. files))
  13926. (setq files (delq nil files))
  13927. (mapc
  13928. (lambda (f)
  13929. (when (featurep (intern (file-name-nondirectory f)))
  13930. (if (and (not uncompiled)
  13931. (file-exists-p (concat f ".elc")))
  13932. (load (concat f ".elc") nil nil t)
  13933. (load (concat f ".el") nil nil t))))
  13934. files))
  13935. (org-version))
  13936. ;;;###autoload
  13937. (defun org-customize ()
  13938. "Call the customize function with org as argument."
  13939. (interactive)
  13940. (org-load-modules-maybe)
  13941. (org-require-autoloaded-modules)
  13942. (customize-browse 'org))
  13943. (defun org-create-customize-menu ()
  13944. "Create a full customization menu for Org-mode, insert it into the menu."
  13945. (interactive)
  13946. (org-load-modules-maybe)
  13947. (org-require-autoloaded-modules)
  13948. (if (fboundp 'customize-menu-create)
  13949. (progn
  13950. (easy-menu-change
  13951. '("Org") "Customize"
  13952. `(["Browse Org group" org-customize t]
  13953. "--"
  13954. ,(customize-menu-create 'org)
  13955. ["Set" Custom-set t]
  13956. ["Save" Custom-save t]
  13957. ["Reset to Current" Custom-reset-current t]
  13958. ["Reset to Saved" Custom-reset-saved t]
  13959. ["Reset to Standard Settings" Custom-reset-standard t]))
  13960. (message "\"Org\"-menu now contains full customization menu"))
  13961. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13962. ;;;; Miscellaneous stuff
  13963. ;;; Generally useful functions
  13964. (defun org-find-text-property-in-string (prop s)
  13965. "Return the first non-nil value of property PROP in string S."
  13966. (or (get-text-property 0 prop s)
  13967. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13968. prop s)))
  13969. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13970. "Display the given MESSAGE as a warning."
  13971. (if (fboundp 'display-warning)
  13972. (display-warning 'org message
  13973. (if (featurep 'xemacs)
  13974. 'warning
  13975. :warning))
  13976. (let ((buf (get-buffer-create "*Org warnings*")))
  13977. (with-current-buffer buf
  13978. (goto-char (point-max))
  13979. (insert "Warning (Org): " message)
  13980. (unless (bolp)
  13981. (newline)))
  13982. (display-buffer buf)
  13983. (sit-for 0))))
  13984. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13985. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13986. (if (and marker (marker-buffer marker)
  13987. (buffer-live-p (marker-buffer marker)))
  13988. (progn
  13989. (switch-to-buffer (marker-buffer marker))
  13990. (if (or (> marker (point-max)) (< marker (point-min)))
  13991. (widen))
  13992. (goto-char marker)
  13993. (org-show-context 'org-goto))
  13994. (if bookmark
  13995. (bookmark-jump bookmark)
  13996. (error "Cannot find location"))))
  13997. (defun org-quote-csv-field (s)
  13998. "Quote field for inclusion in CSV material."
  13999. (if (string-match "[\",]" s)
  14000. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14001. s))
  14002. (defun org-plist-delete (plist property)
  14003. "Delete PROPERTY from PLIST.
  14004. This is in contrast to merely setting it to 0."
  14005. (let (p)
  14006. (while plist
  14007. (if (not (eq property (car plist)))
  14008. (setq p (plist-put p (car plist) (nth 1 plist))))
  14009. (setq plist (cddr plist)))
  14010. p))
  14011. (defun org-force-self-insert (N)
  14012. "Needed to enforce self-insert under remapping."
  14013. (interactive "p")
  14014. (self-insert-command N))
  14015. (defun org-string-width (s)
  14016. "Compute width of string, ignoring invisible characters.
  14017. This ignores character with invisibility property `org-link', and also
  14018. characters with property `org-cwidth', because these will become invisible
  14019. upon the next fontification round."
  14020. (let (b l)
  14021. (when (or (eq t buffer-invisibility-spec)
  14022. (assq 'org-link buffer-invisibility-spec))
  14023. (while (setq b (text-property-any 0 (length s)
  14024. 'invisible 'org-link s))
  14025. (setq s (concat (substring s 0 b)
  14026. (substring s (or (next-single-property-change
  14027. b 'invisible s) (length s)))))))
  14028. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14029. (setq s (concat (substring s 0 b)
  14030. (substring s (or (next-single-property-change
  14031. b 'org-cwidth s) (length s))))))
  14032. (setq l (string-width s) b -1)
  14033. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14034. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14035. l))
  14036. (defun org-get-indentation (&optional line)
  14037. "Get the indentation of the current line, interpreting tabs.
  14038. When LINE is given, assume it represents a line and compute its indentation."
  14039. (if line
  14040. (if (string-match "^ *" (org-remove-tabs line))
  14041. (match-end 0))
  14042. (save-excursion
  14043. (beginning-of-line 1)
  14044. (skip-chars-forward " \t")
  14045. (current-column))))
  14046. (defun org-remove-tabs (s &optional width)
  14047. "Replace tabulators in S with spaces.
  14048. Assumes that s is a single line, starting in column 0."
  14049. (setq width (or width tab-width))
  14050. (while (string-match "\t" s)
  14051. (setq s (replace-match
  14052. (make-string
  14053. (- (* width (/ (+ (match-beginning 0) width) width))
  14054. (match-beginning 0)) ?\ )
  14055. t t s)))
  14056. s)
  14057. (defun org-fix-indentation (line ind)
  14058. "Fix indentation in LINE.
  14059. IND is a cons cell with target and minimum indentation.
  14060. If the current indentation in LINE is smaller than the minimum,
  14061. leave it alone. If it is larger than ind, set it to the target."
  14062. (let* ((l (org-remove-tabs line))
  14063. (i (org-get-indentation l))
  14064. (i1 (car ind)) (i2 (cdr ind)))
  14065. (if (>= i i2) (setq l (substring line i2)))
  14066. (if (> i1 0)
  14067. (concat (make-string i1 ?\ ) l)
  14068. l)))
  14069. (defun org-base-buffer (buffer)
  14070. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14071. (if (not buffer)
  14072. buffer
  14073. (or (buffer-base-buffer buffer)
  14074. buffer)))
  14075. (defun org-trim (s)
  14076. "Remove whitespace at beginning and end of string."
  14077. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14078. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14079. s)
  14080. (defun org-wrap (string &optional width lines)
  14081. "Wrap string to either a number of lines, or a width in characters.
  14082. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14083. that costs. If there is a word longer than WIDTH, the text is actually
  14084. wrapped to the length of that word.
  14085. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14086. many lines, whatever width that takes.
  14087. The return value is a list of lines, without newlines at the end."
  14088. (let* ((words (org-split-string string "[ \t\n]+"))
  14089. (maxword (apply 'max (mapcar 'org-string-width words)))
  14090. w ll)
  14091. (cond (width
  14092. (org-do-wrap words (max maxword width)))
  14093. (lines
  14094. (setq w maxword)
  14095. (setq ll (org-do-wrap words maxword))
  14096. (if (<= (length ll) lines)
  14097. ll
  14098. (setq ll words)
  14099. (while (> (length ll) lines)
  14100. (setq w (1+ w))
  14101. (setq ll (org-do-wrap words w)))
  14102. ll))
  14103. (t (error "Cannot wrap this")))))
  14104. (defun org-do-wrap (words width)
  14105. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14106. (let (lines line)
  14107. (while words
  14108. (setq line (pop words))
  14109. (while (and words (< (+ (length line) (length (car words))) width))
  14110. (setq line (concat line " " (pop words))))
  14111. (setq lines (push line lines)))
  14112. (nreverse lines)))
  14113. (defun org-split-string (string &optional separators)
  14114. "Splits STRING into substrings at SEPARATORS.
  14115. No empty strings are returned if there are matches at the beginning
  14116. and end of string."
  14117. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14118. (start 0)
  14119. notfirst
  14120. (list nil))
  14121. (while (and (string-match rexp string
  14122. (if (and notfirst
  14123. (= start (match-beginning 0))
  14124. (< start (length string)))
  14125. (1+ start) start))
  14126. (< (match-beginning 0) (length string)))
  14127. (setq notfirst t)
  14128. (or (eq (match-beginning 0) 0)
  14129. (and (eq (match-beginning 0) (match-end 0))
  14130. (eq (match-beginning 0) start))
  14131. (setq list
  14132. (cons (substring string start (match-beginning 0))
  14133. list)))
  14134. (setq start (match-end 0)))
  14135. (or (eq start (length string))
  14136. (setq list
  14137. (cons (substring string start)
  14138. list)))
  14139. (nreverse list)))
  14140. (defun org-uuidgen-p (s)
  14141. "Is S an ID created by UUIDGEN?"
  14142. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14143. (defun org-context ()
  14144. "Return a list of contexts of the current cursor position.
  14145. If several contexts apply, all are returned.
  14146. Each context entry is a list with a symbol naming the context, and
  14147. two positions indicating start and end of the context. Possible
  14148. contexts are:
  14149. :headline anywhere in a headline
  14150. :headline-stars on the leading stars in a headline
  14151. :todo-keyword on a TODO keyword (including DONE) in a headline
  14152. :tags on the TAGS in a headline
  14153. :priority on the priority cookie in a headline
  14154. :item on the first line of a plain list item
  14155. :item-bullet on the bullet/number of a plain list item
  14156. :checkbox on the checkbox in a plain list item
  14157. :table in an org-mode table
  14158. :table-special on a special filed in a table
  14159. :table-table in a table.el table
  14160. :link on a hyperlink
  14161. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14162. :target on a <<target>>
  14163. :radio-target on a <<<radio-target>>>
  14164. :latex-fragment on a LaTeX fragment
  14165. :latex-preview on a LaTeX fragment with overlayed preview image
  14166. This function expects the position to be visible because it uses font-lock
  14167. faces as a help to recognize the following contexts: :table-special, :link,
  14168. and :keyword."
  14169. (let* ((f (get-text-property (point) 'face))
  14170. (faces (if (listp f) f (list f)))
  14171. (p (point)) clist o)
  14172. ;; First the large context
  14173. (cond
  14174. ((org-on-heading-p t)
  14175. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14176. (when (progn
  14177. (beginning-of-line 1)
  14178. (looking-at org-todo-line-tags-regexp))
  14179. (push (org-point-in-group p 1 :headline-stars) clist)
  14180. (push (org-point-in-group p 2 :todo-keyword) clist)
  14181. (push (org-point-in-group p 4 :tags) clist))
  14182. (goto-char p)
  14183. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14184. (if (looking-at "\\[#[A-Z0-9]\\]")
  14185. (push (org-point-in-group p 0 :priority) clist)))
  14186. ((org-at-item-p)
  14187. (push (org-point-in-group p 2 :item-bullet) clist)
  14188. (push (list :item (point-at-bol)
  14189. (save-excursion (org-end-of-item) (point)))
  14190. clist)
  14191. (and (org-at-item-checkbox-p)
  14192. (push (org-point-in-group p 0 :checkbox) clist)))
  14193. ((org-at-table-p)
  14194. (push (list :table (org-table-begin) (org-table-end)) clist)
  14195. (if (memq 'org-formula faces)
  14196. (push (list :table-special
  14197. (previous-single-property-change p 'face)
  14198. (next-single-property-change p 'face)) clist)))
  14199. ((org-at-table-p 'any)
  14200. (push (list :table-table) clist)))
  14201. (goto-char p)
  14202. ;; Now the small context
  14203. (cond
  14204. ((org-at-timestamp-p)
  14205. (push (org-point-in-group p 0 :timestamp) clist))
  14206. ((memq 'org-link faces)
  14207. (push (list :link
  14208. (previous-single-property-change p 'face)
  14209. (next-single-property-change p 'face)) clist))
  14210. ((memq 'org-special-keyword faces)
  14211. (push (list :keyword
  14212. (previous-single-property-change p 'face)
  14213. (next-single-property-change p 'face)) clist))
  14214. ((org-on-target-p)
  14215. (push (org-point-in-group p 0 :target) clist)
  14216. (goto-char (1- (match-beginning 0)))
  14217. (if (looking-at org-radio-target-regexp)
  14218. (push (org-point-in-group p 0 :radio-target) clist))
  14219. (goto-char p))
  14220. ((setq o (car (delq nil
  14221. (mapcar
  14222. (lambda (x)
  14223. (if (memq x org-latex-fragment-image-overlays) x))
  14224. (org-overlays-at (point))))))
  14225. (push (list :latex-fragment
  14226. (org-overlay-start o) (org-overlay-end o)) clist)
  14227. (push (list :latex-preview
  14228. (org-overlay-start o) (org-overlay-end o)) clist))
  14229. ((org-inside-LaTeX-fragment-p)
  14230. ;; FIXME: positions wrong.
  14231. (push (list :latex-fragment (point) (point)) clist)))
  14232. (setq clist (nreverse (delq nil clist)))
  14233. clist))
  14234. ;; FIXME: Compare with at-regexp-p Do we need both?
  14235. (defun org-in-regexp (re &optional nlines visually)
  14236. "Check if point is inside a match of regexp.
  14237. Normally only the current line is checked, but you can include NLINES extra
  14238. lines both before and after point into the search.
  14239. If VISUALLY is set, require that the cursor is not after the match but
  14240. really on, so that the block visually is on the match."
  14241. (catch 'exit
  14242. (let ((pos (point))
  14243. (eol (point-at-eol (+ 1 (or nlines 0))))
  14244. (inc (if visually 1 0)))
  14245. (save-excursion
  14246. (beginning-of-line (- 1 (or nlines 0)))
  14247. (while (re-search-forward re eol t)
  14248. (if (and (<= (match-beginning 0) pos)
  14249. (>= (+ inc (match-end 0)) pos))
  14250. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14251. (defun org-at-regexp-p (regexp)
  14252. "Is point inside a match of REGEXP in the current line?"
  14253. (catch 'exit
  14254. (save-excursion
  14255. (let ((pos (point)) (end (point-at-eol)))
  14256. (beginning-of-line 1)
  14257. (while (re-search-forward regexp end t)
  14258. (if (and (<= (match-beginning 0) pos)
  14259. (>= (match-end 0) pos))
  14260. (throw 'exit t)))
  14261. nil))))
  14262. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14263. "Call `multi-occur' with buffers for all agenda files."
  14264. (interactive "sOrg-files matching: \np")
  14265. (let* ((files (org-agenda-files))
  14266. (tnames (mapcar 'file-truename files))
  14267. (extra org-agenda-text-search-extra-files)
  14268. f)
  14269. (when (eq (car extra) 'agenda-archives)
  14270. (setq extra (cdr extra))
  14271. (setq files (org-add-archive-files files)))
  14272. (while (setq f (pop extra))
  14273. (unless (member (file-truename f) tnames)
  14274. (add-to-list 'files f 'append)
  14275. (add-to-list 'tnames (file-truename f) 'append)))
  14276. (multi-occur
  14277. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14278. regexp)))
  14279. (if (boundp 'occur-mode-find-occurrence-hook)
  14280. ;; Emacs 23
  14281. (add-hook 'occur-mode-find-occurrence-hook
  14282. (lambda ()
  14283. (when (org-mode-p)
  14284. (org-reveal))))
  14285. ;; Emacs 22
  14286. (defadvice occur-mode-goto-occurrence
  14287. (after org-occur-reveal activate)
  14288. (and (org-mode-p) (org-reveal)))
  14289. (defadvice occur-mode-goto-occurrence-other-window
  14290. (after org-occur-reveal activate)
  14291. (and (org-mode-p) (org-reveal)))
  14292. (defadvice occur-mode-display-occurrence
  14293. (after org-occur-reveal activate)
  14294. (when (org-mode-p)
  14295. (let ((pos (occur-mode-find-occurrence)))
  14296. (with-current-buffer (marker-buffer pos)
  14297. (save-excursion
  14298. (goto-char pos)
  14299. (org-reveal)))))))
  14300. (defun org-uniquify (list)
  14301. "Remove duplicate elements from LIST."
  14302. (let (res)
  14303. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14304. res))
  14305. (defun org-delete-all (elts list)
  14306. "Remove all elements in ELTS from LIST."
  14307. (while elts
  14308. (setq list (delete (pop elts) list)))
  14309. list)
  14310. (defun org-back-over-empty-lines ()
  14311. "Move backwards over whitespace, to the beginning of the first empty line.
  14312. Returns the number of empty lines passed."
  14313. (let ((pos (point)))
  14314. (skip-chars-backward " \t\n\r")
  14315. (beginning-of-line 2)
  14316. (goto-char (min (point) pos))
  14317. (count-lines (point) pos)))
  14318. (defun org-skip-whitespace ()
  14319. (skip-chars-forward " \t\n\r"))
  14320. (defun org-point-in-group (point group &optional context)
  14321. "Check if POINT is in match-group GROUP.
  14322. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14323. match. If the match group does ot exist or point is not inside it,
  14324. return nil."
  14325. (and (match-beginning group)
  14326. (>= point (match-beginning group))
  14327. (<= point (match-end group))
  14328. (if context
  14329. (list context (match-beginning group) (match-end group))
  14330. t)))
  14331. (defun org-switch-to-buffer-other-window (&rest args)
  14332. "Switch to buffer in a second window on the current frame.
  14333. In particular, do not allow pop-up frames."
  14334. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14335. special-display-function)
  14336. (apply 'switch-to-buffer-other-window args)))
  14337. (defun org-combine-plists (&rest plists)
  14338. "Create a single property list from all plists in PLISTS.
  14339. The process starts by copying the first list, and then setting properties
  14340. from the other lists. Settings in the last list are the most significant
  14341. ones and overrule settings in the other lists."
  14342. (let ((rtn (copy-sequence (pop plists)))
  14343. p v ls)
  14344. (while plists
  14345. (setq ls (pop plists))
  14346. (while ls
  14347. (setq p (pop ls) v (pop ls))
  14348. (setq rtn (plist-put rtn p v))))
  14349. rtn))
  14350. (defun org-move-line-down (arg)
  14351. "Move the current line down. With prefix argument, move it past ARG lines."
  14352. (interactive "p")
  14353. (let ((col (current-column))
  14354. beg end pos)
  14355. (beginning-of-line 1) (setq beg (point))
  14356. (beginning-of-line 2) (setq end (point))
  14357. (beginning-of-line (+ 1 arg))
  14358. (setq pos (move-marker (make-marker) (point)))
  14359. (insert (delete-and-extract-region beg end))
  14360. (goto-char pos)
  14361. (org-move-to-column col)))
  14362. (defun org-move-line-up (arg)
  14363. "Move the current line up. With prefix argument, move it past ARG lines."
  14364. (interactive "p")
  14365. (let ((col (current-column))
  14366. beg end pos)
  14367. (beginning-of-line 1) (setq beg (point))
  14368. (beginning-of-line 2) (setq end (point))
  14369. (beginning-of-line (- arg))
  14370. (setq pos (move-marker (make-marker) (point)))
  14371. (insert (delete-and-extract-region beg end))
  14372. (goto-char pos)
  14373. (org-move-to-column col)))
  14374. (defun org-replace-escapes (string table)
  14375. "Replace %-escapes in STRING with values in TABLE.
  14376. TABLE is an association list with keys like \"%a\" and string values.
  14377. The sequences in STRING may contain normal field width and padding information,
  14378. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14379. so values can contain further %-escapes if they are define later in TABLE."
  14380. (let ((case-fold-search nil)
  14381. e re rpl)
  14382. (while (setq e (pop table))
  14383. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14384. (while (string-match re string)
  14385. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14386. (cdr e)))
  14387. (setq string (replace-match rpl t t string))))
  14388. string))
  14389. (defun org-sublist (list start end)
  14390. "Return a section of LIST, from START to END.
  14391. Counting starts at 1."
  14392. (let (rtn (c start))
  14393. (setq list (nthcdr (1- start) list))
  14394. (while (and list (<= c end))
  14395. (push (pop list) rtn)
  14396. (setq c (1+ c)))
  14397. (nreverse rtn)))
  14398. (defun org-find-base-buffer-visiting (file)
  14399. "Like `find-buffer-visiting' but always return the base buffer and
  14400. not an indirect buffer."
  14401. (let ((buf (or (get-file-buffer file)
  14402. (find-buffer-visiting file))))
  14403. (if buf
  14404. (or (buffer-base-buffer buf) buf)
  14405. nil)))
  14406. (defun org-image-file-name-regexp (&optional extensions)
  14407. "Return regexp matching the file names of images.
  14408. If EXTENSIONS is given, only match these."
  14409. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14410. (image-file-name-regexp)
  14411. (let ((image-file-name-extensions
  14412. (or extensions
  14413. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14414. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14415. (concat "\\."
  14416. (regexp-opt (nconc (mapcar 'upcase
  14417. image-file-name-extensions)
  14418. image-file-name-extensions)
  14419. t)
  14420. "\\'"))))
  14421. (defun org-file-image-p (file &optional extensions)
  14422. "Return non-nil if FILE is an image."
  14423. (save-match-data
  14424. (string-match (org-image-file-name-regexp extensions) file)))
  14425. (defun org-get-cursor-date ()
  14426. "Return the date at cursor in as a time.
  14427. This works in the calendar and in the agenda, anywhere else it just
  14428. returns the current time."
  14429. (let (date day defd)
  14430. (cond
  14431. ((eq major-mode 'calendar-mode)
  14432. (setq date (calendar-cursor-to-date)
  14433. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14434. ((eq major-mode 'org-agenda-mode)
  14435. (setq day (get-text-property (point) 'day))
  14436. (if day
  14437. (setq date (calendar-gregorian-from-absolute day)
  14438. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14439. (nth 2 date))))))
  14440. (or defd (current-time))))
  14441. (defvar org-agenda-action-marker (make-marker)
  14442. "Marker pointing to the entry for the next agenda action.")
  14443. (defun org-mark-entry-for-agenda-action ()
  14444. "Mark the current entry as target of an agenda action.
  14445. Agenda actions are actions executed from the agenda with the key `k',
  14446. which make use of the date at the cursor."
  14447. (interactive)
  14448. (move-marker org-agenda-action-marker
  14449. (save-excursion (org-back-to-heading t) (point))
  14450. (current-buffer))
  14451. (message
  14452. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14453. ;;; Paragraph filling stuff.
  14454. ;; We want this to be just right, so use the full arsenal.
  14455. (defun org-indent-line-function ()
  14456. "Indent line like previous, but further if previous was headline or item."
  14457. (interactive)
  14458. (let* ((pos (point))
  14459. (itemp (org-at-item-p))
  14460. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14461. column bpos bcol tpos tcol bullet btype bullet-type)
  14462. ;; Find the previous relevant line
  14463. (beginning-of-line 1)
  14464. (cond
  14465. ((looking-at "#") (setq column 0))
  14466. ((looking-at "\\*+ ") (setq column 0))
  14467. ((and (looking-at "[ \t]*:END:")
  14468. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14469. (save-excursion
  14470. (goto-char (1- (match-beginning 1)))
  14471. (setq column (current-column))))
  14472. (t
  14473. (beginning-of-line 0)
  14474. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14475. (not (looking-at "[ \t]*:END:"))
  14476. (not (looking-at org-drawer-regexp)))
  14477. (beginning-of-line 0))
  14478. (cond
  14479. ((looking-at "\\*+[ \t]+")
  14480. (if (not org-adapt-indentation)
  14481. (setq column 0)
  14482. (goto-char (match-end 0))
  14483. (setq column (current-column))))
  14484. ((looking-at org-drawer-regexp)
  14485. (goto-char (1- (match-beginning 1)))
  14486. (setq column (current-column)))
  14487. ((looking-at "\\([ \t]*\\):END:")
  14488. (goto-char (match-end 1))
  14489. (setq column (current-column)))
  14490. ((org-in-item-p)
  14491. (org-beginning-of-item)
  14492. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14493. (setq bpos (match-beginning 1) tpos (match-end 0)
  14494. bcol (progn (goto-char bpos) (current-column))
  14495. tcol (progn (goto-char tpos) (current-column))
  14496. bullet (match-string 1)
  14497. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14498. (if (> tcol (+ bcol org-description-max-indent))
  14499. (setq tcol (+ bcol 5)))
  14500. (if (not itemp)
  14501. (setq column tcol)
  14502. (goto-char pos)
  14503. (beginning-of-line 1)
  14504. (if (looking-at "\\S-")
  14505. (progn
  14506. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14507. (setq bullet (match-string 1)
  14508. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14509. (setq column (if (equal btype bullet-type) bcol tcol)))
  14510. (setq column (org-get-indentation)))))
  14511. (t (setq column (org-get-indentation))))))
  14512. (goto-char pos)
  14513. (if (<= (current-column) (current-indentation))
  14514. (org-indent-line-to column)
  14515. (save-excursion (org-indent-line-to column)))
  14516. (setq column (current-column))
  14517. (beginning-of-line 1)
  14518. (if (looking-at
  14519. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14520. (replace-match (concat "\\1" (format org-property-format
  14521. (match-string 2) (match-string 3)))
  14522. t nil))
  14523. (org-move-to-column column)))
  14524. (defun org-set-autofill-regexps ()
  14525. (interactive)
  14526. ;; In the paragraph separator we include headlines, because filling
  14527. ;; text in a line directly attached to a headline would otherwise
  14528. ;; fill the headline as well.
  14529. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14530. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14531. ;; The paragraph starter includes hand-formatted lists.
  14532. (org-set-local 'paragraph-start
  14533. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14534. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14535. ;; But only if the user has not turned off tables or fixed-width regions
  14536. (org-set-local
  14537. 'auto-fill-inhibit-regexp
  14538. (concat "\\*+ \\|#\\+"
  14539. "\\|[ \t]*" org-keyword-time-regexp
  14540. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14541. (concat
  14542. "\\|[ \t]*["
  14543. (if org-enable-table-editor "|" "")
  14544. (if org-enable-fixed-width-editor ":" "")
  14545. "]"))))
  14546. ;; We use our own fill-paragraph function, to make sure that tables
  14547. ;; and fixed-width regions are not wrapped. That function will pass
  14548. ;; through to `fill-paragraph' when appropriate.
  14549. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14550. ; Adaptive filling: To get full control, first make sure that
  14551. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14552. (org-set-local 'adaptive-fill-regexp "\000")
  14553. (org-set-local 'adaptive-fill-function
  14554. 'org-adaptive-fill-function)
  14555. (org-set-local
  14556. 'align-mode-rules-list
  14557. '((org-in-buffer-settings
  14558. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14559. (modes . '(org-mode))))))
  14560. (defun org-fill-paragraph (&optional justify)
  14561. "Re-align a table, pass through to fill-paragraph if no table."
  14562. (let ((table-p (org-at-table-p))
  14563. (table.el-p (org-at-table.el-p)))
  14564. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14565. (save-excursion (goto-char (point-at-bol))
  14566. (looking-at outline-regexp)))
  14567. t) ; skip headlines
  14568. (table.el-p t) ; skip table.el tables
  14569. (table-p (org-table-align) t) ; align org-mode tables
  14570. (t nil)))) ; call paragraph-fill
  14571. ;; For reference, this is the default value of adaptive-fill-regexp
  14572. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14573. (defun org-adaptive-fill-function ()
  14574. "Return a fill prefix for org-mode files.
  14575. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14576. work correctly."
  14577. (cond ((looking-at "#[ \t]+")
  14578. (match-string 0))
  14579. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14580. (save-excursion
  14581. (if (> (match-end 1) (+ (match-beginning 1)
  14582. org-description-max-indent))
  14583. (goto-char (+ (match-beginning 1) 5))
  14584. (goto-char (match-end 0)))
  14585. (make-string (current-column) ?\ )))
  14586. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14587. (save-excursion
  14588. (goto-char (match-end 0))
  14589. (make-string (current-column) ?\ )))
  14590. (t nil)))
  14591. ;;; Other stuff.
  14592. (defun org-toggle-fixed-width-section (arg)
  14593. "Toggle the fixed-width export.
  14594. If there is no active region, the QUOTE keyword at the current headline is
  14595. inserted or removed. When present, it causes the text between this headline
  14596. and the next to be exported as fixed-width text, and unmodified.
  14597. If there is an active region, this command adds or removes a colon as the
  14598. first character of this line. If the first character of a line is a colon,
  14599. this line is also exported in fixed-width font."
  14600. (interactive "P")
  14601. (let* ((cc 0)
  14602. (regionp (org-region-active-p))
  14603. (beg (if regionp (region-beginning) (point)))
  14604. (end (if regionp (region-end)))
  14605. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14606. (case-fold-search nil)
  14607. (re "[ \t]*\\(: \\)")
  14608. off)
  14609. (if regionp
  14610. (save-excursion
  14611. (goto-char beg)
  14612. (setq cc (current-column))
  14613. (beginning-of-line 1)
  14614. (setq off (looking-at re))
  14615. (while (> nlines 0)
  14616. (setq nlines (1- nlines))
  14617. (beginning-of-line 1)
  14618. (cond
  14619. (arg
  14620. (org-move-to-column cc t)
  14621. (insert ": \n")
  14622. (forward-line -1))
  14623. ((and off (looking-at re))
  14624. (replace-match "" t t nil 1))
  14625. ((not off) (org-move-to-column cc t) (insert ": ")))
  14626. (forward-line 1)))
  14627. (save-excursion
  14628. (org-back-to-heading)
  14629. (if (looking-at (concat outline-regexp
  14630. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14631. (replace-match "" t t nil 1)
  14632. (if (looking-at outline-regexp)
  14633. (progn
  14634. (goto-char (match-end 0))
  14635. (insert org-quote-string " "))))))))
  14636. (defun org-reftex-citation ()
  14637. "Use reftex-citation to insert a citation into the buffer.
  14638. This looks for a line like
  14639. #+BIBLIOGRAPHY: foo plain option:-d
  14640. and derives from it that foo.bib is the bbliography file relevant
  14641. for this document. It then installs the necessary environment for RefTeX
  14642. to work in this buffer and calls `reftex-citation' to insert a citation
  14643. into the buffer.
  14644. Export of such citations to both LaTeX and HTML is handled by the contributed
  14645. package org-exp-bibtex by Taru Karttunen."
  14646. (interactive)
  14647. (let ((reftex-docstruct-symbol 'rds)
  14648. (reftex-cite-format "\\cite{%l}")
  14649. rds bib)
  14650. (save-excursion
  14651. (save-restriction
  14652. (widen)
  14653. (let ((case-fold-search t)
  14654. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14655. (if (not (save-excursion
  14656. (or (re-search-forward re nil t)
  14657. (re-search-backward re nil t))))
  14658. (error "No bibliography defined in file")
  14659. (setq bib (concat (match-string 1) ".bib")
  14660. rds (list (list 'bib bib)))))))
  14661. (call-interactively 'reftex-citation)))
  14662. ;;;; Functions extending outline functionality
  14663. (defun org-beginning-of-line (&optional arg)
  14664. "Go to the beginning of the current line. If that is invisible, continue
  14665. to a visible line beginning. This makes the function of C-a more intuitive.
  14666. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14667. first attempt, and only move to after the tags when the cursor is already
  14668. beyond the end of the headline."
  14669. (interactive "P")
  14670. (let ((pos (point))
  14671. (special (if (consp org-special-ctrl-a/e)
  14672. (car org-special-ctrl-a/e)
  14673. org-special-ctrl-a/e))
  14674. refpos)
  14675. (beginning-of-line 1)
  14676. (if (and arg (fboundp 'move-beginning-of-line))
  14677. (call-interactively 'move-beginning-of-line)
  14678. (if (bobp)
  14679. nil
  14680. (backward-char 1)
  14681. (if (org-invisible-p)
  14682. (while (and (not (bobp)) (org-invisible-p))
  14683. (backward-char 1)
  14684. (beginning-of-line 1))
  14685. (forward-char 1))))
  14686. (when special
  14687. (cond
  14688. ((and (looking-at org-complex-heading-regexp)
  14689. (= (char-after (match-end 1)) ?\ ))
  14690. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14691. (point-at-eol)))
  14692. (goto-char
  14693. (if (eq special t)
  14694. (cond ((> pos refpos) refpos)
  14695. ((= pos (point)) refpos)
  14696. (t (point)))
  14697. (cond ((> pos (point)) (point))
  14698. ((not (eq last-command this-command)) (point))
  14699. (t refpos)))))
  14700. ((org-at-item-p)
  14701. (goto-char
  14702. (if (eq special t)
  14703. (cond ((> pos (match-end 4)) (match-end 4))
  14704. ((= pos (point)) (match-end 4))
  14705. (t (point)))
  14706. (cond ((> pos (point)) (point))
  14707. ((not (eq last-command this-command)) (point))
  14708. (t (match-end 4))))))))
  14709. (org-no-warnings
  14710. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14711. (defun org-end-of-line (&optional arg)
  14712. "Go to the end of the line.
  14713. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14714. first attempt, and only move to after the tags when the cursor is already
  14715. beyond the end of the headline."
  14716. (interactive "P")
  14717. (let ((special (if (consp org-special-ctrl-a/e)
  14718. (cdr org-special-ctrl-a/e)
  14719. org-special-ctrl-a/e)))
  14720. (if (or (not special)
  14721. (not (org-on-heading-p))
  14722. arg)
  14723. (call-interactively (if (fboundp 'move-end-of-line)
  14724. 'move-end-of-line
  14725. 'end-of-line))
  14726. (let ((pos (point)))
  14727. (beginning-of-line 1)
  14728. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14729. (if (eq special t)
  14730. (if (or (< pos (match-beginning 1))
  14731. (= pos (match-end 0)))
  14732. (goto-char (match-beginning 1))
  14733. (goto-char (match-end 0)))
  14734. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14735. (goto-char (match-end 0))
  14736. (goto-char (match-beginning 1))))
  14737. (call-interactively (if (fboundp 'move-end-of-line)
  14738. 'move-end-of-line
  14739. 'end-of-line)))))
  14740. (org-no-warnings
  14741. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14742. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14743. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14744. (defun org-backward-sentence (&optional arg)
  14745. "Go to beginning of sentence, or beginning of table field.
  14746. This will call `backward-sentence' or `org-table-beginning-of-field',
  14747. depending on context."
  14748. (interactive "P")
  14749. (cond
  14750. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14751. (t (call-interactively 'backward-sentence))))
  14752. (defun org-forward-sentence (&optional arg)
  14753. "Go to end of sentence, or end of table field.
  14754. This will call `forward-sentence' or `org-table-end-of-field',
  14755. depending on context."
  14756. (interactive "P")
  14757. (cond
  14758. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14759. (t (call-interactively 'forward-sentence))))
  14760. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14761. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14762. (defun org-kill-line (&optional arg)
  14763. "Kill line, to tags or end of line."
  14764. (interactive "P")
  14765. (cond
  14766. ((or (not org-special-ctrl-k)
  14767. (bolp)
  14768. (not (org-on-heading-p)))
  14769. (call-interactively 'kill-line))
  14770. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14771. (kill-region (point) (match-beginning 1))
  14772. (org-set-tags nil t))
  14773. (t (kill-region (point) (point-at-eol)))))
  14774. (define-key org-mode-map "\C-k" 'org-kill-line)
  14775. (defun org-yank (&optional arg)
  14776. "Yank. If the kill is a subtree, treat it specially.
  14777. This command will look at the current kill and check if is a single
  14778. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14779. cursor is at the beginning of a line or after the stars of a currently
  14780. empty headline, then the yank is handled specially. How exactly depends
  14781. on the value of the following variables, both set by default.
  14782. org-yank-folded-subtrees
  14783. When set, the subtree(s) will be folded after insertion, but only
  14784. if doing so would now swallow text after the yanked text.
  14785. org-yank-adjusted-subtrees
  14786. When set, the subtree will be promoted or demoted in order to
  14787. fit into the local outline tree structure, which means that the level
  14788. will be adjusted so that it becomes the smaller one of the two
  14789. *visible* surrounding headings.
  14790. Any prefix to this command will cause `yank' to be called directly with
  14791. no special treatment. In particular, a simple `C-u' prefix will just
  14792. plainly yank the text as it is.
  14793. \[1] The test checks if the first non-white line is a heading
  14794. and if there are no other headings with fewer stars."
  14795. (interactive "P")
  14796. (org-yank-generic 'yank arg))
  14797. (defun org-yank-generic (command arg)
  14798. "Perform some yank-like command.
  14799. This function implements the behavior described in the `org-yank'
  14800. documentation. However, it has been generalized to work for any
  14801. interactive command with similar behavior."
  14802. ;; pretend to be command COMMAND
  14803. (setq this-command command)
  14804. (if arg
  14805. (call-interactively command)
  14806. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14807. (and (org-kill-is-subtree-p)
  14808. (or (bolp)
  14809. (and (looking-at "[ \t]*$")
  14810. (string-match
  14811. "\\`\\*+\\'"
  14812. (buffer-substring (point-at-bol) (point)))))))
  14813. swallowp)
  14814. (cond
  14815. ((and subtreep org-yank-folded-subtrees)
  14816. (let ((beg (point))
  14817. end)
  14818. (if (and subtreep org-yank-adjusted-subtrees)
  14819. (org-paste-subtree nil nil 'for-yank)
  14820. (call-interactively command))
  14821. (setq end (point))
  14822. (goto-char beg)
  14823. (when (and (bolp) subtreep
  14824. (not (setq swallowp
  14825. (org-yank-folding-would-swallow-text beg end))))
  14826. (or (looking-at outline-regexp)
  14827. (re-search-forward (concat "^" outline-regexp) end t))
  14828. (while (and (< (point) end) (looking-at outline-regexp))
  14829. (hide-subtree)
  14830. (org-cycle-show-empty-lines 'folded)
  14831. (condition-case nil
  14832. (outline-forward-same-level 1)
  14833. (error (goto-char end)))))
  14834. (when swallowp
  14835. (message
  14836. "Inserted text not folded because that would swallow text"))
  14837. (goto-char end)
  14838. (skip-chars-forward " \t\n\r")
  14839. (beginning-of-line 1)
  14840. (push-mark beg 'nomsg)))
  14841. ((and subtreep org-yank-adjusted-subtrees)
  14842. (let ((beg (point-at-bol)))
  14843. (org-paste-subtree nil nil 'for-yank)
  14844. (push-mark beg 'nomsg)))
  14845. (t
  14846. (call-interactively command))))))
  14847. (defun org-yank-folding-would-swallow-text (beg end)
  14848. "Would hide-subtree at BEG swallow any text after END?"
  14849. (let (level)
  14850. (save-excursion
  14851. (goto-char beg)
  14852. (when (or (looking-at outline-regexp)
  14853. (re-search-forward (concat "^" outline-regexp) end t))
  14854. (setq level (org-outline-level)))
  14855. (goto-char end)
  14856. (skip-chars-forward " \t\r\n\v\f")
  14857. (if (or (eobp)
  14858. (and (bolp) (looking-at org-outline-regexp)
  14859. (<= (org-outline-level) level)))
  14860. nil ; Nothing would be swallowed
  14861. t)))) ; something would swallow
  14862. (define-key org-mode-map "\C-y" 'org-yank)
  14863. (defun org-invisible-p ()
  14864. "Check if point is at a character currently not visible."
  14865. ;; Early versions of noutline don't have `outline-invisible-p'.
  14866. (if (fboundp 'outline-invisible-p)
  14867. (outline-invisible-p)
  14868. (get-char-property (point) 'invisible)))
  14869. (defun org-invisible-p2 ()
  14870. "Check if point is at a character currently not visible."
  14871. (save-excursion
  14872. (if (and (eolp) (not (bobp))) (backward-char 1))
  14873. ;; Early versions of noutline don't have `outline-invisible-p'.
  14874. (if (fboundp 'outline-invisible-p)
  14875. (outline-invisible-p)
  14876. (get-char-property (point) 'invisible))))
  14877. (defun org-back-to-heading (&optional invisible-ok)
  14878. "Call `outline-back-to-heading', but provide a better error message."
  14879. (condition-case nil
  14880. (outline-back-to-heading invisible-ok)
  14881. (error (error "Before first headline at position %d in buffer %s"
  14882. (point) (current-buffer)))))
  14883. (defun org-before-first-heading-p ()
  14884. "Before first heading?"
  14885. (save-excursion
  14886. (null (re-search-backward "^\\*+ " nil t))))
  14887. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14888. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14889. (defun org-at-heading-or-item-p ()
  14890. (or (org-on-heading-p) (org-at-item-p)))
  14891. (defun org-on-target-p ()
  14892. (or (org-in-regexp org-radio-target-regexp)
  14893. (org-in-regexp org-target-regexp)))
  14894. (defun org-up-heading-all (arg)
  14895. "Move to the heading line of which the present line is a subheading.
  14896. This function considers both visible and invisible heading lines.
  14897. With argument, move up ARG levels."
  14898. (if (fboundp 'outline-up-heading-all)
  14899. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14900. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14901. (defun org-up-heading-safe ()
  14902. "Move to the heading line of which the present line is a subheading.
  14903. This version will not throw an error. It will return the level of the
  14904. headline found, or nil if no higher level is found.
  14905. Also, this function will be a lot faster than `outline-up-heading',
  14906. because it relies on stars being the outline starters. This can really
  14907. make a significant difference in outlines with very many siblings."
  14908. (let (start-level re)
  14909. (org-back-to-heading t)
  14910. (setq start-level (funcall outline-level))
  14911. (if (equal start-level 1)
  14912. nil
  14913. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14914. (if (re-search-backward re nil t)
  14915. (funcall outline-level)))))
  14916. (defun org-first-sibling-p ()
  14917. "Is this heading the first child of its parents?"
  14918. (interactive)
  14919. (let ((re (concat "^" outline-regexp))
  14920. level l)
  14921. (unless (org-at-heading-p t)
  14922. (error "Not at a heading"))
  14923. (setq level (funcall outline-level))
  14924. (save-excursion
  14925. (if (not (re-search-backward re nil t))
  14926. t
  14927. (setq l (funcall outline-level))
  14928. (< l level)))))
  14929. (defun org-goto-sibling (&optional previous)
  14930. "Goto the next sibling, even if it is invisible.
  14931. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14932. when a sibling was found. When none is found, return nil and don't
  14933. move point."
  14934. (let ((fun (if previous 're-search-backward 're-search-forward))
  14935. (pos (point))
  14936. (re (concat "^" outline-regexp))
  14937. level l)
  14938. (when (condition-case nil (org-back-to-heading t) (error nil))
  14939. (setq level (funcall outline-level))
  14940. (catch 'exit
  14941. (or previous (forward-char 1))
  14942. (while (funcall fun re nil t)
  14943. (setq l (funcall outline-level))
  14944. (when (< l level) (goto-char pos) (throw 'exit nil))
  14945. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14946. (goto-char pos)
  14947. nil))))
  14948. (defun org-show-siblings ()
  14949. "Show all siblings of the current headline."
  14950. (save-excursion
  14951. (while (org-goto-sibling) (org-flag-heading nil)))
  14952. (save-excursion
  14953. (while (org-goto-sibling 'previous)
  14954. (org-flag-heading nil))))
  14955. (defun org-show-hidden-entry ()
  14956. "Show an entry where even the heading is hidden."
  14957. (save-excursion
  14958. (org-show-entry)))
  14959. (defun org-flag-heading (flag &optional entry)
  14960. "Flag the current heading. FLAG non-nil means make invisible.
  14961. When ENTRY is non-nil, show the entire entry."
  14962. (save-excursion
  14963. (org-back-to-heading t)
  14964. ;; Check if we should show the entire entry
  14965. (if entry
  14966. (progn
  14967. (org-show-entry)
  14968. (save-excursion
  14969. (and (outline-next-heading)
  14970. (org-flag-heading nil))))
  14971. (outline-flag-region (max (point-min) (1- (point)))
  14972. (save-excursion (outline-end-of-heading) (point))
  14973. flag))))
  14974. (defun org-forward-same-level (arg)
  14975. "Move forward to the ARG'th subheading at same level as this one.
  14976. Stop at the first and last subheadings of a superior heading.
  14977. This is like outline-forward-same-level, but invisible headings are ok."
  14978. (interactive "p")
  14979. (org-back-to-heading t)
  14980. (while (> arg 0)
  14981. (let ((point-to-move-to (save-excursion
  14982. (org-get-next-sibling))))
  14983. (if point-to-move-to
  14984. (progn
  14985. (goto-char point-to-move-to)
  14986. (setq arg (1- arg)))
  14987. (progn
  14988. (setq arg 0)
  14989. (error "No following same-level heading"))))))
  14990. (defun org-get-next-sibling ()
  14991. "Move to next heading of the same level, and return point.
  14992. If there is no such heading, return nil.
  14993. This is like outline-next-sibling, but invisible headings are ok."
  14994. (let ((level (funcall outline-level)))
  14995. (outline-next-heading)
  14996. (while (and (not (eobp)) (> (funcall outline-level) level))
  14997. (outline-next-heading))
  14998. (if (or (eobp) (< (funcall outline-level) level))
  14999. nil
  15000. (point))))
  15001. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15002. ;; This contains an exact copy of the original function, but it uses
  15003. ;; `org-back-to-heading', to make it work also in invisible
  15004. ;; trees. And is uses an invisible-OK argument.
  15005. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15006. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15007. ;; with many children and grandchildren etc, this can be much faster
  15008. ;; than the outline version.
  15009. (org-back-to-heading invisible-OK)
  15010. (let ((first t)
  15011. (level (funcall outline-level)))
  15012. (if (and (org-mode-p) (< level 1000))
  15013. ;; A true heading (not a plain list item), in Org-mode
  15014. ;; This means we can easily find the end by looking
  15015. ;; only for the right number of stars. Using a regexp to do
  15016. ;; this is so much faster than using a Lisp loop.
  15017. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15018. (forward-char 1)
  15019. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15020. ;; something else, do it the slow way
  15021. (while (and (not (eobp))
  15022. (or first (> (funcall outline-level) level)))
  15023. (setq first nil)
  15024. (outline-next-heading)))
  15025. (unless to-heading
  15026. (if (memq (preceding-char) '(?\n ?\^M))
  15027. (progn
  15028. ;; Go to end of line before heading
  15029. (forward-char -1)
  15030. (if (memq (preceding-char) '(?\n ?\^M))
  15031. ;; leave blank line before heading
  15032. (forward-char -1))))))
  15033. (point))
  15034. (defun org-show-subtree ()
  15035. "Show everything after this heading at deeper levels."
  15036. (outline-flag-region
  15037. (point)
  15038. (save-excursion
  15039. (outline-end-of-subtree) (outline-next-heading) (point))
  15040. nil))
  15041. (defun org-show-entry ()
  15042. "Show the body directly following this heading.
  15043. Show the heading too, if it is currently invisible."
  15044. (interactive)
  15045. (save-excursion
  15046. (condition-case nil
  15047. (progn
  15048. (org-back-to-heading t)
  15049. (outline-flag-region
  15050. (max (point-min) (1- (point)))
  15051. (save-excursion
  15052. (if (re-search-forward
  15053. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15054. (match-beginning 1)
  15055. (point-max)))
  15056. nil))
  15057. (error nil))))
  15058. (defun org-make-options-regexp (kwds &optional extra)
  15059. "Make a regular expression for keyword lines."
  15060. (concat
  15061. "^"
  15062. "#?[ \t]*\\+\\("
  15063. (mapconcat 'regexp-quote kwds "\\|")
  15064. (if extra (concat "\\|" extra))
  15065. "\\):[ \t]*"
  15066. "\\(.+\\)"))
  15067. ;; Make isearch reveal the necessary context
  15068. (defun org-isearch-end ()
  15069. "Reveal context after isearch exits."
  15070. (when isearch-success ; only if search was successful
  15071. (if (featurep 'xemacs)
  15072. ;; Under XEmacs, the hook is run in the correct place,
  15073. ;; we directly show the context.
  15074. (org-show-context 'isearch)
  15075. ;; In Emacs the hook runs *before* restoring the overlays.
  15076. ;; So we have to use a one-time post-command-hook to do this.
  15077. ;; (Emacs 22 has a special variable, see function `org-mode')
  15078. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15079. isearch-mode-end-hook-quit)
  15080. ;; Only when the isearch was not quitted.
  15081. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15082. 'append 'local)))))
  15083. (defun org-isearch-post-command ()
  15084. "Remove self from hook, and show context."
  15085. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15086. (org-show-context 'isearch))
  15087. ;;;; Integration with and fixes for other packages
  15088. ;;; Imenu support
  15089. (defvar org-imenu-markers nil
  15090. "All markers currently used by Imenu.")
  15091. (make-variable-buffer-local 'org-imenu-markers)
  15092. (defun org-imenu-new-marker (&optional pos)
  15093. "Return a new marker for use by Imenu, and remember the marker."
  15094. (let ((m (make-marker)))
  15095. (move-marker m (or pos (point)))
  15096. (push m org-imenu-markers)
  15097. m))
  15098. (defun org-imenu-get-tree ()
  15099. "Produce the index for Imenu."
  15100. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15101. (setq org-imenu-markers nil)
  15102. (let* ((n org-imenu-depth)
  15103. (re (concat "^" outline-regexp))
  15104. (subs (make-vector (1+ n) nil))
  15105. (last-level 0)
  15106. m level head)
  15107. (save-excursion
  15108. (save-restriction
  15109. (widen)
  15110. (goto-char (point-max))
  15111. (while (re-search-backward re nil t)
  15112. (setq level (org-reduced-level (funcall outline-level)))
  15113. (when (<= level n)
  15114. (looking-at org-complex-heading-regexp)
  15115. (setq head (org-link-display-format
  15116. (org-match-string-no-properties 4))
  15117. m (org-imenu-new-marker))
  15118. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15119. (if (>= level last-level)
  15120. (push (cons head m) (aref subs level))
  15121. (push (cons head (aref subs (1+ level))) (aref subs level))
  15122. (loop for i from (1+ level) to n do (aset subs i nil)))
  15123. (setq last-level level)))))
  15124. (aref subs 1)))
  15125. (eval-after-load "imenu"
  15126. '(progn
  15127. (add-hook 'imenu-after-jump-hook
  15128. (lambda ()
  15129. (if (eq major-mode 'org-mode)
  15130. (org-show-context 'org-goto))))))
  15131. (defun org-link-display-format (link)
  15132. "Replace a link with either the description, or the link target
  15133. if no description is present"
  15134. (save-match-data
  15135. (if (string-match org-bracket-link-analytic-regexp link)
  15136. (replace-match (or (match-string 5 link)
  15137. (concat (match-string 1 link)
  15138. (match-string 3 link)))
  15139. nil nil link)
  15140. link)))
  15141. ;; Speedbar support
  15142. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15143. "Overlay marking the agenda restriction line in speedbar.")
  15144. (org-overlay-put org-speedbar-restriction-lock-overlay
  15145. 'face 'org-agenda-restriction-lock)
  15146. (org-overlay-put org-speedbar-restriction-lock-overlay
  15147. 'help-echo "Agendas are currently limited to this item.")
  15148. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15149. (defun org-speedbar-set-agenda-restriction ()
  15150. "Restrict future agenda commands to the location at point in speedbar.
  15151. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15152. (interactive)
  15153. (require 'org-agenda)
  15154. (let (p m tp np dir txt)
  15155. (cond
  15156. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15157. 'org-imenu t))
  15158. (setq m (get-text-property p 'org-imenu-marker))
  15159. (save-excursion
  15160. (save-restriction
  15161. (set-buffer (marker-buffer m))
  15162. (goto-char m)
  15163. (org-agenda-set-restriction-lock 'subtree))))
  15164. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15165. 'speedbar-function 'speedbar-find-file))
  15166. (setq tp (previous-single-property-change
  15167. (1+ p) 'speedbar-function)
  15168. np (next-single-property-change
  15169. tp 'speedbar-function)
  15170. dir (speedbar-line-directory)
  15171. txt (buffer-substring-no-properties (or tp (point-min))
  15172. (or np (point-max))))
  15173. (save-excursion
  15174. (save-restriction
  15175. (set-buffer (find-file-noselect
  15176. (let ((default-directory dir))
  15177. (expand-file-name txt))))
  15178. (unless (org-mode-p)
  15179. (error "Cannot restrict to non-Org-mode file"))
  15180. (org-agenda-set-restriction-lock 'file))))
  15181. (t (error "Don't know how to restrict Org-mode's agenda")))
  15182. (org-move-overlay org-speedbar-restriction-lock-overlay
  15183. (point-at-bol) (point-at-eol))
  15184. (setq current-prefix-arg nil)
  15185. (org-agenda-maybe-redo)))
  15186. (eval-after-load "speedbar"
  15187. '(progn
  15188. (speedbar-add-supported-extension ".org")
  15189. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15190. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15191. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15192. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15193. (add-hook 'speedbar-visiting-tag-hook
  15194. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15195. ;;; Fixes and Hacks for problems with other packages
  15196. ;; Make flyspell not check words in links, to not mess up our keymap
  15197. (defun org-mode-flyspell-verify ()
  15198. "Don't let flyspell put overlays at active buttons."
  15199. (not (get-text-property (point) 'keymap)))
  15200. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15201. (eval-after-load "bookmark"
  15202. '(if (boundp 'bookmark-after-jump-hook)
  15203. ;; We can use the hook
  15204. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15205. ;; Hook not available, use advice
  15206. (defadvice bookmark-jump (after org-make-visible activate)
  15207. "Make the position visible."
  15208. (org-bookmark-jump-unhide))))
  15209. ;; Make sure saveplace show the location if it was hidden
  15210. (eval-after-load "saveplace"
  15211. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15212. "Make the position visible."
  15213. (org-bookmark-jump-unhide)))
  15214. (defun org-bookmark-jump-unhide ()
  15215. "Unhide the current position, to show the bookmark location."
  15216. (and (org-mode-p)
  15217. (or (org-invisible-p)
  15218. (save-excursion (goto-char (max (point-min) (1- (point))))
  15219. (org-invisible-p)))
  15220. (org-show-context 'bookmark-jump)))
  15221. ;; Make session.el ignore our circular variable
  15222. (eval-after-load "session"
  15223. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15224. ;;;; Experimental code
  15225. (defun org-closed-in-range ()
  15226. "Sparse tree of items closed in a certain time range.
  15227. Still experimental, may disappear in the future."
  15228. (interactive)
  15229. ;; Get the time interval from the user.
  15230. (let* ((time1 (time-to-seconds
  15231. (org-read-date nil 'to-time nil "Starting date: ")))
  15232. (time2 (time-to-seconds
  15233. (org-read-date nil 'to-time nil "End date:")))
  15234. ;; callback function
  15235. (callback (lambda ()
  15236. (let ((time
  15237. (time-to-seconds
  15238. (apply 'encode-time
  15239. (org-parse-time-string
  15240. (match-string 1))))))
  15241. ;; check if time in interval
  15242. (and (>= time time1) (<= time time2))))))
  15243. ;; make tree, check each match with the callback
  15244. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15245. ;;;; Finish up
  15246. (provide 'org)
  15247. (run-hooks 'org-load-hook)
  15248. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15249. ;;; org.el ends here