org.el 656 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.32trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.32trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let* ((version org-version)
  96. (git-version)
  97. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  98. (if (and (file-exists-p (expand-file-name ".git" dir))
  99. (executable-find "git"))
  100. (let ((pwd (substring (pwd) 10)))
  101. (cd dir)
  102. (if (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  103. (save-excursion
  104. (set-buffer "*Shell Command Output*")
  105. (goto-char (point-min))
  106. (re-search-forward "[^\n]+")
  107. (setq git-version (match-string 0))
  108. (subst-char-in-string ?- ?. git-version t)
  109. (shell-command "git diff-index --name-only HEAD --")
  110. (unless (eql 1 (point-max))
  111. (setq git-version (concat git-version ".dirty")))
  112. (setq version (concat version " (" git-version ")")))
  113. (cd pwd))))
  114. (setq version (format "Org-mode version %s" version))
  115. (if here (insert version))
  116. (message version)
  117. version))
  118. ;;; Compatibility constants
  119. ;;; The custom variables
  120. (defgroup org nil
  121. "Outline-based notes management and organizer."
  122. :tag "Org"
  123. :group 'outlines
  124. :group 'hypermedia
  125. :group 'calendar)
  126. (defcustom org-mode-hook nil
  127. "Mode hook for Org-mode, run after the mode was turned on."
  128. :group 'org
  129. :type 'hook)
  130. (defcustom org-load-hook nil
  131. "Hook that is run after org.el has been loaded."
  132. :group 'org
  133. :type 'hook)
  134. (defvar org-modules) ; defined below
  135. (defvar org-modules-loaded nil
  136. "Have the modules been loaded already?")
  137. (defun org-load-modules-maybe (&optional force)
  138. "Load all extensions listed in `org-modules'."
  139. (when (or force (not org-modules-loaded))
  140. (mapc (lambda (ext)
  141. (condition-case nil (require ext)
  142. (error (message "Problems while trying to load feature `%s'" ext))))
  143. org-modules)
  144. (setq org-modules-loaded t)))
  145. (defun org-set-modules (var value)
  146. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  147. (set var value)
  148. (when (featurep 'org)
  149. (org-load-modules-maybe 'force)))
  150. (when (org-bound-and-true-p org-modules)
  151. (let ((a (member 'org-infojs org-modules)))
  152. (and a (setcar a 'org-jsinfo))))
  153. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  154. "Modules that should always be loaded together with org.el.
  155. If a description starts with <C>, the file is not part of Emacs
  156. and loading it will require that you have downloaded and properly installed
  157. the org-mode distribution.
  158. You can also use this system to load external packages (i.e. neither Org
  159. core modules, nor modules from the CONTRIB directory). Just add symbols
  160. to the end of the list. If the package is called org-xyz.el, then you need
  161. to add the symbol `xyz', and the package must have a call to
  162. (provide 'org-xyz)"
  163. :group 'org
  164. :set 'org-set-modules
  165. :type
  166. '(set :greedy t
  167. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  168. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  169. (const :tag " crypt: Encryption of subtrees" org-crypt)
  170. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  171. (const :tag " id: Global IDs for identifying entries" org-id)
  172. (const :tag " info: Links to Info nodes" org-info)
  173. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  174. (const :tag " habit: Track your consistency with habits" org-habit)
  175. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  176. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  177. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  178. (const :tag " mew Links to Mew folders/messages" org-mew)
  179. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  180. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  181. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  182. (const :tag " vm: Links to VM folders/messages" org-vm)
  183. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  184. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  185. (const :tag " mouse: Additional mouse support" org-mouse)
  186. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  187. (const :tag "C annotation-helper: Call Remember directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-annotation-helper)
  188. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  189. (const :tag "C browser-url: Store link, directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-browser-url)
  190. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  191. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  192. (const :tag "C collector: Collect properties into tables" org-collector)
  193. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  194. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  195. (const :tag "C eval: Include command output as text" org-eval)
  196. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  197. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  198. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  199. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  200. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  201. (const :tag "C invoice Help manage client invoices in Org-mode" org-invoice)
  202. (const :tag "C jira Add a jira:ticket protocol to Org-mode" org-jira)
  203. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  204. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  205. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  206. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  207. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  208. (const :tag "C R: Computation using the R language" org-R)
  209. (const :tag "C registry: A registry for Org-mode links" org-registry)
  210. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  211. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  212. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  213. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  214. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  215. (const :tag "C track: Keep up with Org-mode development" org-track)
  216. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  217. (defcustom org-support-shift-select nil
  218. "Non-nil means, make shift-cursor commands select text when possible.
  219. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  220. selecting a region, or enlarge thusly regions started in this way.
  221. In Org-mode, in special contexts, these same keys are used for other
  222. purposes, important enough to compete with shift selection. Org tries
  223. to balance these needs by supporting `shift-select-mode' outside these
  224. special contexts, under control of this variable.
  225. The default of this variable is nil, to avoid confusing behavior. Shifted
  226. cursor keys will then execute Org commands in the following contexts:
  227. - on a headline, changing TODO state (left/right) and priority (up/down)
  228. - on a time stamp, changing the time
  229. - in a plain list item, changing the bullet type
  230. - in a property definition line, switching between allowed values
  231. - in the BEGIN line of a clock table (changing the time block).
  232. Outside these contexts, the commands will throw an error.
  233. When this variable is t and the cursor is not in a special context,
  234. Org-mode will support shift-selection for making and enlarging regions.
  235. To make this more effective, the bullet cycling will no longer happen
  236. anywhere in an item line, but only if the cursor is exactly on the bullet.
  237. If you set this variable to the symbol `always', then the keys
  238. will not be special in headlines, property lines, and item lines, to make
  239. shift selection work there as well. If this is what you want, you can
  240. use the following alternative commands: `C-c C-t' and `C-c ,' to
  241. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  242. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  243. edited by hand or in column view.
  244. However, when the cursor is on a timestamp, shift-cursor commands
  245. will still edit the time stamp - this is just too good to give up.
  246. XEmacs user should have this variable set to nil, because shift-select-mode
  247. is Emacs 23 only."
  248. :group 'org
  249. :type '(choice
  250. (const :tag "Never" nil)
  251. (const :tag "When outside special context" t)
  252. (const :tag "Everywhere except timestamps" always)))
  253. (defgroup org-startup nil
  254. "Options concerning startup of Org-mode."
  255. :tag "Org Startup"
  256. :group 'org)
  257. (defcustom org-startup-folded t
  258. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  259. This can also be configured on a per-file basis by adding one of
  260. the following lines anywhere in the buffer:
  261. #+STARTUP: fold (or `overview', this is equivalent)
  262. #+STARTUP: nofold (or `showall', this is equivalent)
  263. #+STARTUP: content
  264. #+STARTUP: showeverything"
  265. :group 'org-startup
  266. :type '(choice
  267. (const :tag "nofold: show all" nil)
  268. (const :tag "fold: overview" t)
  269. (const :tag "content: all headlines" content)
  270. (const :tag "show everything, even drawers" showeverything)))
  271. (defcustom org-startup-truncated t
  272. "Non-nil means, entering Org-mode will set `truncate-lines'.
  273. This is useful since some lines containing links can be very long and
  274. uninteresting. Also tables look terrible when wrapped."
  275. :group 'org-startup
  276. :type 'boolean)
  277. (defcustom org-startup-indented nil
  278. "Non-nil means, turn on `org-indent-mode' on startup.
  279. This can also be configured on a per-file basis by adding one of
  280. the following lines anywhere in the buffer:
  281. #+STARTUP: indent
  282. #+STARTUP: noindent"
  283. :group 'org-structure
  284. :type '(choice
  285. (const :tag "Not" nil)
  286. (const :tag "Globally (slow on startup in large files)" t)))
  287. (defcustom org-startup-align-all-tables nil
  288. "Non-nil means, align all tables when visiting a file.
  289. This is useful when the column width in tables is forced with <N> cookies
  290. in table fields. Such tables will look correct only after the first re-align.
  291. This can also be configured on a per-file basis by adding one of
  292. the following lines anywhere in the buffer:
  293. #+STARTUP: align
  294. #+STARTUP: noalign"
  295. :group 'org-startup
  296. :type 'boolean)
  297. (defcustom org-insert-mode-line-in-empty-file nil
  298. "Non-nil means insert the first line setting Org-mode in empty files.
  299. When the function `org-mode' is called interactively in an empty file, this
  300. normally means that the file name does not automatically trigger Org-mode.
  301. To ensure that the file will always be in Org-mode in the future, a
  302. line enforcing Org-mode will be inserted into the buffer, if this option
  303. has been set."
  304. :group 'org-startup
  305. :type 'boolean)
  306. (defcustom org-replace-disputed-keys nil
  307. "Non-nil means use alternative key bindings for some keys.
  308. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  309. These keys are also used by other packages like shift-selection-mode'
  310. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  311. If you want to use Org-mode together with one of these other modes,
  312. or more generally if you would like to move some Org-mode commands to
  313. other keys, set this variable and configure the keys with the variable
  314. `org-disputed-keys'.
  315. This option is only relevant at load-time of Org-mode, and must be set
  316. *before* org.el is loaded. Changing it requires a restart of Emacs to
  317. become effective."
  318. :group 'org-startup
  319. :type 'boolean)
  320. (defcustom org-use-extra-keys nil
  321. "Non-nil means use extra key sequence definitions for certain
  322. commands. This happens automatically if you run XEmacs or if
  323. window-system is nil. This variable lets you do the same
  324. manually. You must set it before loading org.
  325. Example: on Carbon Emacs 22 running graphically, with an external
  326. keyboard on a Powerbook, the default way of setting M-left might
  327. not work for either Alt or ESC. Setting this variable will make
  328. it work for ESC."
  329. :group 'org-startup
  330. :type 'boolean)
  331. (if (fboundp 'defvaralias)
  332. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  333. (defcustom org-disputed-keys
  334. '(([(shift up)] . [(meta p)])
  335. ([(shift down)] . [(meta n)])
  336. ([(shift left)] . [(meta -)])
  337. ([(shift right)] . [(meta +)])
  338. ([(control shift right)] . [(meta shift +)])
  339. ([(control shift left)] . [(meta shift -)]))
  340. "Keys for which Org-mode and other modes compete.
  341. This is an alist, cars are the default keys, second element specifies
  342. the alternative to use when `org-replace-disputed-keys' is t.
  343. Keys can be specified in any syntax supported by `define-key'.
  344. The value of this option takes effect only at Org-mode's startup,
  345. therefore you'll have to restart Emacs to apply it after changing."
  346. :group 'org-startup
  347. :type 'alist)
  348. (defun org-key (key)
  349. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  350. Or return the original if not disputed."
  351. (if org-replace-disputed-keys
  352. (let* ((nkey (key-description key))
  353. (x (org-find-if (lambda (x)
  354. (equal (key-description (car x)) nkey))
  355. org-disputed-keys)))
  356. (if x (cdr x) key))
  357. key))
  358. (defun org-find-if (predicate seq)
  359. (catch 'exit
  360. (while seq
  361. (if (funcall predicate (car seq))
  362. (throw 'exit (car seq))
  363. (pop seq)))))
  364. (defun org-defkey (keymap key def)
  365. "Define a key, possibly translated, as returned by `org-key'."
  366. (define-key keymap (org-key key) def))
  367. (defcustom org-ellipsis nil
  368. "The ellipsis to use in the Org-mode outline.
  369. When nil, just use the standard three dots. When a string, use that instead,
  370. When a face, use the standard 3 dots, but with the specified face.
  371. The change affects only Org-mode (which will then use its own display table).
  372. Changing this requires executing `M-x org-mode' in a buffer to become
  373. effective."
  374. :group 'org-startup
  375. :type '(choice (const :tag "Default" nil)
  376. (face :tag "Face" :value org-warning)
  377. (string :tag "String" :value "...#")))
  378. (defvar org-display-table nil
  379. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  380. (defgroup org-keywords nil
  381. "Keywords in Org-mode."
  382. :tag "Org Keywords"
  383. :group 'org)
  384. (defcustom org-deadline-string "DEADLINE:"
  385. "String to mark deadline entries.
  386. A deadline is this string, followed by a time stamp. Should be a word,
  387. terminated by a colon. You can insert a schedule keyword and
  388. a timestamp with \\[org-deadline].
  389. Changes become only effective after restarting Emacs."
  390. :group 'org-keywords
  391. :type 'string)
  392. (defcustom org-scheduled-string "SCHEDULED:"
  393. "String to mark scheduled TODO entries.
  394. A schedule is this string, followed by a time stamp. Should be a word,
  395. terminated by a colon. You can insert a schedule keyword and
  396. a timestamp with \\[org-schedule].
  397. Changes become only effective after restarting Emacs."
  398. :group 'org-keywords
  399. :type 'string)
  400. (defcustom org-closed-string "CLOSED:"
  401. "String used as the prefix for timestamps logging closing a TODO entry."
  402. :group 'org-keywords
  403. :type 'string)
  404. (defcustom org-clock-string "CLOCK:"
  405. "String used as prefix for timestamps clocking work hours on an item."
  406. :group 'org-keywords
  407. :type 'string)
  408. (defcustom org-comment-string "COMMENT"
  409. "Entries starting with this keyword will never be exported.
  410. An entry can be toggled between COMMENT and normal with
  411. \\[org-toggle-comment].
  412. Changes become only effective after restarting Emacs."
  413. :group 'org-keywords
  414. :type 'string)
  415. (defcustom org-quote-string "QUOTE"
  416. "Entries starting with this keyword will be exported in fixed-width font.
  417. Quoting applies only to the text in the entry following the headline, and does
  418. not extend beyond the next headline, even if that is lower level.
  419. An entry can be toggled between QUOTE and normal with
  420. \\[org-toggle-fixed-width-section]."
  421. :group 'org-keywords
  422. :type 'string)
  423. (defconst org-repeat-re
  424. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  425. "Regular expression for specifying repeated events.
  426. After a match, group 1 contains the repeat expression.")
  427. (defgroup org-structure nil
  428. "Options concerning the general structure of Org-mode files."
  429. :tag "Org Structure"
  430. :group 'org)
  431. (defgroup org-reveal-location nil
  432. "Options about how to make context of a location visible."
  433. :tag "Org Reveal Location"
  434. :group 'org-structure)
  435. (defconst org-context-choice
  436. '(choice
  437. (const :tag "Always" t)
  438. (const :tag "Never" nil)
  439. (repeat :greedy t :tag "Individual contexts"
  440. (cons
  441. (choice :tag "Context"
  442. (const agenda)
  443. (const org-goto)
  444. (const occur-tree)
  445. (const tags-tree)
  446. (const link-search)
  447. (const mark-goto)
  448. (const bookmark-jump)
  449. (const isearch)
  450. (const default))
  451. (boolean))))
  452. "Contexts for the reveal options.")
  453. (defcustom org-show-hierarchy-above '((default . t))
  454. "Non-nil means, show full hierarchy when revealing a location.
  455. Org-mode often shows locations in an org-mode file which might have
  456. been invisible before. When this is set, the hierarchy of headings
  457. above the exposed location is shown.
  458. Turning this off for example for sparse trees makes them very compact.
  459. Instead of t, this can also be an alist specifying this option for different
  460. contexts. Valid contexts are
  461. agenda when exposing an entry from the agenda
  462. org-goto when using the command `org-goto' on key C-c C-j
  463. occur-tree when using the command `org-occur' on key C-c /
  464. tags-tree when constructing a sparse tree based on tags matches
  465. link-search when exposing search matches associated with a link
  466. mark-goto when exposing the jump goal of a mark
  467. bookmark-jump when exposing a bookmark location
  468. isearch when exiting from an incremental search
  469. default default for all contexts not set explicitly"
  470. :group 'org-reveal-location
  471. :type org-context-choice)
  472. (defcustom org-show-following-heading '((default . nil))
  473. "Non-nil means, show following heading when revealing a location.
  474. Org-mode often shows locations in an org-mode file which might have
  475. been invisible before. When this is set, the heading following the
  476. match is shown.
  477. Turning this off for example for sparse trees makes them very compact,
  478. but makes it harder to edit the location of the match. In such a case,
  479. use the command \\[org-reveal] to show more context.
  480. Instead of t, this can also be an alist specifying this option for different
  481. contexts. See `org-show-hierarchy-above' for valid contexts."
  482. :group 'org-reveal-location
  483. :type org-context-choice)
  484. (defcustom org-show-siblings '((default . nil) (isearch t))
  485. "Non-nil means, show all sibling heading when revealing a location.
  486. Org-mode often shows locations in an org-mode file which might have
  487. been invisible before. When this is set, the sibling of the current entry
  488. heading are all made visible. If `org-show-hierarchy-above' is t,
  489. the same happens on each level of the hierarchy above the current entry.
  490. By default this is on for the isearch context, off for all other contexts.
  491. Turning this off for example for sparse trees makes them very compact,
  492. but makes it harder to edit the location of the match. In such a case,
  493. use the command \\[org-reveal] to show more context.
  494. Instead of t, this can also be an alist specifying this option for different
  495. contexts. See `org-show-hierarchy-above' for valid contexts."
  496. :group 'org-reveal-location
  497. :type org-context-choice)
  498. (defcustom org-show-entry-below '((default . nil))
  499. "Non-nil means, show the entry below a headline when revealing a location.
  500. Org-mode often shows locations in an org-mode file which might have
  501. been invisible before. When this is set, the text below the headline that is
  502. exposed is also shown.
  503. By default this is off for all contexts.
  504. Instead of t, this can also be an alist specifying this option for different
  505. contexts. See `org-show-hierarchy-above' for valid contexts."
  506. :group 'org-reveal-location
  507. :type org-context-choice)
  508. (defcustom org-indirect-buffer-display 'other-window
  509. "How should indirect tree buffers be displayed?
  510. This applies to indirect buffers created with the commands
  511. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  512. Valid values are:
  513. current-window Display in the current window
  514. other-window Just display in another window.
  515. dedicated-frame Create one new frame, and re-use it each time.
  516. new-frame Make a new frame each time. Note that in this case
  517. previously-made indirect buffers are kept, and you need to
  518. kill these buffers yourself."
  519. :group 'org-structure
  520. :group 'org-agenda-windows
  521. :type '(choice
  522. (const :tag "In current window" current-window)
  523. (const :tag "In current frame, other window" other-window)
  524. (const :tag "Each time a new frame" new-frame)
  525. (const :tag "One dedicated frame" dedicated-frame)))
  526. (defcustom org-use-speed-commands nil
  527. "Non-nil means, activate single letter commands at beginning of a headline."
  528. :group 'org-structure
  529. :type 'boolean)
  530. (defcustom org-speed-commands-user nil
  531. "Alist of additional speed commands.
  532. This list will be checked before `org-speed-commands-default'
  533. when the variable `org-use-speed-commands' is non-nil
  534. and when the cursor is at the beginning of a headline.
  535. The car if each entry is a string with a single letter, which must
  536. be assigned to `self-insert-command' in the global map.
  537. The cdr is either a command to be called interactively, a function
  538. to be called, or a form to be evaluated."
  539. :group 'org-structure
  540. :type '(repeat
  541. (cons
  542. (string "Command letter")
  543. (choice
  544. (function)
  545. (sexp)))))
  546. (defgroup org-cycle nil
  547. "Options concerning visibility cycling in Org-mode."
  548. :tag "Org Cycle"
  549. :group 'org-structure)
  550. (defcustom org-cycle-skip-children-state-if-no-children t
  551. "Non-nil means, skip CHILDREN state in entries that don't have any."
  552. :group 'org-cycle
  553. :type 'boolean)
  554. (defcustom org-cycle-max-level nil
  555. "Maximum level which should still be subject to visibility cycling.
  556. Levels higher than this will, for cycling, be treated as text, not a headline.
  557. When `org-odd-levels-only' is set, a value of N in this variable actually
  558. means 2N-1 stars as the limiting headline.
  559. When nil, cycle all levels.
  560. Note that the limiting level of cycling is also influenced by
  561. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  562. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  563. than its value."
  564. :group 'org-cycle
  565. :type '(choice
  566. (const :tag "No limit" nil)
  567. (integer :tag "Maximum level")))
  568. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  569. "Names of drawers. Drawers are not opened by cycling on the headline above.
  570. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  571. this:
  572. :DRAWERNAME:
  573. .....
  574. :END:
  575. The drawer \"PROPERTIES\" is special for capturing properties through
  576. the property API.
  577. Drawers can be defined on the per-file basis with a line like:
  578. #+DRAWERS: HIDDEN STATE PROPERTIES"
  579. :group 'org-structure
  580. :group 'org-cycle
  581. :type '(repeat (string :tag "Drawer Name")))
  582. (defcustom org-hide-block-startup nil
  583. "Non-nil means, , entering Org-mode will fold all blocks.
  584. This can also be set in on a per-file basis with
  585. #+STARTUP: hideblocks
  586. #+STARTUP: showblocks"
  587. :group 'org-startup
  588. :group 'org-cycle
  589. :type 'boolean)
  590. (defcustom org-cycle-global-at-bob nil
  591. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  592. This makes it possible to do global cycling without having to use S-TAB or
  593. C-u TAB. For this special case to work, the first line of the buffer
  594. must not be a headline - it may be empty or some other text. When used in
  595. this way, `org-cycle-hook' is disables temporarily, to make sure the
  596. cursor stays at the beginning of the buffer.
  597. When this option is nil, don't do anything special at the beginning
  598. of the buffer."
  599. :group 'org-cycle
  600. :type 'boolean)
  601. (defcustom org-cycle-level-after-item/entry-creation t
  602. "Non-nil means, cycle entry level or item indentation in new empty entries.
  603. When the cursor is at the end of an empty headline, i.e with only stars
  604. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  605. and then all possible anchestor states, before returning to the original state.
  606. This makes data entry extremely fast: M-RET to create a new hedline,
  607. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  608. When the cursor is at the end of an empty plain list item, one TAB will
  609. make it a subitem, two or more tabs will back up to make this an item
  610. higher up in the item hierarchy."
  611. :group 'org-cycle
  612. :type 'boolean)
  613. (defcustom org-cycle-emulate-tab t
  614. "Where should `org-cycle' emulate TAB.
  615. nil Never
  616. white Only in completely white lines
  617. whitestart Only at the beginning of lines, before the first non-white char
  618. t Everywhere except in headlines
  619. exc-hl-bol Everywhere except at the start of a headline
  620. If TAB is used in a place where it does not emulate TAB, the current subtree
  621. visibility is cycled."
  622. :group 'org-cycle
  623. :type '(choice (const :tag "Never" nil)
  624. (const :tag "Only in completely white lines" white)
  625. (const :tag "Before first char in a line" whitestart)
  626. (const :tag "Everywhere except in headlines" t)
  627. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  628. ))
  629. (defcustom org-cycle-separator-lines 2
  630. "Number of empty lines needed to keep an empty line between collapsed trees.
  631. If you leave an empty line between the end of a subtree and the following
  632. headline, this empty line is hidden when the subtree is folded.
  633. Org-mode will leave (exactly) one empty line visible if the number of
  634. empty lines is equal or larger to the number given in this variable.
  635. So the default 2 means, at least 2 empty lines after the end of a subtree
  636. are needed to produce free space between a collapsed subtree and the
  637. following headline.
  638. If the number is negative, and the number of empty lines is at least -N,
  639. all empty lines are shown.
  640. Special case: when 0, never leave empty lines in collapsed view."
  641. :group 'org-cycle
  642. :type 'integer)
  643. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  644. (defcustom org-pre-cycle-hook nil
  645. "Hook that is run before visibility cycling is happening.
  646. The function(s) in this hook must accept a single argument which indicates
  647. the new state that will be set right after running this hook. The
  648. argument is a symbol. Before a global state change, it can have the values
  649. `overview', `content', or `all'. Before a local state change, it can have
  650. the values `folded', `children', or `subtree'."
  651. :group 'org-cycle
  652. :type 'hook)
  653. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  654. org-cycle-hide-drawers
  655. org-cycle-show-empty-lines
  656. org-optimize-window-after-visibility-change)
  657. "Hook that is run after `org-cycle' has changed the buffer visibility.
  658. The function(s) in this hook must accept a single argument which indicates
  659. the new state that was set by the most recent `org-cycle' command. The
  660. argument is a symbol. After a global state change, it can have the values
  661. `overview', `content', or `all'. After a local state change, it can have
  662. the values `folded', `children', or `subtree'."
  663. :group 'org-cycle
  664. :type 'hook)
  665. (defgroup org-edit-structure nil
  666. "Options concerning structure editing in Org-mode."
  667. :tag "Org Edit Structure"
  668. :group 'org-structure)
  669. (defcustom org-odd-levels-only nil
  670. "Non-nil means, skip even levels and only use odd levels for the outline.
  671. This has the effect that two stars are being added/taken away in
  672. promotion/demotion commands. It also influences how levels are
  673. handled by the exporters.
  674. Changing it requires restart of `font-lock-mode' to become effective
  675. for fontification also in regions already fontified.
  676. You may also set this on a per-file basis by adding one of the following
  677. lines to the buffer:
  678. #+STARTUP: odd
  679. #+STARTUP: oddeven"
  680. :group 'org-edit-structure
  681. :group 'org-font-lock
  682. :type 'boolean)
  683. (defcustom org-adapt-indentation t
  684. "Non-nil means, adapt indentation to outline node level.
  685. When this variable is set, Org assumes that you write outlines by
  686. indenting text in each node to align with the headline (after the stars).
  687. The following issues are influenced by this variable:
  688. - When this is set and the *entire* text in an entry is indented, the
  689. indentation is increased by one space in a demotion command, and
  690. decreased by one in a promotion command. If any line in the entry
  691. body starts with text at column 0, indentation is not changed at all.
  692. - Property drawers and planning information is inserted indented when
  693. this variable s set. When nil, they will not be indented.
  694. - TAB indents a line relative to context. The lines below a headline
  695. will be indented when this variable is set.
  696. Note that this is all about true indentation, by adding and removing
  697. space characters. See also `org-indent.el' which does level-dependent
  698. indentation in a virtual way, i.e. at display time in Emacs."
  699. :group 'org-edit-structure
  700. :type 'boolean)
  701. (defcustom org-special-ctrl-a/e nil
  702. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  703. When t, `C-a' will bring back the cursor to the beginning of the
  704. headline text, i.e. after the stars and after a possible TODO keyword.
  705. In an item, this will be the position after the bullet.
  706. When the cursor is already at that position, another `C-a' will bring
  707. it to the beginning of the line.
  708. `C-e' will jump to the end of the headline, ignoring the presence of tags
  709. in the headline. A second `C-e' will then jump to the true end of the
  710. line, after any tags. This also means that, when this variable is
  711. non-nil, `C-e' also will never jump beyond the end of the heading of a
  712. folded section, i.e. not after the ellipses.
  713. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  714. going to the true line boundary first. Only a directly following, identical
  715. keypress will bring the cursor to the special positions.
  716. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  717. set separately."
  718. :group 'org-edit-structure
  719. :type '(choice
  720. (const :tag "off" nil)
  721. (const :tag "on: after stars/bullet and before tags first" t)
  722. (const :tag "reversed: true line boundary first" reversed)
  723. (cons :tag "Set C-a and C-e separately"
  724. (choice :tag "Special C-a"
  725. (const :tag "off" nil)
  726. (const :tag "on: after stars/bullet first" t)
  727. (const :tag "reversed: before stars/bullet first" reversed))
  728. (choice :tag "Special C-e"
  729. (const :tag "off" nil)
  730. (const :tag "on: before tags first" t)
  731. (const :tag "reversed: after tags first" reversed)))))
  732. (if (fboundp 'defvaralias)
  733. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  734. (defcustom org-special-ctrl-k nil
  735. "Non-nil means `C-k' will behave specially in headlines.
  736. When nil, `C-k' will call the default `kill-line' command.
  737. When t, the following will happen while the cursor is in the headline:
  738. - When the cursor is at the beginning of a headline, kill the entire
  739. line and possible the folded subtree below the line.
  740. - When in the middle of the headline text, kill the headline up to the tags.
  741. - When after the headline text, kill the tags."
  742. :group 'org-edit-structure
  743. :type 'boolean)
  744. (defcustom org-yank-folded-subtrees t
  745. "Non-nil means, when yanking subtrees, fold them.
  746. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  747. it starts with a heading and all other headings in it are either children
  748. or siblings, then fold all the subtrees. However, do this only if no
  749. text after the yank would be swallowed into a folded tree by this action."
  750. :group 'org-edit-structure
  751. :type 'boolean)
  752. (defcustom org-yank-adjusted-subtrees nil
  753. "Non-nil means, when yanking subtrees, adjust the level.
  754. With this setting, `org-paste-subtree' is used to insert the subtree, see
  755. this function for details."
  756. :group 'org-edit-structure
  757. :type 'boolean)
  758. (defcustom org-M-RET-may-split-line '((default . t))
  759. "Non-nil means, M-RET will split the line at the cursor position.
  760. When nil, it will go to the end of the line before making a
  761. new line.
  762. You may also set this option in a different way for different
  763. contexts. Valid contexts are:
  764. headline when creating a new headline
  765. item when creating a new item
  766. table in a table field
  767. default the value to be used for all contexts not explicitly
  768. customized"
  769. :group 'org-structure
  770. :group 'org-table
  771. :type '(choice
  772. (const :tag "Always" t)
  773. (const :tag "Never" nil)
  774. (repeat :greedy t :tag "Individual contexts"
  775. (cons
  776. (choice :tag "Context"
  777. (const headline)
  778. (const item)
  779. (const table)
  780. (const default))
  781. (boolean)))))
  782. (defcustom org-insert-heading-respect-content nil
  783. "Non-nil means, insert new headings after the current subtree.
  784. When nil, the new heading is created directly after the current line.
  785. The commands \\[org-insert-heading-respect-content] and
  786. \\[org-insert-todo-heading-respect-content] turn this variable on
  787. for the duration of the command."
  788. :group 'org-structure
  789. :type 'boolean)
  790. (defcustom org-blank-before-new-entry '((heading . auto)
  791. (plain-list-item . auto))
  792. "Should `org-insert-heading' leave a blank line before new heading/item?
  793. The value is an alist, with `heading' and `plain-list-item' as car,
  794. and a boolean flag as cdr. For plain lists, if the variable
  795. `org-empty-line-terminates-plain-lists' is set, the setting here
  796. is ignored and no empty line is inserted, to keep the list in tact."
  797. :group 'org-edit-structure
  798. :type '(list
  799. (cons (const heading)
  800. (choice (const :tag "Never" nil)
  801. (const :tag "Always" t)
  802. (const :tag "Auto" auto)))
  803. (cons (const plain-list-item)
  804. (choice (const :tag "Never" nil)
  805. (const :tag "Always" t)
  806. (const :tag "Auto" auto)))))
  807. (defcustom org-insert-heading-hook nil
  808. "Hook being run after inserting a new heading."
  809. :group 'org-edit-structure
  810. :type 'hook)
  811. (defcustom org-enable-fixed-width-editor t
  812. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  813. This currently only means, they are never auto-wrapped.
  814. When nil, such lines will be treated like ordinary lines.
  815. See also the QUOTE keyword."
  816. :group 'org-edit-structure
  817. :type 'boolean)
  818. (defcustom org-goto-auto-isearch t
  819. "Non-nil means, typing characters in org-goto starts incremental search."
  820. :group 'org-edit-structure
  821. :type 'boolean)
  822. (defgroup org-sparse-trees nil
  823. "Options concerning sparse trees in Org-mode."
  824. :tag "Org Sparse Trees"
  825. :group 'org-structure)
  826. (defcustom org-highlight-sparse-tree-matches t
  827. "Non-nil means, highlight all matches that define a sparse tree.
  828. The highlights will automatically disappear the next time the buffer is
  829. changed by an edit command."
  830. :group 'org-sparse-trees
  831. :type 'boolean)
  832. (defcustom org-remove-highlights-with-change t
  833. "Non-nil means, any change to the buffer will remove temporary highlights.
  834. Such highlights are created by `org-occur' and `org-clock-display'.
  835. When nil, `C-c C-c needs to be used to get rid of the highlights.
  836. The highlights created by `org-preview-latex-fragment' always need
  837. `C-c C-c' to be removed."
  838. :group 'org-sparse-trees
  839. :group 'org-time
  840. :type 'boolean)
  841. (defcustom org-occur-hook '(org-first-headline-recenter)
  842. "Hook that is run after `org-occur' has constructed a sparse tree.
  843. This can be used to recenter the window to show as much of the structure
  844. as possible."
  845. :group 'org-sparse-trees
  846. :type 'hook)
  847. (defgroup org-imenu-and-speedbar nil
  848. "Options concerning imenu and speedbar in Org-mode."
  849. :tag "Org Imenu and Speedbar"
  850. :group 'org-structure)
  851. (defcustom org-imenu-depth 2
  852. "The maximum level for Imenu access to Org-mode headlines.
  853. This also applied for speedbar access."
  854. :group 'org-imenu-and-speedbar
  855. :type 'integer)
  856. (defgroup org-table nil
  857. "Options concerning tables in Org-mode."
  858. :tag "Org Table"
  859. :group 'org)
  860. (defcustom org-enable-table-editor 'optimized
  861. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  862. When nil, such lines will be treated like ordinary lines.
  863. When equal to the symbol `optimized', the table editor will be optimized to
  864. do the following:
  865. - Automatic overwrite mode in front of whitespace in table fields.
  866. This makes the structure of the table stay in tact as long as the edited
  867. field does not exceed the column width.
  868. - Minimize the number of realigns. Normally, the table is aligned each time
  869. TAB or RET are pressed to move to another field. With optimization this
  870. happens only if changes to a field might have changed the column width.
  871. Optimization requires replacing the functions `self-insert-command',
  872. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  873. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  874. very good at guessing when a re-align will be necessary, but you can always
  875. force one with \\[org-ctrl-c-ctrl-c].
  876. If you would like to use the optimized version in Org-mode, but the
  877. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  878. This variable can be used to turn on and off the table editor during a session,
  879. but in order to toggle optimization, a restart is required.
  880. See also the variable `org-table-auto-blank-field'."
  881. :group 'org-table
  882. :type '(choice
  883. (const :tag "off" nil)
  884. (const :tag "on" t)
  885. (const :tag "on, optimized" optimized)))
  886. (defcustom org-self-insert-cluster-for-undo t
  887. "Non-nil means cluster self-insert commands for undo when possible.
  888. If this is set, then, like in the Emacs command loop, 20 consequtive
  889. characters will be undone together.
  890. This is configurable, because there is some impact on typing performance."
  891. :group 'org-table
  892. :type 'boolean)
  893. (defcustom org-table-tab-recognizes-table.el t
  894. "Non-nil means, TAB will automatically notice a table.el table.
  895. When it sees such a table, it moves point into it and - if necessary -
  896. calls `table-recognize-table'."
  897. :group 'org-table-editing
  898. :type 'boolean)
  899. (defgroup org-link nil
  900. "Options concerning links in Org-mode."
  901. :tag "Org Link"
  902. :group 'org)
  903. (defvar org-link-abbrev-alist-local nil
  904. "Buffer-local version of `org-link-abbrev-alist', which see.
  905. The value of this is taken from the #+LINK lines.")
  906. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  907. (defcustom org-link-abbrev-alist nil
  908. "Alist of link abbreviations.
  909. The car of each element is a string, to be replaced at the start of a link.
  910. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  911. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  912. [[linkkey:tag][description]]
  913. The 'linkkey' must be a word word, starting with a letter, followed
  914. by letters, numbers, '-' or '_'.
  915. If REPLACE is a string, the tag will simply be appended to create the link.
  916. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  917. the placeholder \"%h\" will cause a url-encoded version of the tag to
  918. be inserted at that point (see the function `url-hexify-string').
  919. REPLACE may also be a function that will be called with the tag as the
  920. only argument to create the link, which should be returned as a string.
  921. See the manual for examples."
  922. :group 'org-link
  923. :type '(repeat
  924. (cons
  925. (string :tag "Protocol")
  926. (choice
  927. (string :tag "Format")
  928. (function)))))
  929. (defcustom org-descriptive-links t
  930. "Non-nil means, hide link part and only show description of bracket links.
  931. Bracket links are like [[link][description]]. This variable sets the initial
  932. state in new org-mode buffers. The setting can then be toggled on a
  933. per-buffer basis from the Org->Hyperlinks menu."
  934. :group 'org-link
  935. :type 'boolean)
  936. (defcustom org-link-file-path-type 'adaptive
  937. "How the path name in file links should be stored.
  938. Valid values are:
  939. relative Relative to the current directory, i.e. the directory of the file
  940. into which the link is being inserted.
  941. absolute Absolute path, if possible with ~ for home directory.
  942. noabbrev Absolute path, no abbreviation of home directory.
  943. adaptive Use relative path for files in the current directory and sub-
  944. directories of it. For other files, use an absolute path."
  945. :group 'org-link
  946. :type '(choice
  947. (const relative)
  948. (const absolute)
  949. (const noabbrev)
  950. (const adaptive)))
  951. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  952. "Types of links that should be activated in Org-mode files.
  953. This is a list of symbols, each leading to the activation of a certain link
  954. type. In principle, it does not hurt to turn on most link types - there may
  955. be a small gain when turning off unused link types. The types are:
  956. bracket The recommended [[link][description]] or [[link]] links with hiding.
  957. angular Links in angular brackets that may contain whitespace like
  958. <bbdb:Carsten Dominik>.
  959. plain Plain links in normal text, no whitespace, like http://google.com.
  960. radio Text that is matched by a radio target, see manual for details.
  961. tag Tag settings in a headline (link to tag search).
  962. date Time stamps (link to calendar).
  963. footnote Footnote labels.
  964. Changing this variable requires a restart of Emacs to become effective."
  965. :group 'org-link
  966. :type '(set :greedy t
  967. (const :tag "Double bracket links (new style)" bracket)
  968. (const :tag "Angular bracket links (old style)" angular)
  969. (const :tag "Plain text links" plain)
  970. (const :tag "Radio target matches" radio)
  971. (const :tag "Tags" tag)
  972. (const :tag "Timestamps" date)
  973. (const :tag "Footnotes" footnote)))
  974. (defcustom org-make-link-description-function nil
  975. "Function to use to generate link descriptions from links. If
  976. nil the link location will be used. This function must take two
  977. parameters; the first is the link and the second the description
  978. org-insert-link has generated, and should return the description
  979. to use."
  980. :group 'org-link
  981. :type 'function)
  982. (defgroup org-link-store nil
  983. "Options concerning storing links in Org-mode."
  984. :tag "Org Store Link"
  985. :group 'org-link)
  986. (defcustom org-email-link-description-format "Email %c: %.30s"
  987. "Format of the description part of a link to an email or usenet message.
  988. The following %-escapes will be replaced by corresponding information:
  989. %F full \"From\" field
  990. %f name, taken from \"From\" field, address if no name
  991. %T full \"To\" field
  992. %t first name in \"To\" field, address if no name
  993. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  994. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  995. %s subject
  996. %m message-id.
  997. You may use normal field width specification between the % and the letter.
  998. This is for example useful to limit the length of the subject.
  999. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1000. :group 'org-link-store
  1001. :type 'string)
  1002. (defcustom org-from-is-user-regexp
  1003. (let (r1 r2)
  1004. (when (and user-mail-address (not (string= user-mail-address "")))
  1005. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1006. (when (and user-full-name (not (string= user-full-name "")))
  1007. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1008. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1009. "Regexp matched against the \"From:\" header of an email or usenet message.
  1010. It should match if the message is from the user him/herself."
  1011. :group 'org-link-store
  1012. :type 'regexp)
  1013. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1014. "Non-nil means, storing a link to an Org file will use entry IDs.
  1015. Note that before this variable is even considered, org-id must be loaded,
  1016. so please customize `org-modules' and turn it on.
  1017. The variable can have the following values:
  1018. t Create an ID if needed to make a link to the current entry.
  1019. create-if-interactive
  1020. If `org-store-link' is called directly (interactively, as a user
  1021. command), do create an ID to support the link. But when doing the
  1022. job for remember, only use the ID if it already exists. The
  1023. purpose of this setting is to avoid proliferation of unwanted
  1024. IDs, just because you happen to be in an Org file when you
  1025. call `org-remember' that automatically and preemptively
  1026. creates a link. If you do want to get an ID link in a remember
  1027. template to an entry not having an ID, create it first by
  1028. explicitly creating a link to it, using `C-c C-l' first.
  1029. create-if-interactive-and-no-custom-id
  1030. Like create-if-interactive, but do not create an ID if there is
  1031. a CUSTOM_ID property defined in the entry. This is the default.
  1032. use-existing
  1033. Use existing ID, do not create one.
  1034. nil Never use an ID to make a link, instead link using a text search for
  1035. the headline text."
  1036. :group 'org-link-store
  1037. :type '(choice
  1038. (const :tag "Create ID to make link" t)
  1039. (const :tag "Create if storing link interactively"
  1040. create-if-interactive)
  1041. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1042. create-if-interactive-and-no-custom-id)
  1043. (const :tag "Only use existing" use-existing)
  1044. (const :tag "Do not use ID to create link" nil)))
  1045. (defcustom org-context-in-file-links t
  1046. "Non-nil means, file links from `org-store-link' contain context.
  1047. A search string will be added to the file name with :: as separator and
  1048. used to find the context when the link is activated by the command
  1049. `org-open-at-point'.
  1050. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1051. negates this setting for the duration of the command."
  1052. :group 'org-link-store
  1053. :type 'boolean)
  1054. (defcustom org-keep-stored-link-after-insertion nil
  1055. "Non-nil means, keep link in list for entire session.
  1056. The command `org-store-link' adds a link pointing to the current
  1057. location to an internal list. These links accumulate during a session.
  1058. The command `org-insert-link' can be used to insert links into any
  1059. Org-mode file (offering completion for all stored links). When this
  1060. option is nil, every link which has been inserted once using \\[org-insert-link]
  1061. will be removed from the list, to make completing the unused links
  1062. more efficient."
  1063. :group 'org-link-store
  1064. :type 'boolean)
  1065. (defgroup org-link-follow nil
  1066. "Options concerning following links in Org-mode."
  1067. :tag "Org Follow Link"
  1068. :group 'org-link)
  1069. (defcustom org-link-translation-function nil
  1070. "Function to translate links with different syntax to Org syntax.
  1071. This can be used to translate links created for example by the Planner
  1072. or emacs-wiki packages to Org syntax.
  1073. The function must accept two parameters, a TYPE containing the link
  1074. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1075. which is everything after the link protocol. It should return a cons
  1076. with possibly modified values of type and path.
  1077. Org contains a function for this, so if you set this variable to
  1078. `org-translate-link-from-planner', you should be able follow many
  1079. links created by planner."
  1080. :group 'org-link-follow
  1081. :type 'function)
  1082. (defcustom org-follow-link-hook nil
  1083. "Hook that is run after a link has been followed."
  1084. :group 'org-link-follow
  1085. :type 'hook)
  1086. (defcustom org-tab-follows-link nil
  1087. "Non-nil means, on links TAB will follow the link.
  1088. Needs to be set before org.el is loaded.
  1089. This really should not be used, it does not make sense, and the
  1090. implementation is bad."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defcustom org-return-follows-link nil
  1094. "Non-nil means, on links RET will follow the link.
  1095. Needs to be set before org.el is loaded."
  1096. :group 'org-link-follow
  1097. :type 'boolean)
  1098. (defcustom org-mouse-1-follows-link
  1099. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1100. "Non-nil means, mouse-1 on a link will follow the link.
  1101. A longer mouse click will still set point. Does not work on XEmacs.
  1102. Needs to be set before org.el is loaded."
  1103. :group 'org-link-follow
  1104. :type 'boolean)
  1105. (defcustom org-mark-ring-length 4
  1106. "Number of different positions to be recorded in the ring
  1107. Changing this requires a restart of Emacs to work correctly."
  1108. :group 'org-link-follow
  1109. :type 'integer)
  1110. (defcustom org-link-frame-setup
  1111. '((vm . vm-visit-folder-other-frame)
  1112. (gnus . gnus-other-frame)
  1113. (file . find-file-other-window))
  1114. "Setup the frame configuration for following links.
  1115. When following a link with Emacs, it may often be useful to display
  1116. this link in another window or frame. This variable can be used to
  1117. set this up for the different types of links.
  1118. For VM, use any of
  1119. `vm-visit-folder'
  1120. `vm-visit-folder-other-frame'
  1121. For Gnus, use any of
  1122. `gnus'
  1123. `gnus-other-frame'
  1124. `org-gnus-no-new-news'
  1125. For FILE, use any of
  1126. `find-file'
  1127. `find-file-other-window'
  1128. `find-file-other-frame'
  1129. For the calendar, use the variable `calendar-setup'.
  1130. For BBDB, it is currently only possible to display the matches in
  1131. another window."
  1132. :group 'org-link-follow
  1133. :type '(list
  1134. (cons (const vm)
  1135. (choice
  1136. (const vm-visit-folder)
  1137. (const vm-visit-folder-other-window)
  1138. (const vm-visit-folder-other-frame)))
  1139. (cons (const gnus)
  1140. (choice
  1141. (const gnus)
  1142. (const gnus-other-frame)
  1143. (const org-gnus-no-new-news)))
  1144. (cons (const file)
  1145. (choice
  1146. (const find-file)
  1147. (const find-file-other-window)
  1148. (const find-file-other-frame)))))
  1149. (defcustom org-display-internal-link-with-indirect-buffer nil
  1150. "Non-nil means, use indirect buffer to display infile links.
  1151. Activating internal links (from one location in a file to another location
  1152. in the same file) normally just jumps to the location. When the link is
  1153. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1154. another window. When this option is set, the other window actually displays
  1155. an indirect buffer clone of the current buffer, to avoid any visibility
  1156. changes to the current buffer."
  1157. :group 'org-link-follow
  1158. :type 'boolean)
  1159. (defcustom org-open-non-existing-files nil
  1160. "Non-nil means, `org-open-file' will open non-existing files.
  1161. When nil, an error will be generated.
  1162. This variable applies only to external applications because they
  1163. might choke on non-existing files. If the link is to a file that
  1164. will be openend in Emacs, the variable is ignored."
  1165. :group 'org-link-follow
  1166. :type 'boolean)
  1167. (defcustom org-open-directory-means-index-dot-org nil
  1168. "Non-nil means, a link to a directory really means to index.org.
  1169. When nil, following a directory link will run dired or open a finder/explorer
  1170. window on that directory."
  1171. :group 'org-link-follow
  1172. :type 'boolean)
  1173. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1174. "Function and arguments to call for following mailto links.
  1175. This is a list with the first element being a lisp function, and the
  1176. remaining elements being arguments to the function. In string arguments,
  1177. %a will be replaced by the address, and %s will be replaced by the subject
  1178. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1179. :group 'org-link-follow
  1180. :type '(choice
  1181. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1182. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1183. (const :tag "message-mail" (message-mail "%a" "%s"))
  1184. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1185. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1186. "Non-nil means, ask for confirmation before executing shell links.
  1187. Shell links can be dangerous: just think about a link
  1188. [[shell:rm -rf ~/*][Google Search]]
  1189. This link would show up in your Org-mode document as \"Google Search\",
  1190. but really it would remove your entire home directory.
  1191. Therefore we advise against setting this variable to nil.
  1192. Just change it to `y-or-n-p' if you want to confirm with a
  1193. single keystroke rather than having to type \"yes\"."
  1194. :group 'org-link-follow
  1195. :type '(choice
  1196. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1197. (const :tag "with y-or-n (faster)" y-or-n-p)
  1198. (const :tag "no confirmation (dangerous)" nil)))
  1199. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1200. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1201. Elisp links can be dangerous: just think about a link
  1202. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1203. This link would show up in your Org-mode document as \"Google Search\",
  1204. but really it would remove your entire home directory.
  1205. Therefore we advise against setting this variable to nil.
  1206. Just change it to `y-or-n-p' if you want to confirm with a
  1207. single keystroke rather than having to type \"yes\"."
  1208. :group 'org-link-follow
  1209. :type '(choice
  1210. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1211. (const :tag "with y-or-n (faster)" y-or-n-p)
  1212. (const :tag "no confirmation (dangerous)" nil)))
  1213. (defconst org-file-apps-defaults-gnu
  1214. '((remote . emacs)
  1215. (system . mailcap)
  1216. (t . mailcap))
  1217. "Default file applications on a UNIX or GNU/Linux system.
  1218. See `org-file-apps'.")
  1219. (defconst org-file-apps-defaults-macosx
  1220. '((remote . emacs)
  1221. (t . "open %s")
  1222. (system . "open %s")
  1223. ("ps.gz" . "gv %s")
  1224. ("eps.gz" . "gv %s")
  1225. ("dvi" . "xdvi %s")
  1226. ("fig" . "xfig %s"))
  1227. "Default file applications on a MacOS X system.
  1228. The system \"open\" is known as a default, but we use X11 applications
  1229. for some files for which the OS does not have a good default.
  1230. See `org-file-apps'.")
  1231. (defconst org-file-apps-defaults-windowsnt
  1232. (list
  1233. '(remote . emacs)
  1234. (cons t
  1235. (list (if (featurep 'xemacs)
  1236. 'mswindows-shell-execute
  1237. 'w32-shell-execute)
  1238. "open" 'file))
  1239. (cons 'system
  1240. (list (if (featurep 'xemacs)
  1241. 'mswindows-shell-execute
  1242. 'w32-shell-execute)
  1243. "open" 'file)))
  1244. "Default file applications on a Windows NT system.
  1245. The system \"open\" is used for most files.
  1246. See `org-file-apps'.")
  1247. (defcustom org-file-apps
  1248. '(
  1249. (auto-mode . emacs)
  1250. ("\\.mm\\'" . default)
  1251. ("\\.x?html?\\'" . default)
  1252. ("\\.pdf\\'" . default)
  1253. )
  1254. "External applications for opening `file:path' items in a document.
  1255. Org-mode uses system defaults for different file types, but
  1256. you can use this variable to set the application for a given file
  1257. extension. The entries in this list are cons cells where the car identifies
  1258. files and the cdr the corresponding command. Possible values for the
  1259. file identifier are
  1260. \"regex\" Regular expression matched against the file name. For backward
  1261. compatibility, this can also be a string with only alphanumeric
  1262. characters, which is then interpreted as an extension.
  1263. `directory' Matches a directory
  1264. `remote' Matches a remote file, accessible through tramp or efs.
  1265. Remote files most likely should be visited through Emacs
  1266. because external applications cannot handle such paths.
  1267. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1268. so all files Emacs knows how to handle. Using this with
  1269. command `emacs' will open most files in Emacs. Beware that this
  1270. will also open html files inside Emacs, unless you add
  1271. (\"html\" . default) to the list as well.
  1272. t Default for files not matched by any of the other options.
  1273. `system' The system command to open files, like `open' on Windows
  1274. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1275. that will be selected if you call `C-c C-o' with a double
  1276. `C-u C-u' prefix.
  1277. Possible values for the command are:
  1278. `emacs' The file will be visited by the current Emacs process.
  1279. `default' Use the default application for this file type, which is the
  1280. association for t in the list, most likely in the system-specific
  1281. part.
  1282. This can be used to overrule an unwanted setting in the
  1283. system-specific variable.
  1284. `system' Use the system command for opening files, like \"open\".
  1285. This command is specified by the entry whose car is `system'.
  1286. Most likely, the system-specific version of this variable
  1287. does define this command, but you can overrule/replace it
  1288. here.
  1289. string A command to be executed by a shell; %s will be replaced
  1290. by the path to the file.
  1291. sexp A Lisp form which will be evaluated. The file path will
  1292. be available in the Lisp variable `file'.
  1293. For more examples, see the system specific constants
  1294. `org-file-apps-defaults-macosx'
  1295. `org-file-apps-defaults-windowsnt'
  1296. `org-file-apps-defaults-gnu'."
  1297. :group 'org-link-follow
  1298. :type '(repeat
  1299. (cons (choice :value ""
  1300. (string :tag "Extension")
  1301. (const :tag "System command to open files" system)
  1302. (const :tag "Default for unrecognized files" t)
  1303. (const :tag "Remote file" remote)
  1304. (const :tag "Links to a directory" directory)
  1305. (const :tag "Any files that have Emacs modes"
  1306. auto-mode))
  1307. (choice :value ""
  1308. (const :tag "Visit with Emacs" emacs)
  1309. (const :tag "Use default" default)
  1310. (const :tag "Use the system command" system)
  1311. (string :tag "Command")
  1312. (sexp :tag "Lisp form")))))
  1313. (defgroup org-refile nil
  1314. "Options concerning refiling entries in Org-mode."
  1315. :tag "Org Refile"
  1316. :group 'org)
  1317. (defcustom org-directory "~/org"
  1318. "Directory with org files.
  1319. This is just a default location to look for Org files. There is no need
  1320. at all to put your files into this directory. It is only used in the
  1321. following situations:
  1322. 1. When a remember template specifies a target file that is not an
  1323. absolute path. The path will then be interpreted relative to
  1324. `org-directory'
  1325. 2. When a remember note is filed away in an interactive way (when exiting the
  1326. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1327. with `org-directory' as the default path."
  1328. :group 'org-refile
  1329. :group 'org-remember
  1330. :type 'directory)
  1331. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1332. "Default target for storing notes.
  1333. Used by the hooks for remember.el. This can be a string, or nil to mean
  1334. the value of `remember-data-file'.
  1335. You can set this on a per-template basis with the variable
  1336. `org-remember-templates'."
  1337. :group 'org-refile
  1338. :group 'org-remember
  1339. :type '(choice
  1340. (const :tag "Default from remember-data-file" nil)
  1341. file))
  1342. (defcustom org-goto-interface 'outline
  1343. "The default interface to be used for `org-goto'.
  1344. Allowed values are:
  1345. outline The interface shows an outline of the relevant file
  1346. and the correct heading is found by moving through
  1347. the outline or by searching with incremental search.
  1348. outline-path-completion Headlines in the current buffer are offered via
  1349. completion. This is the interface also used by
  1350. the refile command."
  1351. :group 'org-refile
  1352. :type '(choice
  1353. (const :tag "Outline" outline)
  1354. (const :tag "Outline-path-completion" outline-path-completion)))
  1355. (defcustom org-goto-max-level 5
  1356. "Maximum level to be considered when running org-goto with refile interface."
  1357. :group 'org-refile
  1358. :type 'integer)
  1359. (defcustom org-reverse-note-order nil
  1360. "Non-nil means, store new notes at the beginning of a file or entry.
  1361. When nil, new notes will be filed to the end of a file or entry.
  1362. This can also be a list with cons cells of regular expressions that
  1363. are matched against file names, and values."
  1364. :group 'org-remember
  1365. :group 'org-refile
  1366. :type '(choice
  1367. (const :tag "Reverse always" t)
  1368. (const :tag "Reverse never" nil)
  1369. (repeat :tag "By file name regexp"
  1370. (cons regexp boolean))))
  1371. (defcustom org-refile-targets nil
  1372. "Targets for refiling entries with \\[org-refile].
  1373. This is list of cons cells. Each cell contains:
  1374. - a specification of the files to be considered, either a list of files,
  1375. or a symbol whose function or variable value will be used to retrieve
  1376. a file name or a list of file names. If you use `org-agenda-files' for
  1377. that, all agenda files will be scanned for targets. Nil means, consider
  1378. headings in the current buffer.
  1379. - A specification of how to find candidate refile targets. This may be
  1380. any of:
  1381. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1382. This tag has to be present in all target headlines, inheritance will
  1383. not be considered.
  1384. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1385. todo keyword.
  1386. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1387. headlines that are refiling targets.
  1388. - a cons cell (:level . N). Any headline of level N is considered a target.
  1389. Note that, when `org-odd-levels-only' is set, level corresponds to
  1390. order in hierarchy, not to the number of stars.
  1391. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1392. Note that, when `org-odd-levels-only' is set, level corresponds to
  1393. order in hierarchy, not to the number of stars.
  1394. You can set the variable `org-refile-target-verify-function' to a function
  1395. to verify each headline found by the simple critery above.
  1396. When this variable is nil, all top-level headlines in the current buffer
  1397. are used, equivalent to the value `((nil . (:level . 1))'."
  1398. :group 'org-refile
  1399. :type '(repeat
  1400. (cons
  1401. (choice :value org-agenda-files
  1402. (const :tag "All agenda files" org-agenda-files)
  1403. (const :tag "Current buffer" nil)
  1404. (function) (variable) (file))
  1405. (choice :tag "Identify target headline by"
  1406. (cons :tag "Specific tag" (const :value :tag) (string))
  1407. (cons :tag "TODO keyword" (const :value :todo) (string))
  1408. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1409. (cons :tag "Level number" (const :value :level) (integer))
  1410. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1411. (defcustom org-refile-target-verify-function nil
  1412. "Function to verify if the headline at point should be a refile target.
  1413. The function will be called without arguments, with point at the
  1414. beginning of the headline. It should return t and leave point
  1415. where it is if the headline is a valid target for refiling.
  1416. If the target should not be selected, the function must return nil.
  1417. In addition to this, it may move point to a place from where the search
  1418. should be continued. For example, the function may decide that the entire
  1419. subtree of the current entry should be excluded and move point to the end
  1420. of the subtree."
  1421. :group 'org-refile
  1422. :type 'function)
  1423. (defcustom org-refile-use-outline-path nil
  1424. "Non-nil means, provide refile targets as paths.
  1425. So a level 3 headline will be available as level1/level2/level3.
  1426. When the value is `file', also include the file name (without directory)
  1427. into the path. In this case, you can also stop the completion after
  1428. the file name, to get entries inserted as top level in the file.
  1429. When `full-file-path', include the full file path."
  1430. :group 'org-refile
  1431. :type '(choice
  1432. (const :tag "Not" nil)
  1433. (const :tag "Yes" t)
  1434. (const :tag "Start with file name" file)
  1435. (const :tag "Start with full file path" full-file-path)))
  1436. (defcustom org-outline-path-complete-in-steps t
  1437. "Non-nil means, complete the outline path in hierarchical steps.
  1438. When Org-mode uses the refile interface to select an outline path
  1439. \(see variable `org-refile-use-outline-path'), the completion of
  1440. the path can be done is a single go, or if can be done in steps down
  1441. the headline hierarchy. Going in steps is probably the best if you
  1442. do not use a special completion package like `ido' or `icicles'.
  1443. However, when using these packages, going in one step can be very
  1444. fast, while still showing the whole path to the entry."
  1445. :group 'org-refile
  1446. :type 'boolean)
  1447. (defcustom org-refile-allow-creating-parent-nodes nil
  1448. "Non-nil means, allow to create new nodes as refile targets.
  1449. New nodes are then created by adding \"/new node name\" to the completion
  1450. of an existing node. When the value of this variable is `confirm',
  1451. new node creation must be confirmed by the user (recommended)
  1452. When nil, the completion must match an existing entry.
  1453. Note that, if the new heading is not seen by the criteria
  1454. listed in `org-refile-targets', multiple instances of the same
  1455. heading would be created by trying again to file under the new
  1456. heading."
  1457. :group 'org-refile
  1458. :type '(choice
  1459. (const :tag "Never" nil)
  1460. (const :tag "Always" t)
  1461. (const :tag "Prompt for confirmation" confirm)))
  1462. (defgroup org-todo nil
  1463. "Options concerning TODO items in Org-mode."
  1464. :tag "Org TODO"
  1465. :group 'org)
  1466. (defgroup org-progress nil
  1467. "Options concerning Progress logging in Org-mode."
  1468. :tag "Org Progress"
  1469. :group 'org-time)
  1470. (defvar org-todo-interpretation-widgets
  1471. '(
  1472. (:tag "Sequence (cycling hits every state)" sequence)
  1473. (:tag "Type (cycling directly to DONE)" type))
  1474. "The available interpretation symbols for customizing
  1475. `org-todo-keywords'.
  1476. Interested libraries should add to this list.")
  1477. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1478. "List of TODO entry keyword sequences and their interpretation.
  1479. \\<org-mode-map>This is a list of sequences.
  1480. Each sequence starts with a symbol, either `sequence' or `type',
  1481. indicating if the keywords should be interpreted as a sequence of
  1482. action steps, or as different types of TODO items. The first
  1483. keywords are states requiring action - these states will select a headline
  1484. for inclusion into the global TODO list Org-mode produces. If one of
  1485. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1486. signify that no further action is necessary. If \"|\" is not found,
  1487. the last keyword is treated as the only DONE state of the sequence.
  1488. The command \\[org-todo] cycles an entry through these states, and one
  1489. additional state where no keyword is present. For details about this
  1490. cycling, see the manual.
  1491. TODO keywords and interpretation can also be set on a per-file basis with
  1492. the special #+SEQ_TODO and #+TYP_TODO lines.
  1493. Each keyword can optionally specify a character for fast state selection
  1494. \(in combination with the variable `org-use-fast-todo-selection')
  1495. and specifiers for state change logging, using the same syntax
  1496. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1497. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1498. indicates to record a time stamp each time this state is selected.
  1499. Each keyword may also specify if a timestamp or a note should be
  1500. recorded when entering or leaving the state, by adding additional
  1501. characters in the parenthesis after the keyword. This looks like this:
  1502. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1503. record only the time of the state change. With X and Y being either
  1504. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1505. Y when leaving the state if and only if the *target* state does not
  1506. define X. You may omit any of the fast-selection key or X or /Y,
  1507. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1508. For backward compatibility, this variable may also be just a list
  1509. of keywords - in this case the interpretation (sequence or type) will be
  1510. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1511. :group 'org-todo
  1512. :group 'org-keywords
  1513. :type '(choice
  1514. (repeat :tag "Old syntax, just keywords"
  1515. (string :tag "Keyword"))
  1516. (repeat :tag "New syntax"
  1517. (cons
  1518. (choice
  1519. :tag "Interpretation"
  1520. ;;Quick and dirty way to see
  1521. ;;`org-todo-interpretations'. This takes the
  1522. ;;place of item arguments
  1523. :convert-widget
  1524. (lambda (widget)
  1525. (widget-put widget
  1526. :args (mapcar
  1527. #'(lambda (x)
  1528. (widget-convert
  1529. (cons 'const x)))
  1530. org-todo-interpretation-widgets))
  1531. widget))
  1532. (repeat
  1533. (string :tag "Keyword"))))))
  1534. (defvar org-todo-keywords-1 nil
  1535. "All TODO and DONE keywords active in a buffer.")
  1536. (make-variable-buffer-local 'org-todo-keywords-1)
  1537. (defvar org-todo-keywords-for-agenda nil)
  1538. (defvar org-done-keywords-for-agenda nil)
  1539. (defvar org-drawers-for-agenda nil)
  1540. (defvar org-todo-keyword-alist-for-agenda nil)
  1541. (defvar org-tag-alist-for-agenda nil)
  1542. (defvar org-agenda-contributing-files nil)
  1543. (defvar org-not-done-keywords nil)
  1544. (make-variable-buffer-local 'org-not-done-keywords)
  1545. (defvar org-done-keywords nil)
  1546. (make-variable-buffer-local 'org-done-keywords)
  1547. (defvar org-todo-heads nil)
  1548. (make-variable-buffer-local 'org-todo-heads)
  1549. (defvar org-todo-sets nil)
  1550. (make-variable-buffer-local 'org-todo-sets)
  1551. (defvar org-todo-log-states nil)
  1552. (make-variable-buffer-local 'org-todo-log-states)
  1553. (defvar org-todo-kwd-alist nil)
  1554. (make-variable-buffer-local 'org-todo-kwd-alist)
  1555. (defvar org-todo-key-alist nil)
  1556. (make-variable-buffer-local 'org-todo-key-alist)
  1557. (defvar org-todo-key-trigger nil)
  1558. (make-variable-buffer-local 'org-todo-key-trigger)
  1559. (defcustom org-todo-interpretation 'sequence
  1560. "Controls how TODO keywords are interpreted.
  1561. This variable is in principle obsolete and is only used for
  1562. backward compatibility, if the interpretation of todo keywords is
  1563. not given already in `org-todo-keywords'. See that variable for
  1564. more information."
  1565. :group 'org-todo
  1566. :group 'org-keywords
  1567. :type '(choice (const sequence)
  1568. (const type)))
  1569. (defcustom org-use-fast-todo-selection t
  1570. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1571. This variable describes if and under what circumstances the cycling
  1572. mechanism for TODO keywords will be replaced by a single-key, direct
  1573. selection scheme.
  1574. When nil, fast selection is never used.
  1575. When the symbol `prefix', it will be used when `org-todo' is called with
  1576. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1577. in an agenda buffer.
  1578. When t, fast selection is used by default. In this case, the prefix
  1579. argument forces cycling instead.
  1580. In all cases, the special interface is only used if access keys have actually
  1581. been assigned by the user, i.e. if keywords in the configuration are followed
  1582. by a letter in parenthesis, like TODO(t)."
  1583. :group 'org-todo
  1584. :type '(choice
  1585. (const :tag "Never" nil)
  1586. (const :tag "By default" t)
  1587. (const :tag "Only with C-u C-c C-t" prefix)))
  1588. (defcustom org-provide-todo-statistics t
  1589. "Non-nil means, update todo statistics after insert and toggle.
  1590. ALL-HEADLINES means update todo statistics by including headlines
  1591. with no TODO keyword as well, counting them as not done.
  1592. A list of TODO keywords means the same, but skip keywords that are
  1593. not in this list.
  1594. When this is set, todo statistics is updated in the parent of the
  1595. current entry each time a todo state is changed."
  1596. :group 'org-todo
  1597. :type '(choice
  1598. (const :tag "Yes, only for TODO entries" t)
  1599. (const :tag "Yes, including all entries" 'all-headlines)
  1600. (repeat :tag "Yes, for TODOs in this list"
  1601. (string :tag "TODO keyword"))
  1602. (other :tag "No TODO statistics" nil)))
  1603. (defcustom org-hierarchical-todo-statistics t
  1604. "Non-nil means, TODO statistics covers just direct children.
  1605. When nil, all entries in the subtree are considered.
  1606. This has only an effect if `org-provide-todo-statistics' is set.
  1607. To set this to nil for only a single subtree, use a COOKIE_DATA
  1608. property and include the word \"recursive\" into the value."
  1609. :group 'org-todo
  1610. :type 'boolean)
  1611. (defcustom org-after-todo-state-change-hook nil
  1612. "Hook which is run after the state of a TODO item was changed.
  1613. The new state (a string with a TODO keyword, or nil) is available in the
  1614. Lisp variable `state'."
  1615. :group 'org-todo
  1616. :type 'hook)
  1617. (defvar org-blocker-hook nil
  1618. "Hook for functions that are allowed to block a state change.
  1619. Each function gets as its single argument a property list, see
  1620. `org-trigger-hook' for more information about this list.
  1621. If any of the functions in this hook returns nil, the state change
  1622. is blocked.")
  1623. (defvar org-trigger-hook nil
  1624. "Hook for functions that are triggered by a state change.
  1625. Each function gets as its single argument a property list with at least
  1626. the following elements:
  1627. (:type type-of-change :position pos-at-entry-start
  1628. :from old-state :to new-state)
  1629. Depending on the type, more properties may be present.
  1630. This mechanism is currently implemented for:
  1631. TODO state changes
  1632. ------------------
  1633. :type todo-state-change
  1634. :from previous state (keyword as a string), or nil, or a symbol
  1635. 'todo' or 'done', to indicate the general type of state.
  1636. :to new state, like in :from")
  1637. (defcustom org-enforce-todo-dependencies nil
  1638. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1639. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1640. be blocked if any prior sibling is not yet done.
  1641. Finally, if the parent is blocked because of ordered siblings of its own,
  1642. the child will also be blocked.
  1643. This variable needs to be set before org.el is loaded, and you need to
  1644. restart Emacs after a change to make the change effective. The only way
  1645. to change is while Emacs is running is through the customize interface."
  1646. :set (lambda (var val)
  1647. (set var val)
  1648. (if val
  1649. (add-hook 'org-blocker-hook
  1650. 'org-block-todo-from-children-or-siblings-or-parent)
  1651. (remove-hook 'org-blocker-hook
  1652. 'org-block-todo-from-children-or-siblings-or-parent)))
  1653. :group 'org-todo
  1654. :type 'boolean)
  1655. (defcustom org-enforce-todo-checkbox-dependencies nil
  1656. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1657. When this is nil, checkboxes have no influence on switching TODO states.
  1658. When non-nil, you first need to check off all check boxes before the TODO
  1659. entry can be switched to DONE.
  1660. This variable needs to be set before org.el is loaded, and you need to
  1661. restart Emacs after a change to make the change effective. The only way
  1662. to change is while Emacs is running is through the customize interface."
  1663. :set (lambda (var val)
  1664. (set var val)
  1665. (if val
  1666. (add-hook 'org-blocker-hook
  1667. 'org-block-todo-from-checkboxes)
  1668. (remove-hook 'org-blocker-hook
  1669. 'org-block-todo-from-checkboxes)))
  1670. :group 'org-todo
  1671. :type 'boolean)
  1672. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1673. "Non-nil means, inserting a TODO heading is treated as state change.
  1674. So when the command \\[org-insert-todo-heading] is used, state change
  1675. logging will apply if appropriate. When nil, the new TODO item will
  1676. be inserted directly, and no logging will take place."
  1677. :group 'org-todo
  1678. :type 'boolean)
  1679. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1680. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1681. This is the default behavior. However, setting this to nil allows a
  1682. convenient way to select a TODO state and bypass any logging associated
  1683. with that."
  1684. :group 'org-todo
  1685. :type 'boolean)
  1686. (defcustom org-todo-state-tags-triggers nil
  1687. "Tag changes that should be triggered by TODO state changes.
  1688. This is a list. Each entry is
  1689. (state-change (tag . flag) .......)
  1690. State-change can be a string with a state, and empty string to indicate the
  1691. state that has no TODO keyword, or it can be one of the symbols `todo'
  1692. or `done', meaning any not-done or done state, respectively."
  1693. :group 'org-todo
  1694. :group 'org-tags
  1695. :type '(repeat
  1696. (cons (choice :tag "When changing to"
  1697. (const :tag "Not-done state" todo)
  1698. (const :tag "Done state" done)
  1699. (string :tag "State"))
  1700. (repeat
  1701. (cons :tag "Tag action"
  1702. (string :tag "Tag")
  1703. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1704. (defcustom org-log-done nil
  1705. "Information to record when a task moves to the DONE state.
  1706. Possible values are:
  1707. nil Don't add anything, just change the keyword
  1708. time Add a time stamp to the task
  1709. note Prompt a closing note and add it with template `org-log-note-headings'
  1710. This option can also be set with on a per-file-basis with
  1711. #+STARTUP: nologdone
  1712. #+STARTUP: logdone
  1713. #+STARTUP: lognotedone
  1714. You can have local logging settings for a subtree by setting the LOGGING
  1715. property to one or more of these keywords."
  1716. :group 'org-todo
  1717. :group 'org-progress
  1718. :type '(choice
  1719. (const :tag "No logging" nil)
  1720. (const :tag "Record CLOSED timestamp" time)
  1721. (const :tag "Record CLOSED timestamp with closing note." note)))
  1722. ;; Normalize old uses of org-log-done.
  1723. (cond
  1724. ((eq org-log-done t) (setq org-log-done 'time))
  1725. ((and (listp org-log-done) (memq 'done org-log-done))
  1726. (setq org-log-done 'note)))
  1727. (defcustom org-log-note-clock-out nil
  1728. "Non-nil means, record a note when clocking out of an item.
  1729. This can also be configured on a per-file basis by adding one of
  1730. the following lines anywhere in the buffer:
  1731. #+STARTUP: lognoteclock-out
  1732. #+STARTUP: nolognoteclock-out"
  1733. :group 'org-todo
  1734. :group 'org-progress
  1735. :type 'boolean)
  1736. (defcustom org-log-done-with-time t
  1737. "Non-nil means, the CLOSED time stamp will contain date and time.
  1738. When nil, only the date will be recorded."
  1739. :group 'org-progress
  1740. :type 'boolean)
  1741. (defcustom org-log-note-headings
  1742. '((done . "CLOSING NOTE %t")
  1743. (state . "State %-12s from %-12S %t")
  1744. (note . "Note taken on %t")
  1745. (clock-out . ""))
  1746. "Headings for notes added to entries.
  1747. The value is an alist, with the car being a symbol indicating the note
  1748. context, and the cdr is the heading to be used. The heading may also be the
  1749. empty string.
  1750. %t in the heading will be replaced by a time stamp.
  1751. %s will be replaced by the new TODO state, in double quotes.
  1752. %S will be replaced by the old TODO state, in double quotes.
  1753. %u will be replaced by the user name.
  1754. %U will be replaced by the full user name."
  1755. :group 'org-todo
  1756. :group 'org-progress
  1757. :type '(list :greedy t
  1758. (cons (const :tag "Heading when closing an item" done) string)
  1759. (cons (const :tag
  1760. "Heading when changing todo state (todo sequence only)"
  1761. state) string)
  1762. (cons (const :tag "Heading when just taking a note" note) string)
  1763. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1764. (unless (assq 'note org-log-note-headings)
  1765. (push '(note . "%t") org-log-note-headings))
  1766. (defcustom org-log-into-drawer nil
  1767. "Non-nil means, insert state change notes and time stamps into a drawer.
  1768. When nil, state changes notes will be inserted after the headline and
  1769. any scheduling and clock lines, but not inside a drawer.
  1770. The value of this variable should be the name of the drawer to use.
  1771. LOGBOOK is proposed at the default drawer for this purpose, you can
  1772. also set this to a string to define the drawer of your choice.
  1773. A value of t is also allowed, representing \"LOGBOOK\".
  1774. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1775. will be ignored.
  1776. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1777. a subtree."
  1778. :group 'org-todo
  1779. :group 'org-progress
  1780. :type '(choice
  1781. (const :tag "Not into a drawer" nil)
  1782. (const :tag "LOGBOOK" t)
  1783. (string :tag "Other")))
  1784. (if (fboundp 'defvaralias)
  1785. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1786. (defun org-log-into-drawer ()
  1787. "Return the value of `org-log-into-drawer', but let properties overrule.
  1788. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1789. used instead of the default value."
  1790. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1791. (cond
  1792. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1793. ((equal p "t") "LOGBOOK")
  1794. (t p))))
  1795. (defcustom org-log-state-notes-insert-after-drawers nil
  1796. "Non-nil means, insert state change notes after any drawers in entry.
  1797. Only the drawers that *immediately* follow the headline and the
  1798. deadline/scheduled line are skipped.
  1799. When nil, insert notes right after the heading and perhaps the line
  1800. with deadline/scheduling if present.
  1801. This variable will have no effect if `org-log-into-drawer' is
  1802. set."
  1803. :group 'org-todo
  1804. :group 'org-progress
  1805. :type 'boolean)
  1806. (defcustom org-log-states-order-reversed t
  1807. "Non-nil means, the latest state change note will be directly after heading.
  1808. When nil, the notes will be orderer according to time."
  1809. :group 'org-todo
  1810. :group 'org-progress
  1811. :type 'boolean)
  1812. (defcustom org-log-repeat 'time
  1813. "Non-nil means, record moving through the DONE state when triggering repeat.
  1814. An auto-repeating task is immediately switched back to TODO when
  1815. marked DONE. If you are not logging state changes (by adding \"@\"
  1816. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1817. record a closing note, there will be no record of the task moving
  1818. through DONE. This variable forces taking a note anyway.
  1819. nil Don't force a record
  1820. time Record a time stamp
  1821. note Record a note
  1822. This option can also be set with on a per-file-basis with
  1823. #+STARTUP: logrepeat
  1824. #+STARTUP: lognoterepeat
  1825. #+STARTUP: nologrepeat
  1826. You can have local logging settings for a subtree by setting the LOGGING
  1827. property to one or more of these keywords."
  1828. :group 'org-todo
  1829. :group 'org-progress
  1830. :type '(choice
  1831. (const :tag "Don't force a record" nil)
  1832. (const :tag "Force recording the DONE state" time)
  1833. (const :tag "Force recording a note with the DONE state" note)))
  1834. (defgroup org-priorities nil
  1835. "Priorities in Org-mode."
  1836. :tag "Org Priorities"
  1837. :group 'org-todo)
  1838. (defcustom org-enable-priority-commands t
  1839. "Non-nil means, priority commands are active.
  1840. When nil, these commands will be disabled, so that you never accidentally
  1841. set a priority."
  1842. :group 'org-priorities
  1843. :type 'boolean)
  1844. (defcustom org-highest-priority ?A
  1845. "The highest priority of TODO items. A character like ?A, ?B etc.
  1846. Must have a smaller ASCII number than `org-lowest-priority'."
  1847. :group 'org-priorities
  1848. :type 'character)
  1849. (defcustom org-lowest-priority ?C
  1850. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1851. Must have a larger ASCII number than `org-highest-priority'."
  1852. :group 'org-priorities
  1853. :type 'character)
  1854. (defcustom org-default-priority ?B
  1855. "The default priority of TODO items.
  1856. This is the priority an item get if no explicit priority is given."
  1857. :group 'org-priorities
  1858. :type 'character)
  1859. (defcustom org-priority-start-cycle-with-default t
  1860. "Non-nil means, start with default priority when starting to cycle.
  1861. When this is nil, the first step in the cycle will be (depending on the
  1862. command used) one higher or lower that the default priority."
  1863. :group 'org-priorities
  1864. :type 'boolean)
  1865. (defgroup org-time nil
  1866. "Options concerning time stamps and deadlines in Org-mode."
  1867. :tag "Org Time"
  1868. :group 'org)
  1869. (defcustom org-insert-labeled-timestamps-at-point nil
  1870. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1871. When nil, these labeled time stamps are forces into the second line of an
  1872. entry, just after the headline. When scheduling from the global TODO list,
  1873. the time stamp will always be forced into the second line."
  1874. :group 'org-time
  1875. :type 'boolean)
  1876. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1877. "Formats for `format-time-string' which are used for time stamps.
  1878. It is not recommended to change this constant.")
  1879. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1880. "Number of minutes to round time stamps to.
  1881. These are two values, the first applies when first creating a time stamp.
  1882. The second applies when changing it with the commands `S-up' and `S-down'.
  1883. When changing the time stamp, this means that it will change in steps
  1884. of N minutes, as given by the second value.
  1885. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1886. numbers should be factors of 60, so for example 5, 10, 15.
  1887. When this is larger than 1, you can still force an exact time-stamp by using
  1888. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1889. and by using a prefix arg to `S-up/down' to specify the exact number
  1890. of minutes to shift."
  1891. :group 'org-time
  1892. :get '(lambda (var) ; Make sure all entries have 5 elements
  1893. (if (integerp (default-value var))
  1894. (list (default-value var) 5)
  1895. (default-value var)))
  1896. :type '(list
  1897. (integer :tag "when inserting times")
  1898. (integer :tag "when modifying times")))
  1899. ;; Normalize old customizations of this variable.
  1900. (when (integerp org-time-stamp-rounding-minutes)
  1901. (setq org-time-stamp-rounding-minutes
  1902. (list org-time-stamp-rounding-minutes
  1903. org-time-stamp-rounding-minutes)))
  1904. (defcustom org-display-custom-times nil
  1905. "Non-nil means, overlay custom formats over all time stamps.
  1906. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1907. To turn this on on a per-file basis, insert anywhere in the file:
  1908. #+STARTUP: customtime"
  1909. :group 'org-time
  1910. :set 'set-default
  1911. :type 'sexp)
  1912. (make-variable-buffer-local 'org-display-custom-times)
  1913. (defcustom org-time-stamp-custom-formats
  1914. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1915. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1916. These are overlayed over the default ISO format if the variable
  1917. `org-display-custom-times' is set. Time like %H:%M should be at the
  1918. end of the second format. The custom formats are also honored by export
  1919. commands, if custom time display is turned on at the time of export."
  1920. :group 'org-time
  1921. :type 'sexp)
  1922. (defun org-time-stamp-format (&optional long inactive)
  1923. "Get the right format for a time string."
  1924. (let ((f (if long (cdr org-time-stamp-formats)
  1925. (car org-time-stamp-formats))))
  1926. (if inactive
  1927. (concat "[" (substring f 1 -1) "]")
  1928. f)))
  1929. (defcustom org-time-clocksum-format "%d:%02d"
  1930. "The format string used when creating CLOCKSUM lines, or when
  1931. org-mode generates a time duration."
  1932. :group 'org-time
  1933. :type 'string)
  1934. (defcustom org-time-clocksum-use-fractional nil
  1935. "If non-nil, \\[org-clock-display] uses fractional times.
  1936. org-mode generates a time duration."
  1937. :group 'org-time
  1938. :type 'boolean)
  1939. (defcustom org-time-clocksum-fractional-format "%.2f"
  1940. "The format string used when creating CLOCKSUM lines, or when
  1941. org-mode generates a time duration."
  1942. :group 'org-time
  1943. :type 'string)
  1944. (defcustom org-deadline-warning-days 14
  1945. "No. of days before expiration during which a deadline becomes active.
  1946. This variable governs the display in sparse trees and in the agenda.
  1947. When 0 or negative, it means use this number (the absolute value of it)
  1948. even if a deadline has a different individual lead time specified.
  1949. Custom commands can set this variable in the options section."
  1950. :group 'org-time
  1951. :group 'org-agenda-daily/weekly
  1952. :type 'integer)
  1953. (defcustom org-read-date-prefer-future t
  1954. "Non-nil means, assume future for incomplete date input from user.
  1955. This affects the following situations:
  1956. 1. The user gives a month but not a year.
  1957. For example, if it is april and you enter \"feb 2\", this will be read
  1958. as feb 2, *next* year. \"May 5\", however, will be this year.
  1959. 2. The user gives a day, but no month.
  1960. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1961. read this as the third of *next* month. However, if you enter \"17\",
  1962. it will be considered as *this* month.
  1963. If you set this variable to the symbol `time', then also the following
  1964. will work:
  1965. 3. If the user gives a time, but no day. If the time is before now,
  1966. to will be interpreted as tomorrow.
  1967. Currently none of this works for ISO week specifications.
  1968. When this option is nil, the current day, month and year will always be
  1969. used as defaults."
  1970. :group 'org-time
  1971. :type '(choice
  1972. (const :tag "Never" nil)
  1973. (const :tag "Check month and day" t)
  1974. (const :tag "Check month, day, and time" time)))
  1975. (defcustom org-read-date-display-live t
  1976. "Non-nil means, display current interpretation of date prompt live.
  1977. This display will be in an overlay, in the minibuffer."
  1978. :group 'org-time
  1979. :type 'boolean)
  1980. (defcustom org-read-date-popup-calendar t
  1981. "Non-nil means, pop up a calendar when prompting for a date.
  1982. In the calendar, the date can be selected with mouse-1. However, the
  1983. minibuffer will also be active, and you can simply enter the date as well.
  1984. When nil, only the minibuffer will be available."
  1985. :group 'org-time
  1986. :type 'boolean)
  1987. (if (fboundp 'defvaralias)
  1988. (defvaralias 'org-popup-calendar-for-date-prompt
  1989. 'org-read-date-popup-calendar))
  1990. (defcustom org-read-date-minibuffer-setup-hook nil
  1991. "Hook to be used to set up keys for the date/time interface.
  1992. Add key definitions to `minibuffer-local-map', which will be a temporary
  1993. copy."
  1994. :group 'org-time
  1995. :type 'hook)
  1996. (defcustom org-extend-today-until 0
  1997. "The hour when your day really ends. Must be an integer.
  1998. This has influence for the following applications:
  1999. - When switching the agenda to \"today\". It it is still earlier than
  2000. the time given here, the day recognized as TODAY is actually yesterday.
  2001. - When a date is read from the user and it is still before the time given
  2002. here, the current date and time will be assumed to be yesterday, 23:59.
  2003. Also, timestamps inserted in remember templates follow this rule.
  2004. IMPORTANT: This is a feature whose implementation is and likely will
  2005. remain incomplete. Really, it is only here because past midnight seems to
  2006. be the favorite working time of John Wiegley :-)"
  2007. :group 'org-time
  2008. :type 'integer)
  2009. (defcustom org-edit-timestamp-down-means-later nil
  2010. "Non-nil means, S-down will increase the time in a time stamp.
  2011. When nil, S-up will increase."
  2012. :group 'org-time
  2013. :type 'boolean)
  2014. (defcustom org-calendar-follow-timestamp-change t
  2015. "Non-nil means, make the calendar window follow timestamp changes.
  2016. When a timestamp is modified and the calendar window is visible, it will be
  2017. moved to the new date."
  2018. :group 'org-time
  2019. :type 'boolean)
  2020. (defgroup org-tags nil
  2021. "Options concerning tags in Org-mode."
  2022. :tag "Org Tags"
  2023. :group 'org)
  2024. (defcustom org-tag-alist nil
  2025. "List of tags allowed in Org-mode files.
  2026. When this list is nil, Org-mode will base TAG input on what is already in the
  2027. buffer.
  2028. The value of this variable is an alist, the car of each entry must be a
  2029. keyword as a string, the cdr may be a character that is used to select
  2030. that tag through the fast-tag-selection interface.
  2031. See the manual for details."
  2032. :group 'org-tags
  2033. :type '(repeat
  2034. (choice
  2035. (cons (string :tag "Tag name")
  2036. (character :tag "Access char"))
  2037. (list :tag "Start radio group"
  2038. (const :startgroup)
  2039. (option (string :tag "Group description")))
  2040. (list :tag "End radio group"
  2041. (const :endgroup)
  2042. (option (string :tag "Group description")))
  2043. (const :tag "New line" (:newline)))))
  2044. (defcustom org-tag-persistent-alist nil
  2045. "List of tags that will always appear in all Org-mode files.
  2046. This is in addition to any in buffer settings or customizations
  2047. of `org-tag-alist'.
  2048. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2049. The value of this variable is an alist, the car of each entry must be a
  2050. keyword as a string, the cdr may be a character that is used to select
  2051. that tag through the fast-tag-selection interface.
  2052. See the manual for details.
  2053. To disable these tags on a per-file basis, insert anywhere in the file:
  2054. #+STARTUP: noptag"
  2055. :group 'org-tags
  2056. :type '(repeat
  2057. (choice
  2058. (cons (string :tag "Tag name")
  2059. (character :tag "Access char"))
  2060. (const :tag "Start radio group" (:startgroup))
  2061. (const :tag "End radio group" (:endgroup))
  2062. (const :tag "New line" (:newline)))))
  2063. (defvar org-file-tags nil
  2064. "List of tags that can be inherited by all entries in the file.
  2065. The tags will be inherited if the variable `org-use-tag-inheritance'
  2066. says they should be.
  2067. This variable is populated from #+FILETAGS lines.")
  2068. (defcustom org-use-fast-tag-selection 'auto
  2069. "Non-nil means, use fast tag selection scheme.
  2070. This is a special interface to select and deselect tags with single keys.
  2071. When nil, fast selection is never used.
  2072. When the symbol `auto', fast selection is used if and only if selection
  2073. characters for tags have been configured, either through the variable
  2074. `org-tag-alist' or through a #+TAGS line in the buffer.
  2075. When t, fast selection is always used and selection keys are assigned
  2076. automatically if necessary."
  2077. :group 'org-tags
  2078. :type '(choice
  2079. (const :tag "Always" t)
  2080. (const :tag "Never" nil)
  2081. (const :tag "When selection characters are configured" 'auto)))
  2082. (defcustom org-fast-tag-selection-single-key nil
  2083. "Non-nil means, fast tag selection exits after first change.
  2084. When nil, you have to press RET to exit it.
  2085. During fast tag selection, you can toggle this flag with `C-c'.
  2086. This variable can also have the value `expert'. In this case, the window
  2087. displaying the tags menu is not even shown, until you press C-c again."
  2088. :group 'org-tags
  2089. :type '(choice
  2090. (const :tag "No" nil)
  2091. (const :tag "Yes" t)
  2092. (const :tag "Expert" expert)))
  2093. (defvar org-fast-tag-selection-include-todo nil
  2094. "Non-nil means, fast tags selection interface will also offer TODO states.
  2095. This is an undocumented feature, you should not rely on it.")
  2096. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2097. "The column to which tags should be indented in a headline.
  2098. If this number is positive, it specifies the column. If it is negative,
  2099. it means that the tags should be flushright to that column. For example,
  2100. -80 works well for a normal 80 character screen."
  2101. :group 'org-tags
  2102. :type 'integer)
  2103. (defcustom org-auto-align-tags t
  2104. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2105. These operations change the length of a headline and therefore shift
  2106. the tags around. With this options turned on, after each such operation
  2107. the tags are again aligned to `org-tags-column'."
  2108. :group 'org-tags
  2109. :type 'boolean)
  2110. (defcustom org-use-tag-inheritance t
  2111. "Non-nil means, tags in levels apply also for sublevels.
  2112. When nil, only the tags directly given in a specific line apply there.
  2113. This may also be a list of tags that should be inherited, or a regexp that
  2114. matches tags that should be inherited. Additional control is possible
  2115. with the variable `org-tags-exclude-from-inheritance' which gives an
  2116. explicit list of tags to be excluded from inheritance., even if the value of
  2117. `org-use-tag-inheritance' would select it for inheritance.
  2118. If this option is t, a match early-on in a tree can lead to a large
  2119. number of matches in the subtree when constructing the agenda or creating
  2120. a sparse tree. If you only want to see the first match in a tree during
  2121. a search, check out the variable `org-tags-match-list-sublevels'."
  2122. :group 'org-tags
  2123. :type '(choice
  2124. (const :tag "Not" nil)
  2125. (const :tag "Always" t)
  2126. (repeat :tag "Specific tags" (string :tag "Tag"))
  2127. (regexp :tag "Tags matched by regexp")))
  2128. (defcustom org-tags-exclude-from-inheritance nil
  2129. "List of tags that should never be inherited.
  2130. This is a way to exclude a few tags from inheritance. For way to do
  2131. the opposite, to actively allow inheritance for selected tags,
  2132. see the variable `org-use-tag-inheritance'."
  2133. :group 'org-tags
  2134. :type '(repeat (string :tag "Tag")))
  2135. (defun org-tag-inherit-p (tag)
  2136. "Check if TAG is one that should be inherited."
  2137. (cond
  2138. ((member tag org-tags-exclude-from-inheritance) nil)
  2139. ((eq org-use-tag-inheritance t) t)
  2140. ((not org-use-tag-inheritance) nil)
  2141. ((stringp org-use-tag-inheritance)
  2142. (string-match org-use-tag-inheritance tag))
  2143. ((listp org-use-tag-inheritance)
  2144. (member tag org-use-tag-inheritance))
  2145. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2146. (defcustom org-tags-match-list-sublevels t
  2147. "Non-nil means list also sublevels of headlines matching a search.
  2148. This variable applies to tags/property searches, and also to stuck
  2149. projects because this search is based on a tags match as well.
  2150. When set to the symbol `indented', sublevels are indented with
  2151. leading dots.
  2152. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2153. the sublevels of a headline matching a tag search often also match
  2154. the same search. Listing all of them can create very long lists.
  2155. Setting this variable to nil causes subtrees of a match to be skipped.
  2156. This variable is semi-obsolete and probably should always be true. It
  2157. is better to limit inheritance to certain tags using the variables
  2158. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2159. :group 'org-tags
  2160. :type '(choice
  2161. (const :tag "No, don't list them" nil)
  2162. (const :tag "Yes, do list them" t)
  2163. (const :tag "List them, indented with leading dots" indented)))
  2164. (defcustom org-tags-sort-function nil
  2165. "When set, tags are sorted using this function as a comparator"
  2166. :group 'org-tags
  2167. :type '(choice
  2168. (const :tag "No sorting" nil)
  2169. (const :tag "Alphabetical" string<)
  2170. (const :tag "Reverse alphabetical" string>)
  2171. (function :tag "Custom function" nil)))
  2172. (defvar org-tags-history nil
  2173. "History of minibuffer reads for tags.")
  2174. (defvar org-last-tags-completion-table nil
  2175. "The last used completion table for tags.")
  2176. (defvar org-after-tags-change-hook nil
  2177. "Hook that is run after the tags in a line have changed.")
  2178. (defgroup org-properties nil
  2179. "Options concerning properties in Org-mode."
  2180. :tag "Org Properties"
  2181. :group 'org)
  2182. (defcustom org-property-format "%-10s %s"
  2183. "How property key/value pairs should be formatted by `indent-line'.
  2184. When `indent-line' hits a property definition, it will format the line
  2185. according to this format, mainly to make sure that the values are
  2186. lined-up with respect to each other."
  2187. :group 'org-properties
  2188. :type 'string)
  2189. (defcustom org-use-property-inheritance nil
  2190. "Non-nil means, properties apply also for sublevels.
  2191. This setting is chiefly used during property searches. Turning it on can
  2192. cause significant overhead when doing a search, which is why it is not
  2193. on by default.
  2194. When nil, only the properties directly given in the current entry count.
  2195. When t, every property is inherited. The value may also be a list of
  2196. properties that should have inheritance, or a regular expression matching
  2197. properties that should be inherited.
  2198. However, note that some special properties use inheritance under special
  2199. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2200. and the properties ending in \"_ALL\" when they are used as descriptor
  2201. for valid values of a property.
  2202. Note for programmers:
  2203. When querying an entry with `org-entry-get', you can control if inheritance
  2204. should be used. By default, `org-entry-get' looks only at the local
  2205. properties. You can request inheritance by setting the inherit argument
  2206. to t (to force inheritance) or to `selective' (to respect the setting
  2207. in this variable)."
  2208. :group 'org-properties
  2209. :type '(choice
  2210. (const :tag "Not" nil)
  2211. (const :tag "Always" t)
  2212. (repeat :tag "Specific properties" (string :tag "Property"))
  2213. (regexp :tag "Properties matched by regexp")))
  2214. (defun org-property-inherit-p (property)
  2215. "Check if PROPERTY is one that should be inherited."
  2216. (cond
  2217. ((eq org-use-property-inheritance t) t)
  2218. ((not org-use-property-inheritance) nil)
  2219. ((stringp org-use-property-inheritance)
  2220. (string-match org-use-property-inheritance property))
  2221. ((listp org-use-property-inheritance)
  2222. (member property org-use-property-inheritance))
  2223. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2224. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2225. "The default column format, if no other format has been defined.
  2226. This variable can be set on the per-file basis by inserting a line
  2227. #+COLUMNS: %25ITEM ....."
  2228. :group 'org-properties
  2229. :type 'string)
  2230. (defcustom org-columns-ellipses ".."
  2231. "The ellipses to be used when a field in column view is truncated.
  2232. When this is the empty string, as many characters as possible are shown,
  2233. but then there will be no visual indication that the field has been truncated.
  2234. When this is a string of length N, the last N characters of a truncated
  2235. field are replaced by this string. If the column is narrower than the
  2236. ellipses string, only part of the ellipses string will be shown."
  2237. :group 'org-properties
  2238. :type 'string)
  2239. (defcustom org-columns-modify-value-for-display-function nil
  2240. "Function that modifies values for display in column view.
  2241. For example, it can be used to cut out a certain part from a time stamp.
  2242. The function must take 2 arguments:
  2243. column-title The title of the column (*not* the property name)
  2244. value The value that should be modified.
  2245. The function should return the value that should be displayed,
  2246. or nil if the normal value should be used."
  2247. :group 'org-properties
  2248. :type 'function)
  2249. (defcustom org-effort-property "Effort"
  2250. "The property that is being used to keep track of effort estimates.
  2251. Effort estimates given in this property need to have the format H:MM."
  2252. :group 'org-properties
  2253. :group 'org-progress
  2254. :type '(string :tag "Property"))
  2255. (defconst org-global-properties-fixed
  2256. '(("VISIBILITY_ALL" . "folded children content all")
  2257. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2258. "List of property/value pairs that can be inherited by any entry.
  2259. These are fixed values, for the preset properties. The user variable
  2260. that can be used to add to this list is `org-global-properties'.
  2261. The entries in this list are cons cells where the car is a property
  2262. name and cdr is a string with the value. If the value represents
  2263. multiple items like an \"_ALL\" property, separate the items by
  2264. spaces.")
  2265. (defcustom org-global-properties nil
  2266. "List of property/value pairs that can be inherited by any entry.
  2267. This list will be combined with the constant `org-global-properties-fixed'.
  2268. The entries in this list are cons cells where the car is a property
  2269. name and cdr is a string with the value.
  2270. You can set buffer-local values for the same purpose in the variable
  2271. `org-file-properties' this by adding lines like
  2272. #+PROPERTY: NAME VALUE"
  2273. :group 'org-properties
  2274. :type '(repeat
  2275. (cons (string :tag "Property")
  2276. (string :tag "Value"))))
  2277. (defvar org-file-properties nil
  2278. "List of property/value pairs that can be inherited by any entry.
  2279. Valid for the current buffer.
  2280. This variable is populated from #+PROPERTY lines.")
  2281. (make-variable-buffer-local 'org-file-properties)
  2282. (defgroup org-agenda nil
  2283. "Options concerning agenda views in Org-mode."
  2284. :tag "Org Agenda"
  2285. :group 'org)
  2286. (defvar org-category nil
  2287. "Variable used by org files to set a category for agenda display.
  2288. Such files should use a file variable to set it, for example
  2289. # -*- mode: org; org-category: \"ELisp\"
  2290. or contain a special line
  2291. #+CATEGORY: ELisp
  2292. If the file does not specify a category, then file's base name
  2293. is used instead.")
  2294. (make-variable-buffer-local 'org-category)
  2295. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2296. (defcustom org-agenda-files nil
  2297. "The files to be used for agenda display.
  2298. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2299. \\[org-remove-file]. You can also use customize to edit the list.
  2300. If an entry is a directory, all files in that directory that are matched by
  2301. `org-agenda-file-regexp' will be part of the file list.
  2302. If the value of the variable is not a list but a single file name, then
  2303. the list of agenda files is actually stored and maintained in that file, one
  2304. agenda file per line."
  2305. :group 'org-agenda
  2306. :type '(choice
  2307. (repeat :tag "List of files and directories" file)
  2308. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2309. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2310. "Regular expression to match files for `org-agenda-files'.
  2311. If any element in the list in that variable contains a directory instead
  2312. of a normal file, all files in that directory that are matched by this
  2313. regular expression will be included."
  2314. :group 'org-agenda
  2315. :type 'regexp)
  2316. (defcustom org-agenda-text-search-extra-files nil
  2317. "List of extra files to be searched by text search commands.
  2318. These files will be search in addition to the agenda files by the
  2319. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2320. Note that these files will only be searched for text search commands,
  2321. not for the other agenda views like todo lists, tag searches or the weekly
  2322. agenda. This variable is intended to list notes and possibly archive files
  2323. that should also be searched by these two commands.
  2324. In fact, if the first element in the list is the symbol `agenda-archives',
  2325. than all archive files of all agenda files will be added to the search
  2326. scope."
  2327. :group 'org-agenda
  2328. :type '(set :greedy t
  2329. (const :tag "Agenda Archives" agenda-archives)
  2330. (repeat :inline t (file))))
  2331. (if (fboundp 'defvaralias)
  2332. (defvaralias 'org-agenda-multi-occur-extra-files
  2333. 'org-agenda-text-search-extra-files))
  2334. (defcustom org-agenda-skip-unavailable-files nil
  2335. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2336. A nil value means to remove them, after a query, from the list."
  2337. :group 'org-agenda
  2338. :type 'boolean)
  2339. (defcustom org-calendar-to-agenda-key [?c]
  2340. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2341. The command `org-calendar-goto-agenda' will be bound to this key. The
  2342. default is the character `c' because then `c' can be used to switch back and
  2343. forth between agenda and calendar."
  2344. :group 'org-agenda
  2345. :type 'sexp)
  2346. (defcustom org-calendar-agenda-action-key [?k]
  2347. "The key to be installed in `calendar-mode-map' for agenda-action.
  2348. The command `org-agenda-action' will be bound to this key. The
  2349. default is the character `k' because we use the same key in the agenda."
  2350. :group 'org-agenda
  2351. :type 'sexp)
  2352. (eval-after-load "calendar"
  2353. '(progn
  2354. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2355. 'org-calendar-goto-agenda)
  2356. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2357. 'org-agenda-action)))
  2358. (defgroup org-latex nil
  2359. "Options for embedding LaTeX code into Org-mode."
  2360. :tag "Org LaTeX"
  2361. :group 'org)
  2362. (defcustom org-format-latex-options
  2363. '(:foreground default :background default :scale 1.0
  2364. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2365. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2366. "Options for creating images from LaTeX fragments.
  2367. This is a property list with the following properties:
  2368. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2369. `default' means use the foreground of the default face.
  2370. :background the background color, or \"Transparent\".
  2371. `default' means use the background of the default face.
  2372. :scale a scaling factor for the size of the images.
  2373. :html-foreground, :html-background, :html-scale
  2374. the same numbers for HTML export.
  2375. :matchers a list indicating which matchers should be used to
  2376. find LaTeX fragments. Valid members of this list are:
  2377. \"begin\" find environments
  2378. \"$1\" find single characters surrounded by $.$
  2379. \"$\" find math expressions surrounded by $...$
  2380. \"$$\" find math expressions surrounded by $$....$$
  2381. \"\\(\" find math expressions surrounded by \\(...\\)
  2382. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2383. :group 'org-latex
  2384. :type 'plist)
  2385. (defcustom org-format-latex-header "\\documentclass{article}
  2386. \\usepackage{amssymb}
  2387. \\usepackage[usenames]{color}
  2388. \\usepackage{amsmath}
  2389. \\usepackage{latexsym}
  2390. \\usepackage[mathscr]{eucal}
  2391. \\pagestyle{empty} % do not remove
  2392. % The settings below are copied from fullpage.sty
  2393. \\setlength{\\textwidth}{\\paperwidth}
  2394. \\addtolength{\\textwidth}{-3cm}
  2395. \\setlength{\\oddsidemargin}{1.5cm}
  2396. \\addtolength{\\oddsidemargin}{-2.54cm}
  2397. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2398. \\setlength{\\textheight}{\\paperheight}
  2399. \\addtolength{\\textheight}{-\\headheight}
  2400. \\addtolength{\\textheight}{-\\headsep}
  2401. \\addtolength{\\textheight}{-\\footskip}
  2402. \\addtolength{\\textheight}{-3cm}
  2403. \\setlength{\\topmargin}{1.5cm}
  2404. \\addtolength{\\topmargin}{-2.54cm}"
  2405. "The document header used for processing LaTeX fragments.
  2406. It is imperative that this header make sure that no page number
  2407. appears on the page."
  2408. :group 'org-latex
  2409. :type 'string)
  2410. (defgroup org-font-lock nil
  2411. "Font-lock settings for highlighting in Org-mode."
  2412. :tag "Org Font Lock"
  2413. :group 'org)
  2414. (defcustom org-level-color-stars-only nil
  2415. "Non-nil means fontify only the stars in each headline.
  2416. When nil, the entire headline is fontified.
  2417. Changing it requires restart of `font-lock-mode' to become effective
  2418. also in regions already fontified."
  2419. :group 'org-font-lock
  2420. :type 'boolean)
  2421. (defcustom org-hide-leading-stars nil
  2422. "Non-nil means, hide the first N-1 stars in a headline.
  2423. This works by using the face `org-hide' for these stars. This
  2424. face is white for a light background, and black for a dark
  2425. background. You may have to customize the face `org-hide' to
  2426. make this work.
  2427. Changing it requires restart of `font-lock-mode' to become effective
  2428. also in regions already fontified.
  2429. You may also set this on a per-file basis by adding one of the following
  2430. lines to the buffer:
  2431. #+STARTUP: hidestars
  2432. #+STARTUP: showstars"
  2433. :group 'org-font-lock
  2434. :type 'boolean)
  2435. (defcustom org-fontify-done-headline nil
  2436. "Non-nil means, change the face of a headline if it is marked DONE.
  2437. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2438. When this is non-nil, the headline after the keyword is set to the
  2439. `org-headline-done' as an additional indication."
  2440. :group 'org-font-lock
  2441. :type 'boolean)
  2442. (defcustom org-fontify-emphasized-text t
  2443. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2444. Changing this variable requires a restart of Emacs to take effect."
  2445. :group 'org-font-lock
  2446. :type 'boolean)
  2447. (defcustom org-fontify-whole-heading-line nil
  2448. "Non-nil means fontify the whole line for headings.
  2449. This is useful when setting a background color for the
  2450. org-leve-* faces."
  2451. :group 'org-font-lock
  2452. :type 'boolean)
  2453. (defcustom org-highlight-latex-fragments-and-specials nil
  2454. "Non-nil means, fontify what is treated specially by the exporters."
  2455. :group 'org-font-lock
  2456. :type 'boolean)
  2457. (defcustom org-hide-emphasis-markers nil
  2458. "Non-nil mean font-lock should hide the emphasis marker characters."
  2459. :group 'org-font-lock
  2460. :type 'boolean)
  2461. (defvar org-emph-re nil
  2462. "Regular expression for matching emphasis.")
  2463. (defvar org-verbatim-re nil
  2464. "Regular expression for matching verbatim text.")
  2465. (defvar org-emphasis-regexp-components) ; defined just below
  2466. (defvar org-emphasis-alist) ; defined just below
  2467. (defun org-set-emph-re (var val)
  2468. "Set variable and compute the emphasis regular expression."
  2469. (set var val)
  2470. (when (and (boundp 'org-emphasis-alist)
  2471. (boundp 'org-emphasis-regexp-components)
  2472. org-emphasis-alist org-emphasis-regexp-components)
  2473. (let* ((e org-emphasis-regexp-components)
  2474. (pre (car e))
  2475. (post (nth 1 e))
  2476. (border (nth 2 e))
  2477. (body (nth 3 e))
  2478. (nl (nth 4 e))
  2479. (body1 (concat body "*?"))
  2480. (markers (mapconcat 'car org-emphasis-alist ""))
  2481. (vmarkers (mapconcat
  2482. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2483. org-emphasis-alist "")))
  2484. ;; make sure special characters appear at the right position in the class
  2485. (if (string-match "\\^" markers)
  2486. (setq markers (concat (replace-match "" t t markers) "^")))
  2487. (if (string-match "-" markers)
  2488. (setq markers (concat (replace-match "" t t markers) "-")))
  2489. (if (string-match "\\^" vmarkers)
  2490. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2491. (if (string-match "-" vmarkers)
  2492. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2493. (if (> nl 0)
  2494. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2495. (int-to-string nl) "\\}")))
  2496. ;; Make the regexp
  2497. (setq org-emph-re
  2498. (concat "\\([" pre "]\\|^\\)"
  2499. "\\("
  2500. "\\([" markers "]\\)"
  2501. "\\("
  2502. "[^" border "]\\|"
  2503. "[^" border "]"
  2504. body1
  2505. "[^" border "]"
  2506. "\\)"
  2507. "\\3\\)"
  2508. "\\([" post "]\\|$\\)"))
  2509. (setq org-verbatim-re
  2510. (concat "\\([" pre "]\\|^\\)"
  2511. "\\("
  2512. "\\([" vmarkers "]\\)"
  2513. "\\("
  2514. "[^" border "]\\|"
  2515. "[^" border "]"
  2516. body1
  2517. "[^" border "]"
  2518. "\\)"
  2519. "\\3\\)"
  2520. "\\([" post "]\\|$\\)")))))
  2521. (defcustom org-emphasis-regexp-components
  2522. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2523. "Components used to build the regular expression for emphasis.
  2524. This is a list with 6 entries. Terminology: In an emphasis string
  2525. like \" *strong word* \", we call the initial space PREMATCH, the final
  2526. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2527. and \"trong wor\" is the body. The different components in this variable
  2528. specify what is allowed/forbidden in each part:
  2529. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2530. post Chars allowed as postmatch. End of line will be allowed too.
  2531. border The chars *forbidden* as border characters.
  2532. body-regexp A regexp like \".\" to match a body character. Don't use
  2533. non-shy groups here, and don't allow newline here.
  2534. newline The maximum number of newlines allowed in an emphasis exp.
  2535. Use customize to modify this, or restart Emacs after changing it."
  2536. :group 'org-font-lock
  2537. :set 'org-set-emph-re
  2538. :type '(list
  2539. (sexp :tag "Allowed chars in pre ")
  2540. (sexp :tag "Allowed chars in post ")
  2541. (sexp :tag "Forbidden chars in border ")
  2542. (sexp :tag "Regexp for body ")
  2543. (integer :tag "number of newlines allowed")
  2544. (option (boolean :tag "Please ignore this button"))))
  2545. (defcustom org-emphasis-alist
  2546. `(("*" bold "<b>" "</b>")
  2547. ("/" italic "<i>" "</i>")
  2548. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2549. ("=" org-code "<code>" "</code>" verbatim)
  2550. ("~" org-verbatim "<code>" "</code>" verbatim)
  2551. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2552. "<del>" "</del>")
  2553. )
  2554. "Special syntax for emphasized text.
  2555. Text starting and ending with a special character will be emphasized, for
  2556. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2557. characters, the face to be used by font-lock for highlighting in Org-mode
  2558. Emacs buffers, and the HTML tags to be used for this.
  2559. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2560. Use customize to modify this, or restart Emacs after changing it."
  2561. :group 'org-font-lock
  2562. :set 'org-set-emph-re
  2563. :type '(repeat
  2564. (list
  2565. (string :tag "Marker character")
  2566. (choice
  2567. (face :tag "Font-lock-face")
  2568. (plist :tag "Face property list"))
  2569. (string :tag "HTML start tag")
  2570. (string :tag "HTML end tag")
  2571. (option (const verbatim)))))
  2572. (defvar org-protecting-blocks
  2573. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2574. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2575. This is needed for font-lock setup.")
  2576. ;;; Miscellaneous options
  2577. (defgroup org-completion nil
  2578. "Completion in Org-mode."
  2579. :tag "Org Completion"
  2580. :group 'org)
  2581. (defcustom org-completion-use-ido nil
  2582. "Non-nil means, use ido completion wherever possible.
  2583. Note that `ido-mode' must be active for this variable to be relevant.
  2584. If you decide to turn this variable on, you might well want to turn off
  2585. `org-outline-path-complete-in-steps'.
  2586. See also `org-completion-use-iswitchb'."
  2587. :group 'org-completion
  2588. :type 'boolean)
  2589. (defcustom org-completion-use-iswitchb nil
  2590. "Non-nil means, use iswitchb completion wherever possible.
  2591. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2592. If you decide to turn this variable on, you might well want to turn off
  2593. `org-outline-path-complete-in-steps'.
  2594. Note that thi variable has only an effect if `org-completion-use-ido' is nil."
  2595. :group 'org-completion
  2596. :type 'boolean)
  2597. (defcustom org-completion-fallback-command 'hippie-expand
  2598. "The expansion command called by \\[org-complete] in normal context.
  2599. Normal means, no org-mode-specific context."
  2600. :group 'org-completion
  2601. :type 'function)
  2602. ;;; Functions and variables from ther packages
  2603. ;; Declared here to avoid compiler warnings
  2604. ;; XEmacs only
  2605. (defvar outline-mode-menu-heading)
  2606. (defvar outline-mode-menu-show)
  2607. (defvar outline-mode-menu-hide)
  2608. (defvar zmacs-regions) ; XEmacs regions
  2609. ;; Emacs only
  2610. (defvar mark-active)
  2611. ;; Various packages
  2612. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2613. (declare-function calendar-forward-day "cal-move" (arg))
  2614. (declare-function calendar-goto-date "cal-move" (date))
  2615. (declare-function calendar-goto-today "cal-move" ())
  2616. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2617. (defvar calc-embedded-close-formula)
  2618. (defvar calc-embedded-open-formula)
  2619. (declare-function cdlatex-tab "ext:cdlatex" ())
  2620. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2621. (defvar font-lock-unfontify-region-function)
  2622. (declare-function iswitchb-read-buffer "iswitchb"
  2623. (prompt &optional default require-match start matches-set))
  2624. (defvar iswitchb-temp-buflist)
  2625. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2626. (defvar org-agenda-tags-todo-honor-ignore-options)
  2627. (declare-function org-agenda-skip "org-agenda" ())
  2628. (declare-function org-format-agenda-item "org-agenda"
  2629. (extra txt &optional category tags dotime noprefix remove-re))
  2630. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2631. (declare-function org-agenda-change-all-lines "org-agenda"
  2632. (newhead hdmarker &optional fixface just-this))
  2633. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2634. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2635. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2636. (beg end))
  2637. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2638. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2639. "org-agenda" (&optional end))
  2640. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2641. (declare-function org-indent-mode "org-indent" (&optional arg))
  2642. (declare-function parse-time-string "parse-time" (string))
  2643. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2644. (defvar remember-data-file)
  2645. (defvar texmathp-why)
  2646. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2647. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2648. (defvar w3m-current-url)
  2649. (defvar w3m-current-title)
  2650. (defvar org-latex-regexps)
  2651. ;;; Autoload and prepare some org modules
  2652. ;; Some table stuff that needs to be defined here, because it is used
  2653. ;; by the functions setting up org-mode or checking for table context.
  2654. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2655. "Detects an org-type or table-type table.")
  2656. (defconst org-table-line-regexp "^[ \t]*|"
  2657. "Detects an org-type table line.")
  2658. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2659. "Detects an org-type table line.")
  2660. (defconst org-table-hline-regexp "^[ \t]*|-"
  2661. "Detects an org-type table hline.")
  2662. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2663. "Detects a table-type table hline.")
  2664. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2665. "Searching from within a table (any type) this finds the first line
  2666. outside the table.")
  2667. ;; Autoload the functions in org-table.el that are needed by functions here.
  2668. (eval-and-compile
  2669. (org-autoload "org-table"
  2670. '(org-table-align org-table-begin org-table-blank-field
  2671. org-table-convert org-table-convert-region org-table-copy-down
  2672. org-table-copy-region org-table-create
  2673. org-table-create-or-convert-from-region
  2674. org-table-create-with-table.el org-table-current-dline
  2675. org-table-cut-region org-table-delete-column org-table-edit-field
  2676. org-table-edit-formulas org-table-end org-table-eval-formula
  2677. org-table-export org-table-field-info
  2678. org-table-get-stored-formulas org-table-goto-column
  2679. org-table-hline-and-move org-table-import org-table-insert-column
  2680. org-table-insert-hline org-table-insert-row org-table-iterate
  2681. org-table-justify-field-maybe org-table-kill-row
  2682. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2683. org-table-move-column org-table-move-column-left
  2684. org-table-move-column-right org-table-move-row
  2685. org-table-move-row-down org-table-move-row-up
  2686. org-table-next-field org-table-next-row org-table-paste-rectangle
  2687. org-table-previous-field org-table-recalculate
  2688. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2689. org-table-toggle-coordinate-overlays
  2690. org-table-toggle-formula-debugger org-table-wrap-region
  2691. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2692. (defun org-at-table-p (&optional table-type)
  2693. "Return t if the cursor is inside an org-type table.
  2694. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2695. (if org-enable-table-editor
  2696. (save-excursion
  2697. (beginning-of-line 1)
  2698. (looking-at (if table-type org-table-any-line-regexp
  2699. org-table-line-regexp)))
  2700. nil))
  2701. (defsubst org-table-p () (org-at-table-p))
  2702. (defun org-at-table.el-p ()
  2703. "Return t if and only if we are at a table.el table."
  2704. (and (org-at-table-p 'any)
  2705. (save-excursion
  2706. (goto-char (org-table-begin 'any))
  2707. (looking-at org-table1-hline-regexp))))
  2708. (defun org-table-recognize-table.el ()
  2709. "If there is a table.el table nearby, recognize it and move into it."
  2710. (if org-table-tab-recognizes-table.el
  2711. (if (org-at-table.el-p)
  2712. (progn
  2713. (beginning-of-line 1)
  2714. (if (looking-at org-table-dataline-regexp)
  2715. nil
  2716. (if (looking-at org-table1-hline-regexp)
  2717. (progn
  2718. (beginning-of-line 2)
  2719. (if (looking-at org-table-any-border-regexp)
  2720. (beginning-of-line -1)))))
  2721. (if (re-search-forward "|" (org-table-end t) t)
  2722. (progn
  2723. (require 'table)
  2724. (if (table--at-cell-p (point))
  2725. t
  2726. (message "recognizing table.el table...")
  2727. (table-recognize-table)
  2728. (message "recognizing table.el table...done")))
  2729. (error "This should not happen..."))
  2730. t)
  2731. nil)
  2732. nil))
  2733. (defun org-at-table-hline-p ()
  2734. "Return t if the cursor is inside a hline in a table."
  2735. (if org-enable-table-editor
  2736. (save-excursion
  2737. (beginning-of-line 1)
  2738. (looking-at org-table-hline-regexp))
  2739. nil))
  2740. (defvar org-table-clean-did-remove-column nil)
  2741. (defun org-table-map-tables (function)
  2742. "Apply FUNCTION to the start of all tables in the buffer."
  2743. (save-excursion
  2744. (save-restriction
  2745. (widen)
  2746. (goto-char (point-min))
  2747. (while (re-search-forward org-table-any-line-regexp nil t)
  2748. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2749. (beginning-of-line 1)
  2750. (when (looking-at org-table-line-regexp)
  2751. (save-excursion (funcall function))
  2752. (or (looking-at org-table-line-regexp)
  2753. (forward-char 1)))
  2754. (re-search-forward org-table-any-border-regexp nil 1))))
  2755. (message "Mapping tables: done"))
  2756. ;; Declare and autoload functions from org-exp.el & Co
  2757. (declare-function org-default-export-plist "org-exp")
  2758. (declare-function org-infile-export-plist "org-exp")
  2759. (declare-function org-get-current-options "org-exp")
  2760. (eval-and-compile
  2761. (org-autoload "org-exp"
  2762. '(org-export org-export-visible
  2763. org-insert-export-options-template
  2764. org-table-clean-before-export))
  2765. (org-autoload "org-ascii"
  2766. '(org-export-as-ascii org-export-ascii-preprocess
  2767. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2768. org-export-region-as-ascii))
  2769. (org-autoload "org-html"
  2770. '(org-export-as-html-and-open
  2771. org-export-as-html-batch org-export-as-html-to-buffer
  2772. org-replace-region-by-html org-export-region-as-html
  2773. org-export-as-html))
  2774. (org-autoload "org-icalendar"
  2775. '(org-export-icalendar-this-file
  2776. org-export-icalendar-all-agenda-files
  2777. org-export-icalendar-combine-agenda-files))
  2778. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2779. ;; Declare and autoload functions from org-agenda.el
  2780. (eval-and-compile
  2781. (org-autoload "org-agenda"
  2782. '(org-agenda org-agenda-list org-search-view
  2783. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2784. org-diary org-agenda-to-appt
  2785. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2786. ;; Autoload org-remember
  2787. (eval-and-compile
  2788. (org-autoload "org-remember"
  2789. '(org-remember-insinuate org-remember-annotation
  2790. org-remember-apply-template org-remember org-remember-handler)))
  2791. ;; Autoload org-clock.el
  2792. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2793. (beg end))
  2794. (declare-function org-clock-update-mode-line "org-clock" ())
  2795. (declare-function org-resolve-clocks "org-clock"
  2796. (&optional also-non-dangling-p prompt last-valid))
  2797. (defvar org-clock-start-time)
  2798. (defvar org-clock-marker (make-marker)
  2799. "Marker recording the last clock-in.")
  2800. (defvar org-clock-hd-marker (make-marker)
  2801. "Marker recording the last clock-in, but the headline position.")
  2802. (defvar org-clock-heading ""
  2803. "The heading of the current clock entry.")
  2804. (defun org-clock-is-active ()
  2805. "Return non-nil if clock is currently running.
  2806. The return value is actually the clock marker."
  2807. (marker-buffer org-clock-marker))
  2808. (eval-and-compile
  2809. (org-autoload
  2810. "org-clock"
  2811. '(org-clock-in org-clock-out org-clock-cancel
  2812. org-clock-goto org-clock-sum org-clock-display
  2813. org-clock-remove-overlays org-clock-report
  2814. org-clocktable-shift org-dblock-write:clocktable
  2815. org-get-clocktable org-resolve-clocks)))
  2816. (defun org-clock-update-time-maybe ()
  2817. "If this is a CLOCK line, update it and return t.
  2818. Otherwise, return nil."
  2819. (interactive)
  2820. (save-excursion
  2821. (beginning-of-line 1)
  2822. (skip-chars-forward " \t")
  2823. (when (looking-at org-clock-string)
  2824. (let ((re (concat "[ \t]*" org-clock-string
  2825. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2826. "\\([ \t]*=>.*\\)?\\)?"))
  2827. ts te h m s neg)
  2828. (cond
  2829. ((not (looking-at re))
  2830. nil)
  2831. ((not (match-end 2))
  2832. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2833. (> org-clock-marker (point))
  2834. (<= org-clock-marker (point-at-eol)))
  2835. ;; The clock is running here
  2836. (setq org-clock-start-time
  2837. (apply 'encode-time
  2838. (org-parse-time-string (match-string 1))))
  2839. (org-clock-update-mode-line)))
  2840. (t
  2841. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2842. (end-of-line 1)
  2843. (setq ts (match-string 1)
  2844. te (match-string 3))
  2845. (setq s (- (org-float-time
  2846. (apply 'encode-time (org-parse-time-string te)))
  2847. (org-float-time
  2848. (apply 'encode-time (org-parse-time-string ts))))
  2849. neg (< s 0)
  2850. s (abs s)
  2851. h (floor (/ s 3600))
  2852. s (- s (* 3600 h))
  2853. m (floor (/ s 60))
  2854. s (- s (* 60 s)))
  2855. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2856. t))))))
  2857. (defun org-check-running-clock ()
  2858. "Check if the current buffer contains the running clock.
  2859. If yes, offer to stop it and to save the buffer with the changes."
  2860. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2861. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2862. (buffer-name))))
  2863. (org-clock-out)
  2864. (when (y-or-n-p "Save changed buffer?")
  2865. (save-buffer))))
  2866. (defun org-clocktable-try-shift (dir n)
  2867. "Check if this line starts a clock table, if yes, shift the time block."
  2868. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2869. (org-clocktable-shift dir n)))
  2870. ;; Autoload org-timer.el
  2871. (eval-and-compile
  2872. (org-autoload
  2873. "org-timer"
  2874. '(org-timer-start org-timer org-timer-item
  2875. org-timer-change-times-in-region
  2876. org-timer-set-timer
  2877. org-timer-reset-timers
  2878. org-timer-show-remaining-time)))
  2879. ;; Autoload org-feed.el
  2880. (eval-and-compile
  2881. (org-autoload
  2882. "org-feed"
  2883. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2884. ;; Autoload org-indent.el
  2885. (eval-and-compile
  2886. (org-autoload
  2887. "org-indent"
  2888. '(org-indent-mode)))
  2889. ;; Autoload org-mobile.el
  2890. (eval-and-compile
  2891. (org-autoload
  2892. "org-mobile"
  2893. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  2894. ;; Autoload archiving code
  2895. ;; The stuff that is needed for cycling and tags has to be defined here.
  2896. (defgroup org-archive nil
  2897. "Options concerning archiving in Org-mode."
  2898. :tag "Org Archive"
  2899. :group 'org-structure)
  2900. (defcustom org-archive-location "%s_archive::"
  2901. "The location where subtrees should be archived.
  2902. The value of this variable is a string, consisting of two parts,
  2903. separated by a double-colon. The first part is a filename and
  2904. the second part is a headline.
  2905. When the filename is omitted, archiving happens in the same file.
  2906. %s in the filename will be replaced by the current file
  2907. name (without the directory part). Archiving to a different file
  2908. is useful to keep archived entries from contributing to the
  2909. Org-mode Agenda.
  2910. The archived entries will be filed as subtrees of the specified
  2911. headline. When the headline is omitted, the subtrees are simply
  2912. filed away at the end of the file, as top-level entries. Also in
  2913. the heading you can use %s to represent the file name, this can be
  2914. useful when using the same archive for a number of different files.
  2915. Here are a few examples:
  2916. \"%s_archive::\"
  2917. If the current file is Projects.org, archive in file
  2918. Projects.org_archive, as top-level trees. This is the default.
  2919. \"::* Archived Tasks\"
  2920. Archive in the current file, under the top-level headline
  2921. \"* Archived Tasks\".
  2922. \"~/org/archive.org::\"
  2923. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2924. \"~/org/archive.org::From %s\"
  2925. Archive in file ~/org/archive.org (absolute path), und headlines
  2926. \"From FILENAME\" where file name is the current file name.
  2927. \"basement::** Finished Tasks\"
  2928. Archive in file ./basement (relative path), as level 3 trees
  2929. below the level 2 heading \"** Finished Tasks\".
  2930. You may set this option on a per-file basis by adding to the buffer a
  2931. line like
  2932. #+ARCHIVE: basement::** Finished Tasks
  2933. You may also define it locally for a subtree by setting an ARCHIVE property
  2934. in the entry. If such a property is found in an entry, or anywhere up
  2935. the hierarchy, it will be used."
  2936. :group 'org-archive
  2937. :type 'string)
  2938. (defcustom org-archive-tag "ARCHIVE"
  2939. "The tag that marks a subtree as archived.
  2940. An archived subtree does not open during visibility cycling, and does
  2941. not contribute to the agenda listings.
  2942. After changing this, font-lock must be restarted in the relevant buffers to
  2943. get the proper fontification."
  2944. :group 'org-archive
  2945. :group 'org-keywords
  2946. :type 'string)
  2947. (defcustom org-agenda-skip-archived-trees t
  2948. "Non-nil means, the agenda will skip any items located in archived trees.
  2949. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2950. variable is no longer recommended, you should leave it at the value t.
  2951. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2952. :group 'org-archive
  2953. :group 'org-agenda-skip
  2954. :type 'boolean)
  2955. (defcustom org-columns-skip-arrchived-trees t
  2956. "Non-nil means, irgnore archived trees when creating column view."
  2957. :group 'org-archive
  2958. :group 'org-properties
  2959. :type 'boolean)
  2960. (defcustom org-cycle-open-archived-trees nil
  2961. "Non-nil means, `org-cycle' will open archived trees.
  2962. An archived tree is a tree marked with the tag ARCHIVE.
  2963. When nil, archived trees will stay folded. You can still open them with
  2964. normal outline commands like `show-all', but not with the cycling commands."
  2965. :group 'org-archive
  2966. :group 'org-cycle
  2967. :type 'boolean)
  2968. (defcustom org-sparse-tree-open-archived-trees nil
  2969. "Non-nil means sparse tree construction shows matches in archived trees.
  2970. When nil, matches in these trees are highlighted, but the trees are kept in
  2971. collapsed state."
  2972. :group 'org-archive
  2973. :group 'org-sparse-trees
  2974. :type 'boolean)
  2975. (defun org-cycle-hide-archived-subtrees (state)
  2976. "Re-hide all archived subtrees after a visibility state change."
  2977. (when (and (not org-cycle-open-archived-trees)
  2978. (not (memq state '(overview folded))))
  2979. (save-excursion
  2980. (let* ((globalp (memq state '(contents all)))
  2981. (beg (if globalp (point-min) (point)))
  2982. (end (if globalp (point-max) (org-end-of-subtree t))))
  2983. (org-hide-archived-subtrees beg end)
  2984. (goto-char beg)
  2985. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2986. (message "%s" (substitute-command-keys
  2987. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2988. (defun org-force-cycle-archived ()
  2989. "Cycle subtree even if it is archived."
  2990. (interactive)
  2991. (setq this-command 'org-cycle)
  2992. (let ((org-cycle-open-archived-trees t))
  2993. (call-interactively 'org-cycle)))
  2994. (defun org-hide-archived-subtrees (beg end)
  2995. "Re-hide all archived subtrees after a visibility state change."
  2996. (save-excursion
  2997. (let* ((re (concat ":" org-archive-tag ":")))
  2998. (goto-char beg)
  2999. (while (re-search-forward re end t)
  3000. (and (org-on-heading-p) (org-flag-subtree t))
  3001. (org-end-of-subtree t)))))
  3002. (defun org-flag-subtree (flag)
  3003. (save-excursion
  3004. (org-back-to-heading t)
  3005. (outline-end-of-heading)
  3006. (outline-flag-region (point)
  3007. (progn (org-end-of-subtree t) (point))
  3008. flag)))
  3009. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3010. (eval-and-compile
  3011. (org-autoload "org-archive"
  3012. '(org-add-archive-files org-archive-subtree
  3013. org-archive-to-archive-sibling org-toggle-archive-tag)))
  3014. ;; Autoload Column View Code
  3015. (declare-function org-columns-number-to-string "org-colview")
  3016. (declare-function org-columns-get-format-and-top-level "org-colview")
  3017. (declare-function org-columns-compute "org-colview")
  3018. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3019. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3020. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3021. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3022. ;; Autoload ID code
  3023. (declare-function org-id-store-link "org-id")
  3024. (declare-function org-id-locations-load "org-id")
  3025. (declare-function org-id-locations-save "org-id")
  3026. (defvar org-id-track-globally)
  3027. (org-autoload "org-id"
  3028. '(org-id-get-create org-id-new org-id-copy org-id-get
  3029. org-id-get-with-outline-path-completion
  3030. org-id-get-with-outline-drilling
  3031. org-id-goto org-id-find org-id-store-link))
  3032. ;; Autoload Plotting Code
  3033. (org-autoload "org-plot"
  3034. '(org-plot/gnuplot))
  3035. ;;; Variables for pre-computed regular expressions, all buffer local
  3036. (defvar org-drawer-regexp nil
  3037. "Matches first line of a hidden block.")
  3038. (make-variable-buffer-local 'org-drawer-regexp)
  3039. (defvar org-todo-regexp nil
  3040. "Matches any of the TODO state keywords.")
  3041. (make-variable-buffer-local 'org-todo-regexp)
  3042. (defvar org-not-done-regexp nil
  3043. "Matches any of the TODO state keywords except the last one.")
  3044. (make-variable-buffer-local 'org-not-done-regexp)
  3045. (defvar org-not-done-heading-regexp nil
  3046. "Matches a TODO headline that is not done.")
  3047. (make-variable-buffer-local 'org-not-done-regexp)
  3048. (defvar org-todo-line-regexp nil
  3049. "Matches a headline and puts TODO state into group 2 if present.")
  3050. (make-variable-buffer-local 'org-todo-line-regexp)
  3051. (defvar org-complex-heading-regexp nil
  3052. "Matches a headline and puts everything into groups:
  3053. group 1: the stars
  3054. group 2: The todo keyword, maybe
  3055. group 3: Priority cookie
  3056. group 4: True headline
  3057. group 5: Tags")
  3058. (make-variable-buffer-local 'org-complex-heading-regexp)
  3059. (defvar org-complex-heading-regexp-format nil)
  3060. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3061. (defvar org-todo-line-tags-regexp nil
  3062. "Matches a headline and puts TODO state into group 2 if present.
  3063. Also put tags into group 4 if tags are present.")
  3064. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3065. (defvar org-nl-done-regexp nil
  3066. "Matches newline followed by a headline with the DONE keyword.")
  3067. (make-variable-buffer-local 'org-nl-done-regexp)
  3068. (defvar org-looking-at-done-regexp nil
  3069. "Matches the DONE keyword a point.")
  3070. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3071. (defvar org-ds-keyword-length 12
  3072. "Maximum length of the Deadline and SCHEDULED keywords.")
  3073. (make-variable-buffer-local 'org-ds-keyword-length)
  3074. (defvar org-deadline-regexp nil
  3075. "Matches the DEADLINE keyword.")
  3076. (make-variable-buffer-local 'org-deadline-regexp)
  3077. (defvar org-deadline-time-regexp nil
  3078. "Matches the DEADLINE keyword together with a time stamp.")
  3079. (make-variable-buffer-local 'org-deadline-time-regexp)
  3080. (defvar org-deadline-line-regexp nil
  3081. "Matches the DEADLINE keyword and the rest of the line.")
  3082. (make-variable-buffer-local 'org-deadline-line-regexp)
  3083. (defvar org-scheduled-regexp nil
  3084. "Matches the SCHEDULED keyword.")
  3085. (make-variable-buffer-local 'org-scheduled-regexp)
  3086. (defvar org-scheduled-time-regexp nil
  3087. "Matches the SCHEDULED keyword together with a time stamp.")
  3088. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3089. (defvar org-closed-time-regexp nil
  3090. "Matches the CLOSED keyword together with a time stamp.")
  3091. (make-variable-buffer-local 'org-closed-time-regexp)
  3092. (defvar org-keyword-time-regexp nil
  3093. "Matches any of the 4 keywords, together with the time stamp.")
  3094. (make-variable-buffer-local 'org-keyword-time-regexp)
  3095. (defvar org-keyword-time-not-clock-regexp nil
  3096. "Matches any of the 3 keywords, together with the time stamp.")
  3097. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3098. (defvar org-maybe-keyword-time-regexp nil
  3099. "Matches a timestamp, possibly preceeded by a keyword.")
  3100. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3101. (defvar org-planning-or-clock-line-re nil
  3102. "Matches a line with planning or clock info.")
  3103. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3104. (defconst org-plain-time-of-day-regexp
  3105. (concat
  3106. "\\(\\<[012]?[0-9]"
  3107. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3108. "\\(--?"
  3109. "\\(\\<[012]?[0-9]"
  3110. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3111. "\\)?")
  3112. "Regular expression to match a plain time or time range.
  3113. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3114. groups carry important information:
  3115. 0 the full match
  3116. 1 the first time, range or not
  3117. 8 the second time, if it is a range.")
  3118. (defconst org-plain-time-extension-regexp
  3119. (concat
  3120. "\\(\\<[012]?[0-9]"
  3121. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3122. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3123. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3124. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3125. groups carry important information:
  3126. 0 the full match
  3127. 7 hours of duration
  3128. 9 minutes of duration")
  3129. (defconst org-stamp-time-of-day-regexp
  3130. (concat
  3131. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3132. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3133. "\\(--?"
  3134. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3135. "Regular expression to match a timestamp time or time range.
  3136. After a match, the following groups carry important information:
  3137. 0 the full match
  3138. 1 date plus weekday, for backreferencing to make sure both times on same day
  3139. 2 the first time, range or not
  3140. 4 the second time, if it is a range.")
  3141. (defconst org-startup-options
  3142. '(("fold" org-startup-folded t)
  3143. ("overview" org-startup-folded t)
  3144. ("nofold" org-startup-folded nil)
  3145. ("showall" org-startup-folded nil)
  3146. ("showeverything" org-startup-folded showeverything)
  3147. ("content" org-startup-folded content)
  3148. ("indent" org-startup-indented t)
  3149. ("noindent" org-startup-indented nil)
  3150. ("hidestars" org-hide-leading-stars t)
  3151. ("showstars" org-hide-leading-stars nil)
  3152. ("odd" org-odd-levels-only t)
  3153. ("oddeven" org-odd-levels-only nil)
  3154. ("align" org-startup-align-all-tables t)
  3155. ("noalign" org-startup-align-all-tables nil)
  3156. ("customtime" org-display-custom-times t)
  3157. ("logdone" org-log-done time)
  3158. ("lognotedone" org-log-done note)
  3159. ("nologdone" org-log-done nil)
  3160. ("lognoteclock-out" org-log-note-clock-out t)
  3161. ("nolognoteclock-out" org-log-note-clock-out nil)
  3162. ("logrepeat" org-log-repeat state)
  3163. ("lognoterepeat" org-log-repeat note)
  3164. ("nologrepeat" org-log-repeat nil)
  3165. ("fninline" org-footnote-define-inline t)
  3166. ("nofninline" org-footnote-define-inline nil)
  3167. ("fnlocal" org-footnote-section nil)
  3168. ("fnauto" org-footnote-auto-label t)
  3169. ("fnprompt" org-footnote-auto-label nil)
  3170. ("fnconfirm" org-footnote-auto-label confirm)
  3171. ("fnplain" org-footnote-auto-label plain)
  3172. ("fnadjust" org-footnote-auto-adjust t)
  3173. ("nofnadjust" org-footnote-auto-adjust nil)
  3174. ("constcgs" constants-unit-system cgs)
  3175. ("constSI" constants-unit-system SI)
  3176. ("noptag" org-tag-persistent-alist nil)
  3177. ("hideblocks" org-hide-block-startup t)
  3178. ("nohideblocks" org-hide-block-startup nil))
  3179. "Variable associated with STARTUP options for org-mode.
  3180. Each element is a list of three items: The startup options as written
  3181. in the #+STARTUP line, the corresponding variable, and the value to
  3182. set this variable to if the option is found. An optional forth element PUSH
  3183. means to push this value onto the list in the variable.")
  3184. (defun org-set-regexps-and-options ()
  3185. "Precompute regular expressions for current buffer."
  3186. (when (org-mode-p)
  3187. (org-set-local 'org-todo-kwd-alist nil)
  3188. (org-set-local 'org-todo-key-alist nil)
  3189. (org-set-local 'org-todo-key-trigger nil)
  3190. (org-set-local 'org-todo-keywords-1 nil)
  3191. (org-set-local 'org-done-keywords nil)
  3192. (org-set-local 'org-todo-heads nil)
  3193. (org-set-local 'org-todo-sets nil)
  3194. (org-set-local 'org-todo-log-states nil)
  3195. (org-set-local 'org-file-properties nil)
  3196. (org-set-local 'org-file-tags nil)
  3197. (let ((re (org-make-options-regexp
  3198. '("CATEGORY" "TODO" "COLUMNS"
  3199. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3200. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3201. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3202. (splitre "[ \t]+")
  3203. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3204. tail sep kws1 prio props ftags drawers
  3205. ext-setup-or-nil setup-contents (start 0))
  3206. (save-excursion
  3207. (save-restriction
  3208. (widen)
  3209. (goto-char (point-min))
  3210. (while (or (and ext-setup-or-nil
  3211. (string-match re ext-setup-or-nil start)
  3212. (setq start (match-end 0)))
  3213. (and (setq ext-setup-or-nil nil start 0)
  3214. (re-search-forward re nil t)))
  3215. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3216. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3217. (cond
  3218. ((equal key "CATEGORY")
  3219. (if (string-match "[ \t]+$" value)
  3220. (setq value (replace-match "" t t value)))
  3221. (setq cat value))
  3222. ((member key '("SEQ_TODO" "TODO"))
  3223. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3224. ((equal key "TYP_TODO")
  3225. (push (cons 'type (org-split-string value splitre)) kwds))
  3226. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3227. ;; general TODO-like setup
  3228. (push (cons (intern (downcase (match-string 1 key)))
  3229. (org-split-string value splitre)) kwds))
  3230. ((equal key "TAGS")
  3231. (setq tags (append tags (if tags '("\\n") nil)
  3232. (org-split-string value splitre))))
  3233. ((equal key "COLUMNS")
  3234. (org-set-local 'org-columns-default-format value))
  3235. ((equal key "LINK")
  3236. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3237. (push (cons (match-string 1 value)
  3238. (org-trim (match-string 2 value)))
  3239. links)))
  3240. ((equal key "PRIORITIES")
  3241. (setq prio (org-split-string value " +")))
  3242. ((equal key "PROPERTY")
  3243. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3244. (push (cons (match-string 1 value) (match-string 2 value))
  3245. props)))
  3246. ((equal key "FILETAGS")
  3247. (when (string-match "\\S-" value)
  3248. (setq ftags
  3249. (append
  3250. ftags
  3251. (apply 'append
  3252. (mapcar (lambda (x) (org-split-string x ":"))
  3253. (org-split-string value)))))))
  3254. ((equal key "DRAWERS")
  3255. (setq drawers (org-split-string value splitre)))
  3256. ((equal key "CONSTANTS")
  3257. (setq const (append const (org-split-string value splitre))))
  3258. ((equal key "STARTUP")
  3259. (let ((opts (org-split-string value splitre))
  3260. l var val)
  3261. (while (setq l (pop opts))
  3262. (when (setq l (assoc l org-startup-options))
  3263. (setq var (nth 1 l) val (nth 2 l))
  3264. (if (not (nth 3 l))
  3265. (set (make-local-variable var) val)
  3266. (if (not (listp (symbol-value var)))
  3267. (set (make-local-variable var) nil))
  3268. (set (make-local-variable var) (symbol-value var))
  3269. (add-to-list var val))))))
  3270. ((equal key "ARCHIVE")
  3271. (string-match " *$" value)
  3272. (setq arch (replace-match "" t t value))
  3273. (remove-text-properties 0 (length arch)
  3274. '(face t fontified t) arch))
  3275. ((equal key "SETUPFILE")
  3276. (setq setup-contents (org-file-contents
  3277. (expand-file-name
  3278. (org-remove-double-quotes value))
  3279. 'noerror))
  3280. (if (not ext-setup-or-nil)
  3281. (setq ext-setup-or-nil setup-contents start 0)
  3282. (setq ext-setup-or-nil
  3283. (concat (substring ext-setup-or-nil 0 start)
  3284. "\n" setup-contents "\n"
  3285. (substring ext-setup-or-nil start)))))
  3286. ))))
  3287. (when cat
  3288. (org-set-local 'org-category (intern cat))
  3289. (push (cons "CATEGORY" cat) props))
  3290. (when prio
  3291. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3292. (setq prio (mapcar 'string-to-char prio))
  3293. (org-set-local 'org-highest-priority (nth 0 prio))
  3294. (org-set-local 'org-lowest-priority (nth 1 prio))
  3295. (org-set-local 'org-default-priority (nth 2 prio)))
  3296. (and props (org-set-local 'org-file-properties (nreverse props)))
  3297. (and ftags (org-set-local 'org-file-tags
  3298. (mapcar 'org-add-prop-inherited ftags)))
  3299. (and drawers (org-set-local 'org-drawers drawers))
  3300. (and arch (org-set-local 'org-archive-location arch))
  3301. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3302. ;; Process the TODO keywords
  3303. (unless kwds
  3304. ;; Use the global values as if they had been given locally.
  3305. (setq kwds (default-value 'org-todo-keywords))
  3306. (if (stringp (car kwds))
  3307. (setq kwds (list (cons org-todo-interpretation
  3308. (default-value 'org-todo-keywords)))))
  3309. (setq kwds (reverse kwds)))
  3310. (setq kwds (nreverse kwds))
  3311. (let (inter kws kw)
  3312. (while (setq kws (pop kwds))
  3313. (let ((kws (or
  3314. (run-hook-with-args-until-success
  3315. 'org-todo-setup-filter-hook kws)
  3316. kws)))
  3317. (setq inter (pop kws) sep (member "|" kws)
  3318. kws0 (delete "|" (copy-sequence kws))
  3319. kwsa nil
  3320. kws1 (mapcar
  3321. (lambda (x)
  3322. ;; 1 2
  3323. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3324. (progn
  3325. (setq kw (match-string 1 x)
  3326. key (and (match-end 2) (match-string 2 x))
  3327. log (org-extract-log-state-settings x))
  3328. (push (cons kw (and key (string-to-char key))) kwsa)
  3329. (and log (push log org-todo-log-states))
  3330. kw)
  3331. (error "Invalid TODO keyword %s" x)))
  3332. kws0)
  3333. kwsa (if kwsa (append '((:startgroup))
  3334. (nreverse kwsa)
  3335. '((:endgroup))))
  3336. hw (car kws1)
  3337. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3338. tail (list inter hw (car dws) (org-last dws))))
  3339. (add-to-list 'org-todo-heads hw 'append)
  3340. (push kws1 org-todo-sets)
  3341. (setq org-done-keywords (append org-done-keywords dws nil))
  3342. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3343. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3344. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3345. (setq org-todo-sets (nreverse org-todo-sets)
  3346. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3347. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3348. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3349. ;; Process the constants
  3350. (when const
  3351. (let (e cst)
  3352. (while (setq e (pop const))
  3353. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3354. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3355. (setq org-table-formula-constants-local cst)))
  3356. ;; Process the tags.
  3357. (when tags
  3358. (let (e tgs)
  3359. (while (setq e (pop tags))
  3360. (cond
  3361. ((equal e "{") (push '(:startgroup) tgs))
  3362. ((equal e "}") (push '(:endgroup) tgs))
  3363. ((equal e "\\n") (push '(:newline) tgs))
  3364. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3365. (push (cons (match-string 1 e)
  3366. (string-to-char (match-string 2 e)))
  3367. tgs))
  3368. (t (push (list e) tgs))))
  3369. (org-set-local 'org-tag-alist nil)
  3370. (while (setq e (pop tgs))
  3371. (or (and (stringp (car e))
  3372. (assoc (car e) org-tag-alist))
  3373. (push e org-tag-alist)))))
  3374. ;; Compute the regular expressions and other local variables
  3375. (if (not org-done-keywords)
  3376. (setq org-done-keywords (and org-todo-keywords-1
  3377. (list (org-last org-todo-keywords-1)))))
  3378. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3379. (length org-scheduled-string)
  3380. (length org-clock-string)
  3381. (length org-closed-string)))
  3382. org-drawer-regexp
  3383. (concat "^[ \t]*:\\("
  3384. (mapconcat 'regexp-quote org-drawers "\\|")
  3385. "\\):[ \t]*$")
  3386. org-not-done-keywords
  3387. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3388. org-todo-regexp
  3389. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3390. "\\|") "\\)\\>")
  3391. org-not-done-regexp
  3392. (concat "\\<\\("
  3393. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3394. "\\)\\>")
  3395. org-not-done-heading-regexp
  3396. (concat "^\\(\\*+\\)[ \t]+\\("
  3397. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3398. "\\)\\>")
  3399. org-todo-line-regexp
  3400. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3401. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3402. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3403. org-complex-heading-regexp
  3404. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3405. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3406. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3407. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3408. org-complex-heading-regexp-format
  3409. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3410. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3411. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3412. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3413. org-nl-done-regexp
  3414. (concat "\n\\*+[ \t]+"
  3415. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3416. "\\)" "\\>")
  3417. org-todo-line-tags-regexp
  3418. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3419. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3420. (org-re
  3421. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3422. org-looking-at-done-regexp
  3423. (concat "^" "\\(?:"
  3424. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3425. "\\>")
  3426. org-deadline-regexp (concat "\\<" org-deadline-string)
  3427. org-deadline-time-regexp
  3428. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3429. org-deadline-line-regexp
  3430. (concat "\\<\\(" org-deadline-string "\\).*")
  3431. org-scheduled-regexp
  3432. (concat "\\<" org-scheduled-string)
  3433. org-scheduled-time-regexp
  3434. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3435. org-closed-time-regexp
  3436. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3437. org-keyword-time-regexp
  3438. (concat "\\<\\(" org-scheduled-string
  3439. "\\|" org-deadline-string
  3440. "\\|" org-closed-string
  3441. "\\|" org-clock-string "\\)"
  3442. " *[[<]\\([^]>]+\\)[]>]")
  3443. org-keyword-time-not-clock-regexp
  3444. (concat "\\<\\(" org-scheduled-string
  3445. "\\|" org-deadline-string
  3446. "\\|" org-closed-string
  3447. "\\)"
  3448. " *[[<]\\([^]>]+\\)[]>]")
  3449. org-maybe-keyword-time-regexp
  3450. (concat "\\(\\<\\(" org-scheduled-string
  3451. "\\|" org-deadline-string
  3452. "\\|" org-closed-string
  3453. "\\|" org-clock-string "\\)\\)?"
  3454. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3455. org-planning-or-clock-line-re
  3456. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3457. "\\|" org-deadline-string
  3458. "\\|" org-closed-string "\\|" org-clock-string
  3459. "\\)\\>\\)")
  3460. )
  3461. (org-compute-latex-and-specials-regexp)
  3462. (org-set-font-lock-defaults))))
  3463. (defun org-file-contents (file &optional noerror)
  3464. "Return the contents of FILE, as a string."
  3465. (if (or (not file)
  3466. (not (file-readable-p file)))
  3467. (if noerror
  3468. (progn
  3469. (message "Cannot read file %s" file)
  3470. (ding) (sit-for 2)
  3471. "")
  3472. (error "Cannot read file %s" file))
  3473. (with-temp-buffer
  3474. (insert-file-contents file)
  3475. (buffer-string))))
  3476. (defun org-extract-log-state-settings (x)
  3477. "Extract the log state setting from a TODO keyword string.
  3478. This will extract info from a string like \"WAIT(w@/!)\"."
  3479. (let (kw key log1 log2)
  3480. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3481. (setq kw (match-string 1 x)
  3482. key (and (match-end 2) (match-string 2 x))
  3483. log1 (and (match-end 3) (match-string 3 x))
  3484. log2 (and (match-end 4) (match-string 4 x)))
  3485. (and (or log1 log2)
  3486. (list kw
  3487. (and log1 (if (equal log1 "!") 'time 'note))
  3488. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3489. (defun org-remove-keyword-keys (list)
  3490. "Remove a pair of parenthesis at the end of each string in LIST."
  3491. (mapcar (lambda (x)
  3492. (if (string-match "(.*)$" x)
  3493. (substring x 0 (match-beginning 0))
  3494. x))
  3495. list))
  3496. ;; FIXME: this could be done much better, using second characters etc.
  3497. (defun org-assign-fast-keys (alist)
  3498. "Assign fast keys to a keyword-key alist.
  3499. Respect keys that are already there."
  3500. (let (new e k c c1 c2 (char ?a))
  3501. (while (setq e (pop alist))
  3502. (cond
  3503. ((equal e '(:startgroup)) (push e new))
  3504. ((equal e '(:endgroup)) (push e new))
  3505. ((equal e '(:newline)) (push e new))
  3506. (t
  3507. (setq k (car e) c2 nil)
  3508. (if (cdr e)
  3509. (setq c (cdr e))
  3510. ;; automatically assign a character.
  3511. (setq c1 (string-to-char
  3512. (downcase (substring
  3513. k (if (= (string-to-char k) ?@) 1 0)))))
  3514. (if (or (rassoc c1 new) (rassoc c1 alist))
  3515. (while (or (rassoc char new) (rassoc char alist))
  3516. (setq char (1+ char)))
  3517. (setq c2 c1))
  3518. (setq c (or c2 char)))
  3519. (push (cons k c) new))))
  3520. (nreverse new)))
  3521. ;;; Some variables used in various places
  3522. (defvar org-window-configuration nil
  3523. "Used in various places to store a window configuration.")
  3524. (defvar org-selected-window nil
  3525. "Used in various places to store a window configuration.")
  3526. (defvar org-finish-function nil
  3527. "Function to be called when `C-c C-c' is used.
  3528. This is for getting out of special buffers like remember.")
  3529. ;; FIXME: Occasionally check by commenting these, to make sure
  3530. ;; no other functions uses these, forgetting to let-bind them.
  3531. (defvar entry)
  3532. (defvar last-state)
  3533. (defvar date)
  3534. ;; Defined somewhere in this file, but used before definition.
  3535. (defvar org-html-entities)
  3536. (defvar org-struct-menu)
  3537. (defvar org-org-menu)
  3538. (defvar org-tbl-menu)
  3539. (defvar org-agenda-keymap)
  3540. ;;;; Define the Org-mode
  3541. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3542. (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."))
  3543. ;; We use a before-change function to check if a table might need
  3544. ;; an update.
  3545. (defvar org-table-may-need-update t
  3546. "Indicates that a table might need an update.
  3547. This variable is set by `org-before-change-function'.
  3548. `org-table-align' sets it back to nil.")
  3549. (defun org-before-change-function (beg end)
  3550. "Every change indicates that a table might need an update."
  3551. (setq org-table-may-need-update t))
  3552. (defvar org-mode-map)
  3553. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3554. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3555. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3556. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3557. (defvar org-table-buffer-is-an nil)
  3558. (defconst org-outline-regexp "\\*+ ")
  3559. ;;;###autoload
  3560. (define-derived-mode org-mode outline-mode "Org"
  3561. "Outline-based notes management and organizer, alias
  3562. \"Carsten's outline-mode for keeping track of everything.\"
  3563. Org-mode develops organizational tasks around a NOTES file which
  3564. contains information about projects as plain text. Org-mode is
  3565. implemented on top of outline-mode, which is ideal to keep the content
  3566. of large files well structured. It supports ToDo items, deadlines and
  3567. time stamps, which magically appear in the diary listing of the Emacs
  3568. calendar. Tables are easily created with a built-in table editor.
  3569. Plain text URL-like links connect to websites, emails (VM), Usenet
  3570. messages (Gnus), BBDB entries, and any files related to the project.
  3571. For printing and sharing of notes, an Org-mode file (or a part of it)
  3572. can be exported as a structured ASCII or HTML file.
  3573. The following commands are available:
  3574. \\{org-mode-map}"
  3575. ;; Get rid of Outline menus, they are not needed
  3576. ;; Need to do this here because define-derived-mode sets up
  3577. ;; the keymap so late. Still, it is a waste to call this each time
  3578. ;; we switch another buffer into org-mode.
  3579. (if (featurep 'xemacs)
  3580. (when (boundp 'outline-mode-menu-heading)
  3581. ;; Assume this is Greg's port, it used easymenu
  3582. (easy-menu-remove outline-mode-menu-heading)
  3583. (easy-menu-remove outline-mode-menu-show)
  3584. (easy-menu-remove outline-mode-menu-hide))
  3585. (define-key org-mode-map [menu-bar headings] 'undefined)
  3586. (define-key org-mode-map [menu-bar hide] 'undefined)
  3587. (define-key org-mode-map [menu-bar show] 'undefined))
  3588. (org-load-modules-maybe)
  3589. (easy-menu-add org-org-menu)
  3590. (easy-menu-add org-tbl-menu)
  3591. (org-install-agenda-files-menu)
  3592. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3593. (org-add-to-invisibility-spec '(org-cwidth))
  3594. (org-add-to-invisibility-spec '(org-hide-block . t))
  3595. (when (featurep 'xemacs)
  3596. (org-set-local 'line-move-ignore-invisible t))
  3597. (org-set-local 'outline-regexp org-outline-regexp)
  3598. (org-set-local 'outline-level 'org-outline-level)
  3599. (when (and org-ellipsis
  3600. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3601. (fboundp 'make-glyph-code))
  3602. (unless org-display-table
  3603. (setq org-display-table (make-display-table)))
  3604. (set-display-table-slot
  3605. org-display-table 4
  3606. (vconcat (mapcar
  3607. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3608. org-ellipsis)))
  3609. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3610. (setq buffer-display-table org-display-table))
  3611. (org-set-regexps-and-options)
  3612. (when (and org-tag-faces (not org-tags-special-faces-re))
  3613. ;; tag faces set outside customize.... force initialization.
  3614. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3615. ;; Calc embedded
  3616. (org-set-local 'calc-embedded-open-mode "# ")
  3617. (modify-syntax-entry ?# "<")
  3618. (modify-syntax-entry ?@ "w")
  3619. (if org-startup-truncated (setq truncate-lines t))
  3620. (org-set-local 'font-lock-unfontify-region-function
  3621. 'org-unfontify-region)
  3622. ;; Activate before-change-function
  3623. (org-set-local 'org-table-may-need-update t)
  3624. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3625. 'local)
  3626. ;; Check for running clock before killing a buffer
  3627. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3628. ;; Paragraphs and auto-filling
  3629. (org-set-autofill-regexps)
  3630. (setq indent-line-function 'org-indent-line-function)
  3631. (org-update-radio-target-regexp)
  3632. ;; Make sure dependence stuff works reliably, even for users who set it
  3633. ;; too late :-(
  3634. (if org-enforce-todo-dependencies
  3635. (add-hook 'org-blocker-hook
  3636. 'org-block-todo-from-children-or-siblings-or-parent)
  3637. (remove-hook 'org-blocker-hook
  3638. 'org-block-todo-from-children-or-siblings-or-parent))
  3639. (if org-enforce-todo-checkbox-dependencies
  3640. (add-hook 'org-blocker-hook
  3641. 'org-block-todo-from-checkboxes)
  3642. (remove-hook 'org-blocker-hook
  3643. 'org-block-todo-from-checkboxes))
  3644. ;; Comment characters
  3645. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3646. (org-set-local 'comment-padding " ")
  3647. ;; Align options lines
  3648. (org-set-local
  3649. 'align-mode-rules-list
  3650. '((org-in-buffer-settings
  3651. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3652. (modes . '(org-mode)))))
  3653. ;; Imenu
  3654. (org-set-local 'imenu-create-index-function
  3655. 'org-imenu-get-tree)
  3656. ;; Make isearch reveal context
  3657. (if (or (featurep 'xemacs)
  3658. (not (boundp 'outline-isearch-open-invisible-function)))
  3659. ;; Emacs 21 and XEmacs make use of the hook
  3660. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3661. ;; Emacs 22 deals with this through a special variable
  3662. (org-set-local 'outline-isearch-open-invisible-function
  3663. (lambda (&rest ignore) (org-show-context 'isearch))))
  3664. ;; If empty file that did not turn on org-mode automatically, make it to.
  3665. (if (and org-insert-mode-line-in-empty-file
  3666. (interactive-p)
  3667. (= (point-min) (point-max)))
  3668. (insert "# -*- mode: org -*-\n\n"))
  3669. (unless org-inhibit-startup
  3670. (when org-startup-align-all-tables
  3671. (let ((bmp (buffer-modified-p)))
  3672. (org-table-map-tables 'org-table-align)
  3673. (set-buffer-modified-p bmp)))
  3674. (when org-startup-indented
  3675. (require 'org-indent)
  3676. (org-indent-mode 1))
  3677. (org-set-startup-visibility)))
  3678. (when (fboundp 'abbrev-table-put)
  3679. (abbrev-table-put org-mode-abbrev-table
  3680. :parents (list text-mode-abbrev-table)))
  3681. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3682. (defun org-current-time ()
  3683. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3684. (if (> (car org-time-stamp-rounding-minutes) 1)
  3685. (let ((r (car org-time-stamp-rounding-minutes))
  3686. (time (decode-time)))
  3687. (apply 'encode-time
  3688. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3689. (nthcdr 2 time))))
  3690. (current-time)))
  3691. ;;;; Font-Lock stuff, including the activators
  3692. (defvar org-mouse-map (make-sparse-keymap))
  3693. (org-defkey org-mouse-map
  3694. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3695. (org-defkey org-mouse-map
  3696. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3697. (when org-mouse-1-follows-link
  3698. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3699. (when org-tab-follows-link
  3700. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3701. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3702. (require 'font-lock)
  3703. (defconst org-non-link-chars "]\t\n\r<>")
  3704. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3705. "shell" "elisp"))
  3706. (defvar org-link-types-re nil
  3707. "Matches a link that has a url-like prefix like \"http:\"")
  3708. (defvar org-link-re-with-space nil
  3709. "Matches a link with spaces, optional angular brackets around it.")
  3710. (defvar org-link-re-with-space2 nil
  3711. "Matches a link with spaces, optional angular brackets around it.")
  3712. (defvar org-link-re-with-space3 nil
  3713. "Matches a link with spaces, only for internal part in bracket links.")
  3714. (defvar org-angle-link-re nil
  3715. "Matches link with angular brackets, spaces are allowed.")
  3716. (defvar org-plain-link-re nil
  3717. "Matches plain link, without spaces.")
  3718. (defvar org-bracket-link-regexp nil
  3719. "Matches a link in double brackets.")
  3720. (defvar org-bracket-link-analytic-regexp nil
  3721. "Regular expression used to analyze links.
  3722. Here is what the match groups contain after a match:
  3723. 1: http:
  3724. 2: http
  3725. 3: path
  3726. 4: [desc]
  3727. 5: desc")
  3728. (defvar org-bracket-link-analytic-regexp++ nil
  3729. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3730. (defvar org-any-link-re nil
  3731. "Regular expression matching any link.")
  3732. (defun org-make-link-regexps ()
  3733. "Update the link regular expressions.
  3734. This should be called after the variable `org-link-types' has changed."
  3735. (setq org-link-types-re
  3736. (concat
  3737. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3738. org-link-re-with-space
  3739. (concat
  3740. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3741. "\\([^" org-non-link-chars " ]"
  3742. "[^" org-non-link-chars "]*"
  3743. "[^" org-non-link-chars " ]\\)>?")
  3744. org-link-re-with-space2
  3745. (concat
  3746. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3747. "\\([^" org-non-link-chars " ]"
  3748. "[^\t\n\r]*"
  3749. "[^" org-non-link-chars " ]\\)>?")
  3750. org-link-re-with-space3
  3751. (concat
  3752. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3753. "\\([^" org-non-link-chars " ]"
  3754. "[^\t\n\r]*\\)")
  3755. org-angle-link-re
  3756. (concat
  3757. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3758. "\\([^" org-non-link-chars " ]"
  3759. "[^" org-non-link-chars "]*"
  3760. "\\)>")
  3761. org-plain-link-re
  3762. (concat
  3763. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3764. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3765. org-bracket-link-regexp
  3766. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3767. org-bracket-link-analytic-regexp
  3768. (concat
  3769. "\\[\\["
  3770. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3771. "\\([^]]+\\)"
  3772. "\\]"
  3773. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3774. "\\]")
  3775. org-bracket-link-analytic-regexp++
  3776. (concat
  3777. "\\[\\["
  3778. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3779. "\\([^]]+\\)"
  3780. "\\]"
  3781. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3782. "\\]")
  3783. org-any-link-re
  3784. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3785. org-angle-link-re "\\)\\|\\("
  3786. org-plain-link-re "\\)")))
  3787. (org-make-link-regexps)
  3788. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3789. "Regular expression for fast time stamp matching.")
  3790. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3791. "Regular expression for fast time stamp matching.")
  3792. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3793. "Regular expression matching time strings for analysis.
  3794. This one does not require the space after the date, so it can be used
  3795. on a string that terminates immediately after the date.")
  3796. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3797. "Regular expression matching time strings for analysis.")
  3798. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3799. "Regular expression matching time stamps, with groups.")
  3800. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3801. "Regular expression matching time stamps (also [..]), with groups.")
  3802. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3803. "Regular expression matching a time stamp range.")
  3804. (defconst org-tr-regexp-both
  3805. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3806. "Regular expression matching a time stamp range.")
  3807. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3808. org-ts-regexp "\\)?")
  3809. "Regular expression matching a time stamp or time stamp range.")
  3810. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3811. org-ts-regexp-both "\\)?")
  3812. "Regular expression matching a time stamp or time stamp range.
  3813. The time stamps may be either active or inactive.")
  3814. (defvar org-emph-face nil)
  3815. (defun org-do-emphasis-faces (limit)
  3816. "Run through the buffer and add overlays to links."
  3817. (let (rtn a)
  3818. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3819. (if (not (= (char-after (match-beginning 3))
  3820. (char-after (match-beginning 4))))
  3821. (progn
  3822. (setq rtn t)
  3823. (setq a (assoc (match-string 3) org-emphasis-alist))
  3824. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3825. 'face
  3826. (nth 1 a))
  3827. (and (nth 4 a)
  3828. (org-remove-flyspell-overlays-in
  3829. (match-beginning 0) (match-end 0)))
  3830. (add-text-properties (match-beginning 2) (match-end 2)
  3831. '(font-lock-multiline t))
  3832. (when org-hide-emphasis-markers
  3833. (add-text-properties (match-end 4) (match-beginning 5)
  3834. '(invisible org-link))
  3835. (add-text-properties (match-beginning 3) (match-end 3)
  3836. '(invisible org-link)))))
  3837. (backward-char 1))
  3838. rtn))
  3839. (defun org-emphasize (&optional char)
  3840. "Insert or change an emphasis, i.e. a font like bold or italic.
  3841. If there is an active region, change that region to a new emphasis.
  3842. If there is no region, just insert the marker characters and position
  3843. the cursor between them.
  3844. CHAR should be either the marker character, or the first character of the
  3845. HTML tag associated with that emphasis. If CHAR is a space, the means
  3846. to remove the emphasis of the selected region.
  3847. If char is not given (for example in an interactive call) it
  3848. will be prompted for."
  3849. (interactive)
  3850. (let ((eal org-emphasis-alist) e det
  3851. (erc org-emphasis-regexp-components)
  3852. (prompt "")
  3853. (string "") beg end move tag c s)
  3854. (if (org-region-active-p)
  3855. (setq beg (region-beginning) end (region-end)
  3856. string (buffer-substring beg end))
  3857. (setq move t))
  3858. (while (setq e (pop eal))
  3859. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3860. c (aref tag 0))
  3861. (push (cons c (string-to-char (car e))) det)
  3862. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3863. (substring tag 1)))))
  3864. (setq det (nreverse det))
  3865. (unless char
  3866. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3867. (setq char (read-char-exclusive)))
  3868. (setq char (or (cdr (assoc char det)) char))
  3869. (if (equal char ?\ )
  3870. (setq s "" move nil)
  3871. (unless (assoc (char-to-string char) org-emphasis-alist)
  3872. (error "No such emphasis marker: \"%c\"" char))
  3873. (setq s (char-to-string char)))
  3874. (while (and (> (length string) 1)
  3875. (equal (substring string 0 1) (substring string -1))
  3876. (assoc (substring string 0 1) org-emphasis-alist))
  3877. (setq string (substring string 1 -1)))
  3878. (setq string (concat s string s))
  3879. (if beg (delete-region beg end))
  3880. (unless (or (bolp)
  3881. (string-match (concat "[" (nth 0 erc) "\n]")
  3882. (char-to-string (char-before (point)))))
  3883. (insert " "))
  3884. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3885. (char-to-string (char-after (point))))
  3886. (insert " ") (backward-char 1))
  3887. (insert string)
  3888. (and move (backward-char 1))))
  3889. (defconst org-nonsticky-props
  3890. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3891. (defsubst org-rear-nonsticky-at (pos)
  3892. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3893. (defun org-activate-plain-links (limit)
  3894. "Run through the buffer and add overlays to links."
  3895. (catch 'exit
  3896. (let (f)
  3897. (if (re-search-forward org-plain-link-re limit t)
  3898. (progn
  3899. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3900. (setq f (get-text-property (match-beginning 0) 'face))
  3901. (if (or (eq f 'org-tag)
  3902. (and (listp f) (memq 'org-tag f)))
  3903. nil
  3904. (add-text-properties (match-beginning 0) (match-end 0)
  3905. (list 'mouse-face 'highlight
  3906. 'keymap org-mouse-map))
  3907. (org-rear-nonsticky-at (match-end 0)))
  3908. t)))))
  3909. (defun org-activate-code (limit)
  3910. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3911. (progn
  3912. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3913. (remove-text-properties (match-beginning 0) (match-end 0)
  3914. '(display t invisible t intangible t))
  3915. t)))
  3916. (defun org-fontify-meta-lines-and-blocks (limit)
  3917. "Fontify #+ lines and blocks, in the correct ways."
  3918. (let ((case-fold-search t))
  3919. (if (re-search-forward
  3920. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3921. limit t)
  3922. (let ((beg (match-beginning 0))
  3923. (beg1 (line-beginning-position 2))
  3924. (dc1 (downcase (match-string 2)))
  3925. (dc3 (downcase (match-string 3)))
  3926. end end1 quoting)
  3927. (cond
  3928. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3929. ;; a single line of backend-specific content
  3930. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3931. (remove-text-properties (match-beginning 0) (match-end 0)
  3932. '(display t invisible t intangible t))
  3933. (add-text-properties (match-beginning 1) (match-end 3)
  3934. '(font-lock-fontified t face org-meta-line))
  3935. (add-text-properties (match-beginning 6) (match-end 6)
  3936. '(font-lock-fontified t face org-block))
  3937. t)
  3938. ((and (match-end 4) (equal dc3 "begin"))
  3939. ;; Truely a block
  3940. (setq quoting (member (downcase (match-string 5))
  3941. org-protecting-blocks))
  3942. (when (re-search-forward
  3943. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3944. nil t) ;; on purpose, we look further than LIMIT
  3945. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3946. (when quoting
  3947. (remove-text-properties beg end
  3948. '(display t invisible t intangible t)))
  3949. (add-text-properties
  3950. beg end
  3951. '(font-lock-fontified t font-lock-multiline t))
  3952. (add-text-properties beg beg1 '(face org-meta-line))
  3953. (add-text-properties end1 end '(face org-meta-line))
  3954. (when quoting
  3955. (add-text-properties beg1 end1 '(face org-block)))
  3956. t))
  3957. ((not (member (char-after beg) '(?\ ?\t)))
  3958. ;; just any other in-buffer setting, but not indented
  3959. (add-text-properties
  3960. beg (match-end 0)
  3961. '(font-lock-fontified t face org-meta-line))
  3962. t)
  3963. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  3964. "orgtbl:" "tblfm:" "tblname:"))
  3965. (and (match-end 4) (equal dc3 "attr")))
  3966. (add-text-properties
  3967. beg (match-end 0)
  3968. '(font-lock-fontified t face org-meta-line))
  3969. t)
  3970. ((member dc3 '(" " ""))
  3971. (add-text-properties
  3972. beg (match-end 0)
  3973. '(font-lock-fontified t face font-lock-comment-face)))
  3974. (t nil))))))
  3975. (defun org-activate-angle-links (limit)
  3976. "Run through the buffer and add overlays to links."
  3977. (if (re-search-forward org-angle-link-re limit t)
  3978. (progn
  3979. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3980. (add-text-properties (match-beginning 0) (match-end 0)
  3981. (list 'mouse-face 'highlight
  3982. 'keymap org-mouse-map))
  3983. (org-rear-nonsticky-at (match-end 0))
  3984. t)))
  3985. (defun org-activate-footnote-links (limit)
  3986. "Run through the buffer and add overlays to links."
  3987. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3988. limit t)
  3989. (progn
  3990. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3991. (add-text-properties (match-beginning 2) (match-end 2)
  3992. (list 'mouse-face 'highlight
  3993. 'keymap org-mouse-map
  3994. 'help-echo
  3995. (if (= (point-at-bol) (match-beginning 2))
  3996. "Footnote definition"
  3997. "Footnote reference")
  3998. ))
  3999. (org-rear-nonsticky-at (match-end 2))
  4000. t)))
  4001. (defun org-activate-bracket-links (limit)
  4002. "Run through the buffer and add overlays to bracketed links."
  4003. (if (re-search-forward org-bracket-link-regexp limit t)
  4004. (let* ((help (concat "LINK: "
  4005. (org-match-string-no-properties 1)))
  4006. ;; FIXME: above we should remove the escapes.
  4007. ;; but that requires another match, protecting match data,
  4008. ;; a lot of overhead for font-lock.
  4009. (ip (org-maybe-intangible
  4010. (list 'invisible 'org-link
  4011. 'keymap org-mouse-map 'mouse-face 'highlight
  4012. 'font-lock-multiline t 'help-echo help)))
  4013. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4014. 'font-lock-multiline t 'help-echo help)))
  4015. ;; We need to remove the invisible property here. Table narrowing
  4016. ;; may have made some of this invisible.
  4017. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4018. (remove-text-properties (match-beginning 0) (match-end 0)
  4019. '(invisible nil))
  4020. (if (match-end 3)
  4021. (progn
  4022. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4023. (org-rear-nonsticky-at (match-beginning 3))
  4024. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4025. (org-rear-nonsticky-at (match-end 3))
  4026. (add-text-properties (match-end 3) (match-end 0) ip)
  4027. (org-rear-nonsticky-at (match-end 0)))
  4028. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4029. (org-rear-nonsticky-at (match-beginning 1))
  4030. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4031. (org-rear-nonsticky-at (match-end 1))
  4032. (add-text-properties (match-end 1) (match-end 0) ip)
  4033. (org-rear-nonsticky-at (match-end 0)))
  4034. t)))
  4035. (defun org-activate-dates (limit)
  4036. "Run through the buffer and add overlays to dates."
  4037. (if (re-search-forward org-tsr-regexp-both limit t)
  4038. (progn
  4039. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4040. (add-text-properties (match-beginning 0) (match-end 0)
  4041. (list 'mouse-face 'highlight
  4042. 'keymap org-mouse-map))
  4043. (org-rear-nonsticky-at (match-end 0))
  4044. (when org-display-custom-times
  4045. (if (match-end 3)
  4046. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4047. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4048. t)))
  4049. (defvar org-target-link-regexp nil
  4050. "Regular expression matching radio targets in plain text.")
  4051. (make-variable-buffer-local 'org-target-link-regexp)
  4052. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4053. "Regular expression matching a link target.")
  4054. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4055. "Regular expression matching a radio target.")
  4056. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4057. "Regular expression matching any target.")
  4058. (defun org-activate-target-links (limit)
  4059. "Run through the buffer and add overlays to target matches."
  4060. (when org-target-link-regexp
  4061. (let ((case-fold-search t))
  4062. (if (re-search-forward org-target-link-regexp limit t)
  4063. (progn
  4064. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4065. (add-text-properties (match-beginning 0) (match-end 0)
  4066. (list 'mouse-face 'highlight
  4067. 'keymap org-mouse-map
  4068. 'help-echo "Radio target link"
  4069. 'org-linked-text t))
  4070. (org-rear-nonsticky-at (match-end 0))
  4071. t)))))
  4072. (defun org-update-radio-target-regexp ()
  4073. "Find all radio targets in this file and update the regular expression."
  4074. (interactive)
  4075. (when (memq 'radio org-activate-links)
  4076. (setq org-target-link-regexp
  4077. (org-make-target-link-regexp (org-all-targets 'radio)))
  4078. (org-restart-font-lock)))
  4079. (defun org-hide-wide-columns (limit)
  4080. (let (s e)
  4081. (setq s (text-property-any (point) (or limit (point-max))
  4082. 'org-cwidth t))
  4083. (when s
  4084. (setq e (next-single-property-change s 'org-cwidth))
  4085. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4086. (goto-char e)
  4087. t)))
  4088. (defvar org-latex-and-specials-regexp nil
  4089. "Regular expression for highlighting export special stuff.")
  4090. (defvar org-match-substring-regexp)
  4091. (defvar org-match-substring-with-braces-regexp)
  4092. ;; This should be with the exporter code, but we also use if for font-locking
  4093. (defconst org-export-html-special-string-regexps
  4094. '(("\\\\-" . "&shy;")
  4095. ("---\\([^-]\\)" . "&mdash;\\1")
  4096. ("--\\([^-]\\)" . "&ndash;\\1")
  4097. ("\\.\\.\\." . "&hellip;"))
  4098. "Regular expressions for special string conversion.")
  4099. (defun org-compute-latex-and-specials-regexp ()
  4100. "Compute regular expression for stuff treated specially by exporters."
  4101. (if (not org-highlight-latex-fragments-and-specials)
  4102. (org-set-local 'org-latex-and-specials-regexp nil)
  4103. (require 'org-exp)
  4104. (let*
  4105. ((matchers (plist-get org-format-latex-options :matchers))
  4106. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4107. org-latex-regexps)))
  4108. (options (org-combine-plists (org-default-export-plist)
  4109. (org-infile-export-plist)))
  4110. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4111. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4112. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4113. (org-export-html-expand (plist-get options :expand-quoted-html))
  4114. (org-export-with-special-strings (plist-get options :special-strings))
  4115. (re-sub
  4116. (cond
  4117. ((equal org-export-with-sub-superscripts '{})
  4118. (list org-match-substring-with-braces-regexp))
  4119. (org-export-with-sub-superscripts
  4120. (list org-match-substring-regexp))
  4121. (t nil)))
  4122. (re-latex
  4123. (if org-export-with-LaTeX-fragments
  4124. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4125. (re-macros
  4126. (if org-export-with-TeX-macros
  4127. (list (concat "\\\\"
  4128. (regexp-opt
  4129. (append (mapcar 'car org-html-entities)
  4130. (if (boundp 'org-latex-entities)
  4131. (mapcar (lambda (x)
  4132. (or (car-safe x) x))
  4133. org-latex-entities)
  4134. nil))
  4135. 'words))) ; FIXME
  4136. ))
  4137. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4138. (re-special (if org-export-with-special-strings
  4139. (mapcar (lambda (x) (car x))
  4140. org-export-html-special-string-regexps)))
  4141. (re-rest
  4142. (delq nil
  4143. (list
  4144. (if org-export-html-expand "@<[^>\n]+>")
  4145. ))))
  4146. (org-set-local
  4147. 'org-latex-and-specials-regexp
  4148. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4149. re-rest) "\\|")))))
  4150. (defun org-do-latex-and-special-faces (limit)
  4151. "Run through the buffer and add overlays to links."
  4152. (when org-latex-and-specials-regexp
  4153. (let (rtn d)
  4154. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4155. limit t))
  4156. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4157. 'face))
  4158. '(org-code org-verbatim underline)))
  4159. (progn
  4160. (setq rtn t
  4161. d (cond ((member (char-after (1+ (match-beginning 0)))
  4162. '(?_ ?^)) 1)
  4163. (t 0)))
  4164. (font-lock-prepend-text-property
  4165. (+ d (match-beginning 0)) (match-end 0)
  4166. 'face 'org-latex-and-export-specials)
  4167. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4168. '(font-lock-multiline t)))))
  4169. rtn)))
  4170. (defun org-restart-font-lock ()
  4171. "Restart font-lock-mode, to force refontification."
  4172. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4173. (font-lock-mode -1)
  4174. (font-lock-mode 1)))
  4175. (defun org-all-targets (&optional radio)
  4176. "Return a list of all targets in this file.
  4177. With optional argument RADIO, only find radio targets."
  4178. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4179. rtn)
  4180. (save-excursion
  4181. (goto-char (point-min))
  4182. (while (re-search-forward re nil t)
  4183. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4184. rtn)))
  4185. (defun org-make-target-link-regexp (targets)
  4186. "Make regular expression matching all strings in TARGETS.
  4187. The regular expression finds the targets also if there is a line break
  4188. between words."
  4189. (and targets
  4190. (concat
  4191. "\\<\\("
  4192. (mapconcat
  4193. (lambda (x)
  4194. (while (string-match " +" x)
  4195. (setq x (replace-match "\\s-+" t t x)))
  4196. x)
  4197. targets
  4198. "\\|")
  4199. "\\)\\>")))
  4200. (defun org-activate-tags (limit)
  4201. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4202. (progn
  4203. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4204. (add-text-properties (match-beginning 1) (match-end 1)
  4205. (list 'mouse-face 'highlight
  4206. 'keymap org-mouse-map))
  4207. (org-rear-nonsticky-at (match-end 1))
  4208. t)))
  4209. (defun org-outline-level ()
  4210. "Compute the outline level of the heading at point.
  4211. This function assumes that the cursor is at the beginning of a line matched
  4212. by outline-regexp. Otherwise it returns garbage.
  4213. If this is called at a normal headline, the level is the number of stars.
  4214. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4215. For plain list items, if they are matched by `outline-regexp', this returns
  4216. 1000 plus the line indentation."
  4217. (save-excursion
  4218. (looking-at outline-regexp)
  4219. (if (match-beginning 1)
  4220. (+ (org-get-string-indentation (match-string 1)) 1000)
  4221. (1- (- (match-end 0) (match-beginning 0))))))
  4222. (defvar org-font-lock-keywords nil)
  4223. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4224. "Regular expression matching a property line.")
  4225. (defvar org-font-lock-hook nil
  4226. "Functions to be called for special font lock stuff.")
  4227. (defun org-font-lock-hook (limit)
  4228. (run-hook-with-args 'org-font-lock-hook limit))
  4229. (defun org-set-font-lock-defaults ()
  4230. (let* ((em org-fontify-emphasized-text)
  4231. (lk org-activate-links)
  4232. (org-font-lock-extra-keywords
  4233. (list
  4234. ;; Call the hook
  4235. '(org-font-lock-hook)
  4236. ;; Headlines
  4237. `(,(if org-fontify-whole-heading-line
  4238. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4239. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4240. (1 (org-get-level-face 1))
  4241. (2 (org-get-level-face 2))
  4242. (3 (org-get-level-face 3)))
  4243. ;; Table lines
  4244. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4245. (1 'org-table t))
  4246. ;; Table internals
  4247. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4248. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4249. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4250. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4251. ;; Drawers
  4252. (list org-drawer-regexp '(0 'org-special-keyword t))
  4253. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4254. ;; Properties
  4255. (list org-property-re
  4256. '(1 'org-special-keyword t)
  4257. '(3 'org-property-value t))
  4258. ;; Links
  4259. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4260. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4261. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4262. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4263. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4264. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4265. (if (memq 'footnote lk) '(org-activate-footnote-links
  4266. (2 'org-footnote t)))
  4267. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4268. '(org-hide-wide-columns (0 nil append))
  4269. ;; TODO lines
  4270. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4271. '(1 (org-get-todo-face 1) t))
  4272. ;; DONE
  4273. (if org-fontify-done-headline
  4274. (list (concat "^[*]+ +\\<\\("
  4275. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4276. "\\)\\(.*\\)")
  4277. '(2 'org-headline-done t))
  4278. nil)
  4279. ;; Priorities
  4280. '(org-font-lock-add-priority-faces)
  4281. ;; Tags
  4282. '(org-font-lock-add-tag-faces)
  4283. ;; Special keywords
  4284. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4285. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4286. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4287. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4288. ;; Emphasis
  4289. (if em
  4290. (if (featurep 'xemacs)
  4291. '(org-do-emphasis-faces (0 nil append))
  4292. '(org-do-emphasis-faces)))
  4293. ;; Checkboxes
  4294. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4295. 2 'org-checkbox prepend)
  4296. (if org-provide-checkbox-statistics
  4297. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4298. (0 (org-get-checkbox-statistics-face) t)))
  4299. ;; Description list items
  4300. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4301. 2 'bold prepend)
  4302. ;; ARCHIVEd headings
  4303. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4304. '(1 'org-archived prepend))
  4305. ;; Specials
  4306. '(org-do-latex-and-special-faces)
  4307. ;; Code
  4308. '(org-activate-code (1 'org-code t))
  4309. ;; COMMENT
  4310. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4311. "\\|" org-quote-string "\\)\\>")
  4312. '(1 'org-special-keyword t))
  4313. '("^#.*" (0 'font-lock-comment-face t))
  4314. ;; Blocks and meta lines
  4315. '(org-fontify-meta-lines-and-blocks)
  4316. )))
  4317. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4318. ;; Now set the full font-lock-keywords
  4319. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4320. (org-set-local 'font-lock-defaults
  4321. '(org-font-lock-keywords t nil nil backward-paragraph))
  4322. (kill-local-variable 'font-lock-keywords) nil))
  4323. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4324. "Fontify string S like in Org-mode"
  4325. (with-temp-buffer
  4326. (insert s)
  4327. (let ((org-odd-levels-only odd-levels))
  4328. (org-mode)
  4329. (font-lock-fontify-buffer)
  4330. (buffer-string))))
  4331. (defvar org-m nil)
  4332. (defvar org-l nil)
  4333. (defvar org-f nil)
  4334. (defun org-get-level-face (n)
  4335. "Get the right face for match N in font-lock matching of headlines."
  4336. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4337. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4338. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4339. (cond
  4340. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4341. ((eq n 2) org-f)
  4342. (t (if org-level-color-stars-only nil org-f))))
  4343. (defun org-get-todo-face (kwd)
  4344. "Get the right face for a TODO keyword KWD.
  4345. If KWD is a number, get the corresponding match group."
  4346. (if (numberp kwd) (setq kwd (match-string kwd)))
  4347. (or (cdr (assoc kwd org-todo-keyword-faces))
  4348. (and (member kwd org-done-keywords) 'org-done)
  4349. 'org-todo))
  4350. (defun org-font-lock-add-tag-faces (limit)
  4351. "Add the special tag faces."
  4352. (when (and org-tag-faces org-tags-special-faces-re)
  4353. (while (re-search-forward org-tags-special-faces-re limit t)
  4354. (add-text-properties (match-beginning 1) (match-end 1)
  4355. (list 'face (org-get-tag-face 1)
  4356. 'font-lock-fontified t))
  4357. (backward-char 1))))
  4358. (defun org-font-lock-add-priority-faces (limit)
  4359. "Add the special priority faces."
  4360. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4361. (add-text-properties
  4362. (match-beginning 0) (match-end 0)
  4363. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4364. org-priority-faces))
  4365. 'org-special-keyword)
  4366. 'font-lock-fontified t))))
  4367. (defun org-get-tag-face (kwd)
  4368. "Get the right face for a TODO keyword KWD.
  4369. If KWD is a number, get the corresponding match group."
  4370. (if (numberp kwd) (setq kwd (match-string kwd)))
  4371. (or (cdr (assoc kwd org-tag-faces))
  4372. 'org-tag))
  4373. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4374. "Remove fontification and activation overlays from links."
  4375. (font-lock-default-unfontify-region beg end)
  4376. (let* ((buffer-undo-list t)
  4377. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4378. (inhibit-modification-hooks t)
  4379. deactivate-mark buffer-file-name buffer-file-truename)
  4380. (remove-text-properties beg end
  4381. '(mouse-face t keymap t org-linked-text t
  4382. invisible t intangible t
  4383. line-prefix t wrap-prefix t
  4384. org-no-flyspell t))))
  4385. ;;;; Visibility cycling, including org-goto and indirect buffer
  4386. ;;; Cycling
  4387. (defvar org-cycle-global-status nil)
  4388. (make-variable-buffer-local 'org-cycle-global-status)
  4389. (defvar org-cycle-subtree-status nil)
  4390. (make-variable-buffer-local 'org-cycle-subtree-status)
  4391. ;;;###autoload
  4392. (defvar org-inlinetask-min-level)
  4393. (defun org-cycle (&optional arg)
  4394. "TAB-action and visibility cycling for Org-mode.
  4395. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4396. is outine visibility cycling, but it also invokes other actions
  4397. in special contexts.
  4398. - When this function is called with a prefix argument, rotate the entire
  4399. buffer through 3 states (global cycling)
  4400. 1. OVERVIEW: Show only top-level headlines.
  4401. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4402. 3. SHOW ALL: Show everything.
  4403. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4404. determined by the variable `org-startup-folded', and by any VISIBILITY
  4405. properties in the buffer.
  4406. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4407. including any drawers.
  4408. - When inside a table, re-align the table and move to the next field.
  4409. - When point is at the beginning of a headline, rotate the subtree started
  4410. by this line through 3 different states (local cycling)
  4411. 1. FOLDED: Only the main headline is shown.
  4412. 2. CHILDREN: The main headline and the direct children are shown.
  4413. From this state, you can move to one of the children
  4414. and zoom in further.
  4415. 3. SUBTREE: Show the entire subtree, including body text.
  4416. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4417. - When there is a numeric prefix, go up to a heading with level ARG, do
  4418. a `show-subtree' and return to the previous cursor position. If ARG
  4419. is negative, go up that many levels.
  4420. - When point is not at the beginning of a headline, execute the global
  4421. binding for TAB, which is re-indenting the line. See the option
  4422. `org-cycle-emulate-tab' for details.
  4423. - Special case: if point is at the beginning of the buffer and there is
  4424. no headline in line 1, this function will act as if called with prefix arg.
  4425. But only if also the variable `org-cycle-global-at-bob' is t."
  4426. (interactive "P")
  4427. (org-load-modules-maybe)
  4428. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4429. (and org-cycle-level-after-item/entry-creation
  4430. (or (org-cycle-level)
  4431. (org-cycle-item-indentation))))
  4432. (let* ((limit-level
  4433. (or org-cycle-max-level
  4434. (and (boundp 'org-inlinetask-min-level)
  4435. org-inlinetask-min-level
  4436. (1- org-inlinetask-min-level))))
  4437. (nstars (and limit-level
  4438. (if org-odd-levels-only
  4439. (and limit-level (1- (* limit-level 2)))
  4440. limit-level)))
  4441. (outline-regexp
  4442. (cond
  4443. ((not (org-mode-p)) outline-regexp)
  4444. ((or (eq org-cycle-include-plain-lists 'integrate)
  4445. (and org-cycle-include-plain-lists (org-at-item-p)))
  4446. (concat "\\(?:\\*"
  4447. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4448. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4449. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4450. (bob-special (and org-cycle-global-at-bob (bobp)
  4451. (not (looking-at outline-regexp))))
  4452. (org-cycle-hook
  4453. (if bob-special
  4454. (delq 'org-optimize-window-after-visibility-change
  4455. (copy-sequence org-cycle-hook))
  4456. org-cycle-hook))
  4457. (pos (point)))
  4458. (if (or bob-special (equal arg '(4)))
  4459. ;; special case: use global cycling
  4460. (setq arg t))
  4461. (cond
  4462. ((equal arg '(16))
  4463. (org-set-startup-visibility)
  4464. (message "Startup visibility, plus VISIBILITY properties"))
  4465. ((equal arg '(64))
  4466. (show-all)
  4467. (message "Entire buffer visible, including drawers"))
  4468. ((org-at-table-p 'any)
  4469. ;; Enter the table or move to the next field in the table
  4470. (or (org-table-recognize-table.el)
  4471. (progn
  4472. (if arg (org-table-edit-field t)
  4473. (org-table-justify-field-maybe)
  4474. (call-interactively 'org-table-next-field)))))
  4475. ((run-hook-with-args-until-success
  4476. 'org-tab-after-check-for-table-hook))
  4477. ((eq arg t) ;; Global cycling
  4478. (org-cycle-internal-global))
  4479. ((and org-drawers org-drawer-regexp
  4480. (save-excursion
  4481. (beginning-of-line 1)
  4482. (looking-at org-drawer-regexp)))
  4483. ;; Toggle block visibility
  4484. (org-flag-drawer
  4485. (not (get-char-property (match-end 0) 'invisible))))
  4486. ((integerp arg)
  4487. ;; Show-subtree, ARG levels up from here.
  4488. (save-excursion
  4489. (org-back-to-heading)
  4490. (outline-up-heading (if (< arg 0) (- arg)
  4491. (- (funcall outline-level) arg)))
  4492. (org-show-subtree)))
  4493. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4494. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4495. (org-cycle-internal-local))
  4496. ;; TAB emulation and template completion
  4497. (buffer-read-only (org-back-to-heading))
  4498. ((run-hook-with-args-until-success
  4499. 'org-tab-after-check-for-cycling-hook))
  4500. ((org-try-structure-completion))
  4501. ((org-try-cdlatex-tab))
  4502. ((run-hook-with-args-until-success
  4503. 'org-tab-before-tab-emulation-hook))
  4504. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4505. (or (not (bolp))
  4506. (not (looking-at outline-regexp))))
  4507. (call-interactively (global-key-binding "\t")))
  4508. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4509. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4510. (or (and (eq org-cycle-emulate-tab 'white)
  4511. (= (match-end 0) (point-at-eol)))
  4512. (and (eq org-cycle-emulate-tab 'whitestart)
  4513. (>= (match-end 0) pos))))
  4514. t
  4515. (eq org-cycle-emulate-tab t))
  4516. (call-interactively (global-key-binding "\t")))
  4517. (t (save-excursion
  4518. (org-back-to-heading)
  4519. (org-cycle)))))))
  4520. (defun org-cycle-internal-global ()
  4521. "Do the global cycling action."
  4522. (cond
  4523. ((and (eq last-command this-command)
  4524. (eq org-cycle-global-status 'overview))
  4525. ;; We just created the overview - now do table of contents
  4526. ;; This can be slow in very large buffers, so indicate action
  4527. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4528. (message "CONTENTS...")
  4529. (org-content)
  4530. (message "CONTENTS...done")
  4531. (setq org-cycle-global-status 'contents)
  4532. (run-hook-with-args 'org-cycle-hook 'contents))
  4533. ((and (eq last-command this-command)
  4534. (eq org-cycle-global-status 'contents))
  4535. ;; We just showed the table of contents - now show everything
  4536. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4537. (show-all)
  4538. (message "SHOW ALL")
  4539. (setq org-cycle-global-status 'all)
  4540. (run-hook-with-args 'org-cycle-hook 'all))
  4541. (t
  4542. ;; Default action: go to overview
  4543. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4544. (org-overview)
  4545. (message "OVERVIEW")
  4546. (setq org-cycle-global-status 'overview)
  4547. (run-hook-with-args 'org-cycle-hook 'overview))))
  4548. (defun org-cycle-internal-local ()
  4549. "Do the local cycling action."
  4550. (org-back-to-heading)
  4551. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4552. ;; First, some boundaries
  4553. (save-excursion
  4554. (org-back-to-heading)
  4555. (setq level (funcall outline-level))
  4556. (save-excursion
  4557. (beginning-of-line 2)
  4558. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4559. ; XEmacs does not have `next-single-char-property-change'
  4560. ; I'm not sure about Emacs 21.
  4561. (while (and (not (eobp)) ;; this is like `next-line'
  4562. (get-char-property (1- (point)) 'invisible))
  4563. (beginning-of-line 2))
  4564. (while (and (not (eobp)) ;; this is like `next-line'
  4565. (get-char-property (1- (point)) 'invisible))
  4566. (goto-char (next-single-char-property-change (point) 'invisible))
  4567. ;;;??? (or (bolp) (beginning-of-line 2))))
  4568. (and (eolp) (beginning-of-line 2))))
  4569. (setq eol (point)))
  4570. (outline-end-of-heading) (setq eoh (point))
  4571. (save-excursion
  4572. (outline-next-heading)
  4573. (setq has-children (and (org-at-heading-p t)
  4574. (> (funcall outline-level) level))))
  4575. (org-end-of-subtree t)
  4576. (unless (eobp)
  4577. (skip-chars-forward " \t\n")
  4578. (beginning-of-line 1) ; in case this is an item
  4579. )
  4580. (setq eos (if (eobp) (point) (1- (point)))))
  4581. ;; Find out what to do next and set `this-command'
  4582. (cond
  4583. ((= eos eoh)
  4584. ;; Nothing is hidden behind this heading
  4585. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4586. (message "EMPTY ENTRY")
  4587. (setq org-cycle-subtree-status nil)
  4588. (save-excursion
  4589. (goto-char eos)
  4590. (outline-next-heading)
  4591. (if (org-invisible-p) (org-flag-heading nil))))
  4592. ((and (or (>= eol eos)
  4593. (not (string-match "\\S-" (buffer-substring eol eos))))
  4594. (or has-children
  4595. (not (setq children-skipped
  4596. org-cycle-skip-children-state-if-no-children))))
  4597. ;; Entire subtree is hidden in one line: children view
  4598. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4599. (org-show-entry)
  4600. (show-children)
  4601. (message "CHILDREN")
  4602. (save-excursion
  4603. (goto-char eos)
  4604. (outline-next-heading)
  4605. (if (org-invisible-p) (org-flag-heading nil)))
  4606. (setq org-cycle-subtree-status 'children)
  4607. (run-hook-with-args 'org-cycle-hook 'children))
  4608. ((or children-skipped
  4609. (and (eq last-command this-command)
  4610. (eq org-cycle-subtree-status 'children)))
  4611. ;; We just showed the children, or no children are there,
  4612. ;; now show everything.
  4613. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4614. (org-show-subtree)
  4615. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4616. (setq org-cycle-subtree-status 'subtree)
  4617. (run-hook-with-args 'org-cycle-hook 'subtree))
  4618. (t
  4619. ;; Default action: hide the subtree.
  4620. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4621. (hide-subtree)
  4622. (message "FOLDED")
  4623. (setq org-cycle-subtree-status 'folded)
  4624. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4625. ;;;###autoload
  4626. (defun org-global-cycle (&optional arg)
  4627. "Cycle the global visibility. For details see `org-cycle'.
  4628. With C-u prefix arg, switch to startup visibility.
  4629. With a numeric prefix, show all headlines up to that level."
  4630. (interactive "P")
  4631. (let ((org-cycle-include-plain-lists
  4632. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4633. (cond
  4634. ((integerp arg)
  4635. (show-all)
  4636. (hide-sublevels arg)
  4637. (setq org-cycle-global-status 'contents))
  4638. ((equal arg '(4))
  4639. (org-set-startup-visibility)
  4640. (message "Startup visibility, plus VISIBILITY properties."))
  4641. (t
  4642. (org-cycle '(4))))))
  4643. (defun org-set-startup-visibility ()
  4644. "Set the visibility required by startup options and properties."
  4645. (cond
  4646. ((eq org-startup-folded t)
  4647. (org-cycle '(4)))
  4648. ((eq org-startup-folded 'content)
  4649. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4650. (org-cycle '(4)) (org-cycle '(4)))))
  4651. (unless (eq org-startup-folded 'showeverything)
  4652. (if org-hide-block-startup (org-hide-block-all))
  4653. (org-set-visibility-according-to-property 'no-cleanup)
  4654. (org-cycle-hide-archived-subtrees 'all)
  4655. (org-cycle-hide-drawers 'all)
  4656. (org-cycle-show-empty-lines 'all)))
  4657. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4658. "Switch subtree visibilities according to :VISIBILITY: property."
  4659. (interactive)
  4660. (let (org-show-entry-below state)
  4661. (save-excursion
  4662. (goto-char (point-min))
  4663. (while (re-search-forward
  4664. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4665. nil t)
  4666. (setq state (match-string 1))
  4667. (save-excursion
  4668. (org-back-to-heading t)
  4669. (hide-subtree)
  4670. (org-reveal)
  4671. (cond
  4672. ((equal state '("fold" "folded"))
  4673. (hide-subtree))
  4674. ((equal state "children")
  4675. (org-show-hidden-entry)
  4676. (show-children))
  4677. ((equal state "content")
  4678. (save-excursion
  4679. (save-restriction
  4680. (org-narrow-to-subtree)
  4681. (org-content))))
  4682. ((member state '("all" "showall"))
  4683. (show-subtree)))))
  4684. (unless no-cleanup
  4685. (org-cycle-hide-archived-subtrees 'all)
  4686. (org-cycle-hide-drawers 'all)
  4687. (org-cycle-show-empty-lines 'all)))))
  4688. (defun org-overview ()
  4689. "Switch to overview mode, showing only top-level headlines.
  4690. Really, this shows all headlines with level equal or greater than the level
  4691. of the first headline in the buffer. This is important, because if the
  4692. first headline is not level one, then (hide-sublevels 1) gives confusing
  4693. results."
  4694. (interactive)
  4695. (let ((level (save-excursion
  4696. (goto-char (point-min))
  4697. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4698. (progn
  4699. (goto-char (match-beginning 0))
  4700. (funcall outline-level))))))
  4701. (and level (hide-sublevels level))))
  4702. (defun org-content (&optional arg)
  4703. "Show all headlines in the buffer, like a table of contents.
  4704. With numerical argument N, show content up to level N."
  4705. (interactive "P")
  4706. (save-excursion
  4707. ;; Visit all headings and show their offspring
  4708. (and (integerp arg) (org-overview))
  4709. (goto-char (point-max))
  4710. (catch 'exit
  4711. (while (and (progn (condition-case nil
  4712. (outline-previous-visible-heading 1)
  4713. (error (goto-char (point-min))))
  4714. t)
  4715. (looking-at outline-regexp))
  4716. (if (integerp arg)
  4717. (show-children (1- arg))
  4718. (show-branches))
  4719. (if (bobp) (throw 'exit nil))))))
  4720. (defun org-optimize-window-after-visibility-change (state)
  4721. "Adjust the window after a change in outline visibility.
  4722. This function is the default value of the hook `org-cycle-hook'."
  4723. (when (get-buffer-window (current-buffer))
  4724. (cond
  4725. ((eq state 'content) nil)
  4726. ((eq state 'all) nil)
  4727. ((eq state 'folded) nil)
  4728. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4729. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4730. ;; FIXME: no longer in use
  4731. (defun org-compact-display-after-subtree-move ()
  4732. "Show a compacter version of the tree of the entry's parent."
  4733. (save-excursion
  4734. (if (org-up-heading-safe)
  4735. (progn
  4736. (hide-subtree)
  4737. (show-entry)
  4738. (show-children)
  4739. (org-cycle-show-empty-lines 'children)
  4740. (org-cycle-hide-drawers 'children))
  4741. (org-overview))))
  4742. (defun org-remove-empty-overlays-at (pos)
  4743. "Remove outline overlays that do not contain non-white stuff."
  4744. (mapc
  4745. (lambda (o)
  4746. (and (eq 'outline (org-overlay-get o 'invisible))
  4747. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4748. (org-overlay-end o))))
  4749. (org-delete-overlay o)))
  4750. (org-overlays-at pos)))
  4751. (defun org-clean-visibility-after-subtree-move ()
  4752. "Fix visibility issues after moving a subtree."
  4753. ;; First, find a reasonable region to look at:
  4754. ;; Start two siblings above, end three below
  4755. (let* ((beg (save-excursion
  4756. (and (org-get-last-sibling)
  4757. (org-get-last-sibling))
  4758. (point)))
  4759. (end (save-excursion
  4760. (and (org-get-next-sibling)
  4761. (org-get-next-sibling)
  4762. (org-get-next-sibling))
  4763. (if (org-at-heading-p)
  4764. (point-at-eol)
  4765. (point))))
  4766. (level (looking-at "\\*+"))
  4767. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4768. (save-excursion
  4769. (save-restriction
  4770. (narrow-to-region beg end)
  4771. (when re
  4772. ;; Properly fold already folded siblings
  4773. (goto-char (point-min))
  4774. (while (re-search-forward re nil t)
  4775. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4776. (hide-entry))))
  4777. (org-cycle-show-empty-lines 'overview)
  4778. (org-cycle-hide-drawers 'overview)))))
  4779. (defun org-cycle-show-empty-lines (state)
  4780. "Show empty lines above all visible headlines.
  4781. The region to be covered depends on STATE when called through
  4782. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4783. entire buffer covered. Note that an empty line is only shown if there
  4784. are at least `org-cycle-separator-lines' empty lines before the headline."
  4785. (when (not (= org-cycle-separator-lines 0))
  4786. (save-excursion
  4787. (let* ((n (abs org-cycle-separator-lines))
  4788. (re (cond
  4789. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4790. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4791. (t (let ((ns (number-to-string (- n 2))))
  4792. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4793. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4794. beg end b e)
  4795. (cond
  4796. ((memq state '(overview contents t))
  4797. (setq beg (point-min) end (point-max)))
  4798. ((memq state '(children folded))
  4799. (setq beg (point) end (progn (org-end-of-subtree t t)
  4800. (beginning-of-line 2)
  4801. (point)))))
  4802. (when beg
  4803. (goto-char beg)
  4804. (while (re-search-forward re end t)
  4805. (unless (get-char-property (match-end 1) 'invisible)
  4806. (setq e (match-end 1))
  4807. (if (< org-cycle-separator-lines 0)
  4808. (setq b (save-excursion
  4809. (goto-char (match-beginning 0))
  4810. (org-back-over-empty-lines)
  4811. (if (save-excursion
  4812. (goto-char (max (point-min) (1- (point))))
  4813. (org-on-heading-p))
  4814. (1- (point))
  4815. (point))))
  4816. (setq b (match-beginning 1)))
  4817. (outline-flag-region b e nil)))))))
  4818. ;; Never hide empty lines at the end of the file.
  4819. (save-excursion
  4820. (goto-char (point-max))
  4821. (outline-previous-heading)
  4822. (outline-end-of-heading)
  4823. (if (and (looking-at "[ \t\n]+")
  4824. (= (match-end 0) (point-max)))
  4825. (outline-flag-region (point) (match-end 0) nil))))
  4826. (defun org-show-empty-lines-in-parent ()
  4827. "Move to the parent and re-show empty lines before visible headlines."
  4828. (save-excursion
  4829. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4830. (org-cycle-show-empty-lines context))))
  4831. (defun org-files-list ()
  4832. "Return `org-agenda-files' list, plus all open org-mode files.
  4833. This is useful for operations that need to scan all of a user's
  4834. open and agenda-wise Org files."
  4835. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  4836. (dolist (buf (buffer-list))
  4837. (with-current-buffer buf
  4838. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  4839. (let ((file (expand-file-name (buffer-file-name))))
  4840. (unless (member file files)
  4841. (push file files))))))
  4842. files))
  4843. (defsubst org-entry-beginning-position ()
  4844. "Return the beginning position of the current entry."
  4845. (save-excursion (outline-back-to-heading t) (point)))
  4846. (defsubst org-entry-end-position ()
  4847. "Return the end position of the current entry."
  4848. (save-excursion (outline-next-heading) (point)))
  4849. (defun org-cycle-hide-drawers (state)
  4850. "Re-hide all drawers after a visibility state change."
  4851. (when (and (org-mode-p)
  4852. (not (memq state '(overview folded contents))))
  4853. (save-excursion
  4854. (let* ((globalp (memq state '(contents all)))
  4855. (beg (if globalp (point-min) (point)))
  4856. (end (if globalp (point-max)
  4857. (if (eq state 'children)
  4858. (save-excursion (outline-next-heading) (point))
  4859. (org-end-of-subtree t)))))
  4860. (goto-char beg)
  4861. (while (re-search-forward org-drawer-regexp end t)
  4862. (org-flag-drawer t))))))
  4863. (defun org-flag-drawer (flag)
  4864. (save-excursion
  4865. (beginning-of-line 1)
  4866. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4867. (let ((b (match-end 0))
  4868. (outline-regexp org-outline-regexp))
  4869. (if (re-search-forward
  4870. "^[ \t]*:END:"
  4871. (save-excursion (outline-next-heading) (point)) t)
  4872. (outline-flag-region b (point-at-eol) flag)
  4873. (error ":END: line missing at position %s" b))))))
  4874. (defun org-subtree-end-visible-p ()
  4875. "Is the end of the current subtree visible?"
  4876. (pos-visible-in-window-p
  4877. (save-excursion (org-end-of-subtree t) (point))))
  4878. (defun org-first-headline-recenter (&optional N)
  4879. "Move cursor to the first headline and recenter the headline.
  4880. Optional argument N means, put the headline into the Nth line of the window."
  4881. (goto-char (point-min))
  4882. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4883. (beginning-of-line)
  4884. (recenter (prefix-numeric-value N))))
  4885. ;;; Folding of blocks
  4886. (defconst org-block-regexp
  4887. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4888. "Regular expression for hiding blocks.")
  4889. (defvar org-hide-block-overlays nil
  4890. "Overays hiding blocks.")
  4891. (make-variable-buffer-local 'org-hide-block-overlays)
  4892. (defun org-block-map (function &optional start end)
  4893. "Call func at the head of all source blocks in the current
  4894. buffer. Optional arguments START and END can be used to limit
  4895. the range."
  4896. (let ((start (or start (point-min)))
  4897. (end (or end (point-max))))
  4898. (save-excursion
  4899. (goto-char start)
  4900. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4901. (save-excursion
  4902. (save-match-data
  4903. (goto-char (match-beginning 0))
  4904. (funcall function)))))))
  4905. (defun org-hide-block-toggle-all ()
  4906. "Toggle the visibility of all blocks in the current buffer."
  4907. (org-block-map #'org-hide-block-toggle))
  4908. (defun org-hide-block-all ()
  4909. "Fold all blocks in the current buffer."
  4910. (interactive)
  4911. (org-show-block-all)
  4912. (org-block-map #'org-hide-block-toggle-maybe))
  4913. (defun org-show-block-all ()
  4914. "Unfold all blocks in the current buffer."
  4915. (mapc 'org-delete-overlay org-hide-block-overlays)
  4916. (setq org-hide-block-overlays nil))
  4917. (defun org-hide-block-toggle-maybe ()
  4918. "Toggle visibility of block at point."
  4919. (interactive)
  4920. (let ((case-fold-search t))
  4921. (if (save-excursion
  4922. (beginning-of-line 1)
  4923. (looking-at org-block-regexp))
  4924. (progn (org-hide-block-toggle)
  4925. t) ;; to signal that we took action
  4926. nil))) ;; to signal that we did not
  4927. (defun org-hide-block-toggle (&optional force)
  4928. "Toggle the visibility of the current block."
  4929. (interactive)
  4930. (save-excursion
  4931. (beginning-of-line)
  4932. (if (re-search-forward org-block-regexp nil t)
  4933. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4934. (end (match-end 0)) ;; end of entire body
  4935. ov)
  4936. (if (memq t (mapcar (lambda (overlay)
  4937. (eq (org-overlay-get overlay 'invisible)
  4938. 'org-hide-block))
  4939. (org-overlays-at start)))
  4940. (if (or (not force) (eq force 'off))
  4941. (mapc (lambda (ov)
  4942. (when (member ov org-hide-block-overlays)
  4943. (setq org-hide-block-overlays
  4944. (delq ov org-hide-block-overlays)))
  4945. (when (eq (org-overlay-get ov 'invisible)
  4946. 'org-hide-block)
  4947. (org-delete-overlay ov)))
  4948. (org-overlays-at start)))
  4949. (setq ov (org-make-overlay start end))
  4950. (org-overlay-put ov 'invisible 'org-hide-block)
  4951. ;; make the block accessible to isearch
  4952. (org-overlay-put
  4953. ov 'isearch-open-invisible
  4954. (lambda (ov)
  4955. (when (member ov org-hide-block-overlays)
  4956. (setq org-hide-block-overlays
  4957. (delq ov org-hide-block-overlays)))
  4958. (when (eq (org-overlay-get ov 'invisible)
  4959. 'org-hide-block)
  4960. (org-delete-overlay ov))))
  4961. (push ov org-hide-block-overlays)))
  4962. (error "Not looking at a source block"))))
  4963. ;; org-tab-after-check-for-cycling-hook
  4964. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4965. ;; Remove overlays when changing major mode
  4966. (add-hook 'org-mode-hook
  4967. (lambda () (org-add-hook 'change-major-mode-hook
  4968. 'org-show-block-all 'append 'local)))
  4969. ;;; Org-goto
  4970. (defvar org-goto-window-configuration nil)
  4971. (defvar org-goto-marker nil)
  4972. (defvar org-goto-map
  4973. (let ((map (make-sparse-keymap)))
  4974. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4975. (while (setq cmd (pop cmds))
  4976. (substitute-key-definition cmd cmd map global-map)))
  4977. (suppress-keymap map)
  4978. (org-defkey map "\C-m" 'org-goto-ret)
  4979. (org-defkey map [(return)] 'org-goto-ret)
  4980. (org-defkey map [(left)] 'org-goto-left)
  4981. (org-defkey map [(right)] 'org-goto-right)
  4982. (org-defkey map [(control ?g)] 'org-goto-quit)
  4983. (org-defkey map "\C-i" 'org-cycle)
  4984. (org-defkey map [(tab)] 'org-cycle)
  4985. (org-defkey map [(down)] 'outline-next-visible-heading)
  4986. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4987. (if org-goto-auto-isearch
  4988. (if (fboundp 'define-key-after)
  4989. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4990. nil)
  4991. (org-defkey map "q" 'org-goto-quit)
  4992. (org-defkey map "n" 'outline-next-visible-heading)
  4993. (org-defkey map "p" 'outline-previous-visible-heading)
  4994. (org-defkey map "f" 'outline-forward-same-level)
  4995. (org-defkey map "b" 'outline-backward-same-level)
  4996. (org-defkey map "u" 'outline-up-heading))
  4997. (org-defkey map "/" 'org-occur)
  4998. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4999. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5000. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5001. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5002. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5003. map))
  5004. (defconst org-goto-help
  5005. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5006. RET=jump to location [Q]uit and return to previous location
  5007. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5008. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5009. ;; FIXME: Docstring doe not mention both interfaces
  5010. (defun org-goto (&optional alternative-interface)
  5011. "Look up a different location in the current file, keeping current visibility.
  5012. When you want look-up or go to a different location in a document, the
  5013. fastest way is often to fold the entire buffer and then dive into the tree.
  5014. This method has the disadvantage, that the previous location will be folded,
  5015. which may not be what you want.
  5016. This command works around this by showing a copy of the current buffer
  5017. in an indirect buffer, in overview mode. You can dive into the tree in
  5018. that copy, use org-occur and incremental search to find a location.
  5019. When pressing RET or `Q', the command returns to the original buffer in
  5020. which the visibility is still unchanged. After RET is will also jump to
  5021. the location selected in the indirect buffer and expose the
  5022. the headline hierarchy above."
  5023. (interactive "P")
  5024. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5025. (org-refile-use-outline-path t)
  5026. (org-refile-target-verify-function nil)
  5027. (interface
  5028. (if (not alternative-interface)
  5029. org-goto-interface
  5030. (if (eq org-goto-interface 'outline)
  5031. 'outline-path-completion
  5032. 'outline)))
  5033. (org-goto-start-pos (point))
  5034. (selected-point
  5035. (if (eq interface 'outline)
  5036. (car (org-get-location (current-buffer) org-goto-help))
  5037. (nth 3 (org-refile-get-location "Goto: ")))))
  5038. (if selected-point
  5039. (progn
  5040. (org-mark-ring-push org-goto-start-pos)
  5041. (goto-char selected-point)
  5042. (if (or (org-invisible-p) (org-invisible-p2))
  5043. (org-show-context 'org-goto)))
  5044. (message "Quit"))))
  5045. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5046. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5047. (defvar org-goto-local-auto-isearch-map) ; defined below
  5048. (defun org-get-location (buf help)
  5049. "Let the user select a location in the Org-mode buffer BUF.
  5050. This function uses a recursive edit. It returns the selected position
  5051. or nil."
  5052. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5053. (isearch-hide-immediately nil)
  5054. (isearch-search-fun-function
  5055. (lambda () 'org-goto-local-search-headings))
  5056. (org-goto-selected-point org-goto-exit-command))
  5057. (save-excursion
  5058. (save-window-excursion
  5059. (delete-other-windows)
  5060. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5061. (switch-to-buffer
  5062. (condition-case nil
  5063. (make-indirect-buffer (current-buffer) "*org-goto*")
  5064. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5065. (with-output-to-temp-buffer "*Help*"
  5066. (princ help))
  5067. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5068. (setq buffer-read-only nil)
  5069. (let ((org-startup-truncated t)
  5070. (org-startup-folded nil)
  5071. (org-startup-align-all-tables nil))
  5072. (org-mode)
  5073. (org-overview))
  5074. (setq buffer-read-only t)
  5075. (if (and (boundp 'org-goto-start-pos)
  5076. (integer-or-marker-p org-goto-start-pos))
  5077. (let ((org-show-hierarchy-above t)
  5078. (org-show-siblings t)
  5079. (org-show-following-heading t))
  5080. (goto-char org-goto-start-pos)
  5081. (and (org-invisible-p) (org-show-context)))
  5082. (goto-char (point-min)))
  5083. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5084. (message "Select location and press RET")
  5085. (use-local-map org-goto-map)
  5086. (recursive-edit)
  5087. ))
  5088. (kill-buffer "*org-goto*")
  5089. (cons org-goto-selected-point org-goto-exit-command)))
  5090. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5091. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5092. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5093. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5094. (defun org-goto-local-search-headings (string bound noerror)
  5095. "Search and make sure that any matches are in headlines."
  5096. (catch 'return
  5097. (while (if isearch-forward
  5098. (search-forward string bound noerror)
  5099. (search-backward string bound noerror))
  5100. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5101. (and (member :headline context)
  5102. (not (member :tags context))))
  5103. (throw 'return (point))))))
  5104. (defun org-goto-local-auto-isearch ()
  5105. "Start isearch."
  5106. (interactive)
  5107. (goto-char (point-min))
  5108. (let ((keys (this-command-keys)))
  5109. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5110. (isearch-mode t)
  5111. (isearch-process-search-char (string-to-char keys)))))
  5112. (defun org-goto-ret (&optional arg)
  5113. "Finish `org-goto' by going to the new location."
  5114. (interactive "P")
  5115. (setq org-goto-selected-point (point)
  5116. org-goto-exit-command 'return)
  5117. (throw 'exit nil))
  5118. (defun org-goto-left ()
  5119. "Finish `org-goto' by going to the new location."
  5120. (interactive)
  5121. (if (org-on-heading-p)
  5122. (progn
  5123. (beginning-of-line 1)
  5124. (setq org-goto-selected-point (point)
  5125. org-goto-exit-command 'left)
  5126. (throw 'exit nil))
  5127. (error "Not on a heading")))
  5128. (defun org-goto-right ()
  5129. "Finish `org-goto' by going to the new location."
  5130. (interactive)
  5131. (if (org-on-heading-p)
  5132. (progn
  5133. (setq org-goto-selected-point (point)
  5134. org-goto-exit-command 'right)
  5135. (throw 'exit nil))
  5136. (error "Not on a heading")))
  5137. (defun org-goto-quit ()
  5138. "Finish `org-goto' without cursor motion."
  5139. (interactive)
  5140. (setq org-goto-selected-point nil)
  5141. (setq org-goto-exit-command 'quit)
  5142. (throw 'exit nil))
  5143. ;;; Indirect buffer display of subtrees
  5144. (defvar org-indirect-dedicated-frame nil
  5145. "This is the frame being used for indirect tree display.")
  5146. (defvar org-last-indirect-buffer nil)
  5147. (defun org-tree-to-indirect-buffer (&optional arg)
  5148. "Create indirect buffer and narrow it to current subtree.
  5149. With numerical prefix ARG, go up to this level and then take that tree.
  5150. If ARG is negative, go up that many levels.
  5151. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5152. indirect buffer previously made with this command, to avoid proliferation of
  5153. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5154. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5155. is kept so that you can work with several indirect buffers at the same time.
  5156. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5157. requests that a new frame be made for the new buffer, so that the dedicated
  5158. frame is not changed."
  5159. (interactive "P")
  5160. (let ((cbuf (current-buffer))
  5161. (cwin (selected-window))
  5162. (pos (point))
  5163. beg end level heading ibuf)
  5164. (save-excursion
  5165. (org-back-to-heading t)
  5166. (when (numberp arg)
  5167. (setq level (org-outline-level))
  5168. (if (< arg 0) (setq arg (+ level arg)))
  5169. (while (> (setq level (org-outline-level)) arg)
  5170. (outline-up-heading 1 t)))
  5171. (setq beg (point)
  5172. heading (org-get-heading))
  5173. (org-end-of-subtree t t) (setq end (point)))
  5174. (if (and (buffer-live-p org-last-indirect-buffer)
  5175. (not (eq org-indirect-buffer-display 'new-frame))
  5176. (not arg))
  5177. (kill-buffer org-last-indirect-buffer))
  5178. (setq ibuf (org-get-indirect-buffer cbuf)
  5179. org-last-indirect-buffer ibuf)
  5180. (cond
  5181. ((or (eq org-indirect-buffer-display 'new-frame)
  5182. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5183. (select-frame (make-frame))
  5184. (delete-other-windows)
  5185. (switch-to-buffer ibuf)
  5186. (org-set-frame-title heading))
  5187. ((eq org-indirect-buffer-display 'dedicated-frame)
  5188. (raise-frame
  5189. (select-frame (or (and org-indirect-dedicated-frame
  5190. (frame-live-p org-indirect-dedicated-frame)
  5191. org-indirect-dedicated-frame)
  5192. (setq org-indirect-dedicated-frame (make-frame)))))
  5193. (delete-other-windows)
  5194. (switch-to-buffer ibuf)
  5195. (org-set-frame-title (concat "Indirect: " heading)))
  5196. ((eq org-indirect-buffer-display 'current-window)
  5197. (switch-to-buffer ibuf))
  5198. ((eq org-indirect-buffer-display 'other-window)
  5199. (pop-to-buffer ibuf))
  5200. (t (error "Invalid value")))
  5201. (if (featurep 'xemacs)
  5202. (save-excursion (org-mode) (turn-on-font-lock)))
  5203. (narrow-to-region beg end)
  5204. (show-all)
  5205. (goto-char pos)
  5206. (and (window-live-p cwin) (select-window cwin))))
  5207. (defun org-get-indirect-buffer (&optional buffer)
  5208. (setq buffer (or buffer (current-buffer)))
  5209. (let ((n 1) (base (buffer-name buffer)) bname)
  5210. (while (buffer-live-p
  5211. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5212. (setq n (1+ n)))
  5213. (condition-case nil
  5214. (make-indirect-buffer buffer bname 'clone)
  5215. (error (make-indirect-buffer buffer bname)))))
  5216. (defun org-set-frame-title (title)
  5217. "Set the title of the current frame to the string TITLE."
  5218. ;; FIXME: how to name a single frame in XEmacs???
  5219. (unless (featurep 'xemacs)
  5220. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5221. ;;;; Structure editing
  5222. ;;; Inserting headlines
  5223. (defun org-previous-line-empty-p ()
  5224. (save-excursion
  5225. (and (not (bobp))
  5226. (or (beginning-of-line 0) t)
  5227. (save-match-data
  5228. (looking-at "[ \t]*$")))))
  5229. (defun org-insert-heading (&optional force-heading)
  5230. "Insert a new heading or item with same depth at point.
  5231. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5232. If point is at the beginning of a headline, insert a sibling before the
  5233. current headline. If point is not at the beginning, do not split the line,
  5234. but create the new headline after the current line."
  5235. (interactive "P")
  5236. (if (= (buffer-size) 0)
  5237. (insert "\n* ")
  5238. (when (or force-heading (not (org-insert-item)))
  5239. (let* ((empty-line-p nil)
  5240. (head (save-excursion
  5241. (condition-case nil
  5242. (progn
  5243. (org-back-to-heading)
  5244. (setq empty-line-p (org-previous-line-empty-p))
  5245. (match-string 0))
  5246. (error "*"))))
  5247. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5248. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5249. pos hide-previous previous-pos)
  5250. (cond
  5251. ((and (org-on-heading-p) (bolp)
  5252. (or (bobp)
  5253. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5254. ;; insert before the current line
  5255. (open-line (if blank 2 1)))
  5256. ((and (bolp)
  5257. (not org-insert-heading-respect-content)
  5258. (or (bobp)
  5259. (save-excursion
  5260. (backward-char 1) (not (org-invisible-p)))))
  5261. ;; insert right here
  5262. nil)
  5263. (t
  5264. ;; somewhere in the line
  5265. (save-excursion
  5266. (setq previous-pos (point-at-bol))
  5267. (end-of-line)
  5268. (setq hide-previous (org-invisible-p)))
  5269. (and org-insert-heading-respect-content (org-show-subtree))
  5270. (let ((split
  5271. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5272. (save-excursion
  5273. (let ((p (point)))
  5274. (goto-char (point-at-bol))
  5275. (and (looking-at org-complex-heading-regexp)
  5276. (> p (match-beginning 4)))))))
  5277. tags pos)
  5278. (cond
  5279. (org-insert-heading-respect-content
  5280. (org-end-of-subtree nil t)
  5281. (or (bolp) (newline))
  5282. (or (org-previous-line-empty-p)
  5283. (and blank (newline)))
  5284. (open-line 1))
  5285. ((org-on-heading-p)
  5286. (when hide-previous
  5287. (show-children)
  5288. (org-show-entry))
  5289. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5290. (setq tags (and (match-end 2) (match-string 2)))
  5291. (and (match-end 1)
  5292. (delete-region (match-beginning 1) (match-end 1)))
  5293. (setq pos (point-at-bol))
  5294. (or split (end-of-line 1))
  5295. (delete-horizontal-space)
  5296. (newline (if blank 2 1))
  5297. (when tags
  5298. (save-excursion
  5299. (goto-char pos)
  5300. (end-of-line 1)
  5301. (insert " " tags)
  5302. (org-set-tags nil 'align))))
  5303. (t
  5304. (or split (end-of-line 1))
  5305. (newline (if blank 2 1)))))))
  5306. (insert head) (just-one-space)
  5307. (setq pos (point))
  5308. (end-of-line 1)
  5309. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5310. (when (and org-insert-heading-respect-content hide-previous)
  5311. (save-excursion
  5312. (goto-char previous-pos)
  5313. (hide-subtree)))
  5314. (run-hooks 'org-insert-heading-hook)))))
  5315. (defun org-get-heading (&optional no-tags)
  5316. "Return the heading of the current entry, without the stars."
  5317. (save-excursion
  5318. (org-back-to-heading t)
  5319. (if (looking-at
  5320. (if no-tags
  5321. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5322. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5323. (match-string 1) "")))
  5324. (defun org-heading-components ()
  5325. "Return the components of the current heading.
  5326. This is a list with the following elements:
  5327. - the level as an integer
  5328. - the reduced level, different if `org-odd-levels-only' is set.
  5329. - the TODO keyword, or nil
  5330. - the priority character, like ?A, or nil if no priority is given
  5331. - the headline text itself, or the tags string if no headline text
  5332. - the tags string, or nil."
  5333. (save-excursion
  5334. (org-back-to-heading t)
  5335. (if (looking-at org-complex-heading-regexp)
  5336. (list (length (match-string 1))
  5337. (org-reduced-level (length (match-string 1)))
  5338. (org-match-string-no-properties 2)
  5339. (and (match-end 3) (aref (match-string 3) 2))
  5340. (org-match-string-no-properties 4)
  5341. (org-match-string-no-properties 5)))))
  5342. (defun org-get-entry ()
  5343. "Get the entry text, after heading, entire subtree."
  5344. (save-excursion
  5345. (org-back-to-heading t)
  5346. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5347. (defun org-insert-heading-after-current ()
  5348. "Insert a new heading with same level as current, after current subtree."
  5349. (interactive)
  5350. (org-back-to-heading)
  5351. (org-insert-heading)
  5352. (org-move-subtree-down)
  5353. (end-of-line 1))
  5354. (defun org-insert-heading-respect-content ()
  5355. (interactive)
  5356. (let ((org-insert-heading-respect-content t))
  5357. (org-insert-heading t)))
  5358. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5359. (interactive "P")
  5360. (let ((org-insert-heading-respect-content t))
  5361. (org-insert-todo-heading force-state t)))
  5362. (defun org-insert-todo-heading (arg &optional force-heading)
  5363. "Insert a new heading with the same level and TODO state as current heading.
  5364. If the heading has no TODO state, or if the state is DONE, use the first
  5365. state (TODO by default). Also with prefix arg, force first state."
  5366. (interactive "P")
  5367. (when (or force-heading (not (org-insert-item 'checkbox)))
  5368. (org-insert-heading force-heading)
  5369. (save-excursion
  5370. (org-back-to-heading)
  5371. (outline-previous-heading)
  5372. (looking-at org-todo-line-regexp))
  5373. (let*
  5374. ((new-mark-x
  5375. (if (or arg
  5376. (not (match-beginning 2))
  5377. (member (match-string 2) org-done-keywords))
  5378. (car org-todo-keywords-1)
  5379. (match-string 2)))
  5380. (new-mark
  5381. (or
  5382. (run-hook-with-args-until-success
  5383. 'org-todo-get-default-hook new-mark-x nil)
  5384. new-mark-x)))
  5385. (beginning-of-line 1)
  5386. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5387. (if org-treat-insert-todo-heading-as-state-change
  5388. (org-todo new-mark)
  5389. (insert new-mark " "))))
  5390. (when org-provide-todo-statistics
  5391. (org-update-parent-todo-statistics))))
  5392. (defun org-insert-subheading (arg)
  5393. "Insert a new subheading and demote it.
  5394. Works for outline headings and for plain lists alike."
  5395. (interactive "P")
  5396. (org-insert-heading arg)
  5397. (cond
  5398. ((org-on-heading-p) (org-do-demote))
  5399. ((org-at-item-p) (org-indent-item 1))))
  5400. (defun org-insert-todo-subheading (arg)
  5401. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5402. Works for outline headings and for plain lists alike."
  5403. (interactive "P")
  5404. (org-insert-todo-heading arg)
  5405. (cond
  5406. ((org-on-heading-p) (org-do-demote))
  5407. ((org-at-item-p) (org-indent-item 1))))
  5408. ;;; Promotion and Demotion
  5409. (defvar org-after-demote-entry-hook nil
  5410. "Hook run after an entry has been demoted.
  5411. The cursor will be at the beginning of the entry.
  5412. When a subtree is being demoted, the hook will be called for each node.")
  5413. (defvar org-after-promote-entry-hook nil
  5414. "Hook run after an entry has been promoted.
  5415. The cursor will be at the beginning of the entry.
  5416. When a subtree is being promoted, the hook will be called for each node.")
  5417. (defun org-promote-subtree ()
  5418. "Promote the entire subtree.
  5419. See also `org-promote'."
  5420. (interactive)
  5421. (save-excursion
  5422. (org-map-tree 'org-promote))
  5423. (org-fix-position-after-promote))
  5424. (defun org-demote-subtree ()
  5425. "Demote the entire subtree. See `org-demote'.
  5426. See also `org-promote'."
  5427. (interactive)
  5428. (save-excursion
  5429. (org-map-tree 'org-demote))
  5430. (org-fix-position-after-promote))
  5431. (defun org-do-promote ()
  5432. "Promote the current heading higher up the tree.
  5433. If the region is active in `transient-mark-mode', promote all headings
  5434. in the region."
  5435. (interactive)
  5436. (save-excursion
  5437. (if (org-region-active-p)
  5438. (org-map-region 'org-promote (region-beginning) (region-end))
  5439. (org-promote)))
  5440. (org-fix-position-after-promote))
  5441. (defun org-do-demote ()
  5442. "Demote the current heading lower down the tree.
  5443. If the region is active in `transient-mark-mode', demote all headings
  5444. in the region."
  5445. (interactive)
  5446. (save-excursion
  5447. (if (org-region-active-p)
  5448. (org-map-region 'org-demote (region-beginning) (region-end))
  5449. (org-demote)))
  5450. (org-fix-position-after-promote))
  5451. (defun org-fix-position-after-promote ()
  5452. "Make sure that after pro/demotion cursor position is right."
  5453. (let ((pos (point)))
  5454. (when (save-excursion
  5455. (beginning-of-line 1)
  5456. (looking-at org-todo-line-regexp)
  5457. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5458. (cond ((eobp) (insert " "))
  5459. ((eolp) (insert " "))
  5460. ((equal (char-after) ?\ ) (forward-char 1))))))
  5461. (defun org-current-level ()
  5462. "Return the level of the current entry, or nil if before the first headline.
  5463. The level is the number of stars at the beginning of the headline."
  5464. (save-excursion
  5465. (condition-case nil
  5466. (progn
  5467. (org-back-to-heading t)
  5468. (funcall outline-level))
  5469. (error nil))))
  5470. (defun org-reduced-level (l)
  5471. "Compute the effective level of a heading.
  5472. This takes into account the setting of `org-odd-levels-only'."
  5473. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5474. (defun org-get-valid-level (level &optional change)
  5475. "Rectify a level change under the influence of `org-odd-levels-only'
  5476. LEVEL is a current level, CHANGE is by how much the level should be
  5477. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5478. even level numbers will become the next higher odd number."
  5479. (if org-odd-levels-only
  5480. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5481. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5482. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5483. (max 1 (+ level (or change 0)))))
  5484. (if (boundp 'define-obsolete-function-alias)
  5485. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5486. (define-obsolete-function-alias 'org-get-legal-level
  5487. 'org-get-valid-level)
  5488. (define-obsolete-function-alias 'org-get-legal-level
  5489. 'org-get-valid-level "23.1")))
  5490. (defun org-promote ()
  5491. "Promote the current heading higher up the tree.
  5492. If the region is active in `transient-mark-mode', promote all headings
  5493. in the region."
  5494. (org-back-to-heading t)
  5495. (let* ((level (save-match-data (funcall outline-level)))
  5496. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5497. (diff (abs (- level (length up-head) -1))))
  5498. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5499. (replace-match up-head nil t)
  5500. ;; Fixup tag positioning
  5501. (and org-auto-align-tags (org-set-tags nil t))
  5502. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5503. (run-hooks 'org-after-promote-entry-hook)))
  5504. (defun org-demote ()
  5505. "Demote the current heading lower down the tree.
  5506. If the region is active in `transient-mark-mode', demote all headings
  5507. in the region."
  5508. (org-back-to-heading t)
  5509. (let* ((level (save-match-data (funcall outline-level)))
  5510. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5511. (diff (abs (- level (length down-head) -1))))
  5512. (replace-match down-head nil t)
  5513. ;; Fixup tag positioning
  5514. (and org-auto-align-tags (org-set-tags nil t))
  5515. (if org-adapt-indentation (org-fixup-indentation diff))
  5516. (run-hooks 'org-after-demote-entry-hook)))
  5517. (defvar org-tab-ind-state nil)
  5518. (defun org-cycle-level ()
  5519. (let ((org-adapt-indentation nil))
  5520. (when (and (looking-at "[ \t]*$")
  5521. (looking-back
  5522. (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp "\\)?[ \t]*")))
  5523. (setq this-command 'org-cycle-level)
  5524. (if (eq last-command 'org-cycle-level)
  5525. (condition-case nil
  5526. (progn (org-do-promote)
  5527. (if (equal org-tab-ind-state (org-current-level))
  5528. (org-do-promote)))
  5529. (error
  5530. (progn
  5531. (save-excursion
  5532. (beginning-of-line 1)
  5533. (and (looking-at "\\*+")
  5534. (replace-match
  5535. (make-string org-tab-ind-state ?*))))
  5536. (setq this-command 'org-cycle))))
  5537. (setq org-tab-ind-state (- (match-end 1) (match-beginning 1)))
  5538. (org-do-demote))
  5539. t)))
  5540. (defun org-map-tree (fun)
  5541. "Call FUN for every heading underneath the current one."
  5542. (org-back-to-heading)
  5543. (let ((level (funcall outline-level)))
  5544. (save-excursion
  5545. (funcall fun)
  5546. (while (and (progn
  5547. (outline-next-heading)
  5548. (> (funcall outline-level) level))
  5549. (not (eobp)))
  5550. (funcall fun)))))
  5551. (defun org-map-region (fun beg end)
  5552. "Call FUN for every heading between BEG and END."
  5553. (let ((org-ignore-region t))
  5554. (save-excursion
  5555. (setq end (copy-marker end))
  5556. (goto-char beg)
  5557. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5558. (< (point) end))
  5559. (funcall fun))
  5560. (while (and (progn
  5561. (outline-next-heading)
  5562. (< (point) end))
  5563. (not (eobp)))
  5564. (funcall fun)))))
  5565. (defun org-fixup-indentation (diff)
  5566. "Change the indentation in the current entry by DIFF
  5567. However, if any line in the current entry has no indentation, or if it
  5568. would end up with no indentation after the change, nothing at all is done."
  5569. (save-excursion
  5570. (let ((end (save-excursion (outline-next-heading)
  5571. (point-marker)))
  5572. (prohibit (if (> diff 0)
  5573. "^\\S-"
  5574. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5575. col)
  5576. (unless (save-excursion (end-of-line 1)
  5577. (re-search-forward prohibit end t))
  5578. (while (and (< (point) end)
  5579. (re-search-forward "^[ \t]+" end t))
  5580. (goto-char (match-end 0))
  5581. (setq col (current-column))
  5582. (if (< diff 0) (replace-match ""))
  5583. (org-indent-to-column (+ diff col))))
  5584. (move-marker end nil))))
  5585. (defun org-convert-to-odd-levels ()
  5586. "Convert an org-mode file with all levels allowed to one with odd levels.
  5587. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5588. level 5 etc."
  5589. (interactive)
  5590. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5591. (let ((outline-regexp org-outline-regexp)
  5592. (outline-level 'org-outline-level)
  5593. (org-odd-levels-only nil) n)
  5594. (save-excursion
  5595. (goto-char (point-min))
  5596. (while (re-search-forward "^\\*\\*+ " nil t)
  5597. (setq n (- (length (match-string 0)) 2))
  5598. (while (>= (setq n (1- n)) 0)
  5599. (org-demote))
  5600. (end-of-line 1))))))
  5601. (defun org-convert-to-oddeven-levels ()
  5602. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5603. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5604. section with an even level, conversion would destroy the structure of the file. An error
  5605. is signaled in this case."
  5606. (interactive)
  5607. (goto-char (point-min))
  5608. ;; First check if there are no even levels
  5609. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5610. (org-show-context t)
  5611. (error "Not all levels are odd in this file. Conversion not possible"))
  5612. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5613. (let ((outline-regexp org-outline-regexp)
  5614. (outline-level 'org-outline-level)
  5615. (org-odd-levels-only nil) n)
  5616. (save-excursion
  5617. (goto-char (point-min))
  5618. (while (re-search-forward "^\\*\\*+ " nil t)
  5619. (setq n (/ (1- (length (match-string 0))) 2))
  5620. (while (>= (setq n (1- n)) 0)
  5621. (org-promote))
  5622. (end-of-line 1))))))
  5623. (defun org-tr-level (n)
  5624. "Make N odd if required."
  5625. (if org-odd-levels-only (1+ (/ n 2)) n))
  5626. ;;; Vertical tree motion, cutting and pasting of subtrees
  5627. (defun org-move-subtree-up (&optional arg)
  5628. "Move the current subtree up past ARG headlines of the same level."
  5629. (interactive "p")
  5630. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5631. (defun org-move-subtree-down (&optional arg)
  5632. "Move the current subtree down past ARG headlines of the same level."
  5633. (interactive "p")
  5634. (setq arg (prefix-numeric-value arg))
  5635. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5636. 'org-get-last-sibling))
  5637. (ins-point (make-marker))
  5638. (cnt (abs arg))
  5639. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5640. ;; Select the tree
  5641. (org-back-to-heading)
  5642. (setq beg0 (point))
  5643. (save-excursion
  5644. (setq ne-beg (org-back-over-empty-lines))
  5645. (setq beg (point)))
  5646. (save-match-data
  5647. (save-excursion (outline-end-of-heading)
  5648. (setq folded (org-invisible-p)))
  5649. (outline-end-of-subtree))
  5650. (outline-next-heading)
  5651. (setq ne-end (org-back-over-empty-lines))
  5652. (setq end (point))
  5653. (goto-char beg0)
  5654. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5655. ;; include less whitespace
  5656. (save-excursion
  5657. (goto-char beg)
  5658. (forward-line (- ne-beg ne-end))
  5659. (setq beg (point))))
  5660. ;; Find insertion point, with error handling
  5661. (while (> cnt 0)
  5662. (or (and (funcall movfunc) (looking-at outline-regexp))
  5663. (progn (goto-char beg0)
  5664. (error "Cannot move past superior level or buffer limit")))
  5665. (setq cnt (1- cnt)))
  5666. (if (> arg 0)
  5667. ;; Moving forward - still need to move over subtree
  5668. (progn (org-end-of-subtree t t)
  5669. (save-excursion
  5670. (org-back-over-empty-lines)
  5671. (or (bolp) (newline)))))
  5672. (setq ne-ins (org-back-over-empty-lines))
  5673. (move-marker ins-point (point))
  5674. (setq txt (buffer-substring beg end))
  5675. (org-save-markers-in-region beg end)
  5676. (delete-region beg end)
  5677. (org-remove-empty-overlays-at beg)
  5678. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5679. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5680. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5681. (let ((bbb (point)))
  5682. (insert-before-markers txt)
  5683. (org-reinstall-markers-in-region bbb)
  5684. (move-marker ins-point bbb))
  5685. (or (bolp) (insert "\n"))
  5686. (setq ins-end (point))
  5687. (goto-char ins-point)
  5688. (org-skip-whitespace)
  5689. (when (and (< arg 0)
  5690. (org-first-sibling-p)
  5691. (> ne-ins ne-beg))
  5692. ;; Move whitespace back to beginning
  5693. (save-excursion
  5694. (goto-char ins-end)
  5695. (let ((kill-whole-line t))
  5696. (kill-line (- ne-ins ne-beg)) (point)))
  5697. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5698. (move-marker ins-point nil)
  5699. (if folded
  5700. (hide-subtree)
  5701. (org-show-entry)
  5702. (show-children)
  5703. (org-cycle-hide-drawers 'children))
  5704. (org-clean-visibility-after-subtree-move)))
  5705. (defvar org-subtree-clip ""
  5706. "Clipboard for cut and paste of subtrees.
  5707. This is actually only a copy of the kill, because we use the normal kill
  5708. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5709. (defvar org-subtree-clip-folded nil
  5710. "Was the last copied subtree folded?
  5711. This is used to fold the tree back after pasting.")
  5712. (defun org-cut-subtree (&optional n)
  5713. "Cut the current subtree into the clipboard.
  5714. With prefix arg N, cut this many sequential subtrees.
  5715. This is a short-hand for marking the subtree and then cutting it."
  5716. (interactive "p")
  5717. (org-copy-subtree n 'cut))
  5718. (defun org-copy-subtree (&optional n cut force-store-markers)
  5719. "Cut the current subtree into the clipboard.
  5720. With prefix arg N, cut this many sequential subtrees.
  5721. This is a short-hand for marking the subtree and then copying it.
  5722. If CUT is non-nil, actually cut the subtree.
  5723. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5724. of some markers in the region, even if CUT is non-nil. This is
  5725. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5726. (interactive "p")
  5727. (let (beg end folded (beg0 (point)))
  5728. (if (interactive-p)
  5729. (org-back-to-heading nil) ; take what looks like a subtree
  5730. (org-back-to-heading t)) ; take what is really there
  5731. (org-back-over-empty-lines)
  5732. (setq beg (point))
  5733. (skip-chars-forward " \t\r\n")
  5734. (save-match-data
  5735. (save-excursion (outline-end-of-heading)
  5736. (setq folded (org-invisible-p)))
  5737. (condition-case nil
  5738. (org-forward-same-level (1- n) t)
  5739. (error nil))
  5740. (org-end-of-subtree t t))
  5741. (org-back-over-empty-lines)
  5742. (setq end (point))
  5743. (goto-char beg0)
  5744. (when (> end beg)
  5745. (setq org-subtree-clip-folded folded)
  5746. (when (or cut force-store-markers)
  5747. (org-save-markers-in-region beg end))
  5748. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5749. (setq org-subtree-clip (current-kill 0))
  5750. (message "%s: Subtree(s) with %d characters"
  5751. (if cut "Cut" "Copied")
  5752. (length org-subtree-clip)))))
  5753. (defun org-paste-subtree (&optional level tree for-yank)
  5754. "Paste the clipboard as a subtree, with modification of headline level.
  5755. The entire subtree is promoted or demoted in order to match a new headline
  5756. level.
  5757. If the cursor is at the beginning of a headline, the same level as
  5758. that headline is used to paste the tree
  5759. If not, the new level is derived from the *visible* headings
  5760. before and after the insertion point, and taken to be the inferior headline
  5761. level of the two. So if the previous visible heading is level 3 and the
  5762. next is level 4 (or vice versa), level 4 will be used for insertion.
  5763. This makes sure that the subtree remains an independent subtree and does
  5764. not swallow low level entries.
  5765. You can also force a different level, either by using a numeric prefix
  5766. argument, or by inserting the heading marker by hand. For example, if the
  5767. cursor is after \"*****\", then the tree will be shifted to level 5.
  5768. If optional TREE is given, use this text instead of the kill ring.
  5769. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5770. move back over whitespace before inserting, and move point to the end of
  5771. the inserted text when done."
  5772. (interactive "P")
  5773. (setq tree (or tree (and kill-ring (current-kill 0))))
  5774. (unless (org-kill-is-subtree-p tree)
  5775. (error "%s"
  5776. (substitute-command-keys
  5777. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5778. (let* ((visp (not (org-invisible-p)))
  5779. (txt tree)
  5780. (^re (concat "^\\(" outline-regexp "\\)"))
  5781. (re (concat "\\(" outline-regexp "\\)"))
  5782. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5783. (old-level (if (string-match ^re txt)
  5784. (- (match-end 0) (match-beginning 0) 1)
  5785. -1))
  5786. (force-level (cond (level (prefix-numeric-value level))
  5787. ((and (looking-at "[ \t]*$")
  5788. (string-match
  5789. ^re_ (buffer-substring
  5790. (point-at-bol) (point))))
  5791. (- (match-end 1) (match-beginning 1)))
  5792. ((and (bolp)
  5793. (looking-at org-outline-regexp))
  5794. (- (match-end 0) (point) 1))
  5795. (t nil)))
  5796. (previous-level (save-excursion
  5797. (condition-case nil
  5798. (progn
  5799. (outline-previous-visible-heading 1)
  5800. (if (looking-at re)
  5801. (- (match-end 0) (match-beginning 0) 1)
  5802. 1))
  5803. (error 1))))
  5804. (next-level (save-excursion
  5805. (condition-case nil
  5806. (progn
  5807. (or (looking-at outline-regexp)
  5808. (outline-next-visible-heading 1))
  5809. (if (looking-at re)
  5810. (- (match-end 0) (match-beginning 0) 1)
  5811. 1))
  5812. (error 1))))
  5813. (new-level (or force-level (max previous-level next-level)))
  5814. (shift (if (or (= old-level -1)
  5815. (= new-level -1)
  5816. (= old-level new-level))
  5817. 0
  5818. (- new-level old-level)))
  5819. (delta (if (> shift 0) -1 1))
  5820. (func (if (> shift 0) 'org-demote 'org-promote))
  5821. (org-odd-levels-only nil)
  5822. beg end newend)
  5823. ;; Remove the forced level indicator
  5824. (if force-level
  5825. (delete-region (point-at-bol) (point)))
  5826. ;; Paste
  5827. (beginning-of-line 1)
  5828. (unless for-yank (org-back-over-empty-lines))
  5829. (setq beg (point))
  5830. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5831. (insert-before-markers txt)
  5832. (unless (string-match "\n\\'" txt) (insert "\n"))
  5833. (setq newend (point))
  5834. (org-reinstall-markers-in-region beg)
  5835. (setq end (point))
  5836. (goto-char beg)
  5837. (skip-chars-forward " \t\n\r")
  5838. (setq beg (point))
  5839. (if (and (org-invisible-p) visp)
  5840. (save-excursion (outline-show-heading)))
  5841. ;; Shift if necessary
  5842. (unless (= shift 0)
  5843. (save-restriction
  5844. (narrow-to-region beg end)
  5845. (while (not (= shift 0))
  5846. (org-map-region func (point-min) (point-max))
  5847. (setq shift (+ delta shift)))
  5848. (goto-char (point-min))
  5849. (setq newend (point-max))))
  5850. (when (or (interactive-p) for-yank)
  5851. (message "Clipboard pasted as level %d subtree" new-level))
  5852. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5853. kill-ring
  5854. (eq org-subtree-clip (current-kill 0))
  5855. org-subtree-clip-folded)
  5856. ;; The tree was folded before it was killed/copied
  5857. (hide-subtree))
  5858. (and for-yank (goto-char newend))))
  5859. (defun org-kill-is-subtree-p (&optional txt)
  5860. "Check if the current kill is an outline subtree, or a set of trees.
  5861. Returns nil if kill does not start with a headline, or if the first
  5862. headline level is not the largest headline level in the tree.
  5863. So this will actually accept several entries of equal levels as well,
  5864. which is OK for `org-paste-subtree'.
  5865. If optional TXT is given, check this string instead of the current kill."
  5866. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5867. (start-level (and kill
  5868. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5869. org-outline-regexp "\\)")
  5870. kill)
  5871. (- (match-end 2) (match-beginning 2) 1)))
  5872. (re (concat "^" org-outline-regexp))
  5873. (start (1+ (or (match-beginning 2) -1))))
  5874. (if (not start-level)
  5875. (progn
  5876. nil) ;; does not even start with a heading
  5877. (catch 'exit
  5878. (while (setq start (string-match re kill (1+ start)))
  5879. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5880. (throw 'exit nil)))
  5881. t))))
  5882. (defvar org-markers-to-move nil
  5883. "Markers that should be moved with a cut-and-paste operation.
  5884. Those markers are stored together with their positions relative to
  5885. the start of the region.")
  5886. (defun org-save-markers-in-region (beg end)
  5887. "Check markers in region.
  5888. If these markers are between BEG and END, record their position relative
  5889. to BEG, so that after moving the block of text, we can put the markers back
  5890. into place.
  5891. This function gets called just before an entry or tree gets cut from the
  5892. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5893. called immediately, to move the markers with the entries."
  5894. (setq org-markers-to-move nil)
  5895. (when (featurep 'org-clock)
  5896. (org-clock-save-markers-for-cut-and-paste beg end))
  5897. (when (featurep 'org-agenda)
  5898. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5899. (defun org-check-and-save-marker (marker beg end)
  5900. "Check if MARKER is between BEG and END.
  5901. If yes, remember the marker and the distance to BEG."
  5902. (when (and (marker-buffer marker)
  5903. (equal (marker-buffer marker) (current-buffer)))
  5904. (if (and (>= marker beg) (< marker end))
  5905. (push (cons marker (- marker beg)) org-markers-to-move))))
  5906. (defun org-reinstall-markers-in-region (beg)
  5907. "Move all remembered markers to their position relative to BEG."
  5908. (mapc (lambda (x)
  5909. (move-marker (car x) (+ beg (cdr x))))
  5910. org-markers-to-move)
  5911. (setq org-markers-to-move nil))
  5912. (defun org-narrow-to-subtree ()
  5913. "Narrow buffer to the current subtree."
  5914. (interactive)
  5915. (save-excursion
  5916. (save-match-data
  5917. (narrow-to-region
  5918. (progn (org-back-to-heading t) (point))
  5919. (progn (org-end-of-subtree t t) (point))))))
  5920. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5921. "Clone the task (subtree) at point N times.
  5922. The clones will be inserted as siblings.
  5923. In interactive use, the user will be prompted for the number of clones
  5924. to be produced, and for a time SHIFT, which may be a repeater as used
  5925. in time stamps, for example `+3d'.
  5926. When a valid repeater is given and the entry contains any time stamps,
  5927. the clones will become a sequence in time, with time stamps in the
  5928. subtree shifted for each clone produced. If SHIFT is nil or the
  5929. empty string, time stamps will be left alone.
  5930. If the original subtree did contain time stamps with a repeater,
  5931. the following will happen:
  5932. - the repeater will be removed in each clone
  5933. - an additional clone will be produced, with the current, unshifted
  5934. date(s) in the entry.
  5935. - the original entry will be placed *after* all the clones, with
  5936. repeater intact.
  5937. - the start days in the repeater in the original entry will be shifted
  5938. to past the last clone.
  5939. I this way you can spell out a number of instances of a repeating task,
  5940. and still retain the repeater to cover future instances of the task."
  5941. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5942. (let (beg end template task
  5943. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5944. (if (not (and (integerp n) (> n 0)))
  5945. (error "Invalid number of replications %s" n))
  5946. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5947. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5948. shift)))
  5949. (error "Invalid shift specification %s" shift))
  5950. (when doshift
  5951. (setq shift-n (string-to-number (match-string 1 shift))
  5952. shift-what (cdr (assoc (match-string 2 shift)
  5953. '(("d" . day) ("w" . week)
  5954. ("m" . month) ("y" . year))))))
  5955. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5956. (setq nmin 1 nmax n)
  5957. (org-back-to-heading t)
  5958. (setq beg (point))
  5959. (org-end-of-subtree t t)
  5960. (setq end (point))
  5961. (setq template (buffer-substring beg end))
  5962. (when (and doshift
  5963. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5964. (delete-region beg end)
  5965. (setq end beg)
  5966. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5967. (goto-char end)
  5968. (loop for n from nmin to nmax do
  5969. (if (not doshift)
  5970. (setq task template)
  5971. (with-temp-buffer
  5972. (insert template)
  5973. (org-mode)
  5974. (goto-char (point-min))
  5975. (while (re-search-forward org-ts-regexp-both nil t)
  5976. (org-timestamp-change (* n shift-n) shift-what))
  5977. (unless (= n n-no-remove)
  5978. (goto-char (point-min))
  5979. (while (re-search-forward org-ts-regexp nil t)
  5980. (save-excursion
  5981. (goto-char (match-beginning 0))
  5982. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5983. (delete-region (match-beginning 1) (match-end 1))))))
  5984. (setq task (buffer-string))))
  5985. (insert task))
  5986. (goto-char beg)))
  5987. ;;; Outline Sorting
  5988. (defun org-sort (with-case)
  5989. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5990. Optional argument WITH-CASE means sort case-sensitively.
  5991. With a double prefix argument, also remove duplicate entries."
  5992. (interactive "P")
  5993. (if (org-at-table-p)
  5994. (org-call-with-arg 'org-table-sort-lines with-case)
  5995. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5996. (defun org-sort-remove-invisible (s)
  5997. (remove-text-properties 0 (length s) org-rm-props s)
  5998. (while (string-match org-bracket-link-regexp s)
  5999. (setq s (replace-match (if (match-end 2)
  6000. (match-string 3 s)
  6001. (match-string 1 s)) t t s)))
  6002. s)
  6003. (defvar org-priority-regexp) ; defined later in the file
  6004. (defvar org-after-sorting-entries-or-items-hook nil
  6005. "Hook that is run after a bunch of entries or items have been sorted.
  6006. When children are sorted, the cursor is in the parent line when this
  6007. hook gets called. When a region or a plain list is sorted, the cursor
  6008. will be in the first entry of the sorted region/list.")
  6009. (defun org-sort-entries-or-items
  6010. (&optional with-case sorting-type getkey-func compare-func property)
  6011. "Sort entries on a certain level of an outline tree, or plain list items.
  6012. If there is an active region, the entries in the region are sorted.
  6013. Else, if the cursor is before the first entry, sort the top-level items.
  6014. Else, the children of the entry at point are sorted.
  6015. If the cursor is at the first item in a plain list, the list items will be
  6016. sorted.
  6017. Sorting can be alphabetically, numerically, by date/time as given by
  6018. a time stamp, by a property or by priority.
  6019. The command prompts for the sorting type unless it has been given to the
  6020. function through the SORTING-TYPE argument, which needs to a character,
  6021. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6022. precise meaning of each character:
  6023. n Numerically, by converting the beginning of the entry/item to a number.
  6024. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6025. t By date/time, either the first active time stamp in the entry, or, if
  6026. none exist, by the first inactive one.
  6027. In items, only the first line will be chekced.
  6028. s By the scheduled date/time.
  6029. d By deadline date/time.
  6030. c By creation time, which is assumed to be the first inactive time stamp
  6031. at the beginning of a line.
  6032. p By priority according to the cookie.
  6033. r By the value of a property.
  6034. Capital letters will reverse the sort order.
  6035. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6036. called with point at the beginning of the record. It must return either
  6037. a string or a number that should serve as the sorting key for that record.
  6038. Comparing entries ignores case by default. However, with an optional argument
  6039. WITH-CASE, the sorting considers case as well."
  6040. (interactive "P")
  6041. (let ((case-func (if with-case 'identity 'downcase))
  6042. start beg end stars re re2
  6043. txt what tmp plain-list-p)
  6044. ;; Find beginning and end of region to sort
  6045. (cond
  6046. ((org-region-active-p)
  6047. ;; we will sort the region
  6048. (setq end (region-end)
  6049. what "region")
  6050. (goto-char (region-beginning))
  6051. (if (not (org-on-heading-p)) (outline-next-heading))
  6052. (setq start (point)))
  6053. ((org-at-item-p)
  6054. ;; we will sort this plain list
  6055. (org-beginning-of-item-list) (setq start (point))
  6056. (org-end-of-item-list) (setq end (point))
  6057. (goto-char start)
  6058. (setq plain-list-p t
  6059. what "plain list"))
  6060. ((or (org-on-heading-p)
  6061. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6062. ;; we will sort the children of the current headline
  6063. (org-back-to-heading)
  6064. (setq start (point)
  6065. end (progn (org-end-of-subtree t t)
  6066. (org-back-over-empty-lines)
  6067. (point))
  6068. what "children")
  6069. (goto-char start)
  6070. (show-subtree)
  6071. (outline-next-heading))
  6072. (t
  6073. ;; we will sort the top-level entries in this file
  6074. (goto-char (point-min))
  6075. (or (org-on-heading-p) (outline-next-heading))
  6076. (setq start (point) end (point-max) what "top-level")
  6077. (goto-char start)
  6078. (show-all)))
  6079. (setq beg (point))
  6080. (if (>= beg end) (error "Nothing to sort"))
  6081. (unless plain-list-p
  6082. (looking-at "\\(\\*+\\)")
  6083. (setq stars (match-string 1)
  6084. re (concat "^" (regexp-quote stars) " +")
  6085. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6086. txt (buffer-substring beg end))
  6087. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6088. (if (and (not (equal stars "*")) (string-match re2 txt))
  6089. (error "Region to sort contains a level above the first entry")))
  6090. (unless sorting-type
  6091. (message
  6092. (if plain-list-p
  6093. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6094. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6095. [t]ime [s]cheduled [d]eadline [c]reated
  6096. A/N/T/S/D/C/P/O/F means reversed:")
  6097. what)
  6098. (setq sorting-type (read-char-exclusive))
  6099. (and (= (downcase sorting-type) ?f)
  6100. (setq getkey-func
  6101. (org-icompleting-read "Sort using function: "
  6102. obarray 'fboundp t nil nil))
  6103. (setq getkey-func (intern getkey-func)))
  6104. (and (= (downcase sorting-type) ?r)
  6105. (setq property
  6106. (org-icompleting-read "Property: "
  6107. (mapcar 'list (org-buffer-property-keys t))
  6108. nil t))))
  6109. (message "Sorting entries...")
  6110. (save-restriction
  6111. (narrow-to-region start end)
  6112. (let ((dcst (downcase sorting-type))
  6113. (case-fold-search nil)
  6114. (now (current-time)))
  6115. (sort-subr
  6116. (/= dcst sorting-type)
  6117. ;; This function moves to the beginning character of the "record" to
  6118. ;; be sorted.
  6119. (if plain-list-p
  6120. (lambda nil
  6121. (if (org-at-item-p) t (goto-char (point-max))))
  6122. (lambda nil
  6123. (if (re-search-forward re nil t)
  6124. (goto-char (match-beginning 0))
  6125. (goto-char (point-max)))))
  6126. ;; This function moves to the last character of the "record" being
  6127. ;; sorted.
  6128. (if plain-list-p
  6129. 'org-end-of-item
  6130. (lambda nil
  6131. (save-match-data
  6132. (condition-case nil
  6133. (outline-forward-same-level 1)
  6134. (error
  6135. (goto-char (point-max)))))))
  6136. ;; This function returns the value that gets sorted against.
  6137. (if plain-list-p
  6138. (lambda nil
  6139. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6140. (cond
  6141. ((= dcst ?n)
  6142. (string-to-number (buffer-substring (match-end 0)
  6143. (point-at-eol))))
  6144. ((= dcst ?a)
  6145. (buffer-substring (match-end 0) (point-at-eol)))
  6146. ((= dcst ?t)
  6147. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6148. (re-search-forward org-ts-regexp-both
  6149. (point-at-eol) t))
  6150. (org-time-string-to-seconds (match-string 0))
  6151. (org-float-time now)))
  6152. ((= dcst ?f)
  6153. (if getkey-func
  6154. (progn
  6155. (setq tmp (funcall getkey-func))
  6156. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6157. tmp)
  6158. (error "Invalid key function `%s'" getkey-func)))
  6159. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6160. (lambda nil
  6161. (cond
  6162. ((= dcst ?n)
  6163. (if (looking-at org-complex-heading-regexp)
  6164. (string-to-number (match-string 4))
  6165. nil))
  6166. ((= dcst ?a)
  6167. (if (looking-at org-complex-heading-regexp)
  6168. (funcall case-func (match-string 4))
  6169. nil))
  6170. ((= dcst ?t)
  6171. (let ((end (save-excursion (outline-next-heading) (point))))
  6172. (if (or (re-search-forward org-ts-regexp end t)
  6173. (re-search-forward org-ts-regexp-both end t))
  6174. (org-time-string-to-seconds (match-string 0))
  6175. (org-float-time now))))
  6176. ((= dcst ?c)
  6177. (let ((end (save-excursion (outline-next-heading) (point))))
  6178. (if (re-search-forward
  6179. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6180. end t)
  6181. (org-time-string-to-seconds (match-string 0))
  6182. (org-float-time now))))
  6183. ((= dcst ?s)
  6184. (let ((end (save-excursion (outline-next-heading) (point))))
  6185. (if (re-search-forward org-scheduled-time-regexp end t)
  6186. (org-time-string-to-seconds (match-string 1))
  6187. (org-float-time now))))
  6188. ((= dcst ?d)
  6189. (let ((end (save-excursion (outline-next-heading) (point))))
  6190. (if (re-search-forward org-deadline-time-regexp end t)
  6191. (org-time-string-to-seconds (match-string 1))
  6192. (org-float-time now))))
  6193. ((= dcst ?p)
  6194. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6195. (string-to-char (match-string 2))
  6196. org-default-priority))
  6197. ((= dcst ?r)
  6198. (or (org-entry-get nil property) ""))
  6199. ((= dcst ?o)
  6200. (if (looking-at org-complex-heading-regexp)
  6201. (- 9999 (length (member (match-string 2)
  6202. org-todo-keywords-1)))))
  6203. ((= dcst ?f)
  6204. (if getkey-func
  6205. (progn
  6206. (setq tmp (funcall getkey-func))
  6207. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6208. tmp)
  6209. (error "Invalid key function `%s'" getkey-func)))
  6210. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6211. nil
  6212. (cond
  6213. ((= dcst ?a) 'string<)
  6214. ((= dcst ?f) compare-func)
  6215. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6216. (t nil)))))
  6217. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6218. (message "Sorting entries...done")))
  6219. (defun org-do-sort (table what &optional with-case sorting-type)
  6220. "Sort TABLE of WHAT according to SORTING-TYPE.
  6221. The user will be prompted for the SORTING-TYPE if the call to this
  6222. function does not specify it. WHAT is only for the prompt, to indicate
  6223. what is being sorted. The sorting key will be extracted from
  6224. the car of the elements of the table.
  6225. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6226. (unless sorting-type
  6227. (message
  6228. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6229. what)
  6230. (setq sorting-type (read-char-exclusive)))
  6231. (let ((dcst (downcase sorting-type))
  6232. extractfun comparefun)
  6233. ;; Define the appropriate functions
  6234. (cond
  6235. ((= dcst ?n)
  6236. (setq extractfun 'string-to-number
  6237. comparefun (if (= dcst sorting-type) '< '>)))
  6238. ((= dcst ?a)
  6239. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6240. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6241. comparefun (if (= dcst sorting-type)
  6242. 'string<
  6243. (lambda (a b) (and (not (string< a b))
  6244. (not (string= a b)))))))
  6245. ((= dcst ?t)
  6246. (setq extractfun
  6247. (lambda (x)
  6248. (if (or (string-match org-ts-regexp x)
  6249. (string-match org-ts-regexp-both x))
  6250. (org-float-time
  6251. (org-time-string-to-time (match-string 0 x)))
  6252. 0))
  6253. comparefun (if (= dcst sorting-type) '< '>)))
  6254. (t (error "Invalid sorting type `%c'" sorting-type)))
  6255. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6256. table)
  6257. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6258. ;;; The orgstruct minor mode
  6259. ;; Define a minor mode which can be used in other modes in order to
  6260. ;; integrate the org-mode structure editing commands.
  6261. ;; This is really a hack, because the org-mode structure commands use
  6262. ;; keys which normally belong to the major mode. Here is how it
  6263. ;; works: The minor mode defines all the keys necessary to operate the
  6264. ;; structure commands, but wraps the commands into a function which
  6265. ;; tests if the cursor is currently at a headline or a plain list
  6266. ;; item. If that is the case, the structure command is used,
  6267. ;; temporarily setting many Org-mode variables like regular
  6268. ;; expressions for filling etc. However, when any of those keys is
  6269. ;; used at a different location, function uses `key-binding' to look
  6270. ;; up if the key has an associated command in another currently active
  6271. ;; keymap (minor modes, major mode, global), and executes that
  6272. ;; command. There might be problems if any of the keys is otherwise
  6273. ;; used as a prefix key.
  6274. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6275. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6276. ;; addresses this by checking explicitly for both bindings.
  6277. (defvar orgstruct-mode-map (make-sparse-keymap)
  6278. "Keymap for the minor `orgstruct-mode'.")
  6279. (defvar org-local-vars nil
  6280. "List of local variables, for use by `orgstruct-mode'")
  6281. ;;;###autoload
  6282. (define-minor-mode orgstruct-mode
  6283. "Toggle the minor more `orgstruct-mode'.
  6284. This mode is for using Org-mode structure commands in other modes.
  6285. The following key behave as if Org-mode was active, if the cursor
  6286. is on a headline, or on a plain list item (both in the definition
  6287. of Org-mode).
  6288. M-up Move entry/item up
  6289. M-down Move entry/item down
  6290. M-left Promote
  6291. M-right Demote
  6292. M-S-up Move entry/item up
  6293. M-S-down Move entry/item down
  6294. M-S-left Promote subtree
  6295. M-S-right Demote subtree
  6296. M-q Fill paragraph and items like in Org-mode
  6297. C-c ^ Sort entries
  6298. C-c - Cycle list bullet
  6299. TAB Cycle item visibility
  6300. M-RET Insert new heading/item
  6301. S-M-RET Insert new TODO heading / Checkbox item
  6302. C-c C-c Set tags / toggle checkbox"
  6303. nil " OrgStruct" nil
  6304. (org-load-modules-maybe)
  6305. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6306. ;;;###autoload
  6307. (defun turn-on-orgstruct ()
  6308. "Unconditionally turn on `orgstruct-mode'."
  6309. (orgstruct-mode 1))
  6310. (defun orgstruct++-mode (&optional arg)
  6311. "Toggle `orgstruct-mode', the enhanced version of it.
  6312. In addition to setting orgstruct-mode, this also exports all indentation
  6313. and autofilling variables from org-mode into the buffer. It will also
  6314. recognize item context in multiline items.
  6315. Note that turning off orgstruct-mode will *not* remove the
  6316. indentation/paragraph settings. This can only be done by refreshing the
  6317. major mode, for example with \\[normal-mode]."
  6318. (interactive "P")
  6319. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6320. (if (< arg 1)
  6321. (orgstruct-mode -1)
  6322. (orgstruct-mode 1)
  6323. (let (var val)
  6324. (mapc
  6325. (lambda (x)
  6326. (when (string-match
  6327. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6328. (symbol-name (car x)))
  6329. (setq var (car x) val (nth 1 x))
  6330. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6331. org-local-vars)
  6332. (org-set-local 'orgstruct-is-++ t))))
  6333. (defvar orgstruct-is-++ nil
  6334. "Is orgstruct-mode in ++ version in the current-buffer?")
  6335. (make-variable-buffer-local 'orgstruct-is-++)
  6336. ;;;###autoload
  6337. (defun turn-on-orgstruct++ ()
  6338. "Unconditionally turn on `orgstruct++-mode'."
  6339. (orgstruct++-mode 1))
  6340. (defun orgstruct-error ()
  6341. "Error when there is no default binding for a structure key."
  6342. (interactive)
  6343. (error "This key has no function outside structure elements"))
  6344. (defun orgstruct-setup ()
  6345. "Setup orgstruct keymaps."
  6346. (let ((nfunc 0)
  6347. (bindings
  6348. (list
  6349. '([(meta up)] org-metaup)
  6350. '([(meta down)] org-metadown)
  6351. '([(meta left)] org-metaleft)
  6352. '([(meta right)] org-metaright)
  6353. '([(meta shift up)] org-shiftmetaup)
  6354. '([(meta shift down)] org-shiftmetadown)
  6355. '([(meta shift left)] org-shiftmetaleft)
  6356. '([(meta shift right)] org-shiftmetaright)
  6357. '([?\e (up)] org-metaup)
  6358. '([?\e (down)] org-metadown)
  6359. '([?\e (left)] org-metaleft)
  6360. '([?\e (right)] org-metaright)
  6361. '([?\e (shift up)] org-shiftmetaup)
  6362. '([?\e (shift down)] org-shiftmetadown)
  6363. '([?\e (shift left)] org-shiftmetaleft)
  6364. '([?\e (shift right)] org-shiftmetaright)
  6365. '([(shift up)] org-shiftup)
  6366. '([(shift down)] org-shiftdown)
  6367. '([(shift left)] org-shiftleft)
  6368. '([(shift right)] org-shiftright)
  6369. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6370. '("\M-q" fill-paragraph)
  6371. '("\C-c^" org-sort)
  6372. '("\C-c-" org-cycle-list-bullet)))
  6373. elt key fun cmd)
  6374. (while (setq elt (pop bindings))
  6375. (setq nfunc (1+ nfunc))
  6376. (setq key (org-key (car elt))
  6377. fun (nth 1 elt)
  6378. cmd (orgstruct-make-binding fun nfunc key))
  6379. (org-defkey orgstruct-mode-map key cmd))
  6380. ;; Special treatment needed for TAB and RET
  6381. (org-defkey orgstruct-mode-map [(tab)]
  6382. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6383. (org-defkey orgstruct-mode-map "\C-i"
  6384. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6385. (org-defkey orgstruct-mode-map "\M-\C-m"
  6386. (orgstruct-make-binding 'org-insert-heading 105
  6387. "\M-\C-m" [(meta return)]))
  6388. (org-defkey orgstruct-mode-map [(meta return)]
  6389. (orgstruct-make-binding 'org-insert-heading 106
  6390. [(meta return)] "\M-\C-m"))
  6391. (org-defkey orgstruct-mode-map [(shift meta return)]
  6392. (orgstruct-make-binding 'org-insert-todo-heading 107
  6393. [(meta return)] "\M-\C-m"))
  6394. (org-defkey orgstruct-mode-map "\e\C-m"
  6395. (orgstruct-make-binding 'org-insert-heading 108
  6396. "\e\C-m" [?\e (return)]))
  6397. (org-defkey orgstruct-mode-map [?\e (return)]
  6398. (orgstruct-make-binding 'org-insert-heading 109
  6399. [?\e (return)] "\e\C-m"))
  6400. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6401. (orgstruct-make-binding 'org-insert-todo-heading 110
  6402. [?\e (return)] "\e\C-m"))
  6403. (unless org-local-vars
  6404. (setq org-local-vars (org-get-local-variables)))
  6405. t))
  6406. (defun orgstruct-make-binding (fun n &rest keys)
  6407. "Create a function for binding in the structure minor mode.
  6408. FUN is the command to call inside a table. N is used to create a unique
  6409. command name. KEYS are keys that should be checked in for a command
  6410. to execute outside of tables."
  6411. (eval
  6412. (list 'defun
  6413. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6414. '(arg)
  6415. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6416. "Outside of structure, run the binding of `"
  6417. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6418. "'.")
  6419. '(interactive "p")
  6420. (list 'if
  6421. `(org-context-p 'headline 'item
  6422. (and orgstruct-is-++
  6423. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6424. 'item-body))
  6425. (list 'org-run-like-in-org-mode (list 'quote fun))
  6426. (list 'let '(orgstruct-mode)
  6427. (list 'call-interactively
  6428. (append '(or)
  6429. (mapcar (lambda (k)
  6430. (list 'key-binding k))
  6431. keys)
  6432. '('orgstruct-error))))))))
  6433. (defun org-context-p (&rest contexts)
  6434. "Check if local context is any of CONTEXTS.
  6435. Possible values in the list of contexts are `table', `headline', and `item'."
  6436. (let ((pos (point)))
  6437. (goto-char (point-at-bol))
  6438. (prog1 (or (and (memq 'table contexts)
  6439. (looking-at "[ \t]*|"))
  6440. (and (memq 'headline contexts)
  6441. ;;????????? (looking-at "\\*+"))
  6442. (looking-at outline-regexp))
  6443. (and (memq 'item contexts)
  6444. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6445. (and (memq 'item-body contexts)
  6446. (org-in-item-p)))
  6447. (goto-char pos))))
  6448. (defun org-get-local-variables ()
  6449. "Return a list of all local variables in an org-mode buffer."
  6450. (let (varlist)
  6451. (with-current-buffer (get-buffer-create "*Org tmp*")
  6452. (erase-buffer)
  6453. (org-mode)
  6454. (setq varlist (buffer-local-variables)))
  6455. (kill-buffer "*Org tmp*")
  6456. (delq nil
  6457. (mapcar
  6458. (lambda (x)
  6459. (setq x
  6460. (if (symbolp x)
  6461. (list x)
  6462. (list (car x) (list 'quote (cdr x)))))
  6463. (if (string-match
  6464. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6465. (symbol-name (car x)))
  6466. x nil))
  6467. varlist))))
  6468. ;;;###autoload
  6469. (defun org-run-like-in-org-mode (cmd)
  6470. "Run a command, pretending that the current buffer is in Org-mode.
  6471. This will temporarily bind local variables that are typically bound in
  6472. Org-mode to the values they have in Org-mode, and then interactively
  6473. call CMD."
  6474. (org-load-modules-maybe)
  6475. (unless org-local-vars
  6476. (setq org-local-vars (org-get-local-variables)))
  6477. (eval (list 'let org-local-vars
  6478. (list 'call-interactively (list 'quote cmd)))))
  6479. ;;;; Archiving
  6480. (defun org-get-category (&optional pos)
  6481. "Get the category applying to position POS."
  6482. (get-text-property (or pos (point)) 'org-category))
  6483. (defun org-refresh-category-properties ()
  6484. "Refresh category text properties in the buffer."
  6485. (let ((def-cat (cond
  6486. ((null org-category)
  6487. (if buffer-file-name
  6488. (file-name-sans-extension
  6489. (file-name-nondirectory buffer-file-name))
  6490. "???"))
  6491. ((symbolp org-category) (symbol-name org-category))
  6492. (t org-category)))
  6493. beg end cat pos optionp)
  6494. (org-unmodified
  6495. (save-excursion
  6496. (save-restriction
  6497. (widen)
  6498. (goto-char (point-min))
  6499. (put-text-property (point) (point-max) 'org-category def-cat)
  6500. (while (re-search-forward
  6501. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6502. (setq pos (match-end 0)
  6503. optionp (equal (char-after (match-beginning 0)) ?#)
  6504. cat (org-trim (match-string 2)))
  6505. (if optionp
  6506. (setq beg (point-at-bol) end (point-max))
  6507. (org-back-to-heading t)
  6508. (setq beg (point) end (org-end-of-subtree t t)))
  6509. (put-text-property beg end 'org-category cat)
  6510. (goto-char pos)))))))
  6511. ;;;; Link Stuff
  6512. ;;; Link abbreviations
  6513. (defun org-link-expand-abbrev (link)
  6514. "Apply replacements as defined in `org-link-abbrev-alist."
  6515. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6516. (let* ((key (match-string 1 link))
  6517. (as (or (assoc key org-link-abbrev-alist-local)
  6518. (assoc key org-link-abbrev-alist)))
  6519. (tag (and (match-end 2) (match-string 3 link)))
  6520. rpl)
  6521. (if (not as)
  6522. link
  6523. (setq rpl (cdr as))
  6524. (cond
  6525. ((symbolp rpl) (funcall rpl tag))
  6526. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6527. ((string-match "%h" rpl)
  6528. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6529. (t (concat rpl tag)))))
  6530. link))
  6531. ;;; Storing and inserting links
  6532. (defvar org-insert-link-history nil
  6533. "Minibuffer history for links inserted with `org-insert-link'.")
  6534. (defvar org-stored-links nil
  6535. "Contains the links stored with `org-store-link'.")
  6536. (defvar org-store-link-plist nil
  6537. "Plist with info about the most recently link created with `org-store-link'.")
  6538. (defvar org-link-protocols nil
  6539. "Link protocols added to Org-mode using `org-add-link-type'.")
  6540. (defvar org-store-link-functions nil
  6541. "List of functions that are called to create and store a link.
  6542. Each function will be called in turn until one returns a non-nil
  6543. value. Each function should check if it is responsible for creating
  6544. this link (for example by looking at the major mode).
  6545. If not, it must exit and return nil.
  6546. If yes, it should return a non-nil value after a calling
  6547. `org-store-link-props' with a list of properties and values.
  6548. Special properties are:
  6549. :type The link prefix. like \"http\". This must be given.
  6550. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6551. This is obligatory as well.
  6552. :description Optional default description for the second pair
  6553. of brackets in an Org-mode link. The user can still change
  6554. this when inserting this link into an Org-mode buffer.
  6555. In addition to these, any additional properties can be specified
  6556. and then used in remember templates.")
  6557. (defun org-add-link-type (type &optional follow export)
  6558. "Add TYPE to the list of `org-link-types'.
  6559. Re-compute all regular expressions depending on `org-link-types'
  6560. FOLLOW and EXPORT are two functions.
  6561. FOLLOW should take the link path as the single argument and do whatever
  6562. is necessary to follow the link, for example find a file or display
  6563. a mail message.
  6564. EXPORT should format the link path for export to one of the export formats.
  6565. It should be a function accepting three arguments:
  6566. path the path of the link, the text after the prefix (like \"http:\")
  6567. desc the description of the link, if any, nil if there was no description
  6568. format the export format, a symbol like `html' or `latex'.
  6569. The function may use the FORMAT information to return different values
  6570. depending on the format. The return value will be put literally into
  6571. the exported file.
  6572. Org-mode has a built-in default for exporting links. If you are happy with
  6573. this default, there is no need to define an export function for the link
  6574. type. For a simple example of an export function, see `org-bbdb.el'."
  6575. (add-to-list 'org-link-types type t)
  6576. (org-make-link-regexps)
  6577. (if (assoc type org-link-protocols)
  6578. (setcdr (assoc type org-link-protocols) (list follow export))
  6579. (push (list type follow export) org-link-protocols)))
  6580. (defvar org-agenda-buffer-name)
  6581. ;;;###autoload
  6582. (defun org-store-link (arg)
  6583. "\\<org-mode-map>Store an org-link to the current location.
  6584. This link is added to `org-stored-links' and can later be inserted
  6585. into an org-buffer with \\[org-insert-link].
  6586. For some link types, a prefix arg is interpreted:
  6587. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6588. For file links, arg negates `org-context-in-file-links'."
  6589. (interactive "P")
  6590. (org-load-modules-maybe)
  6591. (setq org-store-link-plist nil) ; reset
  6592. (let ((outline-regexp (org-get-limited-outline-regexp))
  6593. link cpltxt desc description search txt custom-id)
  6594. (cond
  6595. ((run-hook-with-args-until-success 'org-store-link-functions)
  6596. (setq link (plist-get org-store-link-plist :link)
  6597. desc (or (plist-get org-store-link-plist :description) link)))
  6598. ((equal (buffer-name) "*Org Edit Src Example*")
  6599. (let (label gc)
  6600. (while (or (not label)
  6601. (save-excursion
  6602. (save-restriction
  6603. (widen)
  6604. (goto-char (point-min))
  6605. (re-search-forward
  6606. (regexp-quote (format org-coderef-label-format label))
  6607. nil t))))
  6608. (when label (message "Label exists already") (sit-for 2))
  6609. (setq label (read-string "Code line label: " label)))
  6610. (end-of-line 1)
  6611. (setq link (format org-coderef-label-format label))
  6612. (setq gc (- 79 (length link)))
  6613. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6614. (insert link)
  6615. (setq link (concat "(" label ")") desc nil)))
  6616. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6617. ;; We are in the agenda, link to referenced location
  6618. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6619. (get-text-property (point) 'org-marker))))
  6620. (when m
  6621. (org-with-point-at m
  6622. (call-interactively 'org-store-link)))))
  6623. ((eq major-mode 'calendar-mode)
  6624. (let ((cd (calendar-cursor-to-date)))
  6625. (setq link
  6626. (format-time-string
  6627. (car org-time-stamp-formats)
  6628. (apply 'encode-time
  6629. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6630. nil nil nil))))
  6631. (org-store-link-props :type "calendar" :date cd)))
  6632. ((eq major-mode 'w3-mode)
  6633. (setq cpltxt (if (and (buffer-name)
  6634. (not (string-match "Untitled" (buffer-name))))
  6635. (buffer-name)
  6636. (url-view-url t))
  6637. link (org-make-link (url-view-url t)))
  6638. (org-store-link-props :type "w3" :url (url-view-url t)))
  6639. ((eq major-mode 'w3m-mode)
  6640. (setq cpltxt (or w3m-current-title w3m-current-url)
  6641. link (org-make-link w3m-current-url))
  6642. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6643. ((setq search (run-hook-with-args-until-success
  6644. 'org-create-file-search-functions))
  6645. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6646. "::" search))
  6647. (setq cpltxt (or description link)))
  6648. ((eq major-mode 'image-mode)
  6649. (setq cpltxt (concat "file:"
  6650. (abbreviate-file-name buffer-file-name))
  6651. link (org-make-link cpltxt))
  6652. (org-store-link-props :type "image" :file buffer-file-name))
  6653. ((eq major-mode 'dired-mode)
  6654. ;; link to the file in the current line
  6655. (setq cpltxt (concat "file:"
  6656. (abbreviate-file-name
  6657. (expand-file-name
  6658. (dired-get-filename nil t))))
  6659. link (org-make-link cpltxt)))
  6660. ((and buffer-file-name (org-mode-p))
  6661. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6662. (cond
  6663. ((org-in-regexp "<<\\(.*?\\)>>")
  6664. (setq cpltxt
  6665. (concat "file:"
  6666. (abbreviate-file-name buffer-file-name)
  6667. "::" (match-string 1))
  6668. link (org-make-link cpltxt)))
  6669. ((and (featurep 'org-id)
  6670. (or (eq org-link-to-org-use-id t)
  6671. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6672. (interactive-p))
  6673. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6674. (interactive-p)
  6675. (not custom-id))
  6676. (and org-link-to-org-use-id
  6677. (condition-case nil
  6678. (org-entry-get nil "ID")
  6679. (error nil)))))
  6680. ;; We can make a link using the ID.
  6681. (setq link (condition-case nil
  6682. (prog1 (org-id-store-link)
  6683. (setq desc (plist-get org-store-link-plist
  6684. :description)))
  6685. (error
  6686. ;; probably before first headline, link to file only
  6687. (concat "file:"
  6688. (abbreviate-file-name buffer-file-name))))))
  6689. (t
  6690. ;; Just link to current headline
  6691. (setq cpltxt (concat "file:"
  6692. (abbreviate-file-name buffer-file-name)))
  6693. ;; Add a context search string
  6694. (when (org-xor org-context-in-file-links arg)
  6695. (setq txt (cond
  6696. ((org-on-heading-p) nil)
  6697. ((org-region-active-p)
  6698. (buffer-substring (region-beginning) (region-end)))
  6699. (t nil)))
  6700. (when (or (null txt) (string-match "\\S-" txt))
  6701. (setq cpltxt
  6702. (concat cpltxt "::"
  6703. (condition-case nil
  6704. (org-make-org-heading-search-string txt)
  6705. (error "")))
  6706. desc (or (nth 4 (ignore-errors
  6707. (org-heading-components))) "NONE"))))
  6708. (if (string-match "::\\'" cpltxt)
  6709. (setq cpltxt (substring cpltxt 0 -2)))
  6710. (setq link (org-make-link cpltxt)))))
  6711. ((buffer-file-name (buffer-base-buffer))
  6712. ;; Just link to this file here.
  6713. (setq cpltxt (concat "file:"
  6714. (abbreviate-file-name
  6715. (buffer-file-name (buffer-base-buffer)))))
  6716. ;; Add a context string
  6717. (when (org-xor org-context-in-file-links arg)
  6718. (setq txt (if (org-region-active-p)
  6719. (buffer-substring (region-beginning) (region-end))
  6720. (buffer-substring (point-at-bol) (point-at-eol))))
  6721. ;; Only use search option if there is some text.
  6722. (when (string-match "\\S-" txt)
  6723. (setq cpltxt
  6724. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6725. desc "NONE")))
  6726. (setq link (org-make-link cpltxt)))
  6727. ((interactive-p)
  6728. (error "Cannot link to a buffer which is not visiting a file"))
  6729. (t (setq link nil)))
  6730. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6731. (setq link (or link cpltxt)
  6732. desc (or desc cpltxt))
  6733. (if (equal desc "NONE") (setq desc nil))
  6734. (if (and (or (interactive-p) executing-kbd-macro) link)
  6735. (progn
  6736. (setq org-stored-links
  6737. (cons (list link desc) org-stored-links))
  6738. (message "Stored: %s" (or desc link))
  6739. (when custom-id
  6740. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6741. "::#" custom-id))
  6742. (setq org-stored-links
  6743. (cons (list link desc) org-stored-links))))
  6744. (and link (org-make-link-string link desc)))))
  6745. (defun org-store-link-props (&rest plist)
  6746. "Store link properties, extract names and addresses."
  6747. (let (x adr)
  6748. (when (setq x (plist-get plist :from))
  6749. (setq adr (mail-extract-address-components x))
  6750. (setq plist (plist-put plist :fromname (car adr)))
  6751. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6752. (when (setq x (plist-get plist :to))
  6753. (setq adr (mail-extract-address-components x))
  6754. (setq plist (plist-put plist :toname (car adr)))
  6755. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6756. (let ((from (plist-get plist :from))
  6757. (to (plist-get plist :to)))
  6758. (when (and from to org-from-is-user-regexp)
  6759. (setq plist
  6760. (plist-put plist :fromto
  6761. (if (string-match org-from-is-user-regexp from)
  6762. (concat "to %t")
  6763. (concat "from %f"))))))
  6764. (setq org-store-link-plist plist))
  6765. (defun org-add-link-props (&rest plist)
  6766. "Add these properties to the link property list."
  6767. (let (key value)
  6768. (while plist
  6769. (setq key (pop plist) value (pop plist))
  6770. (setq org-store-link-plist
  6771. (plist-put org-store-link-plist key value)))))
  6772. (defun org-email-link-description (&optional fmt)
  6773. "Return the description part of an email link.
  6774. This takes information from `org-store-link-plist' and formats it
  6775. according to FMT (default from `org-email-link-description-format')."
  6776. (setq fmt (or fmt org-email-link-description-format))
  6777. (let* ((p org-store-link-plist)
  6778. (to (plist-get p :toaddress))
  6779. (from (plist-get p :fromaddress))
  6780. (table
  6781. (list
  6782. (cons "%c" (plist-get p :fromto))
  6783. (cons "%F" (plist-get p :from))
  6784. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6785. (cons "%T" (plist-get p :to))
  6786. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6787. (cons "%s" (plist-get p :subject))
  6788. (cons "%m" (plist-get p :message-id)))))
  6789. (when (string-match "%c" fmt)
  6790. ;; Check if the user wrote this message
  6791. (if (and org-from-is-user-regexp from to
  6792. (save-match-data (string-match org-from-is-user-regexp from)))
  6793. (setq fmt (replace-match "to %t" t t fmt))
  6794. (setq fmt (replace-match "from %f" t t fmt))))
  6795. (org-replace-escapes fmt table)))
  6796. (defun org-make-org-heading-search-string (&optional string heading)
  6797. "Make search string for STRING or current headline."
  6798. (interactive)
  6799. (let ((s (or string (org-get-heading))))
  6800. (unless (and string (not heading))
  6801. ;; We are using a headline, clean up garbage in there.
  6802. (if (string-match org-todo-regexp s)
  6803. (setq s (replace-match "" t t s)))
  6804. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6805. (setq s (replace-match "" t t s)))
  6806. (setq s (org-trim s))
  6807. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6808. org-comment-string "\\)") s)
  6809. (setq s (replace-match "" t t s)))
  6810. (while (string-match org-ts-regexp s)
  6811. (setq s (replace-match "" t t s))))
  6812. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6813. (setq s (replace-match " " t t s)))
  6814. (or string (setq s (concat "*" s))) ; Add * for headlines
  6815. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6816. (defun org-make-link (&rest strings)
  6817. "Concatenate STRINGS."
  6818. (apply 'concat strings))
  6819. (defun org-make-link-string (link &optional description)
  6820. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6821. (unless (string-match "\\S-" link)
  6822. (error "Empty link"))
  6823. (when (stringp description)
  6824. ;; Remove brackets from the description, they are fatal.
  6825. (while (string-match "\\[" description)
  6826. (setq description (replace-match "{" t t description)))
  6827. (while (string-match "\\]" description)
  6828. (setq description (replace-match "}" t t description))))
  6829. (when (equal (org-link-escape link) description)
  6830. ;; No description needed, it is identical
  6831. (setq description nil))
  6832. (when (and (not description)
  6833. (not (equal link (org-link-escape link))))
  6834. (setq description (org-extract-attributes link)))
  6835. (concat "[[" (org-link-escape link) "]"
  6836. (if description (concat "[" description "]") "")
  6837. "]"))
  6838. (defconst org-link-escape-chars
  6839. '((?\ . "%20")
  6840. (?\[ . "%5B")
  6841. (?\] . "%5D")
  6842. (?\340 . "%E0") ; `a
  6843. (?\342 . "%E2") ; ^a
  6844. (?\347 . "%E7") ; ,c
  6845. (?\350 . "%E8") ; `e
  6846. (?\351 . "%E9") ; 'e
  6847. (?\352 . "%EA") ; ^e
  6848. (?\356 . "%EE") ; ^i
  6849. (?\364 . "%F4") ; ^o
  6850. (?\371 . "%F9") ; `u
  6851. (?\373 . "%FB") ; ^u
  6852. (?\; . "%3B")
  6853. (?? . "%3F")
  6854. (?= . "%3D")
  6855. (?+ . "%2B")
  6856. )
  6857. "Association list of escapes for some characters problematic in links.
  6858. This is the list that is used for internal purposes.")
  6859. (defvar org-url-encoding-use-url-hexify nil)
  6860. (defconst org-link-escape-chars-browser
  6861. '((?\ . "%20")) ; 32 for the SPC char
  6862. "Association list of escapes for some characters problematic in links.
  6863. This is the list that is used before handing over to the browser.")
  6864. (defun org-link-escape (text &optional table)
  6865. "Escape characters in TEXT that are problematic for links."
  6866. (if org-url-encoding-use-url-hexify
  6867. (url-hexify-string text)
  6868. (setq table (or table org-link-escape-chars))
  6869. (when text
  6870. (let ((re (mapconcat (lambda (x) (regexp-quote
  6871. (char-to-string (car x))))
  6872. table "\\|")))
  6873. (while (string-match re text)
  6874. (setq text
  6875. (replace-match
  6876. (cdr (assoc (string-to-char (match-string 0 text))
  6877. table))
  6878. t t text)))
  6879. text))))
  6880. (defun org-link-unescape (text &optional table)
  6881. "Reverse the action of `org-link-escape'."
  6882. (if org-url-encoding-use-url-hexify
  6883. (url-unhex-string text)
  6884. (setq table (or table org-link-escape-chars))
  6885. (when text
  6886. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6887. table "\\|")))
  6888. (while (string-match re text)
  6889. (setq text
  6890. (replace-match
  6891. (char-to-string (car (rassoc (match-string 0 text) table)))
  6892. t t text)))
  6893. text))))
  6894. (defun org-xor (a b)
  6895. "Exclusive or."
  6896. (if a (not b) b))
  6897. (defun org-fixup-message-id-for-http (s)
  6898. "Replace special characters in a message id, so it can be used in an http query."
  6899. (while (string-match "<" s)
  6900. (setq s (replace-match "%3C" t t s)))
  6901. (while (string-match ">" s)
  6902. (setq s (replace-match "%3E" t t s)))
  6903. (while (string-match "@" s)
  6904. (setq s (replace-match "%40" t t s)))
  6905. s)
  6906. ;;;###autoload
  6907. (defun org-insert-link-global ()
  6908. "Insert a link like Org-mode does.
  6909. This command can be called in any mode to insert a link in Org-mode syntax."
  6910. (interactive)
  6911. (org-load-modules-maybe)
  6912. (org-run-like-in-org-mode 'org-insert-link))
  6913. (defun org-insert-link (&optional complete-file link-location)
  6914. "Insert a link. At the prompt, enter the link.
  6915. Completion can be used to insert any of the link protocol prefixes like
  6916. http or ftp in use.
  6917. The history can be used to select a link previously stored with
  6918. `org-store-link'. When the empty string is entered (i.e. if you just
  6919. press RET at the prompt), the link defaults to the most recently
  6920. stored link. As SPC triggers completion in the minibuffer, you need to
  6921. use M-SPC or C-q SPC to force the insertion of a space character.
  6922. You will also be prompted for a description, and if one is given, it will
  6923. be displayed in the buffer instead of the link.
  6924. If there is already a link at point, this command will allow you to edit link
  6925. and description parts.
  6926. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6927. be selected using completion. The path to the file will be relative to the
  6928. current directory if the file is in the current directory or a subdirectory.
  6929. Otherwise, the link will be the absolute path as completed in the minibuffer
  6930. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6931. option `org-link-file-path-type'.
  6932. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6933. the current directory or below.
  6934. With three \\[universal-argument] prefixes, negate the meaning of
  6935. `org-keep-stored-link-after-insertion'.
  6936. If `org-make-link-description-function' is non-nil, this function will be
  6937. called with the link target, and the result will be the default
  6938. link description.
  6939. If the LINK-LOCATION parameter is non-nil, this value will be
  6940. used as the link location instead of reading one interactively."
  6941. (interactive "P")
  6942. (let* ((wcf (current-window-configuration))
  6943. (region (if (org-region-active-p)
  6944. (buffer-substring (region-beginning) (region-end))))
  6945. (remove (and region (list (region-beginning) (region-end))))
  6946. (desc region)
  6947. tmphist ; byte-compile incorrectly complains about this
  6948. (link link-location)
  6949. entry file all-prefixes)
  6950. (cond
  6951. (link-location) ; specified by arg, just use it.
  6952. ((org-in-regexp org-bracket-link-regexp 1)
  6953. ;; We do have a link at point, and we are going to edit it.
  6954. (setq remove (list (match-beginning 0) (match-end 0)))
  6955. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6956. (setq link (read-string "Link: "
  6957. (org-link-unescape
  6958. (org-match-string-no-properties 1)))))
  6959. ((or (org-in-regexp org-angle-link-re)
  6960. (org-in-regexp org-plain-link-re))
  6961. ;; Convert to bracket link
  6962. (setq remove (list (match-beginning 0) (match-end 0))
  6963. link (read-string "Link: "
  6964. (org-remove-angle-brackets (match-string 0)))))
  6965. ((member complete-file '((4) (16)))
  6966. ;; Completing read for file names.
  6967. (setq link (org-file-complete-link complete-file)))
  6968. (t
  6969. ;; Read link, with completion for stored links.
  6970. (with-output-to-temp-buffer "*Org Links*"
  6971. (princ "Insert a link.
  6972. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6973. (when org-stored-links
  6974. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6975. (princ (mapconcat
  6976. (lambda (x)
  6977. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6978. (reverse org-stored-links) "\n"))))
  6979. (let ((cw (selected-window)))
  6980. (select-window (get-buffer-window "*Org Links*"))
  6981. (setq truncate-lines t)
  6982. (unless (pos-visible-in-window-p (point-max))
  6983. (org-fit-window-to-buffer))
  6984. (and (window-live-p cw) (select-window cw)))
  6985. ;; Fake a link history, containing the stored links.
  6986. (setq tmphist (append (mapcar 'car org-stored-links)
  6987. org-insert-link-history))
  6988. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6989. (mapcar 'car org-link-abbrev-alist)
  6990. org-link-types))
  6991. (unwind-protect
  6992. (progn
  6993. (setq link
  6994. (let ((org-completion-use-ido nil)
  6995. (org-completion-use-iswitchb nil))
  6996. (org-completing-read
  6997. "Link: "
  6998. (append
  6999. (mapcar (lambda (x) (list (concat x ":")))
  7000. all-prefixes)
  7001. (mapcar 'car org-stored-links))
  7002. nil nil nil
  7003. 'tmphist
  7004. (car (car org-stored-links)))))
  7005. (if (or (member link all-prefixes)
  7006. (and (equal ":" (substring link -1))
  7007. (member (substring link 0 -1) all-prefixes)
  7008. (setq link (substring link 0 -1))))
  7009. (setq link (org-link-try-special-completion link))))
  7010. (set-window-configuration wcf)
  7011. (kill-buffer "*Org Links*"))
  7012. (setq entry (assoc link org-stored-links))
  7013. (or entry (push link org-insert-link-history))
  7014. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7015. (not org-keep-stored-link-after-insertion))
  7016. (setq org-stored-links (delq (assoc link org-stored-links)
  7017. org-stored-links)))
  7018. (setq desc (or desc (nth 1 entry)))))
  7019. (if (string-match org-plain-link-re link)
  7020. ;; URL-like link, normalize the use of angular brackets.
  7021. (setq link (org-make-link (org-remove-angle-brackets link))))
  7022. ;; Check if we are linking to the current file with a search option
  7023. ;; If yes, simplify the link by using only the search option.
  7024. (when (and buffer-file-name
  7025. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7026. (let* ((path (match-string 1 link))
  7027. (case-fold-search nil)
  7028. (search (match-string 2 link)))
  7029. (save-match-data
  7030. (if (equal (file-truename buffer-file-name) (file-truename path))
  7031. ;; We are linking to this same file, with a search option
  7032. (setq link search)))))
  7033. ;; Check if we can/should use a relative path. If yes, simplify the link
  7034. (when (string-match "^file:\\(.*\\)" link)
  7035. (let* ((path (match-string 1 link))
  7036. (origpath path)
  7037. (case-fold-search nil))
  7038. (cond
  7039. ((or (eq org-link-file-path-type 'absolute)
  7040. (equal complete-file '(16)))
  7041. (setq path (abbreviate-file-name (expand-file-name path))))
  7042. ((eq org-link-file-path-type 'noabbrev)
  7043. (setq path (expand-file-name path)))
  7044. ((eq org-link-file-path-type 'relative)
  7045. (setq path (file-relative-name path)))
  7046. (t
  7047. (save-match-data
  7048. (if (string-match (concat "^" (regexp-quote
  7049. (file-name-as-directory
  7050. (expand-file-name "."))))
  7051. (expand-file-name path))
  7052. ;; We are linking a file with relative path name.
  7053. (setq path (substring (expand-file-name path)
  7054. (match-end 0)))
  7055. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7056. (setq link (concat "file:" path))
  7057. (if (equal desc origpath)
  7058. (setq desc path))))
  7059. (if org-make-link-description-function
  7060. (setq desc (funcall org-make-link-description-function link desc)))
  7061. (setq desc (read-string "Description: " desc))
  7062. (unless (string-match "\\S-" desc) (setq desc nil))
  7063. (if remove (apply 'delete-region remove))
  7064. (insert (org-make-link-string link desc))))
  7065. (defun org-link-try-special-completion (type)
  7066. "If there is completion support for link type TYPE, offer it."
  7067. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7068. (if (functionp fun)
  7069. (funcall fun)
  7070. (read-string "Link (no completion support): " (concat type ":")))))
  7071. (defun org-file-complete-link (&optional arg)
  7072. "Create a file link using completion."
  7073. (let (file link)
  7074. (setq file (read-file-name "File: "))
  7075. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7076. (pwd1 (file-name-as-directory (abbreviate-file-name
  7077. (expand-file-name ".")))))
  7078. (cond
  7079. ((equal arg '(16))
  7080. (setq link (org-make-link
  7081. "file:"
  7082. (abbreviate-file-name (expand-file-name file)))))
  7083. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7084. (setq link (org-make-link "file:" (match-string 1 file))))
  7085. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7086. (expand-file-name file))
  7087. (setq link (org-make-link
  7088. "file:" (match-string 1 (expand-file-name file)))))
  7089. (t (setq link (org-make-link "file:" file)))))
  7090. link))
  7091. (defun org-completing-read (&rest args)
  7092. "Completing-read with SPACE being a normal character."
  7093. (let ((minibuffer-local-completion-map
  7094. (copy-keymap minibuffer-local-completion-map)))
  7095. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7096. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7097. (apply 'org-icompleting-read args)))
  7098. (defun org-completing-read-no-i (&rest args)
  7099. (let (org-completion-use-ido org-completion-use-iswitchb)
  7100. (apply 'org-completing-read args)))
  7101. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7102. "Use iswitch as a completing-read replacement to choose from choices.
  7103. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7104. from."
  7105. (let* ((iswitchb-use-virtual-buffers nil)
  7106. (iswitchb-make-buflist-hook
  7107. (lambda ()
  7108. (setq iswitchb-temp-buflist choices))))
  7109. (iswitchb-read-buffer prompt)))
  7110. (defun org-icompleting-read (&rest args)
  7111. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7112. (org-without-partial-completion
  7113. (if (and org-completion-use-ido
  7114. (fboundp 'ido-completing-read)
  7115. (boundp 'ido-mode) ido-mode
  7116. (listp (second args)))
  7117. (let ((ido-enter-matching-directory nil))
  7118. (apply 'ido-completing-read (concat (car args))
  7119. (if (consp (car (nth 1 args)))
  7120. (mapcar (lambda (x) (car x)) (nth 1 args))
  7121. (nth 1 args))
  7122. (cddr args)))
  7123. (if (and org-completion-use-iswitchb
  7124. (boundp 'iswitchb-mode) iswitchb-mode
  7125. (listp (second args)))
  7126. (apply 'org-iswitchb-completing-read (concat (car args))
  7127. (if (consp (car (nth 1 args)))
  7128. (mapcar (lambda (x) (car x)) (nth 1 args))
  7129. (nth 1 args))
  7130. (cddr args))
  7131. (apply 'completing-read args)))))
  7132. (defun org-extract-attributes (s)
  7133. "Extract the attributes cookie from a string and set as text property."
  7134. (let (a attr (start 0) key value)
  7135. (save-match-data
  7136. (when (string-match "{{\\([^}]+\\)}}$" s)
  7137. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7138. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7139. (setq key (match-string 1 a) value (match-string 2 a)
  7140. start (match-end 0)
  7141. attr (plist-put attr (intern key) value))))
  7142. (org-add-props s nil 'org-attr attr))
  7143. s))
  7144. (defun org-extract-attributes-from-string (tag)
  7145. (let (key value attr)
  7146. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7147. (setq key (match-string 1 tag) value (match-string 2 tag)
  7148. tag (replace-match "" t t tag)
  7149. attr (plist-put attr (intern key) value)))
  7150. (cons tag attr)))
  7151. (defun org-attributes-to-string (plist)
  7152. "Format a property list into an HTML attribute list."
  7153. (let ((s "") key value)
  7154. (while plist
  7155. (setq key (pop plist) value (pop plist))
  7156. (and value
  7157. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7158. s))
  7159. ;;; Opening/following a link
  7160. (defvar org-link-search-failed nil)
  7161. (defun org-next-link ()
  7162. "Move forward to the next link.
  7163. If the link is in hidden text, expose it."
  7164. (interactive)
  7165. (when (and org-link-search-failed (eq this-command last-command))
  7166. (goto-char (point-min))
  7167. (message "Link search wrapped back to beginning of buffer"))
  7168. (setq org-link-search-failed nil)
  7169. (let* ((pos (point))
  7170. (ct (org-context))
  7171. (a (assoc :link ct)))
  7172. (if a (goto-char (nth 2 a)))
  7173. (if (re-search-forward org-any-link-re nil t)
  7174. (progn
  7175. (goto-char (match-beginning 0))
  7176. (if (org-invisible-p) (org-show-context)))
  7177. (goto-char pos)
  7178. (setq org-link-search-failed t)
  7179. (error "No further link found"))))
  7180. (defun org-previous-link ()
  7181. "Move backward to the previous link.
  7182. If the link is in hidden text, expose it."
  7183. (interactive)
  7184. (when (and org-link-search-failed (eq this-command last-command))
  7185. (goto-char (point-max))
  7186. (message "Link search wrapped back to end of buffer"))
  7187. (setq org-link-search-failed nil)
  7188. (let* ((pos (point))
  7189. (ct (org-context))
  7190. (a (assoc :link ct)))
  7191. (if a (goto-char (nth 1 a)))
  7192. (if (re-search-backward org-any-link-re nil t)
  7193. (progn
  7194. (goto-char (match-beginning 0))
  7195. (if (org-invisible-p) (org-show-context)))
  7196. (goto-char pos)
  7197. (setq org-link-search-failed t)
  7198. (error "No further link found"))))
  7199. (defun org-translate-link (s)
  7200. "Translate a link string if a translation function has been defined."
  7201. (if (and org-link-translation-function
  7202. (fboundp org-link-translation-function)
  7203. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7204. (progn
  7205. (setq s (funcall org-link-translation-function
  7206. (match-string 1) (match-string 2)))
  7207. (concat (car s) ":" (cdr s)))
  7208. s))
  7209. (defun org-translate-link-from-planner (type path)
  7210. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7211. This is still an experimental function, your mileage may vary."
  7212. (cond
  7213. ((member type '("http" "https" "news" "ftp"))
  7214. ;; standard Internet links are the same.
  7215. nil)
  7216. ((and (equal type "irc") (string-match "^//" path))
  7217. ;; Planner has two / at the beginning of an irc link, we have 1.
  7218. ;; We should have zero, actually....
  7219. (setq path (substring path 1)))
  7220. ((and (equal type "lisp") (string-match "^/" path))
  7221. ;; Planner has a slash, we do not.
  7222. (setq type "elisp" path (substring path 1)))
  7223. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7224. ;; A typical message link. Planner has the id after the fina slash,
  7225. ;; we separate it with a hash mark
  7226. (setq path (concat (match-string 1 path) "#"
  7227. (org-remove-angle-brackets (match-string 2 path)))))
  7228. )
  7229. (cons type path))
  7230. (defun org-find-file-at-mouse (ev)
  7231. "Open file link or URL at mouse."
  7232. (interactive "e")
  7233. (mouse-set-point ev)
  7234. (org-open-at-point 'in-emacs))
  7235. (defun org-open-at-mouse (ev)
  7236. "Open file link or URL at mouse."
  7237. (interactive "e")
  7238. (mouse-set-point ev)
  7239. (if (eq major-mode 'org-agenda-mode)
  7240. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7241. (org-open-at-point))
  7242. (defvar org-window-config-before-follow-link nil
  7243. "The window configuration before following a link.
  7244. This is saved in case the need arises to restore it.")
  7245. (defvar org-open-link-marker (make-marker)
  7246. "Marker pointing to the location where `org-open-at-point; was called.")
  7247. ;;;###autoload
  7248. (defun org-open-at-point-global ()
  7249. "Follow a link like Org-mode does.
  7250. This command can be called in any mode to follow a link that has
  7251. Org-mode syntax."
  7252. (interactive)
  7253. (org-run-like-in-org-mode 'org-open-at-point))
  7254. ;;;###autoload
  7255. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7256. "Open a link in the string S, as if it was in Org-mode."
  7257. (interactive "sLink: \nP")
  7258. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7259. (with-temp-buffer
  7260. (let ((org-inhibit-startup t))
  7261. (org-mode)
  7262. (insert s)
  7263. (goto-char (point-min))
  7264. (org-open-at-point arg reference-buffer)))))
  7265. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7266. "Open link at or after point.
  7267. If there is no link at point, this function will search forward up to
  7268. the end of the current line.
  7269. Normally, files will be opened by an appropriate application. If the
  7270. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7271. With a double prefix argument, try to open outside of Emacs, in the
  7272. application the system uses for this file type."
  7273. (interactive "P")
  7274. (org-load-modules-maybe)
  7275. (move-marker org-open-link-marker (point))
  7276. (setq org-window-config-before-follow-link (current-window-configuration))
  7277. (org-remove-occur-highlights nil nil t)
  7278. (cond
  7279. ((and (org-on-heading-p)
  7280. (not (org-in-regexp
  7281. (concat org-plain-link-re "\\|"
  7282. org-bracket-link-regexp "\\|"
  7283. org-angle-link-re "\\|"
  7284. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7285. (or (org-offer-links-in-entry in-emacs)
  7286. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7287. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7288. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7289. (org-footnote-action))
  7290. (t
  7291. (let (type path link line search (pos (point)))
  7292. (catch 'match
  7293. (save-excursion
  7294. (skip-chars-forward "^]\n\r")
  7295. (when (org-in-regexp org-bracket-link-regexp)
  7296. (setq link (org-extract-attributes
  7297. (org-link-unescape (org-match-string-no-properties 1))))
  7298. (while (string-match " *\n *" link)
  7299. (setq link (replace-match " " t t link)))
  7300. (setq link (org-link-expand-abbrev link))
  7301. (cond
  7302. ((or (file-name-absolute-p link)
  7303. (string-match "^\\.\\.?/" link))
  7304. (setq type "file" path link))
  7305. ((string-match org-link-re-with-space3 link)
  7306. (setq type (match-string 1 link) path (match-string 2 link)))
  7307. (t (setq type "thisfile" path link)))
  7308. (throw 'match t)))
  7309. (when (get-text-property (point) 'org-linked-text)
  7310. (setq type "thisfile"
  7311. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7312. (1+ (point)) (point))
  7313. path (buffer-substring
  7314. (previous-single-property-change pos 'org-linked-text)
  7315. (next-single-property-change pos 'org-linked-text)))
  7316. (throw 'match t))
  7317. (save-excursion
  7318. (when (or (org-in-regexp org-angle-link-re)
  7319. (org-in-regexp org-plain-link-re))
  7320. (setq type (match-string 1) path (match-string 2))
  7321. (throw 'match t)))
  7322. (save-excursion
  7323. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7324. (setq type "tags"
  7325. path (match-string 1))
  7326. (while (string-match ":" path)
  7327. (setq path (replace-match "+" t t path)))
  7328. (throw 'match t)))
  7329. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7330. (setq type "tree-match"
  7331. path (match-string 1))
  7332. (throw 'match t)))
  7333. (unless path
  7334. (error "No link found"))
  7335. ;; switch back to reference buffer
  7336. ;; needed when if called in a temporary buffer through
  7337. ;; org-open-link-from-string
  7338. (with-current-buffer (or reference-buffer (current-buffer))
  7339. ;; Remove any trailing spaces in path
  7340. (if (string-match " +\\'" path)
  7341. (setq path (replace-match "" t t path)))
  7342. (if (and org-link-translation-function
  7343. (fboundp org-link-translation-function))
  7344. ;; Check if we need to translate the link
  7345. (let ((tmp (funcall org-link-translation-function type path)))
  7346. (setq type (car tmp) path (cdr tmp))))
  7347. (cond
  7348. ((assoc type org-link-protocols)
  7349. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7350. ((equal type "mailto")
  7351. (let ((cmd (car org-link-mailto-program))
  7352. (args (cdr org-link-mailto-program)) args1
  7353. (address path) (subject "") a)
  7354. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7355. (setq address (match-string 1 path)
  7356. subject (org-link-escape (match-string 2 path))))
  7357. (while args
  7358. (cond
  7359. ((not (stringp (car args))) (push (pop args) args1))
  7360. (t (setq a (pop args))
  7361. (if (string-match "%a" a)
  7362. (setq a (replace-match address t t a)))
  7363. (if (string-match "%s" a)
  7364. (setq a (replace-match subject t t a)))
  7365. (push a args1))))
  7366. (apply cmd (nreverse args1))))
  7367. ((member type '("http" "https" "ftp" "news"))
  7368. (browse-url (concat type ":" (org-link-escape
  7369. path org-link-escape-chars-browser))))
  7370. ((member type '("message"))
  7371. (browse-url (concat type ":" path)))
  7372. ((string= type "tags")
  7373. (org-tags-view in-emacs path))
  7374. ((string= type "thisfile")
  7375. (if in-emacs
  7376. (switch-to-buffer-other-window
  7377. (org-get-buffer-for-internal-link (current-buffer)))
  7378. (org-mark-ring-push))
  7379. (let ((cmd `(org-link-search
  7380. ,path
  7381. ,(cond ((equal in-emacs '(4)) 'occur)
  7382. ((equal in-emacs '(16)) 'org-occur)
  7383. (t nil))
  7384. ,pos)))
  7385. (condition-case nil (eval cmd)
  7386. (error (progn (widen) (eval cmd))))))
  7387. ((string= type "tree-match")
  7388. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7389. ((string= type "file")
  7390. (if (string-match "::\\([0-9]+\\)\\'" path)
  7391. (setq line (string-to-number (match-string 1 path))
  7392. path (substring path 0 (match-beginning 0)))
  7393. (if (string-match "::\\(.+\\)\\'" path)
  7394. (setq search (match-string 1 path)
  7395. path (substring path 0 (match-beginning 0)))))
  7396. (if (string-match "[*?{]" (file-name-nondirectory path))
  7397. (dired path)
  7398. (org-open-file path in-emacs line search)))
  7399. ((string= type "news")
  7400. (require 'org-gnus)
  7401. (org-gnus-follow-link path))
  7402. ((string= type "shell")
  7403. (let ((cmd path))
  7404. (if (or (not org-confirm-shell-link-function)
  7405. (funcall org-confirm-shell-link-function
  7406. (format "Execute \"%s\" in shell? "
  7407. (org-add-props cmd nil
  7408. 'face 'org-warning))))
  7409. (progn
  7410. (message "Executing %s" cmd)
  7411. (shell-command cmd))
  7412. (error "Abort"))))
  7413. ((string= type "elisp")
  7414. (let ((cmd path))
  7415. (if (or (not org-confirm-elisp-link-function)
  7416. (funcall org-confirm-elisp-link-function
  7417. (format "Execute \"%s\" as elisp? "
  7418. (org-add-props cmd nil
  7419. 'face 'org-warning))))
  7420. (message "%s => %s" cmd
  7421. (if (equal (string-to-char cmd) ?\()
  7422. (eval (read cmd))
  7423. (call-interactively (read cmd))))
  7424. (error "Abort"))))
  7425. (t
  7426. (browse-url-at-point)))))))
  7427. (move-marker org-open-link-marker nil)
  7428. (run-hook-with-args 'org-follow-link-hook))
  7429. (defun org-offer-links-in-entry (&optional nth zero)
  7430. "Offer links in the curren entry and follow the selected link.
  7431. If there is only one link, follow it immediately as well.
  7432. If NTH is an integer, immediately pick the NTH link found.
  7433. If ZERO is a string, check also this string for a link, and if
  7434. there is one, offer it as link number zero."
  7435. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7436. "\\(" org-angle-link-re "\\)\\|"
  7437. "\\(" org-plain-link-re "\\)"))
  7438. (cnt ?0)
  7439. (in-emacs (if (integerp nth) nil nth))
  7440. have-zero end links link c)
  7441. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7442. (push (match-string 0 zero) links)
  7443. (setq cnt (1- cnt) have-zero t))
  7444. (save-excursion
  7445. (org-back-to-heading t)
  7446. (setq end (save-excursion (outline-next-heading) (point)))
  7447. (while (re-search-forward re end t)
  7448. (push (match-string 0) links))
  7449. (setq links (org-uniquify (reverse links))))
  7450. (cond
  7451. ((null links)
  7452. (message "No links"))
  7453. ((equal (length links) 1)
  7454. (setq link (car links)))
  7455. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7456. (setq link (nth (if have-zero nth (1- nth)) links)))
  7457. (t ; we have to select a link
  7458. (save-excursion
  7459. (save-window-excursion
  7460. (delete-other-windows)
  7461. (with-output-to-temp-buffer "*Select Link*"
  7462. (mapc (lambda (l)
  7463. (if (not (string-match org-bracket-link-regexp l))
  7464. (princ (format "[%c] %s\n" (incf cnt)
  7465. (org-remove-angle-brackets l)))
  7466. (if (match-end 3)
  7467. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7468. (match-string 3 l) (match-string 1 l)))
  7469. (princ (format "[%c] %s\n" (incf cnt)
  7470. (match-string 1 l))))))
  7471. links))
  7472. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7473. (message "Select link to open:")
  7474. (setq c (read-char-exclusive))
  7475. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7476. (when (equal c ?q) (error "Abort"))
  7477. (setq nth (- c ?0))
  7478. (if have-zero (setq nth (1+ nth)))
  7479. (unless (and (integerp nth) (>= (length links) nth))
  7480. (error "Invalid link selection"))
  7481. (setq link (nth (1- nth) links))))
  7482. (if link
  7483. (progn (org-open-link-from-string link in-emacs (current-buffer)) t)
  7484. nil)))
  7485. ;;;; Time estimates
  7486. (defun org-get-effort (&optional pom)
  7487. "Get the effort estimate for the current entry."
  7488. (org-entry-get pom org-effort-property))
  7489. ;;; File search
  7490. (defvar org-create-file-search-functions nil
  7491. "List of functions to construct the right search string for a file link.
  7492. These functions are called in turn with point at the location to
  7493. which the link should point.
  7494. A function in the hook should first test if it would like to
  7495. handle this file type, for example by checking the major-mode or
  7496. the file extension. If it decides not to handle this file, it
  7497. should just return nil to give other functions a chance. If it
  7498. does handle the file, it must return the search string to be used
  7499. when following the link. The search string will be part of the
  7500. file link, given after a double colon, and `org-open-at-point'
  7501. will automatically search for it. If special measures must be
  7502. taken to make the search successful, another function should be
  7503. added to the companion hook `org-execute-file-search-functions',
  7504. which see.
  7505. A function in this hook may also use `setq' to set the variable
  7506. `description' to provide a suggestion for the descriptive text to
  7507. be used for this link when it gets inserted into an Org-mode
  7508. buffer with \\[org-insert-link].")
  7509. (defvar org-execute-file-search-functions nil
  7510. "List of functions to execute a file search triggered by a link.
  7511. Functions added to this hook must accept a single argument, the
  7512. search string that was part of the file link, the part after the
  7513. double colon. The function must first check if it would like to
  7514. handle this search, for example by checking the major-mode or the
  7515. file extension. If it decides not to handle this search, it
  7516. should just return nil to give other functions a chance. If it
  7517. does handle the search, it must return a non-nil value to keep
  7518. other functions from trying.
  7519. Each function can access the current prefix argument through the
  7520. variable `current-prefix-argument'. Note that a single prefix is
  7521. used to force opening a link in Emacs, so it may be good to only
  7522. use a numeric or double prefix to guide the search function.
  7523. In case this is needed, a function in this hook can also restore
  7524. the window configuration before `org-open-at-point' was called using:
  7525. (set-window-configuration org-window-config-before-follow-link)")
  7526. (defun org-link-search (s &optional type avoid-pos)
  7527. "Search for a link search option.
  7528. If S is surrounded by forward slashes, it is interpreted as a
  7529. regular expression. In org-mode files, this will create an `org-occur'
  7530. sparse tree. In ordinary files, `occur' will be used to list matches.
  7531. If the current buffer is in `dired-mode', grep will be used to search
  7532. in all files. If AVOID-POS is given, ignore matches near that position."
  7533. (let ((case-fold-search t)
  7534. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7535. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7536. (append '(("") (" ") ("\t") ("\n"))
  7537. org-emphasis-alist)
  7538. "\\|") "\\)"))
  7539. (pos (point))
  7540. (pre nil) (post nil)
  7541. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7542. (cond
  7543. ;; First check if there are any special
  7544. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7545. ;; Now try the builtin stuff
  7546. ((and (equal (string-to-char s0) ?#)
  7547. (> (length s0) 1)
  7548. (save-excursion
  7549. (goto-char (point-min))
  7550. (and
  7551. (re-search-forward
  7552. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7553. (setq type 'dedicated
  7554. pos (match-beginning 0))))
  7555. ;; There is an exact target for this
  7556. (goto-char pos)
  7557. (org-back-to-heading t)))
  7558. ((save-excursion
  7559. (goto-char (point-min))
  7560. (and
  7561. (re-search-forward
  7562. (concat "<<" (regexp-quote s0) ">>") nil t)
  7563. (setq type 'dedicated
  7564. pos (match-beginning 0))))
  7565. ;; There is an exact target for this
  7566. (goto-char pos))
  7567. ((and (string-match "^(\\(.*\\))$" s0)
  7568. (save-excursion
  7569. (goto-char (point-min))
  7570. (and
  7571. (re-search-forward
  7572. (concat "[^[]" (regexp-quote
  7573. (format org-coderef-label-format
  7574. (match-string 1 s0))))
  7575. nil t)
  7576. (setq type 'dedicated
  7577. pos (1+ (match-beginning 0))))))
  7578. ;; There is a coderef target for this
  7579. (goto-char pos))
  7580. ((string-match "^/\\(.*\\)/$" s)
  7581. ;; A regular expression
  7582. (cond
  7583. ((org-mode-p)
  7584. (org-occur (match-string 1 s)))
  7585. ;;((eq major-mode 'dired-mode)
  7586. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7587. (t (org-do-occur (match-string 1 s)))))
  7588. (t
  7589. ;; A normal search strings
  7590. (when (equal (string-to-char s) ?*)
  7591. ;; Anchor on headlines, post may include tags.
  7592. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7593. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7594. s (substring s 1)))
  7595. (remove-text-properties
  7596. 0 (length s)
  7597. '(face nil mouse-face nil keymap nil fontified nil) s)
  7598. ;; Make a series of regular expressions to find a match
  7599. (setq words (org-split-string s "[ \n\r\t]+")
  7600. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7601. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7602. "\\)" markers)
  7603. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7604. re2a (concat "[ \t\r\n]" re2a_)
  7605. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7606. re4 (concat "[^a-zA-Z_]" re4_)
  7607. re1 (concat pre re2 post)
  7608. re3 (concat pre (if pre re4_ re4) post)
  7609. re5 (concat pre ".*" re4)
  7610. re2 (concat pre re2)
  7611. re2a (concat pre (if pre re2a_ re2a))
  7612. re4 (concat pre (if pre re4_ re4))
  7613. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7614. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7615. re5 "\\)"
  7616. ))
  7617. (cond
  7618. ((eq type 'org-occur) (org-occur reall))
  7619. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7620. (t (goto-char (point-min))
  7621. (setq type 'fuzzy)
  7622. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7623. (org-search-not-self 1 re1 nil t)
  7624. (org-search-not-self 1 re2 nil t)
  7625. (org-search-not-self 1 re2a nil t)
  7626. (org-search-not-self 1 re3 nil t)
  7627. (org-search-not-self 1 re4 nil t)
  7628. (org-search-not-self 1 re5 nil t)
  7629. )
  7630. (goto-char (match-beginning 1))
  7631. (goto-char pos)
  7632. (error "No match")))))
  7633. (t
  7634. ;; Normal string-search
  7635. (goto-char (point-min))
  7636. (if (search-forward s nil t)
  7637. (goto-char (match-beginning 0))
  7638. (error "No match"))))
  7639. (and (org-mode-p) (org-show-context 'link-search))
  7640. type))
  7641. (defun org-search-not-self (group &rest args)
  7642. "Execute `re-search-forward', but only accept matches that do not
  7643. enclose the position of `org-open-link-marker'."
  7644. (let ((m org-open-link-marker))
  7645. (catch 'exit
  7646. (while (apply 're-search-forward args)
  7647. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7648. (goto-char (match-end group))
  7649. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7650. (> (match-beginning 0) (marker-position m))
  7651. (< (match-end 0) (marker-position m)))
  7652. (save-match-data
  7653. (or (not (org-in-regexp
  7654. org-bracket-link-analytic-regexp 1))
  7655. (not (match-end 4)) ; no description
  7656. (and (<= (match-beginning 4) (point))
  7657. (>= (match-end 4) (point))))))
  7658. (throw 'exit (point))))))))
  7659. (defun org-get-buffer-for-internal-link (buffer)
  7660. "Return a buffer to be used for displaying the link target of internal links."
  7661. (cond
  7662. ((not org-display-internal-link-with-indirect-buffer)
  7663. buffer)
  7664. ((string-match "(Clone)$" (buffer-name buffer))
  7665. (message "Buffer is already a clone, not making another one")
  7666. ;; we also do not modify visibility in this case
  7667. buffer)
  7668. (t ; make a new indirect buffer for displaying the link
  7669. (let* ((bn (buffer-name buffer))
  7670. (ibn (concat bn "(Clone)"))
  7671. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7672. (with-current-buffer ib (org-overview))
  7673. ib))))
  7674. (defun org-do-occur (regexp &optional cleanup)
  7675. "Call the Emacs command `occur'.
  7676. If CLEANUP is non-nil, remove the printout of the regular expression
  7677. in the *Occur* buffer. This is useful if the regex is long and not useful
  7678. to read."
  7679. (occur regexp)
  7680. (when cleanup
  7681. (let ((cwin (selected-window)) win beg end)
  7682. (when (setq win (get-buffer-window "*Occur*"))
  7683. (select-window win))
  7684. (goto-char (point-min))
  7685. (when (re-search-forward "match[a-z]+" nil t)
  7686. (setq beg (match-end 0))
  7687. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7688. (setq end (1- (match-beginning 0)))))
  7689. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7690. (goto-char (point-min))
  7691. (select-window cwin))))
  7692. ;;; The mark ring for links jumps
  7693. (defvar org-mark-ring nil
  7694. "Mark ring for positions before jumps in Org-mode.")
  7695. (defvar org-mark-ring-last-goto nil
  7696. "Last position in the mark ring used to go back.")
  7697. ;; Fill and close the ring
  7698. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7699. (loop for i from 1 to org-mark-ring-length do
  7700. (push (make-marker) org-mark-ring))
  7701. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7702. org-mark-ring)
  7703. (defun org-mark-ring-push (&optional pos buffer)
  7704. "Put the current position or POS into the mark ring and rotate it."
  7705. (interactive)
  7706. (setq pos (or pos (point)))
  7707. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7708. (move-marker (car org-mark-ring)
  7709. (or pos (point))
  7710. (or buffer (current-buffer)))
  7711. (message "%s"
  7712. (substitute-command-keys
  7713. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7714. (defun org-mark-ring-goto (&optional n)
  7715. "Jump to the previous position in the mark ring.
  7716. With prefix arg N, jump back that many stored positions. When
  7717. called several times in succession, walk through the entire ring.
  7718. Org-mode commands jumping to a different position in the current file,
  7719. or to another Org-mode file, automatically push the old position
  7720. onto the ring."
  7721. (interactive "p")
  7722. (let (p m)
  7723. (if (eq last-command this-command)
  7724. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7725. (setq p org-mark-ring))
  7726. (setq org-mark-ring-last-goto p)
  7727. (setq m (car p))
  7728. (switch-to-buffer (marker-buffer m))
  7729. (goto-char m)
  7730. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7731. (defun org-remove-angle-brackets (s)
  7732. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7733. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7734. s)
  7735. (defun org-add-angle-brackets (s)
  7736. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7737. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7738. s)
  7739. (defun org-remove-double-quotes (s)
  7740. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7741. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7742. s)
  7743. ;;; Following specific links
  7744. (defun org-follow-timestamp-link ()
  7745. (cond
  7746. ((org-at-date-range-p t)
  7747. (let ((org-agenda-start-on-weekday)
  7748. (t1 (match-string 1))
  7749. (t2 (match-string 2)))
  7750. (setq t1 (time-to-days (org-time-string-to-time t1))
  7751. t2 (time-to-days (org-time-string-to-time t2)))
  7752. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7753. ((org-at-timestamp-p t)
  7754. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7755. (substring (match-string 1) 0 10)))
  7756. 1))
  7757. (t (error "This should not happen"))))
  7758. ;;; Following file links
  7759. (defvar org-wait nil)
  7760. (defun org-open-file (path &optional in-emacs line search)
  7761. "Open the file at PATH.
  7762. First, this expands any special file name abbreviations. Then the
  7763. configuration variable `org-file-apps' is checked if it contains an
  7764. entry for this file type, and if yes, the corresponding command is launched.
  7765. If no application is found, Emacs simply visits the file.
  7766. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7767. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7768. and o use an external application to visit the file.
  7769. Optional LINE specifies a line to go to, optional SEARCH a string to
  7770. search for. If LINE or SEARCH is given, the file will always be
  7771. opened in Emacs.
  7772. If the file does not exist, an error is thrown."
  7773. (setq in-emacs (or in-emacs line search))
  7774. (let* ((file (if (equal path "")
  7775. buffer-file-name
  7776. (substitute-in-file-name (expand-file-name path))))
  7777. (apps (append org-file-apps (org-default-apps)))
  7778. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7779. (dirp (if remp nil (file-directory-p file)))
  7780. (file (if (and dirp org-open-directory-means-index-dot-org)
  7781. (concat (file-name-as-directory file) "index.org")
  7782. file))
  7783. (a-m-a-p (assq 'auto-mode apps))
  7784. (dfile (downcase file))
  7785. (old-buffer (current-buffer))
  7786. (old-pos (point))
  7787. (old-mode major-mode)
  7788. ext cmd)
  7789. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7790. (setq ext (match-string 1 dfile))
  7791. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7792. (setq ext (match-string 1 dfile))))
  7793. (cond
  7794. ((equal in-emacs '(16))
  7795. (setq cmd (cdr (assoc 'system apps))))
  7796. (in-emacs (setq cmd 'emacs))
  7797. (t
  7798. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7799. (and dirp (cdr (assoc 'directory apps)))
  7800. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7801. 'string-match)
  7802. (cdr (assoc ext apps))
  7803. (cdr (assoc t apps))))))
  7804. (when (eq cmd 'system)
  7805. (setq cmd (cdr (assoc 'system apps))))
  7806. (when (eq cmd 'default)
  7807. (setq cmd (cdr (assoc t apps))))
  7808. (when (eq cmd 'mailcap)
  7809. (require 'mailcap)
  7810. (mailcap-parse-mailcaps)
  7811. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7812. (command (mailcap-mime-info mime-type)))
  7813. (if (stringp command)
  7814. (setq cmd command)
  7815. (setq cmd 'emacs))))
  7816. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7817. (not (file-exists-p file))
  7818. (not org-open-non-existing-files))
  7819. (error "No such file: %s" file))
  7820. (cond
  7821. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7822. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7823. (while (string-match "['\"]%s['\"]" cmd)
  7824. (setq cmd (replace-match "%s" t t cmd)))
  7825. (while (string-match "%s" cmd)
  7826. (setq cmd (replace-match
  7827. (save-match-data
  7828. (shell-quote-argument
  7829. (convert-standard-filename file)))
  7830. t t cmd)))
  7831. (save-window-excursion
  7832. (start-process-shell-command cmd nil cmd)
  7833. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7834. ))
  7835. ((or (stringp cmd)
  7836. (eq cmd 'emacs))
  7837. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7838. (widen)
  7839. (if line (org-goto-line line)
  7840. (if search (org-link-search search))))
  7841. ((consp cmd)
  7842. (let ((file (convert-standard-filename file)))
  7843. (eval cmd)))
  7844. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7845. (and (org-mode-p) (eq old-mode 'org-mode)
  7846. (or (not (equal old-buffer (current-buffer)))
  7847. (not (equal old-pos (point))))
  7848. (org-mark-ring-push old-pos old-buffer))))
  7849. (defun org-default-apps ()
  7850. "Return the default applications for this operating system."
  7851. (cond
  7852. ((eq system-type 'darwin)
  7853. org-file-apps-defaults-macosx)
  7854. ((eq system-type 'windows-nt)
  7855. org-file-apps-defaults-windowsnt)
  7856. (t org-file-apps-defaults-gnu)))
  7857. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7858. "Convert extensions to regular expressions in the cars of LIST.
  7859. Also, weed out any non-string entries, because the return value is used
  7860. only for regexp matching.
  7861. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7862. point to the symbol `emacs', indicating that the file should
  7863. be opened in Emacs."
  7864. (append
  7865. (delq nil
  7866. (mapcar (lambda (x)
  7867. (if (not (stringp (car x)))
  7868. nil
  7869. (if (string-match "\\W" (car x))
  7870. x
  7871. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7872. list))
  7873. (if add-auto-mode
  7874. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7875. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7876. (defun org-file-remote-p (file)
  7877. "Test whether FILE specifies a location on a remote system.
  7878. Return non-nil if the location is indeed remote.
  7879. For example, the filename \"/user@host:/foo\" specifies a location
  7880. on the system \"/user@host:\"."
  7881. (cond ((fboundp 'file-remote-p)
  7882. (file-remote-p file))
  7883. ((fboundp 'tramp-handle-file-remote-p)
  7884. (tramp-handle-file-remote-p file))
  7885. ((and (boundp 'ange-ftp-name-format)
  7886. (string-match (car ange-ftp-name-format) file))
  7887. t)
  7888. (t nil)))
  7889. ;;;; Refiling
  7890. (defun org-get-org-file ()
  7891. "Read a filename, with default directory `org-directory'."
  7892. (let ((default (or org-default-notes-file remember-data-file)))
  7893. (read-file-name (format "File name [%s]: " default)
  7894. (file-name-as-directory org-directory)
  7895. default)))
  7896. (defun org-notes-order-reversed-p ()
  7897. "Check if the current file should receive notes in reversed order."
  7898. (cond
  7899. ((not org-reverse-note-order) nil)
  7900. ((eq t org-reverse-note-order) t)
  7901. ((not (listp org-reverse-note-order)) nil)
  7902. (t (catch 'exit
  7903. (let ((all org-reverse-note-order)
  7904. entry)
  7905. (while (setq entry (pop all))
  7906. (if (string-match (car entry) buffer-file-name)
  7907. (throw 'exit (cdr entry))))
  7908. nil)))))
  7909. (defvar org-refile-target-table nil
  7910. "The list of refile targets, created by `org-refile'.")
  7911. (defvar org-agenda-new-buffers nil
  7912. "Buffers created to visit agenda files.")
  7913. (defun org-get-refile-targets (&optional default-buffer)
  7914. "Produce a table with refile targets."
  7915. (let ((case-fold-search nil)
  7916. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7917. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7918. targets txt re files f desc descre fast-path-p level pos0)
  7919. (message "Getting targets...")
  7920. (with-current-buffer (or default-buffer (current-buffer))
  7921. (while (setq entry (pop entries))
  7922. (setq files (car entry) desc (cdr entry))
  7923. (setq fast-path-p nil)
  7924. (cond
  7925. ((null files) (setq files (list (current-buffer))))
  7926. ((eq files 'org-agenda-files)
  7927. (setq files (org-agenda-files 'unrestricted)))
  7928. ((and (symbolp files) (fboundp files))
  7929. (setq files (funcall files)))
  7930. ((and (symbolp files) (boundp files))
  7931. (setq files (symbol-value files))))
  7932. (if (stringp files) (setq files (list files)))
  7933. (cond
  7934. ((eq (car desc) :tag)
  7935. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7936. ((eq (car desc) :todo)
  7937. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7938. ((eq (car desc) :regexp)
  7939. (setq descre (cdr desc)))
  7940. ((eq (car desc) :level)
  7941. (setq descre (concat "^\\*\\{" (number-to-string
  7942. (if org-odd-levels-only
  7943. (1- (* 2 (cdr desc)))
  7944. (cdr desc)))
  7945. "\\}[ \t]")))
  7946. ((eq (car desc) :maxlevel)
  7947. (setq fast-path-p t)
  7948. (setq descre (concat "^\\*\\{1," (number-to-string
  7949. (if org-odd-levels-only
  7950. (1- (* 2 (cdr desc)))
  7951. (cdr desc)))
  7952. "\\}[ \t]")))
  7953. (t (error "Bad refiling target description %s" desc)))
  7954. (while (setq f (pop files))
  7955. (save-excursion
  7956. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7957. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7958. (setq f (expand-file-name f))
  7959. (if (eq org-refile-use-outline-path 'file)
  7960. (push (list (file-name-nondirectory f) f nil nil) targets))
  7961. (save-excursion
  7962. (save-restriction
  7963. (widen)
  7964. (goto-char (point-min))
  7965. (while (re-search-forward descre nil t)
  7966. (goto-char (setq pos0 (point-at-bol)))
  7967. (catch 'next
  7968. (when org-refile-target-verify-function
  7969. (save-match-data
  7970. (or (funcall org-refile-target-verify-function)
  7971. (throw 'next t))))
  7972. (when (looking-at org-complex-heading-regexp)
  7973. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7974. txt (org-link-display-format (match-string 4))
  7975. re (concat "^" (regexp-quote
  7976. (buffer-substring (match-beginning 1)
  7977. (match-end 4)))))
  7978. (if (match-end 5) (setq re (concat re "[ \t]+"
  7979. (regexp-quote
  7980. (match-string 5)))))
  7981. (setq re (concat re "[ \t]*$"))
  7982. (when org-refile-use-outline-path
  7983. (setq txt (mapconcat 'org-protect-slash
  7984. (append
  7985. (if (eq org-refile-use-outline-path 'file)
  7986. (list (file-name-nondirectory
  7987. (buffer-file-name (buffer-base-buffer))))
  7988. (if (eq org-refile-use-outline-path 'full-file-path)
  7989. (list (buffer-file-name (buffer-base-buffer)))))
  7990. (org-get-outline-path fast-path-p level txt)
  7991. (list txt))
  7992. "/")))
  7993. (push (list txt f re (point)) targets)))
  7994. (when (= (point) pos0)
  7995. ;; verification function has not moved point
  7996. (goto-char (point-at-eol))))))))))
  7997. (message "Getting targets...done")
  7998. (nreverse targets)))
  7999. (defun org-protect-slash (s)
  8000. (while (string-match "/" s)
  8001. (setq s (replace-match "\\" t t s)))
  8002. s)
  8003. (defvar org-olpa (make-vector 20 nil))
  8004. (defun org-get-outline-path (&optional fastp level heading)
  8005. "Return the outline path to the current entry, as a list."
  8006. (if fastp
  8007. (progn
  8008. (if (> level 19)
  8009. (error "Outline path failure, more than 19 levels."))
  8010. (loop for i from level upto 19 do
  8011. (aset org-olpa i nil))
  8012. (prog1
  8013. (delq nil (append org-olpa nil))
  8014. (aset org-olpa level heading)))
  8015. (let (rtn)
  8016. (save-excursion
  8017. (while (org-up-heading-safe)
  8018. (when (looking-at org-complex-heading-regexp)
  8019. (push (org-match-string-no-properties 4) rtn)))
  8020. rtn))))
  8021. (defvar org-refile-history nil
  8022. "History for refiling operations.")
  8023. (defvar org-after-refile-insert-hook nil
  8024. "Hook run after `org-refile' has inserted its stuff at the new location.
  8025. Note that this is still *before* the stuff will be removed from
  8026. the *old* location.")
  8027. (defun org-refile (&optional goto default-buffer rfloc)
  8028. "Move the entry at point to another heading.
  8029. The list of target headings is compiled using the information in
  8030. `org-refile-targets', which see. This list is created before each use
  8031. and will therefore always be up-to-date.
  8032. At the target location, the entry is filed as a subitem of the target heading.
  8033. Depending on `org-reverse-note-order', the new subitem will either be the
  8034. first or the last subitem.
  8035. If there is an active region, all entries in that region will be moved.
  8036. However, the region must fulfil the requirement that the first heading
  8037. is the first one sets the top-level of the moved text - at most siblings
  8038. below it are allowed.
  8039. With prefix arg GOTO, the command will only visit the target location,
  8040. not actually move anything.
  8041. With a double prefix `C-u C-u', go to the location where the last refiling
  8042. operation has put the subtree.
  8043. With a prefix argument of `2', refile to the running clock.
  8044. RFLOC can be a refile location obtained in a different way.
  8045. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8046. (interactive "P")
  8047. (let* ((cbuf (current-buffer))
  8048. (regionp (org-region-active-p))
  8049. (region-start (and regionp (region-beginning)))
  8050. (region-end (and regionp (region-end)))
  8051. (region-length (and regionp (- region-end region-start)))
  8052. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8053. pos it nbuf file re level reversed)
  8054. (when regionp
  8055. (goto-char region-start)
  8056. (or (bolp) (goto-char (point-at-bol)))
  8057. (setq region-start (point))
  8058. (unless (org-kill-is-subtree-p
  8059. (buffer-substring region-start region-end))
  8060. (error "The region is not a (sequence of) subtree(s)")))
  8061. (if (equal goto '(16))
  8062. (org-refile-goto-last-stored)
  8063. (when (or
  8064. (and org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8065. (prog1
  8066. (setq it (list (or org-clock-heading "running clock")
  8067. (buffer-file-name
  8068. (marker-buffer org-clock-hd-marker))
  8069. ""
  8070. (marker-position org-clock-hd-marker)))
  8071. (setq goto nil)))
  8072. (setq it (or rfloc
  8073. (save-excursion
  8074. (org-refile-get-location
  8075. (if goto "Goto: " "Refile to: ") default-buffer
  8076. org-refile-allow-creating-parent-nodes)))))
  8077. (setq file (nth 1 it)
  8078. re (nth 2 it)
  8079. pos (nth 3 it))
  8080. (if (and (not goto)
  8081. pos
  8082. (equal (buffer-file-name) file)
  8083. (if regionp
  8084. (and (>= pos region-start)
  8085. (<= pos region-end))
  8086. (and (>= pos (point))
  8087. (< pos (save-excursion
  8088. (org-end-of-subtree t t))))))
  8089. (error "Cannot refile to position inside the tree or region"))
  8090. (setq nbuf (or (find-buffer-visiting file)
  8091. (find-file-noselect file)))
  8092. (if goto
  8093. (progn
  8094. (switch-to-buffer nbuf)
  8095. (goto-char pos)
  8096. (org-show-context 'org-goto))
  8097. (if regionp
  8098. (progn
  8099. (org-kill-new (buffer-substring region-start region-end))
  8100. (org-save-markers-in-region region-start region-end))
  8101. (org-copy-subtree 1 nil t))
  8102. (save-excursion
  8103. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  8104. (find-file-noselect file))))
  8105. (setq reversed (org-notes-order-reversed-p))
  8106. (save-excursion
  8107. (save-restriction
  8108. (widen)
  8109. (if pos
  8110. (progn
  8111. (goto-char pos)
  8112. (looking-at outline-regexp)
  8113. (setq level (org-get-valid-level (funcall outline-level) 1))
  8114. (goto-char
  8115. (if reversed
  8116. (or (outline-next-heading) (point-max))
  8117. (or (save-excursion (org-get-next-sibling))
  8118. (org-end-of-subtree t t)
  8119. (point-max)))))
  8120. (setq level 1)
  8121. (if (not reversed)
  8122. (goto-char (point-max))
  8123. (goto-char (point-min))
  8124. (or (outline-next-heading) (goto-char (point-max)))))
  8125. (if (not (bolp)) (newline))
  8126. (bookmark-set "org-refile-last-stored")
  8127. (org-paste-subtree level)
  8128. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8129. (run-hooks 'org-after-refile-insert-hook))))
  8130. (if regionp
  8131. (delete-region (point) (+ (point) region-length))
  8132. (org-cut-subtree))
  8133. (when (featurep 'org-inlinetask)
  8134. (org-inlinetask-remove-END-maybe))
  8135. (setq org-markers-to-move nil)
  8136. (message "Refiled to \"%s\"" (car it))))))
  8137. (org-reveal))
  8138. (defun org-refile-goto-last-stored ()
  8139. "Go to the location where the last refile was stored."
  8140. (interactive)
  8141. (bookmark-jump "org-refile-last-stored")
  8142. (message "This is the location of the last refile"))
  8143. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8144. "Prompt the user for a refile location, using PROMPT."
  8145. (let ((org-refile-targets org-refile-targets)
  8146. (org-refile-use-outline-path org-refile-use-outline-path))
  8147. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8148. (unless org-refile-target-table
  8149. (error "No refile targets"))
  8150. (let* ((cbuf (current-buffer))
  8151. (partial-completion-mode nil)
  8152. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8153. (cfunc (if (and org-refile-use-outline-path
  8154. org-outline-path-complete-in-steps)
  8155. 'org-olpath-completing-read
  8156. 'org-icompleting-read))
  8157. (extra (if org-refile-use-outline-path "/" ""))
  8158. (filename (and cfn (expand-file-name cfn)))
  8159. (tbl (mapcar
  8160. (lambda (x)
  8161. (if (and (not (member org-refile-use-outline-path
  8162. '(file full-file-path)))
  8163. (not (equal filename (nth 1 x))))
  8164. (cons (concat (car x) extra " ("
  8165. (file-name-nondirectory (nth 1 x)) ")")
  8166. (cdr x))
  8167. (cons (concat (car x) extra) (cdr x))))
  8168. org-refile-target-table))
  8169. (completion-ignore-case t)
  8170. pa answ parent-target child parent old-hist)
  8171. (setq old-hist org-refile-history)
  8172. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8173. nil 'org-refile-history))
  8174. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8175. (if pa
  8176. (progn
  8177. (when (or (not org-refile-history)
  8178. (not (eq old-hist org-refile-history))
  8179. (not (equal (car pa) (car org-refile-history))))
  8180. (setq org-refile-history
  8181. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8182. org-refile-history
  8183. (cdr org-refile-history))))
  8184. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8185. (pop org-refile-history)))
  8186. pa)
  8187. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8188. (setq parent (match-string 1 answ)
  8189. child (match-string 2 answ))
  8190. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8191. (when (and parent-target
  8192. (or (eq new-nodes t)
  8193. (and (eq new-nodes 'confirm)
  8194. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8195. (org-refile-new-child parent-target child))))))
  8196. (defun org-refile-new-child (parent-target child)
  8197. "Use refile target PARENT-TARGET to add new CHILD below it."
  8198. (unless parent-target
  8199. (error "Cannot find parent for new node"))
  8200. (let ((file (nth 1 parent-target))
  8201. (pos (nth 3 parent-target))
  8202. level)
  8203. (with-current-buffer (or (find-buffer-visiting file)
  8204. (find-file-noselect file))
  8205. (save-excursion
  8206. (save-restriction
  8207. (widen)
  8208. (if pos
  8209. (goto-char pos)
  8210. (goto-char (point-max))
  8211. (if (not (bolp)) (newline)))
  8212. (when (looking-at outline-regexp)
  8213. (setq level (funcall outline-level))
  8214. (org-end-of-subtree t t))
  8215. (org-back-over-empty-lines)
  8216. (insert "\n" (make-string
  8217. (if pos (org-get-valid-level level 1) 1) ?*)
  8218. " " child "\n")
  8219. (beginning-of-line 0)
  8220. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8221. (defun org-olpath-completing-read (prompt collection &rest args)
  8222. "Read an outline path like a file name."
  8223. (let ((thetable collection)
  8224. (org-completion-use-ido nil) ; does not work with ido.
  8225. (org-completion-use-iswitchb nil)) ; or iswitchb
  8226. (apply
  8227. 'org-icompleting-read prompt
  8228. (lambda (string predicate &optional flag)
  8229. (let (rtn r f (l (length string)))
  8230. (cond
  8231. ((eq flag nil)
  8232. ;; try completion
  8233. (try-completion string thetable))
  8234. ((eq flag t)
  8235. ;; all-completions
  8236. (setq rtn (all-completions string thetable predicate))
  8237. (mapcar
  8238. (lambda (x)
  8239. (setq r (substring x l))
  8240. (if (string-match " ([^)]*)$" x)
  8241. (setq f (match-string 0 x))
  8242. (setq f ""))
  8243. (if (string-match "/" r)
  8244. (concat string (substring r 0 (match-end 0)) f)
  8245. x))
  8246. rtn))
  8247. ((eq flag 'lambda)
  8248. ;; exact match?
  8249. (assoc string thetable)))
  8250. ))
  8251. args)))
  8252. ;;;; Dynamic blocks
  8253. (defun org-find-dblock (name)
  8254. "Find the first dynamic block with name NAME in the buffer.
  8255. If not found, stay at current position and return nil."
  8256. (let (pos)
  8257. (save-excursion
  8258. (goto-char (point-min))
  8259. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8260. nil t)
  8261. (match-beginning 0))))
  8262. (if pos (goto-char pos))
  8263. pos))
  8264. (defconst org-dblock-start-re
  8265. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8266. "Matches the startline of a dynamic block, with parameters.")
  8267. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8268. "Matches the end of a dynamic block.")
  8269. (defun org-create-dblock (plist)
  8270. "Create a dynamic block section, with parameters taken from PLIST.
  8271. PLIST must contain a :name entry which is used as name of the block."
  8272. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8273. (end-of-line 1)
  8274. (newline))
  8275. (let ((col (current-column))
  8276. (name (plist-get plist :name)))
  8277. (insert "#+BEGIN: " name)
  8278. (while plist
  8279. (if (eq (car plist) :name)
  8280. (setq plist (cddr plist))
  8281. (insert " " (prin1-to-string (pop plist)))))
  8282. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8283. (beginning-of-line -2)))
  8284. (defun org-prepare-dblock ()
  8285. "Prepare dynamic block for refresh.
  8286. This empties the block, puts the cursor at the insert position and returns
  8287. the property list including an extra property :name with the block name."
  8288. (unless (looking-at org-dblock-start-re)
  8289. (error "Not at a dynamic block"))
  8290. (let* ((begdel (1+ (match-end 0)))
  8291. (name (org-no-properties (match-string 1)))
  8292. (params (append (list :name name)
  8293. (read (concat "(" (match-string 3) ")")))))
  8294. (save-excursion
  8295. (beginning-of-line 1)
  8296. (skip-chars-forward " \t")
  8297. (setq params (plist-put params :indentation-column (current-column))))
  8298. (unless (re-search-forward org-dblock-end-re nil t)
  8299. (error "Dynamic block not terminated"))
  8300. (setq params
  8301. (append params
  8302. (list :content (buffer-substring
  8303. begdel (match-beginning 0)))))
  8304. (delete-region begdel (match-beginning 0))
  8305. (goto-char begdel)
  8306. (open-line 1)
  8307. params))
  8308. (defun org-map-dblocks (&optional command)
  8309. "Apply COMMAND to all dynamic blocks in the current buffer.
  8310. If COMMAND is not given, use `org-update-dblock'."
  8311. (let ((cmd (or command 'org-update-dblock))
  8312. pos)
  8313. (save-excursion
  8314. (goto-char (point-min))
  8315. (while (re-search-forward org-dblock-start-re nil t)
  8316. (goto-char (setq pos (match-beginning 0)))
  8317. (condition-case nil
  8318. (funcall cmd)
  8319. (error (message "Error during update of dynamic block")))
  8320. (goto-char pos)
  8321. (unless (re-search-forward org-dblock-end-re nil t)
  8322. (error "Dynamic block not terminated"))))))
  8323. (defun org-dblock-update (&optional arg)
  8324. "User command for updating dynamic blocks.
  8325. Update the dynamic block at point. With prefix ARG, update all dynamic
  8326. blocks in the buffer."
  8327. (interactive "P")
  8328. (if arg
  8329. (org-update-all-dblocks)
  8330. (or (looking-at org-dblock-start-re)
  8331. (org-beginning-of-dblock))
  8332. (org-update-dblock)))
  8333. (defun org-update-dblock ()
  8334. "Update the dynamic block at point
  8335. This means to empty the block, parse for parameters and then call
  8336. the correct writing function."
  8337. (save-window-excursion
  8338. (let* ((pos (point))
  8339. (line (org-current-line))
  8340. (params (org-prepare-dblock))
  8341. (name (plist-get params :name))
  8342. (indent (plist-get params :indentation-column))
  8343. (cmd (intern (concat "org-dblock-write:" name))))
  8344. (message "Updating dynamic block `%s' at line %d..." name line)
  8345. (funcall cmd params)
  8346. (message "Updating dynamic block `%s' at line %d...done" name line)
  8347. (goto-char pos)
  8348. (when (and indent (> indent 0))
  8349. (setq indent (make-string indent ?\ ))
  8350. (save-excursion
  8351. (org-beginning-of-dblock)
  8352. (forward-line 1)
  8353. (while (not (looking-at org-dblock-end-re))
  8354. (insert indent)
  8355. (beginning-of-line 2))
  8356. (when (looking-at org-dblock-end-re)
  8357. (and (looking-at "[ \t]+")
  8358. (replace-match ""))
  8359. (insert indent)))))))
  8360. (defun org-beginning-of-dblock ()
  8361. "Find the beginning of the dynamic block at point.
  8362. Error if there is no such block at point."
  8363. (let ((pos (point))
  8364. beg)
  8365. (end-of-line 1)
  8366. (if (and (re-search-backward org-dblock-start-re nil t)
  8367. (setq beg (match-beginning 0))
  8368. (re-search-forward org-dblock-end-re nil t)
  8369. (> (match-end 0) pos))
  8370. (goto-char beg)
  8371. (goto-char pos)
  8372. (error "Not in a dynamic block"))))
  8373. (defun org-update-all-dblocks ()
  8374. "Update all dynamic blocks in the buffer.
  8375. This function can be used in a hook."
  8376. (when (org-mode-p)
  8377. (org-map-dblocks 'org-update-dblock)))
  8378. ;;;; Completion
  8379. (defconst org-additional-option-like-keywords
  8380. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8381. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8382. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
  8383. "BEGIN:" "END:"
  8384. "ORGTBL" "TBLFM:" "TBLNAME:"
  8385. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8386. "BEGIN_QUOTE" "END_QUOTE"
  8387. "BEGIN_VERSE" "END_VERSE"
  8388. "BEGIN_CENTER" "END_CENTER"
  8389. "BEGIN_SRC" "END_SRC"
  8390. "CATEGORY" "COLUMNS"
  8391. "CAPTION" "LABEL"
  8392. "SETUPFILE"
  8393. "BIND"
  8394. "MACRO"))
  8395. (defcustom org-structure-template-alist
  8396. '(
  8397. ("s" "#+begin_src ?\n\n#+end_src"
  8398. "<src lang=\"?\">\n\n</src>")
  8399. ("e" "#+begin_example\n?\n#+end_example"
  8400. "<example>\n?\n</example>")
  8401. ("q" "#+begin_quote\n?\n#+end_quote"
  8402. "<quote>\n?\n</quote>")
  8403. ("v" "#+begin_verse\n?\n#+end_verse"
  8404. "<verse>\n?\n/verse>")
  8405. ("c" "#+begin_center\n?\n#+end_center"
  8406. "<center>\n?\n/center>")
  8407. ("l" "#+begin_latex\n?\n#+end_latex"
  8408. "<literal style=\"latex\">\n?\n</literal>")
  8409. ("L" "#+latex: "
  8410. "<literal style=\"latex\">?</literal>")
  8411. ("h" "#+begin_html\n?\n#+end_html"
  8412. "<literal style=\"html\">\n?\n</literal>")
  8413. ("H" "#+html: "
  8414. "<literal style=\"html\">?</literal>")
  8415. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8416. ("A" "#+ascii: ")
  8417. ("i" "#+include %file ?"
  8418. "<include file=%file markup=\"?\">")
  8419. )
  8420. "Structure completion elements.
  8421. This is a list of abbreviation keys and values. The value gets inserted
  8422. it you type @samp{.} followed by the key and then the completion key,
  8423. usually `M-TAB'. %file will be replaced by a file name after prompting
  8424. for the file using completion.
  8425. There are two templates for each key, the first uses the original Org syntax,
  8426. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8427. the default when the /org-mtags.el/ module has been loaded. See also the
  8428. variable `org-mtags-prefer-muse-templates'.
  8429. This is an experimental feature, it is undecided if it is going to stay in."
  8430. :group 'org-completion
  8431. :type '(repeat
  8432. (string :tag "Key")
  8433. (string :tag "Template")
  8434. (string :tag "Muse Template")))
  8435. (defun org-try-structure-completion ()
  8436. "Try to complete a structure template before point.
  8437. This looks for strings like \"<e\" on an otherwise empty line and
  8438. expands them."
  8439. (let ((l (buffer-substring (point-at-bol) (point)))
  8440. a)
  8441. (when (and (looking-at "[ \t]*$")
  8442. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8443. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8444. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8445. (match-beginning 1)) a)
  8446. t)))
  8447. (defun org-complete-expand-structure-template (start cell)
  8448. "Expand a structure template."
  8449. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8450. (rpl (nth (if musep 2 1) cell))
  8451. (ind ""))
  8452. (delete-region start (point))
  8453. (when (string-match "\\`#\\+" rpl)
  8454. (cond
  8455. ((bolp))
  8456. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8457. (setq ind (buffer-substring (point-at-bol) (point))))
  8458. (t (newline))))
  8459. (setq start (point))
  8460. (if (string-match "%file" rpl)
  8461. (setq rpl (replace-match
  8462. (concat
  8463. "\""
  8464. (save-match-data
  8465. (abbreviate-file-name (read-file-name "Include file: ")))
  8466. "\"")
  8467. t t rpl)))
  8468. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8469. (concat "\n" ind)))
  8470. (insert rpl)
  8471. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8472. (defun org-complete (&optional arg)
  8473. "Perform completion on word at point.
  8474. At the beginning of a headline, this completes TODO keywords as given in
  8475. `org-todo-keywords'.
  8476. If the current word is preceded by a backslash, completes the TeX symbols
  8477. that are supported for HTML support.
  8478. If the current word is preceded by \"#+\", completes special words for
  8479. setting file options.
  8480. In the line after \"#+STARTUP:, complete valid keywords.\"
  8481. At all other locations, this simply calls the value of
  8482. `org-completion-fallback-command'."
  8483. (interactive "P")
  8484. (org-without-partial-completion
  8485. (catch 'exit
  8486. (let* ((a nil)
  8487. (end (point))
  8488. (beg1 (save-excursion
  8489. (skip-chars-backward (org-re "[:alnum:]_@"))
  8490. (point)))
  8491. (beg (save-excursion
  8492. (skip-chars-backward "a-zA-Z0-9_:$")
  8493. (point)))
  8494. (confirm (lambda (x) (stringp (car x))))
  8495. (searchhead (equal (char-before beg) ?*))
  8496. (struct
  8497. (when (and (member (char-before beg1) '(?. ?<))
  8498. (setq a (assoc (buffer-substring beg1 (point))
  8499. org-structure-template-alist)))
  8500. (org-complete-expand-structure-template (1- beg1) a)
  8501. (throw 'exit t)))
  8502. (tag (and (equal (char-before beg1) ?:)
  8503. (equal (char-after (point-at-bol)) ?*)))
  8504. (prop (and (equal (char-before beg1) ?:)
  8505. (not (equal (char-after (point-at-bol)) ?*))))
  8506. (texp (equal (char-before beg) ?\\))
  8507. (link (equal (char-before beg) ?\[))
  8508. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8509. beg)
  8510. "#+"))
  8511. (startup (string-match "^#\\+STARTUP:.*"
  8512. (buffer-substring (point-at-bol) (point))))
  8513. (completion-ignore-case opt)
  8514. (type nil)
  8515. (tbl nil)
  8516. (table (cond
  8517. (opt
  8518. (setq type :opt)
  8519. (require 'org-exp)
  8520. (append
  8521. (delq nil
  8522. (mapcar
  8523. (lambda (x)
  8524. (if (string-match
  8525. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8526. (cons (match-string 2 x)
  8527. (match-string 1 x))))
  8528. (org-split-string (org-get-current-options) "\n")))
  8529. (mapcar 'list org-additional-option-like-keywords)))
  8530. (startup
  8531. (setq type :startup)
  8532. org-startup-options)
  8533. (link (append org-link-abbrev-alist-local
  8534. org-link-abbrev-alist))
  8535. (texp
  8536. (setq type :tex)
  8537. org-html-entities)
  8538. ((string-match "\\`\\*+[ \t]+\\'"
  8539. (buffer-substring (point-at-bol) beg))
  8540. (setq type :todo)
  8541. (mapcar 'list org-todo-keywords-1))
  8542. (searchhead
  8543. (setq type :searchhead)
  8544. (save-excursion
  8545. (goto-char (point-min))
  8546. (while (re-search-forward org-todo-line-regexp nil t)
  8547. (push (list
  8548. (org-make-org-heading-search-string
  8549. (match-string 3) t))
  8550. tbl)))
  8551. tbl)
  8552. (tag (setq type :tag beg beg1)
  8553. (or org-tag-alist (org-get-buffer-tags)))
  8554. (prop (setq type :prop beg beg1)
  8555. (mapcar 'list (org-buffer-property-keys nil t t)))
  8556. (t (progn
  8557. (call-interactively org-completion-fallback-command)
  8558. (throw 'exit nil)))))
  8559. (pattern (buffer-substring-no-properties beg end))
  8560. (completion (try-completion pattern table confirm)))
  8561. (cond ((eq completion t)
  8562. (if (not (assoc (upcase pattern) table))
  8563. (message "Already complete")
  8564. (if (and (equal type :opt)
  8565. (not (member (car (assoc (upcase pattern) table))
  8566. org-additional-option-like-keywords)))
  8567. (insert (substring (cdr (assoc (upcase pattern) table))
  8568. (length pattern)))
  8569. (if (memq type '(:tag :prop)) (insert ":")))))
  8570. ((null completion)
  8571. (message "Can't find completion for \"%s\"" pattern)
  8572. (ding))
  8573. ((not (string= pattern completion))
  8574. (delete-region beg end)
  8575. (if (string-match " +$" completion)
  8576. (setq completion (replace-match "" t t completion)))
  8577. (insert completion)
  8578. (if (get-buffer-window "*Completions*")
  8579. (delete-window (get-buffer-window "*Completions*")))
  8580. (if (assoc completion table)
  8581. (if (eq type :todo) (insert " ")
  8582. (if (memq type '(:tag :prop)) (insert ":"))))
  8583. (if (and (equal type :opt) (assoc completion table))
  8584. (message "%s" (substitute-command-keys
  8585. "Press \\[org-complete] again to insert example settings"))))
  8586. (t
  8587. (message "Making completion list...")
  8588. (let ((list (sort (all-completions pattern table confirm)
  8589. 'string<)))
  8590. (with-output-to-temp-buffer "*Completions*"
  8591. (condition-case nil
  8592. ;; Protection needed for XEmacs and emacs 21
  8593. (display-completion-list list pattern)
  8594. (error (display-completion-list list)))))
  8595. (message "Making completion list...%s" "done")))))))
  8596. ;;;; TODO, DEADLINE, Comments
  8597. (defun org-toggle-comment ()
  8598. "Change the COMMENT state of an entry."
  8599. (interactive)
  8600. (save-excursion
  8601. (org-back-to-heading)
  8602. (let (case-fold-search)
  8603. (if (looking-at (concat outline-regexp
  8604. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8605. (replace-match "" t t nil 1)
  8606. (if (looking-at outline-regexp)
  8607. (progn
  8608. (goto-char (match-end 0))
  8609. (insert org-comment-string " ")))))))
  8610. (defvar org-last-todo-state-is-todo nil
  8611. "This is non-nil when the last TODO state change led to a TODO state.
  8612. If the last change removed the TODO tag or switched to DONE, then
  8613. this is nil.")
  8614. (defvar org-setting-tags nil) ; dynamically skipped
  8615. (defun org-parse-local-options (string var)
  8616. "Parse STRING for startup setting relevant for variable VAR."
  8617. (let ((rtn (symbol-value var))
  8618. e opts)
  8619. (save-match-data
  8620. (if (or (not string) (not (string-match "\\S-" string)))
  8621. rtn
  8622. (setq opts (delq nil (mapcar (lambda (x)
  8623. (setq e (assoc x org-startup-options))
  8624. (if (eq (nth 1 e) var) e nil))
  8625. (org-split-string string "[ \t]+"))))
  8626. (if (not opts)
  8627. rtn
  8628. (setq rtn nil)
  8629. (while (setq e (pop opts))
  8630. (if (not (nth 3 e))
  8631. (setq rtn (nth 2 e))
  8632. (if (not (listp rtn)) (setq rtn nil))
  8633. (push (nth 2 e) rtn)))
  8634. rtn)))))
  8635. (defvar org-todo-setup-filter-hook nil
  8636. "Hook for functions that pre-filter todo specs.
  8637. Each function takes a todo spec and returns either `nil' or the spec
  8638. transformed into canonical form." )
  8639. (defvar org-todo-get-default-hook nil
  8640. "Hook for functions that get a default item for todo.
  8641. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8642. `nil' or a string to be used for the todo mark." )
  8643. (defvar org-agenda-headline-snapshot-before-repeat)
  8644. (defun org-todo (&optional arg)
  8645. "Change the TODO state of an item.
  8646. The state of an item is given by a keyword at the start of the heading,
  8647. like
  8648. *** TODO Write paper
  8649. *** DONE Call mom
  8650. The different keywords are specified in the variable `org-todo-keywords'.
  8651. By default the available states are \"TODO\" and \"DONE\".
  8652. So for this example: when the item starts with TODO, it is changed to DONE.
  8653. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8654. DONE are present, add TODO at the beginning of the heading.
  8655. With C-u prefix arg, use completion to determine the new state.
  8656. With numeric prefix arg, switch to that state.
  8657. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8658. With a tripple C-u prefix, circumvent any state blocking.
  8659. For calling through lisp, arg is also interpreted in the following way:
  8660. 'none -> empty state
  8661. \"\"(empty string) -> switch to empty state
  8662. 'done -> switch to DONE
  8663. 'nextset -> switch to the next set of keywords
  8664. 'previousset -> switch to the previous set of keywords
  8665. \"WAITING\" -> switch to the specified keyword, but only if it
  8666. really is a member of `org-todo-keywords'."
  8667. (interactive "P")
  8668. (if (equal arg '(16)) (setq arg 'nextset))
  8669. (let ((org-blocker-hook org-blocker-hook)
  8670. (case-fold-search nil))
  8671. (when (equal arg '(64))
  8672. (setq arg nil org-blocker-hook nil))
  8673. (when (and org-blocker-hook
  8674. (or org-inhibit-blocking
  8675. (org-entry-get nil "NOBLOCKING")))
  8676. (setq org-blocker-hook nil))
  8677. (save-excursion
  8678. (catch 'exit
  8679. (org-back-to-heading t)
  8680. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8681. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8682. (looking-at " *"))
  8683. (let* ((match-data (match-data))
  8684. (startpos (point-at-bol))
  8685. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8686. (org-log-done org-log-done)
  8687. (org-log-repeat org-log-repeat)
  8688. (org-todo-log-states org-todo-log-states)
  8689. (this (match-string 1))
  8690. (hl-pos (match-beginning 0))
  8691. (head (org-get-todo-sequence-head this))
  8692. (ass (assoc head org-todo-kwd-alist))
  8693. (interpret (nth 1 ass))
  8694. (done-word (nth 3 ass))
  8695. (final-done-word (nth 4 ass))
  8696. (last-state (or this ""))
  8697. (completion-ignore-case t)
  8698. (member (member this org-todo-keywords-1))
  8699. (tail (cdr member))
  8700. (state (cond
  8701. ((and org-todo-key-trigger
  8702. (or (and (equal arg '(4))
  8703. (eq org-use-fast-todo-selection 'prefix))
  8704. (and (not arg) org-use-fast-todo-selection
  8705. (not (eq org-use-fast-todo-selection
  8706. 'prefix)))))
  8707. ;; Use fast selection
  8708. (org-fast-todo-selection))
  8709. ((and (equal arg '(4))
  8710. (or (not org-use-fast-todo-selection)
  8711. (not org-todo-key-trigger)))
  8712. ;; Read a state with completion
  8713. (org-icompleting-read
  8714. "State: " (mapcar (lambda(x) (list x))
  8715. org-todo-keywords-1)
  8716. nil t))
  8717. ((eq arg 'right)
  8718. (if this
  8719. (if tail (car tail) nil)
  8720. (car org-todo-keywords-1)))
  8721. ((eq arg 'left)
  8722. (if (equal member org-todo-keywords-1)
  8723. nil
  8724. (if this
  8725. (nth (- (length org-todo-keywords-1)
  8726. (length tail) 2)
  8727. org-todo-keywords-1)
  8728. (org-last org-todo-keywords-1))))
  8729. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8730. (setq arg nil))) ; hack to fall back to cycling
  8731. (arg
  8732. ;; user or caller requests a specific state
  8733. (cond
  8734. ((equal arg "") nil)
  8735. ((eq arg 'none) nil)
  8736. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8737. ((eq arg 'nextset)
  8738. (or (car (cdr (member head org-todo-heads)))
  8739. (car org-todo-heads)))
  8740. ((eq arg 'previousset)
  8741. (let ((org-todo-heads (reverse org-todo-heads)))
  8742. (or (car (cdr (member head org-todo-heads)))
  8743. (car org-todo-heads))))
  8744. ((car (member arg org-todo-keywords-1)))
  8745. ((stringp arg)
  8746. (error "State `%s' not valid in this file" arg))
  8747. ((nth (1- (prefix-numeric-value arg))
  8748. org-todo-keywords-1))))
  8749. ((null member) (or head (car org-todo-keywords-1)))
  8750. ((equal this final-done-word) nil) ;; -> make empty
  8751. ((null tail) nil) ;; -> first entry
  8752. ((memq interpret '(type priority))
  8753. (if (eq this-command last-command)
  8754. (car tail)
  8755. (if (> (length tail) 0)
  8756. (or done-word (car org-done-keywords))
  8757. nil)))
  8758. (t
  8759. (car tail))))
  8760. (state (or
  8761. (run-hook-with-args-until-success
  8762. 'org-todo-get-default-hook state last-state)
  8763. state))
  8764. (next (if state (concat " " state " ") " "))
  8765. (change-plist (list :type 'todo-state-change :from this :to state
  8766. :position startpos))
  8767. dolog now-done-p)
  8768. (when org-blocker-hook
  8769. (setq org-last-todo-state-is-todo
  8770. (not (member this org-done-keywords)))
  8771. (unless (save-excursion
  8772. (save-match-data
  8773. (run-hook-with-args-until-failure
  8774. 'org-blocker-hook change-plist)))
  8775. (if (interactive-p)
  8776. (error "TODO state change from %s to %s blocked" this state)
  8777. ;; fail silently
  8778. (message "TODO state change from %s to %s blocked" this state)
  8779. (throw 'exit nil))))
  8780. (store-match-data match-data)
  8781. (replace-match next t t)
  8782. (unless (pos-visible-in-window-p hl-pos)
  8783. (message "TODO state changed to %s" (org-trim next)))
  8784. (unless head
  8785. (setq head (org-get-todo-sequence-head state)
  8786. ass (assoc head org-todo-kwd-alist)
  8787. interpret (nth 1 ass)
  8788. done-word (nth 3 ass)
  8789. final-done-word (nth 4 ass)))
  8790. (when (memq arg '(nextset previousset))
  8791. (message "Keyword-Set %d/%d: %s"
  8792. (- (length org-todo-sets) -1
  8793. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8794. (length org-todo-sets)
  8795. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8796. (setq org-last-todo-state-is-todo
  8797. (not (member state org-done-keywords)))
  8798. (setq now-done-p (and (member state org-done-keywords)
  8799. (not (member this org-done-keywords))))
  8800. (and logging (org-local-logging logging))
  8801. (when (and (or org-todo-log-states org-log-done)
  8802. (not (eq org-inhibit-logging t))
  8803. (not (memq arg '(nextset previousset))))
  8804. ;; we need to look at recording a time and note
  8805. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8806. (nth 2 (assoc this org-todo-log-states))))
  8807. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8808. (setq dolog 'time))
  8809. (when (and state
  8810. (member state org-not-done-keywords)
  8811. (not (member this org-not-done-keywords)))
  8812. ;; This is now a todo state and was not one before
  8813. ;; If there was a CLOSED time stamp, get rid of it.
  8814. (org-add-planning-info nil nil 'closed))
  8815. (when (and now-done-p org-log-done)
  8816. ;; It is now done, and it was not done before
  8817. (org-add-planning-info 'closed (org-current-time))
  8818. (if (and (not dolog) (eq 'note org-log-done))
  8819. (org-add-log-setup 'done state this 'findpos 'note)))
  8820. (when (and state dolog)
  8821. ;; This is a non-nil state, and we need to log it
  8822. (org-add-log-setup 'state state this 'findpos dolog)))
  8823. ;; Fixup tag positioning
  8824. (org-todo-trigger-tag-changes state)
  8825. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8826. (when org-provide-todo-statistics
  8827. (org-update-parent-todo-statistics))
  8828. (run-hooks 'org-after-todo-state-change-hook)
  8829. (if (and arg (not (member state org-done-keywords)))
  8830. (setq head (org-get-todo-sequence-head state)))
  8831. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8832. ;; Do we need to trigger a repeat?
  8833. (when now-done-p
  8834. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8835. ;; This is for the agenda, take a snapshot of the headline.
  8836. (save-match-data
  8837. (setq org-agenda-headline-snapshot-before-repeat
  8838. (org-get-heading))))
  8839. (org-auto-repeat-maybe state))
  8840. ;; Fixup cursor location if close to the keyword
  8841. (if (and (outline-on-heading-p)
  8842. (not (bolp))
  8843. (save-excursion (beginning-of-line 1)
  8844. (looking-at org-todo-line-regexp))
  8845. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8846. (progn
  8847. (goto-char (or (match-end 2) (match-end 1)))
  8848. (and (looking-at " ") (just-one-space))))
  8849. (when org-trigger-hook
  8850. (save-excursion
  8851. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8852. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8853. "Block turning an entry into a TODO, using the hierarchy.
  8854. This checks whether the current task should be blocked from state
  8855. changes. Such blocking occurs when:
  8856. 1. The task has children which are not all in a completed state.
  8857. 2. A task has a parent with the property :ORDERED:, and there
  8858. are siblings prior to the current task with incomplete
  8859. status.
  8860. 3. The parent of the task is blocked because it has siblings that should
  8861. be done first, or is child of a block grandparent TODO entry."
  8862. (catch 'dont-block
  8863. ;; If this is not a todo state change, or if this entry is already DONE,
  8864. ;; do not block
  8865. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8866. (member (plist-get change-plist :from)
  8867. (cons 'done org-done-keywords))
  8868. (member (plist-get change-plist :to)
  8869. (cons 'todo org-not-done-keywords)))
  8870. (throw 'dont-block t))
  8871. ;; If this task has children, and any are undone, it's blocked
  8872. (save-excursion
  8873. (org-back-to-heading t)
  8874. (let ((this-level (funcall outline-level)))
  8875. (outline-next-heading)
  8876. (let ((child-level (funcall outline-level)))
  8877. (while (and (not (eobp))
  8878. (> child-level this-level))
  8879. ;; this todo has children, check whether they are all
  8880. ;; completed
  8881. (if (and (not (org-entry-is-done-p))
  8882. (org-entry-is-todo-p))
  8883. (throw 'dont-block nil))
  8884. (outline-next-heading)
  8885. (setq child-level (funcall outline-level))))))
  8886. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8887. ;; any previous siblings are undone, it's blocked
  8888. (save-excursion
  8889. (org-back-to-heading t)
  8890. (let* ((pos (point))
  8891. (parent-pos (and (org-up-heading-safe) (point))))
  8892. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8893. (when (and (org-entry-get (point) "ORDERED")
  8894. (forward-line 1)
  8895. (re-search-forward org-not-done-heading-regexp pos t))
  8896. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8897. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8898. (while t
  8899. (goto-char parent-pos)
  8900. (if (not (looking-at org-not-done-heading-regexp))
  8901. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8902. (setq pos (point))
  8903. (setq parent-pos (and (org-up-heading-safe) (point)))
  8904. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8905. (when (and (org-entry-get (point) "ORDERED")
  8906. (forward-line 1)
  8907. (re-search-forward org-not-done-heading-regexp pos t))
  8908. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8909. (defcustom org-track-ordered-property-with-tag nil
  8910. "Should the ORDERED property also be shown as a tag?
  8911. The ORDERED property decides if an entry should require subtasks to be
  8912. completed in sequence. Since a property is not very visible, setting
  8913. this option means that toggling the ORDERED property with the command
  8914. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8915. not relevant for the behavior, but it makes things more visible.
  8916. Note that toggling the tag with tags commands will not change the property
  8917. and therefore not influence behavior!
  8918. This can be t, meaning the tag ORDERED should be used, It can also be a
  8919. string to select a different tag for this task."
  8920. :group 'org-todo
  8921. :type '(choice
  8922. (const :tag "No tracking" nil)
  8923. (const :tag "Track with ORDERED tag" t)
  8924. (string :tag "Use other tag")))
  8925. (defun org-toggle-ordered-property ()
  8926. "Toggle the ORDERED property of the current entry.
  8927. For better visibility, you can track the value of this property with a tag.
  8928. See variable `org-track-ordered-property-with-tag'."
  8929. (interactive)
  8930. (let* ((t1 org-track-ordered-property-with-tag)
  8931. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8932. (save-excursion
  8933. (org-back-to-heading)
  8934. (if (org-entry-get nil "ORDERED")
  8935. (progn
  8936. (org-delete-property "ORDERED")
  8937. (and tag (org-toggle-tag tag 'off))
  8938. (message "Subtasks can be completed in arbitrary order"))
  8939. (org-entry-put nil "ORDERED" "t")
  8940. (and tag (org-toggle-tag tag 'on))
  8941. (message "Subtasks must be completed in sequence")))))
  8942. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8943. (defun org-block-todo-from-checkboxes (change-plist)
  8944. "Block turning an entry into a TODO, using checkboxes.
  8945. This checks whether the current task should be blocked from state
  8946. changes because there are uncheckd boxes in this entry."
  8947. (catch 'dont-block
  8948. ;; If this is not a todo state change, or if this entry is already DONE,
  8949. ;; do not block
  8950. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8951. (member (plist-get change-plist :from)
  8952. (cons 'done org-done-keywords))
  8953. (member (plist-get change-plist :to)
  8954. (cons 'todo org-not-done-keywords)))
  8955. (throw 'dont-block t))
  8956. ;; If this task has checkboxes that are not checked, it's blocked
  8957. (save-excursion
  8958. (org-back-to-heading t)
  8959. (let ((beg (point)) end)
  8960. (outline-next-heading)
  8961. (setq end (point))
  8962. (goto-char beg)
  8963. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8964. end t)
  8965. (progn
  8966. (if (boundp 'org-blocked-by-checkboxes)
  8967. (setq org-blocked-by-checkboxes t))
  8968. (throw 'dont-block nil)))))
  8969. t)) ; do not block
  8970. (defun org-update-statistics-cookies (all)
  8971. "Update the statistics cookie, either from TODO or from checkboxes.
  8972. This should be called with the cursor in a line with a statistics cookie."
  8973. (interactive "P")
  8974. (if all
  8975. (progn
  8976. (org-update-checkbox-count 'all)
  8977. (org-map-entries 'org-update-parent-todo-statistics))
  8978. (if (not (org-on-heading-p))
  8979. (org-update-checkbox-count)
  8980. (let ((pos (move-marker (make-marker) (point)))
  8981. end l1 l2)
  8982. (ignore-errors (org-back-to-heading t))
  8983. (if (not (org-on-heading-p))
  8984. (org-update-checkbox-count)
  8985. (setq l1 (org-outline-level))
  8986. (setq end (save-excursion
  8987. (outline-next-heading)
  8988. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  8989. (point)))
  8990. (if (and (save-excursion (re-search-forward
  8991. "^[ \t]*[-+*] \\[[- X]\\]" end t))
  8992. (not (save-excursion (re-search-forward
  8993. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  8994. (org-update-checkbox-count)
  8995. (if (and l2 (> l2 l1))
  8996. (progn
  8997. (goto-char end)
  8998. (org-update-parent-todo-statistics))
  8999. (error "No data for statistics cookie"))))
  9000. (goto-char pos)
  9001. (move-marker pos nil)))))
  9002. (defvar org-entry-property-inherited-from) ;; defined below
  9003. (defun org-update-parent-todo-statistics ()
  9004. "Update any statistics cookie in the parent of the current headline.
  9005. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9006. the entire subtree and this will travel up the hierarchy and update
  9007. statistics everywhere."
  9008. (interactive)
  9009. (let* ((lim 0) prop
  9010. (recursive (or (not org-hierarchical-todo-statistics)
  9011. (string-match
  9012. "\\<recursive\\>"
  9013. (or (setq prop (org-entry-get
  9014. nil "COOKIE_DATA" 'inherit)) ""))))
  9015. (lim (or (and prop (marker-position
  9016. org-entry-property-inherited-from))
  9017. lim))
  9018. (first t)
  9019. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9020. level ltoggle l1 new ndel
  9021. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9022. (catch 'exit
  9023. (save-excursion
  9024. (beginning-of-line 1)
  9025. (if (org-at-heading-p)
  9026. (setq ltoggle (funcall outline-level))
  9027. (error "This should not happen"))
  9028. (while (and (setq level (org-up-heading-safe))
  9029. (or recursive first)
  9030. (>= (point) lim))
  9031. (setq first nil)
  9032. (unless (and level
  9033. (not (string-match
  9034. "\\<checkbox\\>"
  9035. (downcase
  9036. (or (org-entry-get
  9037. nil "COOKIE_DATA")
  9038. "")))))
  9039. (throw 'exit nil))
  9040. (while (re-search-forward box-re (point-at-eol) t)
  9041. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9042. (setq is-percent (match-end 2))
  9043. (save-match-data
  9044. (unless (outline-next-heading) (throw 'exit nil))
  9045. (while (and (looking-at org-complex-heading-regexp)
  9046. (> (setq l1 (length (match-string 1))) level))
  9047. (setq kwd (and (or recursive (= l1 ltoggle))
  9048. (match-string 2)))
  9049. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9050. (and (listp org-provide-todo-statistics)
  9051. (or (member kwd org-provide-todo-statistics)
  9052. (member kwd org-done-keywords))))
  9053. (setq cnt-all (1+ cnt-all))
  9054. (if (eq org-provide-todo-statistics t)
  9055. (and kwd (setq cnt-all (1+ cnt-all)))))
  9056. (and (member kwd org-done-keywords)
  9057. (setq cnt-done (1+ cnt-done)))
  9058. (outline-next-heading)))
  9059. (setq new
  9060. (if is-percent
  9061. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9062. (format "[%d/%d]" cnt-done cnt-all))
  9063. ndel (- (match-end 0) (match-beginning 0)))
  9064. (goto-char (match-beginning 0))
  9065. (insert new)
  9066. (delete-region (point) (+ (point) ndel))))
  9067. (when cookie-present
  9068. (run-hook-with-args 'org-after-todo-statistics-hook
  9069. cnt-done (- cnt-all cnt-done)))))
  9070. (run-hooks 'org-todo-statistics-hook)))
  9071. (defvar org-after-todo-statistics-hook nil
  9072. "Hook that is called after a TODO statistics cookie has been updated.
  9073. Each function is called with two arguments: the number of not-done entries
  9074. and the number of done entries.
  9075. For example, the following function, when added to this hook, will switch
  9076. an entry to DONE when all children are done, and back to TODO when new
  9077. entries are set to a TODO status. Note that this hook is only called
  9078. when there is a statistics cookie in the headline!
  9079. (defun org-summary-todo (n-done n-not-done)
  9080. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9081. (let (org-log-done org-log-states) ; turn off logging
  9082. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9083. ")
  9084. (defvar org-todo-statistics-hook nil
  9085. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9086. This hook runs even if there is no statisics cookie present, in which case
  9087. `org-after-todo-statistics-hook' would not run.")
  9088. (defun org-todo-trigger-tag-changes (state)
  9089. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9090. (let ((l org-todo-state-tags-triggers)
  9091. changes)
  9092. (when (or (not state) (equal state ""))
  9093. (setq changes (append changes (cdr (assoc "" l)))))
  9094. (when (and (stringp state) (> (length state) 0))
  9095. (setq changes (append changes (cdr (assoc state l)))))
  9096. (when (member state org-not-done-keywords)
  9097. (setq changes (append changes (cdr (assoc 'todo l)))))
  9098. (when (member state org-done-keywords)
  9099. (setq changes (append changes (cdr (assoc 'done l)))))
  9100. (dolist (c changes)
  9101. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9102. (defun org-local-logging (value)
  9103. "Get logging settings from a property VALUE."
  9104. (let* (words w a)
  9105. ;; directly set the variables, they are already local.
  9106. (setq org-log-done nil
  9107. org-log-repeat nil
  9108. org-todo-log-states nil)
  9109. (setq words (org-split-string value))
  9110. (while (setq w (pop words))
  9111. (cond
  9112. ((setq a (assoc w org-startup-options))
  9113. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9114. (set (nth 1 a) (nth 2 a))))
  9115. ((setq a (org-extract-log-state-settings w))
  9116. (and (member (car a) org-todo-keywords-1)
  9117. (push a org-todo-log-states)))))))
  9118. (defun org-get-todo-sequence-head (kwd)
  9119. "Return the head of the TODO sequence to which KWD belongs.
  9120. If KWD is not set, check if there is a text property remembering the
  9121. right sequence."
  9122. (let (p)
  9123. (cond
  9124. ((not kwd)
  9125. (or (get-text-property (point-at-bol) 'org-todo-head)
  9126. (progn
  9127. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9128. nil (point-at-eol)))
  9129. (get-text-property p 'org-todo-head))))
  9130. ((not (member kwd org-todo-keywords-1))
  9131. (car org-todo-keywords-1))
  9132. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9133. (defun org-fast-todo-selection ()
  9134. "Fast TODO keyword selection with single keys.
  9135. Returns the new TODO keyword, or nil if no state change should occur."
  9136. (let* ((fulltable org-todo-key-alist)
  9137. (done-keywords org-done-keywords) ;; needed for the faces.
  9138. (maxlen (apply 'max (mapcar
  9139. (lambda (x)
  9140. (if (stringp (car x)) (string-width (car x)) 0))
  9141. fulltable)))
  9142. (expert nil)
  9143. (fwidth (+ maxlen 3 1 3))
  9144. (ncol (/ (- (window-width) 4) fwidth))
  9145. tg cnt e c tbl
  9146. groups ingroup)
  9147. (save-excursion
  9148. (save-window-excursion
  9149. (if expert
  9150. (set-buffer (get-buffer-create " *Org todo*"))
  9151. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9152. (erase-buffer)
  9153. (org-set-local 'org-done-keywords done-keywords)
  9154. (setq tbl fulltable cnt 0)
  9155. (while (setq e (pop tbl))
  9156. (cond
  9157. ((equal e '(:startgroup))
  9158. (push '() groups) (setq ingroup t)
  9159. (when (not (= cnt 0))
  9160. (setq cnt 0)
  9161. (insert "\n"))
  9162. (insert "{ "))
  9163. ((equal e '(:endgroup))
  9164. (setq ingroup nil cnt 0)
  9165. (insert "}\n"))
  9166. ((equal e '(:newline))
  9167. (when (not (= cnt 0))
  9168. (setq cnt 0)
  9169. (insert "\n")
  9170. (setq e (car tbl))
  9171. (while (equal (car tbl) '(:newline))
  9172. (insert "\n")
  9173. (setq tbl (cdr tbl)))))
  9174. (t
  9175. (setq tg (car e) c (cdr e))
  9176. (if ingroup (push tg (car groups)))
  9177. (setq tg (org-add-props tg nil 'face
  9178. (org-get-todo-face tg)))
  9179. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9180. (insert "[" c "] " tg (make-string
  9181. (- fwidth 4 (length tg)) ?\ ))
  9182. (when (= (setq cnt (1+ cnt)) ncol)
  9183. (insert "\n")
  9184. (if ingroup (insert " "))
  9185. (setq cnt 0)))))
  9186. (insert "\n")
  9187. (goto-char (point-min))
  9188. (if (not expert) (org-fit-window-to-buffer))
  9189. (message "[a-z..]:Set [SPC]:clear")
  9190. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9191. (cond
  9192. ((or (= c ?\C-g)
  9193. (and (= c ?q) (not (rassoc c fulltable))))
  9194. (setq quit-flag t))
  9195. ((= c ?\ ) nil)
  9196. ((setq e (rassoc c fulltable) tg (car e))
  9197. tg)
  9198. (t (setq quit-flag t)))))))
  9199. (defun org-entry-is-todo-p ()
  9200. (member (org-get-todo-state) org-not-done-keywords))
  9201. (defun org-entry-is-done-p ()
  9202. (member (org-get-todo-state) org-done-keywords))
  9203. (defun org-get-todo-state ()
  9204. (save-excursion
  9205. (org-back-to-heading t)
  9206. (and (looking-at org-todo-line-regexp)
  9207. (match-end 2)
  9208. (match-string 2))))
  9209. (defun org-at-date-range-p (&optional inactive-ok)
  9210. "Is the cursor inside a date range?"
  9211. (interactive)
  9212. (save-excursion
  9213. (catch 'exit
  9214. (let ((pos (point)))
  9215. (skip-chars-backward "^[<\r\n")
  9216. (skip-chars-backward "<[")
  9217. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9218. (>= (match-end 0) pos)
  9219. (throw 'exit t))
  9220. (skip-chars-backward "^<[\r\n")
  9221. (skip-chars-backward "<[")
  9222. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9223. (>= (match-end 0) pos)
  9224. (throw 'exit t)))
  9225. nil)))
  9226. (defun org-get-repeat (&optional tagline)
  9227. "Check if there is a deadline/schedule with repeater in this entry."
  9228. (save-match-data
  9229. (save-excursion
  9230. (org-back-to-heading t)
  9231. (and (re-search-forward (if tagline
  9232. (concat tagline "\\s-*" org-repeat-re)
  9233. org-repeat-re)
  9234. (org-entry-end-position) t)
  9235. (match-string-no-properties 1)))))
  9236. (defvar org-last-changed-timestamp)
  9237. (defvar org-last-inserted-timestamp)
  9238. (defvar org-log-post-message)
  9239. (defvar org-log-note-purpose)
  9240. (defvar org-log-note-how)
  9241. (defvar org-log-note-extra)
  9242. (defun org-auto-repeat-maybe (done-word)
  9243. "Check if the current headline contains a repeated deadline/schedule.
  9244. If yes, set TODO state back to what it was and change the base date
  9245. of repeating deadline/scheduled time stamps to new date.
  9246. This function is run automatically after each state change to a DONE state."
  9247. ;; last-state is dynamically scoped into this function
  9248. (let* ((repeat (org-get-repeat))
  9249. (aa (assoc last-state org-todo-kwd-alist))
  9250. (interpret (nth 1 aa))
  9251. (head (nth 2 aa))
  9252. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9253. (msg "Entry repeats: ")
  9254. (org-log-done nil)
  9255. (org-todo-log-states nil)
  9256. (nshiftmax 10) (nshift 0)
  9257. re type n what ts time)
  9258. (when repeat
  9259. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9260. (org-todo (if (eq interpret 'type) last-state head))
  9261. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9262. (org-time-stamp-format t t)))
  9263. (when org-log-repeat
  9264. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9265. (memq 'org-add-log-note post-command-hook))
  9266. ;; OK, we are already setup for some record
  9267. (if (eq org-log-repeat 'note)
  9268. ;; make sure we take a note, not only a time stamp
  9269. (setq org-log-note-how 'note))
  9270. ;; Set up for taking a record
  9271. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9272. last-state
  9273. 'findpos org-log-repeat)))
  9274. (org-back-to-heading t)
  9275. (org-add-planning-info nil nil 'closed)
  9276. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9277. org-deadline-time-regexp "\\)\\|\\("
  9278. org-ts-regexp "\\)"))
  9279. (while (re-search-forward
  9280. re (save-excursion (outline-next-heading) (point)) t)
  9281. (setq type (if (match-end 1) org-scheduled-string
  9282. (if (match-end 3) org-deadline-string "Plain:"))
  9283. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9284. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9285. (setq n (string-to-number (match-string 2 ts))
  9286. what (match-string 3 ts))
  9287. (if (equal what "w") (setq n (* n 7) what "d"))
  9288. ;; Preparation, see if we need to modify the start date for the change
  9289. (when (match-end 1)
  9290. (setq time (save-match-data (org-time-string-to-time ts)))
  9291. (cond
  9292. ((equal (match-string 1 ts) ".")
  9293. ;; Shift starting date to today
  9294. (org-timestamp-change
  9295. (- (time-to-days (current-time)) (time-to-days time))
  9296. 'day))
  9297. ((equal (match-string 1 ts) "+")
  9298. (while (or (= nshift 0)
  9299. (<= (time-to-days time) (time-to-days (current-time))))
  9300. (when (= (incf nshift) nshiftmax)
  9301. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9302. (error "Abort")))
  9303. (org-timestamp-change n (cdr (assoc what whata)))
  9304. (org-at-timestamp-p t)
  9305. (setq ts (match-string 1))
  9306. (setq time (save-match-data (org-time-string-to-time ts))))
  9307. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9308. ;; rematch, so that we have everything in place for the real shift
  9309. (org-at-timestamp-p t)
  9310. (setq ts (match-string 1))
  9311. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9312. (org-timestamp-change n (cdr (assoc what whata)))
  9313. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9314. (setq org-log-post-message msg)
  9315. (message "%s" msg))))
  9316. (defun org-show-todo-tree (arg)
  9317. "Make a compact tree which shows all headlines marked with TODO.
  9318. The tree will show the lines where the regexp matches, and all higher
  9319. headlines above the match.
  9320. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9321. With a numeric prefix N, construct a sparse tree for the Nth element
  9322. of `org-todo-keywords-1'."
  9323. (interactive "P")
  9324. (let ((case-fold-search nil)
  9325. (kwd-re
  9326. (cond ((null arg) org-not-done-regexp)
  9327. ((equal arg '(4))
  9328. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9329. (mapcar 'list org-todo-keywords-1))))
  9330. (concat "\\("
  9331. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9332. "\\)\\>")))
  9333. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9334. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9335. org-todo-keywords-1)))
  9336. (t (error "Invalid prefix argument: %s" arg)))))
  9337. (message "%d TODO entries found"
  9338. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9339. (defun org-deadline (&optional remove time)
  9340. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9341. With argument REMOVE, remove any deadline from the item.
  9342. When TIME is set, it should be an internal time specification, and the
  9343. scheduling will use the corresponding date."
  9344. (interactive "P")
  9345. (if remove
  9346. (progn
  9347. (org-remove-timestamp-with-keyword org-deadline-string)
  9348. (message "Item no longer has a deadline."))
  9349. (if (org-get-repeat)
  9350. (error "Cannot change deadline on task with repeater, please do that by hand")
  9351. (org-add-planning-info 'deadline time 'closed)
  9352. (message "Deadline on %s" org-last-inserted-timestamp))))
  9353. (defun org-schedule (&optional remove time)
  9354. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9355. With argument REMOVE, remove any scheduling date from the item.
  9356. When TIME is set, it should be an internal time specification, and the
  9357. scheduling will use the corresponding date."
  9358. (interactive "P")
  9359. (if remove
  9360. (progn
  9361. (org-remove-timestamp-with-keyword org-scheduled-string)
  9362. (message "Item is no longer scheduled."))
  9363. (if (org-get-repeat)
  9364. (error "Cannot reschedule task with repeater, please do that by hand")
  9365. (org-add-planning-info 'scheduled time 'closed)
  9366. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9367. (defun org-get-scheduled-time (pom &optional inherit)
  9368. "Get the scheduled time as a time tuple, of a format suitable
  9369. for calling org-schedule with, or if there is no scheduling,
  9370. returns nil."
  9371. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9372. (when time
  9373. (apply 'encode-time (org-parse-time-string time)))))
  9374. (defun org-get-deadline-time (pom &optional inherit)
  9375. "Get the deadine as a time tuple, of a format suitable for
  9376. calling org-deadlin with, or if there is no scheduling, returns
  9377. nil."
  9378. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9379. (when time
  9380. (apply 'encode-time (org-parse-time-string time)))))
  9381. (defun org-remove-timestamp-with-keyword (keyword)
  9382. "Remove all time stamps with KEYWORD in the current entry."
  9383. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9384. beg)
  9385. (save-excursion
  9386. (org-back-to-heading t)
  9387. (setq beg (point))
  9388. (outline-next-heading)
  9389. (while (re-search-backward re beg t)
  9390. (replace-match "")
  9391. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9392. (equal (char-before) ?\ ))
  9393. (backward-delete-char 1)
  9394. (if (string-match "^[ \t]*$" (buffer-substring
  9395. (point-at-bol) (point-at-eol)))
  9396. (delete-region (point-at-bol)
  9397. (min (point-max) (1+ (point-at-eol))))))))))
  9398. (defun org-add-planning-info (what &optional time &rest remove)
  9399. "Insert new timestamp with keyword in the line directly after the headline.
  9400. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9401. If non is given, the user is prompted for a date.
  9402. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9403. be removed."
  9404. (interactive)
  9405. (let (org-time-was-given org-end-time-was-given ts
  9406. end default-time default-input)
  9407. (catch 'exit
  9408. (when (and (not time) (memq what '(scheduled deadline)))
  9409. ;; Try to get a default date/time from existing timestamp
  9410. (save-excursion
  9411. (org-back-to-heading t)
  9412. (setq end (save-excursion (outline-next-heading) (point)))
  9413. (when (re-search-forward (if (eq what 'scheduled)
  9414. org-scheduled-time-regexp
  9415. org-deadline-time-regexp)
  9416. end t)
  9417. (setq ts (match-string 1)
  9418. default-time
  9419. (apply 'encode-time (org-parse-time-string ts))
  9420. default-input (and ts (org-get-compact-tod ts))))))
  9421. (when what
  9422. ;; If necessary, get the time from the user
  9423. (setq time (or time (org-read-date nil 'to-time nil nil
  9424. default-time default-input))))
  9425. (when (and org-insert-labeled-timestamps-at-point
  9426. (member what '(scheduled deadline)))
  9427. (insert
  9428. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9429. (org-insert-time-stamp time org-time-was-given
  9430. nil nil nil (list org-end-time-was-given))
  9431. (setq what nil))
  9432. (save-excursion
  9433. (save-restriction
  9434. (let (col list elt ts buffer-invisibility-spec)
  9435. (org-back-to-heading t)
  9436. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9437. (goto-char (match-end 1))
  9438. (setq col (current-column))
  9439. (goto-char (match-end 0))
  9440. (if (eobp) (insert "\n") (forward-char 1))
  9441. (when (and (not what)
  9442. (not (looking-at
  9443. (concat "[ \t]*"
  9444. org-keyword-time-not-clock-regexp))))
  9445. ;; Nothing to add, nothing to remove...... :-)
  9446. (throw 'exit nil))
  9447. (if (and (not (looking-at outline-regexp))
  9448. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9449. "[^\r\n]*"))
  9450. (not (equal (match-string 1) org-clock-string)))
  9451. (narrow-to-region (match-beginning 0) (match-end 0))
  9452. (insert-before-markers "\n")
  9453. (backward-char 1)
  9454. (narrow-to-region (point) (point))
  9455. (and org-adapt-indentation (org-indent-to-column col)))
  9456. ;; Check if we have to remove something.
  9457. (setq list (cons what remove))
  9458. (while list
  9459. (setq elt (pop list))
  9460. (goto-char (point-min))
  9461. (when (or (and (eq elt 'scheduled)
  9462. (re-search-forward org-scheduled-time-regexp nil t))
  9463. (and (eq elt 'deadline)
  9464. (re-search-forward org-deadline-time-regexp nil t))
  9465. (and (eq elt 'closed)
  9466. (re-search-forward org-closed-time-regexp nil t)))
  9467. (replace-match "")
  9468. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9469. (skip-chars-backward " ")
  9470. (if (looking-at " +") (replace-match ""))))
  9471. (goto-char (point-max))
  9472. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  9473. (when what
  9474. (insert
  9475. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9476. (cond ((eq what 'scheduled) org-scheduled-string)
  9477. ((eq what 'deadline) org-deadline-string)
  9478. ((eq what 'closed) org-closed-string))
  9479. " ")
  9480. (setq ts (org-insert-time-stamp
  9481. time
  9482. (or org-time-was-given
  9483. (and (eq what 'closed) org-log-done-with-time))
  9484. (eq what 'closed)
  9485. nil nil (list org-end-time-was-given)))
  9486. (end-of-line 1))
  9487. (goto-char (point-min))
  9488. (widen)
  9489. (if (and (looking-at "[ \t]+\n")
  9490. (equal (char-before) ?\n))
  9491. (delete-region (1- (point)) (point-at-eol)))
  9492. ts))))))
  9493. (defvar org-log-note-marker (make-marker))
  9494. (defvar org-log-note-purpose nil)
  9495. (defvar org-log-note-state nil)
  9496. (defvar org-log-note-previous-state nil)
  9497. (defvar org-log-note-how nil)
  9498. (defvar org-log-note-extra nil)
  9499. (defvar org-log-note-window-configuration nil)
  9500. (defvar org-log-note-return-to (make-marker))
  9501. (defvar org-log-post-message nil
  9502. "Message to be displayed after a log note has been stored.
  9503. The auto-repeater uses this.")
  9504. (defun org-add-note ()
  9505. "Add a note to the current entry.
  9506. This is done in the same way as adding a state change note."
  9507. (interactive)
  9508. (org-add-log-setup 'note nil nil 'findpos nil))
  9509. (defvar org-property-end-re)
  9510. (defun org-add-log-setup (&optional purpose state prev-state
  9511. findpos how &optional extra)
  9512. "Set up the post command hook to take a note.
  9513. If this is about to TODO state change, the new state is expected in STATE.
  9514. When FINDPOS is non-nil, find the correct position for the note in
  9515. the current entry. If not, assume that it can be inserted at point.
  9516. HOW is an indicator what kind of note should be created.
  9517. EXTRA is additional text that will be inserted into the notes buffer."
  9518. (let* ((org-log-into-drawer (org-log-into-drawer))
  9519. (drawer (cond ((stringp org-log-into-drawer)
  9520. org-log-into-drawer)
  9521. (org-log-into-drawer "LOGBOOK")
  9522. (t nil))))
  9523. (save-restriction
  9524. (save-excursion
  9525. (when findpos
  9526. (org-back-to-heading t)
  9527. (narrow-to-region (point) (save-excursion
  9528. (outline-next-heading) (point)))
  9529. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9530. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9531. "[^\r\n]*\\)?"))
  9532. (goto-char (match-end 0))
  9533. (cond
  9534. (drawer
  9535. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9536. nil t)
  9537. (progn
  9538. (goto-char (match-end 0))
  9539. (or org-log-states-order-reversed
  9540. (and (re-search-forward org-property-end-re nil t)
  9541. (goto-char (1- (match-beginning 0))))))
  9542. (insert "\n:" drawer ":\n:END:")
  9543. (beginning-of-line 0)
  9544. (org-indent-line-function)
  9545. (beginning-of-line 2)
  9546. (org-indent-line-function)
  9547. (end-of-line 0)))
  9548. ((and org-log-state-notes-insert-after-drawers
  9549. (save-excursion
  9550. (forward-line) (looking-at org-drawer-regexp)))
  9551. (forward-line)
  9552. (while (looking-at org-drawer-regexp)
  9553. (goto-char (match-end 0))
  9554. (re-search-forward org-property-end-re (point-max) t)
  9555. (forward-line))
  9556. (forward-line -1)))
  9557. (unless org-log-states-order-reversed
  9558. (and (= (char-after) ?\n) (forward-char 1))
  9559. (org-skip-over-state-notes)
  9560. (skip-chars-backward " \t\n\r")))
  9561. (move-marker org-log-note-marker (point))
  9562. (setq org-log-note-purpose purpose
  9563. org-log-note-state state
  9564. org-log-note-previous-state prev-state
  9565. org-log-note-how how
  9566. org-log-note-extra extra)
  9567. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9568. (defun org-skip-over-state-notes ()
  9569. "Skip past the list of State notes in an entry."
  9570. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9571. (while (looking-at "[ \t]*- State")
  9572. (condition-case nil
  9573. (org-next-item)
  9574. (error (org-end-of-item)))))
  9575. (defun org-add-log-note (&optional purpose)
  9576. "Pop up a window for taking a note, and add this note later at point."
  9577. (remove-hook 'post-command-hook 'org-add-log-note)
  9578. (setq org-log-note-window-configuration (current-window-configuration))
  9579. (delete-other-windows)
  9580. (move-marker org-log-note-return-to (point))
  9581. (switch-to-buffer (marker-buffer org-log-note-marker))
  9582. (goto-char org-log-note-marker)
  9583. (org-switch-to-buffer-other-window "*Org Note*")
  9584. (erase-buffer)
  9585. (if (memq org-log-note-how '(time state))
  9586. (let (current-prefix-arg) (org-store-log-note))
  9587. (let ((org-inhibit-startup t)) (org-mode))
  9588. (insert (format "# Insert note for %s.
  9589. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9590. (cond
  9591. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9592. ((eq org-log-note-purpose 'done) "closed todo item")
  9593. ((eq org-log-note-purpose 'state)
  9594. (format "state change from \"%s\" to \"%s\""
  9595. (or org-log-note-previous-state "")
  9596. (or org-log-note-state "")))
  9597. ((eq org-log-note-purpose 'note)
  9598. "this entry")
  9599. (t (error "This should not happen")))))
  9600. (if org-log-note-extra (insert org-log-note-extra))
  9601. (org-set-local 'org-finish-function 'org-store-log-note)))
  9602. (defvar org-note-abort nil) ; dynamically scoped
  9603. (defun org-store-log-note ()
  9604. "Finish taking a log note, and insert it to where it belongs."
  9605. (let ((txt (buffer-string))
  9606. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9607. lines ind)
  9608. (kill-buffer (current-buffer))
  9609. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9610. (setq txt (replace-match "" t t txt)))
  9611. (if (string-match "\\s-+\\'" txt)
  9612. (setq txt (replace-match "" t t txt)))
  9613. (setq lines (org-split-string txt "\n"))
  9614. (when (and note (string-match "\\S-" note))
  9615. (setq note
  9616. (org-replace-escapes
  9617. note
  9618. (list (cons "%u" (user-login-name))
  9619. (cons "%U" user-full-name)
  9620. (cons "%t" (format-time-string
  9621. (org-time-stamp-format 'long 'inactive)
  9622. (current-time)))
  9623. (cons "%s" (if org-log-note-state
  9624. (concat "\"" org-log-note-state "\"")
  9625. ""))
  9626. (cons "%S" (if org-log-note-previous-state
  9627. (concat "\"" org-log-note-previous-state "\"")
  9628. "\"\"")))))
  9629. (if lines (setq note (concat note " \\\\")))
  9630. (push note lines))
  9631. (when (or current-prefix-arg org-note-abort)
  9632. (when org-log-into-drawer
  9633. (org-remove-empty-drawer-at
  9634. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9635. org-log-note-marker))
  9636. (setq lines nil))
  9637. (when lines
  9638. (save-excursion
  9639. (set-buffer (marker-buffer org-log-note-marker))
  9640. (save-excursion
  9641. (goto-char org-log-note-marker)
  9642. (move-marker org-log-note-marker nil)
  9643. (end-of-line 1)
  9644. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9645. (insert "- " (pop lines))
  9646. (org-indent-line-function)
  9647. (beginning-of-line 1)
  9648. (looking-at "[ \t]*")
  9649. (setq ind (concat (match-string 0) " "))
  9650. (end-of-line 1)
  9651. (while lines (insert "\n" ind (pop lines)))
  9652. (message "Note stored")
  9653. (org-back-to-heading t)
  9654. (org-cycle-hide-drawers 'children)))))
  9655. (set-window-configuration org-log-note-window-configuration)
  9656. (with-current-buffer (marker-buffer org-log-note-return-to)
  9657. (goto-char org-log-note-return-to))
  9658. (move-marker org-log-note-return-to nil)
  9659. (and org-log-post-message (message "%s" org-log-post-message)))
  9660. (defun org-remove-empty-drawer-at (drawer pos)
  9661. "Remove an emptyr DARWER drawer at position POS.
  9662. POS may also be a marker."
  9663. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9664. (save-excursion
  9665. (save-restriction
  9666. (widen)
  9667. (goto-char pos)
  9668. (if (org-in-regexp
  9669. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9670. (replace-match ""))))))
  9671. (defun org-sparse-tree (&optional arg)
  9672. "Create a sparse tree, prompt for the details.
  9673. This command can create sparse trees. You first need to select the type
  9674. of match used to create the tree:
  9675. t Show entries with a specific TODO keyword.
  9676. m Show entries selected by a tags/property match.
  9677. p Enter a property name and its value (both with completion on existing
  9678. names/values) and show entries with that property.
  9679. r Show entries matching a regular expression.
  9680. d Show deadlines due within `org-deadline-warning-days'.
  9681. b Show deadlines and scheduled items before a date.
  9682. a Show deadlines and scheduled items after a date."
  9683. (interactive "P")
  9684. (let (ans kwd value)
  9685. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9686. (setq ans (read-char-exclusive))
  9687. (cond
  9688. ((equal ans ?d)
  9689. (call-interactively 'org-check-deadlines))
  9690. ((equal ans ?b)
  9691. (call-interactively 'org-check-before-date))
  9692. ((equal ans ?a)
  9693. (call-interactively 'org-check-after-date))
  9694. ((equal ans ?t)
  9695. (org-show-todo-tree '(4)))
  9696. ((member ans '(?T ?m))
  9697. (call-interactively 'org-match-sparse-tree))
  9698. ((member ans '(?p ?P))
  9699. (setq kwd (org-icompleting-read "Property: "
  9700. (mapcar 'list (org-buffer-property-keys))))
  9701. (setq value (org-icompleting-read "Value: "
  9702. (mapcar 'list (org-property-values kwd))))
  9703. (unless (string-match "\\`{.*}\\'" value)
  9704. (setq value (concat "\"" value "\"")))
  9705. (org-match-sparse-tree arg (concat kwd "=" value)))
  9706. ((member ans '(?r ?R ?/))
  9707. (call-interactively 'org-occur))
  9708. (t (error "No such sparse tree command \"%c\"" ans)))))
  9709. (defvar org-occur-highlights nil
  9710. "List of overlays used for occur matches.")
  9711. (make-variable-buffer-local 'org-occur-highlights)
  9712. (defvar org-occur-parameters nil
  9713. "Parameters of the active org-occur calls.
  9714. This is a list, each call to org-occur pushes as cons cell,
  9715. containing the regular expression and the callback, onto the list.
  9716. The list can contain several entries if `org-occur' has been called
  9717. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9718. will only contain one set of parameters. When the highlights are
  9719. removed (for example with `C-c C-c', or with the next edit (depending
  9720. on `org-remove-highlights-with-change'), this variable is emptied
  9721. as well.")
  9722. (make-variable-buffer-local 'org-occur-parameters)
  9723. (defun org-occur (regexp &optional keep-previous callback)
  9724. "Make a compact tree which shows all matches of REGEXP.
  9725. The tree will show the lines where the regexp matches, and all higher
  9726. headlines above the match. It will also show the heading after the match,
  9727. to make sure editing the matching entry is easy.
  9728. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9729. call to `org-occur' will be kept, to allow stacking of calls to this
  9730. command.
  9731. If CALLBACK is non-nil, it is a function which is called to confirm
  9732. that the match should indeed be shown."
  9733. (interactive "sRegexp: \nP")
  9734. (when (equal regexp "")
  9735. (error "Regexp cannot be empty"))
  9736. (unless keep-previous
  9737. (org-remove-occur-highlights nil nil t))
  9738. (push (cons regexp callback) org-occur-parameters)
  9739. (let ((cnt 0))
  9740. (save-excursion
  9741. (goto-char (point-min))
  9742. (if (or (not keep-previous) ; do not want to keep
  9743. (not org-occur-highlights)) ; no previous matches
  9744. ;; hide everything
  9745. (org-overview))
  9746. (while (re-search-forward regexp nil t)
  9747. (when (or (not callback)
  9748. (save-match-data (funcall callback)))
  9749. (setq cnt (1+ cnt))
  9750. (when org-highlight-sparse-tree-matches
  9751. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9752. (org-show-context 'occur-tree))))
  9753. (when org-remove-highlights-with-change
  9754. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9755. nil 'local))
  9756. (unless org-sparse-tree-open-archived-trees
  9757. (org-hide-archived-subtrees (point-min) (point-max)))
  9758. (run-hooks 'org-occur-hook)
  9759. (if (interactive-p)
  9760. (message "%d match(es) for regexp %s" cnt regexp))
  9761. cnt))
  9762. (defun org-show-context (&optional key)
  9763. "Make sure point and context and visible.
  9764. How much context is shown depends upon the variables
  9765. `org-show-hierarchy-above', `org-show-following-heading'. and
  9766. `org-show-siblings'."
  9767. (let ((heading-p (org-on-heading-p t))
  9768. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9769. (following-p (org-get-alist-option org-show-following-heading key))
  9770. (entry-p (org-get-alist-option org-show-entry-below key))
  9771. (siblings-p (org-get-alist-option org-show-siblings key)))
  9772. (catch 'exit
  9773. ;; Show heading or entry text
  9774. (if (and heading-p (not entry-p))
  9775. (org-flag-heading nil) ; only show the heading
  9776. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9777. (org-show-hidden-entry))) ; show entire entry
  9778. (when following-p
  9779. ;; Show next sibling, or heading below text
  9780. (save-excursion
  9781. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9782. (org-flag-heading nil))))
  9783. (when siblings-p (org-show-siblings))
  9784. (when hierarchy-p
  9785. ;; show all higher headings, possibly with siblings
  9786. (save-excursion
  9787. (while (and (condition-case nil
  9788. (progn (org-up-heading-all 1) t)
  9789. (error nil))
  9790. (not (bobp)))
  9791. (org-flag-heading nil)
  9792. (when siblings-p (org-show-siblings))))))))
  9793. (defun org-reveal (&optional siblings)
  9794. "Show current entry, hierarchy above it, and the following headline.
  9795. This can be used to show a consistent set of context around locations
  9796. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9797. not t for the search context.
  9798. With optional argument SIBLINGS, on each level of the hierarchy all
  9799. siblings are shown. This repairs the tree structure to what it would
  9800. look like when opened with hierarchical calls to `org-cycle'."
  9801. (interactive "P")
  9802. (let ((org-show-hierarchy-above t)
  9803. (org-show-following-heading t)
  9804. (org-show-siblings (if siblings t org-show-siblings)))
  9805. (org-show-context nil)))
  9806. (defun org-highlight-new-match (beg end)
  9807. "Highlight from BEG to END and mark the highlight is an occur headline."
  9808. (let ((ov (org-make-overlay beg end)))
  9809. (org-overlay-put ov 'face 'secondary-selection)
  9810. (push ov org-occur-highlights)))
  9811. (defun org-remove-occur-highlights (&optional beg end noremove)
  9812. "Remove the occur highlights from the buffer.
  9813. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9814. from the `before-change-functions' in the current buffer."
  9815. (interactive)
  9816. (unless org-inhibit-highlight-removal
  9817. (mapc 'org-delete-overlay org-occur-highlights)
  9818. (setq org-occur-highlights nil)
  9819. (setq org-occur-parameters nil)
  9820. (unless noremove
  9821. (remove-hook 'before-change-functions
  9822. 'org-remove-occur-highlights 'local))))
  9823. ;;;; Priorities
  9824. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9825. "Regular expression matching the priority indicator.")
  9826. (defvar org-remove-priority-next-time nil)
  9827. (defun org-priority-up ()
  9828. "Increase the priority of the current item."
  9829. (interactive)
  9830. (org-priority 'up))
  9831. (defun org-priority-down ()
  9832. "Decrease the priority of the current item."
  9833. (interactive)
  9834. (org-priority 'down))
  9835. (defun org-priority (&optional action)
  9836. "Change the priority of an item by ARG.
  9837. ACTION can be `set', `up', `down', or a character."
  9838. (interactive)
  9839. (unless org-enable-priority-commands
  9840. (error "Priority commands are disabled"))
  9841. (setq action (or action 'set))
  9842. (let (current new news have remove)
  9843. (save-excursion
  9844. (org-back-to-heading t)
  9845. (if (looking-at org-priority-regexp)
  9846. (setq current (string-to-char (match-string 2))
  9847. have t)
  9848. (setq current org-default-priority))
  9849. (cond
  9850. ((eq action 'remove)
  9851. (setq remove t new ?\ ))
  9852. ((or (eq action 'set)
  9853. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9854. (if (not (eq action 'set))
  9855. (setq new action)
  9856. (message "Priority %c-%c, SPC to remove: "
  9857. org-highest-priority org-lowest-priority)
  9858. (setq new (read-char-exclusive)))
  9859. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9860. (= (upcase org-lowest-priority) org-lowest-priority))
  9861. (setq new (upcase new)))
  9862. (cond ((equal new ?\ ) (setq remove t))
  9863. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9864. (error "Priority must be between `%c' and `%c'"
  9865. org-highest-priority org-lowest-priority))))
  9866. ((eq action 'up)
  9867. (if (and (not have) (eq last-command this-command))
  9868. (setq new org-lowest-priority)
  9869. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9870. org-default-priority (1- current)))))
  9871. ((eq action 'down)
  9872. (if (and (not have) (eq last-command this-command))
  9873. (setq new org-highest-priority)
  9874. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9875. org-default-priority (1+ current)))))
  9876. (t (error "Invalid action")))
  9877. (if (or (< (upcase new) org-highest-priority)
  9878. (> (upcase new) org-lowest-priority))
  9879. (setq remove t))
  9880. (setq news (format "%c" new))
  9881. (if have
  9882. (if remove
  9883. (replace-match "" t t nil 1)
  9884. (replace-match news t t nil 2))
  9885. (if remove
  9886. (error "No priority cookie found in line")
  9887. (let ((case-fold-search nil))
  9888. (looking-at org-todo-line-regexp))
  9889. (if (match-end 2)
  9890. (progn
  9891. (goto-char (match-end 2))
  9892. (insert " [#" news "]"))
  9893. (goto-char (match-beginning 3))
  9894. (insert "[#" news "] "))))
  9895. (org-preserve-lc (org-set-tags nil 'align)))
  9896. (if remove
  9897. (message "Priority removed")
  9898. (message "Priority of current item set to %s" news))))
  9899. (defun org-get-priority (s)
  9900. "Find priority cookie and return priority."
  9901. (save-match-data
  9902. (if (not (string-match org-priority-regexp s))
  9903. (* 1000 (- org-lowest-priority org-default-priority))
  9904. (* 1000 (- org-lowest-priority
  9905. (string-to-char (match-string 2 s)))))))
  9906. ;;;; Tags
  9907. (defvar org-agenda-archives-mode)
  9908. (defvar org-map-continue-from nil
  9909. "Position from where mapping should continue.
  9910. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9911. (defvar org-scanner-tags nil
  9912. "The current tag list while the tags scanner is running.")
  9913. (defvar org-trust-scanner-tags nil
  9914. "Should `org-get-tags-at' use the tags fro the scanner.
  9915. This is for internal dynamical scoping only.
  9916. When this is non-nil, the function `org-get-tags-at' will return the value
  9917. of `org-scanner-tags' instead of building the list by itself. This
  9918. can lead to large speed-ups when the tags scanner is used in a file with
  9919. many entries, and when the list of tags is retrieved, for example to
  9920. obtain a list of properties. Building the tags list for each entry in such
  9921. a file becomes an N^2 operation - but with this variable set, it scales
  9922. as N.")
  9923. (defun org-scan-tags (action matcher &optional todo-only)
  9924. "Scan headline tags with inheritance and produce output ACTION.
  9925. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9926. or `agenda' to produce an entry list for an agenda view. It can also be
  9927. a Lisp form or a function that should be called at each matched headline, in
  9928. this case the return value is a list of all return values from these calls.
  9929. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9930. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9931. only lines with a TODO keyword are included in the output."
  9932. (require 'org-agenda)
  9933. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9934. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9935. (org-re
  9936. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9937. (props (list 'face 'default
  9938. 'done-face 'org-agenda-done
  9939. 'undone-face 'default
  9940. 'mouse-face 'highlight
  9941. 'org-not-done-regexp org-not-done-regexp
  9942. 'org-todo-regexp org-todo-regexp
  9943. 'keymap org-agenda-keymap
  9944. 'help-echo
  9945. (format "mouse-2 or RET jump to org file %s"
  9946. (abbreviate-file-name
  9947. (or (buffer-file-name (buffer-base-buffer))
  9948. (buffer-name (buffer-base-buffer)))))))
  9949. (case-fold-search nil)
  9950. (org-map-continue-from nil)
  9951. lspos tags tags-list
  9952. (tags-alist (list (cons 0 org-file-tags)))
  9953. (llast 0) rtn rtn1 level category i txt
  9954. todo marker entry priority)
  9955. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9956. (setq action (list 'lambda nil action)))
  9957. (save-excursion
  9958. (goto-char (point-min))
  9959. (when (eq action 'sparse-tree)
  9960. (org-overview)
  9961. (org-remove-occur-highlights))
  9962. (while (re-search-forward re nil t)
  9963. (catch :skip
  9964. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9965. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9966. (goto-char (setq lspos (match-beginning 0)))
  9967. (setq level (org-reduced-level (funcall outline-level))
  9968. category (org-get-category))
  9969. (setq i llast llast level)
  9970. ;; remove tag lists from same and sublevels
  9971. (while (>= i level)
  9972. (when (setq entry (assoc i tags-alist))
  9973. (setq tags-alist (delete entry tags-alist)))
  9974. (setq i (1- i)))
  9975. ;; add the next tags
  9976. (when tags
  9977. (setq tags (org-split-string tags ":")
  9978. tags-alist
  9979. (cons (cons level tags) tags-alist)))
  9980. ;; compile tags for current headline
  9981. (setq tags-list
  9982. (if org-use-tag-inheritance
  9983. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9984. tags)
  9985. org-scanner-tags tags-list)
  9986. (when org-use-tag-inheritance
  9987. (setcdr (car tags-alist)
  9988. (mapcar (lambda (x)
  9989. (setq x (copy-sequence x))
  9990. (org-add-prop-inherited x))
  9991. (cdar tags-alist))))
  9992. (when (and tags org-use-tag-inheritance
  9993. (or (not (eq t org-use-tag-inheritance))
  9994. org-tags-exclude-from-inheritance))
  9995. ;; selective inheritance, remove uninherited ones
  9996. (setcdr (car tags-alist)
  9997. (org-remove-uniherited-tags (cdar tags-alist))))
  9998. (when (and (or (not todo-only)
  9999. (and (member todo org-not-done-keywords)
  10000. (or (not org-agenda-tags-todo-honor-ignore-options)
  10001. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10002. (let ((case-fold-search t)) (eval matcher))
  10003. (or
  10004. (not (member org-archive-tag tags-list))
  10005. ;; we have an archive tag, should we use this anyway?
  10006. (or (not org-agenda-skip-archived-trees)
  10007. (and (eq action 'agenda) org-agenda-archives-mode))))
  10008. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10009. ;; select this headline
  10010. (cond
  10011. ((eq action 'sparse-tree)
  10012. (and org-highlight-sparse-tree-matches
  10013. (org-get-heading) (match-end 0)
  10014. (org-highlight-new-match
  10015. (match-beginning 0) (match-beginning 1)))
  10016. (org-show-context 'tags-tree))
  10017. ((eq action 'agenda)
  10018. (setq txt (org-format-agenda-item
  10019. ""
  10020. (concat
  10021. (if (eq org-tags-match-list-sublevels 'indented)
  10022. (make-string (1- level) ?.) "")
  10023. (org-get-heading))
  10024. category
  10025. tags-list
  10026. )
  10027. priority (org-get-priority txt))
  10028. (goto-char lspos)
  10029. (setq marker (org-agenda-new-marker))
  10030. (org-add-props txt props
  10031. 'org-marker marker 'org-hd-marker marker 'org-category category
  10032. 'todo-state todo
  10033. 'priority priority 'type "tagsmatch")
  10034. (push txt rtn))
  10035. ((functionp action)
  10036. (setq org-map-continue-from nil)
  10037. (save-excursion
  10038. (setq rtn1 (funcall action))
  10039. (push rtn1 rtn)))
  10040. (t (error "Invalid action")))
  10041. ;; if we are to skip sublevels, jump to end of subtree
  10042. (unless org-tags-match-list-sublevels
  10043. (org-end-of-subtree t)
  10044. (backward-char 1))))
  10045. ;; Get the correct position from where to continue
  10046. (if org-map-continue-from
  10047. (goto-char org-map-continue-from)
  10048. (and (= (point) lspos) (end-of-line 1)))))
  10049. (when (and (eq action 'sparse-tree)
  10050. (not org-sparse-tree-open-archived-trees))
  10051. (org-hide-archived-subtrees (point-min) (point-max)))
  10052. (nreverse rtn)))
  10053. (defun org-remove-uniherited-tags (tags)
  10054. "Remove all tags that are not inherited from the list TAGS."
  10055. (cond
  10056. ((eq org-use-tag-inheritance t)
  10057. (if org-tags-exclude-from-inheritance
  10058. (org-delete-all org-tags-exclude-from-inheritance tags)
  10059. tags))
  10060. ((not org-use-tag-inheritance) nil)
  10061. ((stringp org-use-tag-inheritance)
  10062. (delq nil (mapcar
  10063. (lambda (x)
  10064. (if (and (string-match org-use-tag-inheritance x)
  10065. (not (member x org-tags-exclude-from-inheritance)))
  10066. x nil))
  10067. tags)))
  10068. ((listp org-use-tag-inheritance)
  10069. (delq nil (mapcar
  10070. (lambda (x)
  10071. (if (member x org-use-tag-inheritance) x nil))
  10072. tags)))))
  10073. (defvar todo-only) ;; dynamically scoped
  10074. (defun org-match-sparse-tree (&optional todo-only match)
  10075. "Create a sparse tree according to tags string MATCH.
  10076. MATCH can contain positive and negative selection of tags, like
  10077. \"+WORK+URGENT-WITHBOSS\".
  10078. If optional argument TODO-ONLY is non-nil, only select lines that are
  10079. also TODO lines."
  10080. (interactive "P")
  10081. (org-prepare-agenda-buffers (list (current-buffer)))
  10082. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10083. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10084. (defvar org-cached-props nil)
  10085. (defun org-cached-entry-get (pom property)
  10086. (if (or (eq t org-use-property-inheritance)
  10087. (and (stringp org-use-property-inheritance)
  10088. (string-match org-use-property-inheritance property))
  10089. (and (listp org-use-property-inheritance)
  10090. (member property org-use-property-inheritance)))
  10091. ;; Caching is not possible, check it directly
  10092. (org-entry-get pom property 'inherit)
  10093. ;; Get all properties, so that we can do complicated checks easily
  10094. (cdr (assoc property (or org-cached-props
  10095. (setq org-cached-props
  10096. (org-entry-properties pom)))))))
  10097. (defun org-global-tags-completion-table (&optional files)
  10098. "Return the list of all tags in all agenda buffer/files."
  10099. (save-excursion
  10100. (org-uniquify
  10101. (delq nil
  10102. (apply 'append
  10103. (mapcar
  10104. (lambda (file)
  10105. (set-buffer (find-file-noselect file))
  10106. (append (org-get-buffer-tags)
  10107. (mapcar (lambda (x) (if (stringp (car-safe x))
  10108. (list (car-safe x)) nil))
  10109. org-tag-alist)))
  10110. (if (and files (car files))
  10111. files
  10112. (org-agenda-files))))))))
  10113. (defun org-make-tags-matcher (match)
  10114. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10115. ;; todo-only is scoped dynamically into this function, and the function
  10116. ;; may change it if the matcher asks for it.
  10117. (unless match
  10118. ;; Get a new match request, with completion
  10119. (let ((org-last-tags-completion-table
  10120. (org-global-tags-completion-table)))
  10121. (setq match (org-completing-read-no-i
  10122. "Match: " 'org-tags-completion-function nil nil nil
  10123. 'org-tags-history))))
  10124. ;; Parse the string and create a lisp form
  10125. (let ((match0 match)
  10126. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10127. minus tag mm
  10128. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10129. orterms term orlist re-p str-p level-p level-op time-p
  10130. prop-p pn pv po cat-p gv rest)
  10131. (if (string-match "/+" match)
  10132. ;; match contains also a todo-matching request
  10133. (progn
  10134. (setq tagsmatch (substring match 0 (match-beginning 0))
  10135. todomatch (substring match (match-end 0)))
  10136. (if (string-match "^!" todomatch)
  10137. (setq todo-only t todomatch (substring todomatch 1)))
  10138. (if (string-match "^\\s-*$" todomatch)
  10139. (setq todomatch nil)))
  10140. ;; only matching tags
  10141. (setq tagsmatch match todomatch nil))
  10142. ;; Make the tags matcher
  10143. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10144. (setq tagsmatcher t)
  10145. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10146. (while (setq term (pop orterms))
  10147. (while (and (equal (substring term -1) "\\") orterms)
  10148. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10149. (while (string-match re term)
  10150. (setq rest (substring term (match-end 0))
  10151. minus (and (match-end 1)
  10152. (equal (match-string 1 term) "-"))
  10153. tag (match-string 2 term)
  10154. re-p (equal (string-to-char tag) ?{)
  10155. level-p (match-end 4)
  10156. prop-p (match-end 5)
  10157. mm (cond
  10158. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10159. (level-p
  10160. (setq level-op (org-op-to-function (match-string 3 term)))
  10161. `(,level-op level ,(string-to-number
  10162. (match-string 4 term))))
  10163. (prop-p
  10164. (setq pn (match-string 5 term)
  10165. po (match-string 6 term)
  10166. pv (match-string 7 term)
  10167. cat-p (equal pn "CATEGORY")
  10168. re-p (equal (string-to-char pv) ?{)
  10169. str-p (equal (string-to-char pv) ?\")
  10170. time-p (save-match-data
  10171. (string-match "^\"[[<].*[]>]\"$" pv))
  10172. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10173. (if time-p (setq pv (org-matcher-time pv)))
  10174. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10175. (cond
  10176. ((equal pn "CATEGORY")
  10177. (setq gv '(get-text-property (point) 'org-category)))
  10178. ((equal pn "TODO")
  10179. (setq gv 'todo))
  10180. (t
  10181. (setq gv `(org-cached-entry-get nil ,pn))))
  10182. (if re-p
  10183. (if (eq po 'org<>)
  10184. `(not (string-match ,pv (or ,gv "")))
  10185. `(string-match ,pv (or ,gv "")))
  10186. (if str-p
  10187. `(,po (or ,gv "") ,pv)
  10188. `(,po (string-to-number (or ,gv ""))
  10189. ,(string-to-number pv) ))))
  10190. (t `(member ,tag tags-list)))
  10191. mm (if minus (list 'not mm) mm)
  10192. term rest)
  10193. (push mm tagsmatcher))
  10194. (push (if (> (length tagsmatcher) 1)
  10195. (cons 'and tagsmatcher)
  10196. (car tagsmatcher))
  10197. orlist)
  10198. (setq tagsmatcher nil))
  10199. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10200. (setq tagsmatcher
  10201. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10202. ;; Make the todo matcher
  10203. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10204. (setq todomatcher t)
  10205. (setq orterms (org-split-string todomatch "|") orlist nil)
  10206. (while (setq term (pop orterms))
  10207. (while (string-match re term)
  10208. (setq minus (and (match-end 1)
  10209. (equal (match-string 1 term) "-"))
  10210. kwd (match-string 2 term)
  10211. re-p (equal (string-to-char kwd) ?{)
  10212. term (substring term (match-end 0))
  10213. mm (if re-p
  10214. `(string-match ,(substring kwd 1 -1) todo)
  10215. (list 'equal 'todo kwd))
  10216. mm (if minus (list 'not mm) mm))
  10217. (push mm todomatcher))
  10218. (push (if (> (length todomatcher) 1)
  10219. (cons 'and todomatcher)
  10220. (car todomatcher))
  10221. orlist)
  10222. (setq todomatcher nil))
  10223. (setq todomatcher (if (> (length orlist) 1)
  10224. (cons 'or orlist) (car orlist))))
  10225. ;; Return the string and lisp forms of the matcher
  10226. (setq matcher (if todomatcher
  10227. (list 'and tagsmatcher todomatcher)
  10228. tagsmatcher))
  10229. (cons match0 matcher)))
  10230. (defun org-op-to-function (op &optional stringp)
  10231. "Turn an operator into the appropriate function."
  10232. (setq op
  10233. (cond
  10234. ((equal op "<" ) '(< string< org-time<))
  10235. ((equal op ">" ) '(> org-string> org-time>))
  10236. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10237. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10238. ((member op '("=" "==")) '(= string= org-time=))
  10239. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10240. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10241. (defun org<> (a b) (not (= a b)))
  10242. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10243. (defun org-string>= (a b) (not (string< a b)))
  10244. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10245. (defun org-string<> (a b) (not (string= a b)))
  10246. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10247. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10248. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10249. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10250. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10251. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10252. (defun org-2ft (s)
  10253. "Convert S to a floating point time.
  10254. If S is already a number, just return it. If it is a string, parse
  10255. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10256. (cond
  10257. ((numberp s) s)
  10258. ((stringp s)
  10259. (condition-case nil
  10260. (float-time (apply 'encode-time (org-parse-time-string s)))
  10261. (error 0.)))
  10262. (t 0.)))
  10263. (defun org-time-today ()
  10264. "Time in seconds today at 0:00.
  10265. Returns the float number of seconds since the beginning of the
  10266. epoch to the beginning of today (00:00)."
  10267. (float-time (apply 'encode-time
  10268. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10269. (defun org-matcher-time (s)
  10270. "Interpret a time comparison value."
  10271. (save-match-data
  10272. (cond
  10273. ((string= s "<now>") (float-time))
  10274. ((string= s "<today>") (org-time-today))
  10275. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10276. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10277. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10278. (+ (org-time-today)
  10279. (* (string-to-number (match-string 1 s))
  10280. (cdr (assoc (match-string 2 s)
  10281. '(("d" . 86400.0) ("w" . 604800.0)
  10282. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10283. (t (org-2ft s)))))
  10284. (defun org-match-any-p (re list)
  10285. "Does re match any element of list?"
  10286. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10287. (delq nil list))
  10288. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10289. (defvar org-tags-overlay (org-make-overlay 1 1))
  10290. (org-detach-overlay org-tags-overlay)
  10291. (defun org-get-local-tags-at (&optional pos)
  10292. "Get a list of tags defined in the current headline."
  10293. (org-get-tags-at pos 'local))
  10294. (defun org-get-local-tags ()
  10295. "Get a list of tags defined in the current headline."
  10296. (org-get-tags-at nil 'local))
  10297. (defun org-get-tags-at (&optional pos local)
  10298. "Get a list of all headline tags applicable at POS.
  10299. POS defaults to point. If tags are inherited, the list contains
  10300. the targets in the same sequence as the headlines appear, i.e.
  10301. the tags of the current headline come last.
  10302. When LOCAL is non-nil, only return tags from the current headline,
  10303. ignore inherited ones."
  10304. (interactive)
  10305. (if (and org-trust-scanner-tags
  10306. (or (not pos) (equal pos (point)))
  10307. (not local))
  10308. org-scanner-tags
  10309. (let (tags ltags lastpos parent)
  10310. (save-excursion
  10311. (save-restriction
  10312. (widen)
  10313. (goto-char (or pos (point)))
  10314. (save-match-data
  10315. (catch 'done
  10316. (condition-case nil
  10317. (progn
  10318. (org-back-to-heading t)
  10319. (while (not (equal lastpos (point)))
  10320. (setq lastpos (point))
  10321. (when (looking-at
  10322. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10323. (setq ltags (org-split-string
  10324. (org-match-string-no-properties 1) ":"))
  10325. (when parent
  10326. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10327. (setq tags (append
  10328. (if parent
  10329. (org-remove-uniherited-tags ltags)
  10330. ltags)
  10331. tags)))
  10332. (or org-use-tag-inheritance (throw 'done t))
  10333. (if local (throw 'done t))
  10334. (or (org-up-heading-safe) (error nil))
  10335. (setq parent t)))
  10336. (error nil)))))
  10337. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10338. (defun org-add-prop-inherited (s)
  10339. (add-text-properties 0 (length s) '(inherited t) s)
  10340. s)
  10341. (defun org-toggle-tag (tag &optional onoff)
  10342. "Toggle the tag TAG for the current line.
  10343. If ONOFF is `on' or `off', don't toggle but set to this state."
  10344. (let (res current)
  10345. (save-excursion
  10346. (org-back-to-heading t)
  10347. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10348. (point-at-eol) t)
  10349. (progn
  10350. (setq current (match-string 1))
  10351. (replace-match ""))
  10352. (setq current ""))
  10353. (setq current (nreverse (org-split-string current ":")))
  10354. (cond
  10355. ((eq onoff 'on)
  10356. (setq res t)
  10357. (or (member tag current) (push tag current)))
  10358. ((eq onoff 'off)
  10359. (or (not (member tag current)) (setq current (delete tag current))))
  10360. (t (if (member tag current)
  10361. (setq current (delete tag current))
  10362. (setq res t)
  10363. (push tag current))))
  10364. (end-of-line 1)
  10365. (if current
  10366. (progn
  10367. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10368. (org-set-tags nil t))
  10369. (delete-horizontal-space))
  10370. (run-hooks 'org-after-tags-change-hook))
  10371. res))
  10372. (defun org-align-tags-here (to-col)
  10373. ;; Assumes that this is a headline
  10374. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10375. (beginning-of-line 1)
  10376. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10377. (< pos (match-beginning 2)))
  10378. (progn
  10379. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10380. (goto-char (match-beginning 1))
  10381. (insert " ")
  10382. (delete-region (point) (1+ (match-beginning 2)))
  10383. (setq ncol (max (1+ (current-column))
  10384. (1+ col)
  10385. (if (> to-col 0)
  10386. to-col
  10387. (- (abs to-col) tags-l))))
  10388. (setq p (point))
  10389. (insert (make-string (- ncol (current-column)) ?\ ))
  10390. (setq ncol (current-column))
  10391. (when indent-tabs-mode (tabify p (point-at-eol)))
  10392. (org-move-to-column (min ncol col) t))
  10393. (goto-char pos))))
  10394. (defun org-set-tags-command (&optional arg just-align)
  10395. "Call the set-tags command for the current entry."
  10396. (interactive "P")
  10397. (if (org-on-heading-p)
  10398. (org-set-tags arg just-align)
  10399. (save-excursion
  10400. (org-back-to-heading t)
  10401. (org-set-tags arg just-align))))
  10402. (defun org-set-tags-to (data)
  10403. "Set the tags of the current entry to DATA, replacing the current tags.
  10404. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10405. If DATA is nil or the empty string, any tags will be removed."
  10406. (interactive "sTags: ")
  10407. (setq data
  10408. (cond
  10409. ((eq data nil) "")
  10410. ((equal data "") "")
  10411. ((stringp data)
  10412. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10413. ":"))
  10414. ((listp data)
  10415. (concat ":" (mapconcat 'identity data ":") ":"))
  10416. (t nil)))
  10417. (when data
  10418. (save-excursion
  10419. (org-back-to-heading t)
  10420. (when (looking-at org-complex-heading-regexp)
  10421. (if (match-end 5)
  10422. (progn
  10423. (goto-char (match-beginning 5))
  10424. (insert data)
  10425. (delete-region (point) (point-at-eol))
  10426. (org-set-tags nil 'align))
  10427. (goto-char (point-at-eol))
  10428. (insert " " data)
  10429. (org-set-tags nil 'align)))
  10430. (beginning-of-line 1)
  10431. (if (looking-at ".*?\\([ \t]+\\)$")
  10432. (delete-region (match-beginning 1) (match-end 1))))))
  10433. (defun org-set-tags (&optional arg just-align)
  10434. "Set the tags for the current headline.
  10435. With prefix ARG, realign all tags in headings in the current buffer."
  10436. (interactive "P")
  10437. (let* ((re (concat "^" outline-regexp))
  10438. (current (org-get-tags-string))
  10439. (col (current-column))
  10440. (org-setting-tags t)
  10441. table current-tags inherited-tags ; computed below when needed
  10442. tags p0 c0 c1 rpl)
  10443. (if arg
  10444. (save-excursion
  10445. (goto-char (point-min))
  10446. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10447. (while (re-search-forward re nil t)
  10448. (org-set-tags nil t)
  10449. (end-of-line 1)))
  10450. (message "All tags realigned to column %d" org-tags-column))
  10451. (if just-align
  10452. (setq tags current)
  10453. ;; Get a new set of tags from the user
  10454. (save-excursion
  10455. (setq table (append org-tag-persistent-alist
  10456. (or org-tag-alist (org-get-buffer-tags)))
  10457. org-last-tags-completion-table table
  10458. current-tags (org-split-string current ":")
  10459. inherited-tags (nreverse
  10460. (nthcdr (length current-tags)
  10461. (nreverse (org-get-tags-at))))
  10462. tags
  10463. (if (or (eq t org-use-fast-tag-selection)
  10464. (and org-use-fast-tag-selection
  10465. (delq nil (mapcar 'cdr table))))
  10466. (org-fast-tag-selection
  10467. current-tags inherited-tags table
  10468. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10469. (let ((org-add-colon-after-tag-completion t))
  10470. (org-trim
  10471. (org-without-partial-completion
  10472. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10473. nil nil current 'org-tags-history)))))))
  10474. (while (string-match "[-+&]+" tags)
  10475. ;; No boolean logic, just a list
  10476. (setq tags (replace-match ":" t t tags))))
  10477. (if org-tags-sort-function
  10478. (setq tags (mapconcat 'identity
  10479. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10480. org-tags-sort-function) ":")))
  10481. (if (string-match "\\`[\t ]*\\'" tags)
  10482. (setq tags "")
  10483. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10484. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10485. ;; Insert new tags at the correct column
  10486. (beginning-of-line 1)
  10487. (cond
  10488. ((and (equal current "") (equal tags "")))
  10489. ((re-search-forward
  10490. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10491. (point-at-eol) t)
  10492. (if (equal tags "")
  10493. (setq rpl "")
  10494. (goto-char (match-beginning 0))
  10495. (setq c0 (current-column) p0 (point)
  10496. c1 (max (1+ c0) (if (> org-tags-column 0)
  10497. org-tags-column
  10498. (- (- org-tags-column) (length tags))))
  10499. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10500. (replace-match rpl t t)
  10501. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10502. tags)
  10503. (t (error "Tags alignment failed")))
  10504. (org-move-to-column col)
  10505. (unless just-align
  10506. (run-hooks 'org-after-tags-change-hook)))))
  10507. (defun org-change-tag-in-region (beg end tag off)
  10508. "Add or remove TAG for each entry in the region.
  10509. This works in the agenda, and also in an org-mode buffer."
  10510. (interactive
  10511. (list (region-beginning) (region-end)
  10512. (let ((org-last-tags-completion-table
  10513. (if (org-mode-p)
  10514. (org-get-buffer-tags)
  10515. (org-global-tags-completion-table))))
  10516. (org-icompleting-read
  10517. "Tag: " 'org-tags-completion-function nil nil nil
  10518. 'org-tags-history))
  10519. (progn
  10520. (message "[s]et or [r]emove? ")
  10521. (equal (read-char-exclusive) ?r))))
  10522. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10523. (let ((agendap (equal major-mode 'org-agenda-mode))
  10524. l1 l2 m buf pos newhead (cnt 0))
  10525. (goto-char end)
  10526. (setq l2 (1- (org-current-line)))
  10527. (goto-char beg)
  10528. (setq l1 (org-current-line))
  10529. (loop for l from l1 to l2 do
  10530. (org-goto-line l)
  10531. (setq m (get-text-property (point) 'org-hd-marker))
  10532. (when (or (and (org-mode-p) (org-on-heading-p))
  10533. (and agendap m))
  10534. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10535. pos (if agendap m (point)))
  10536. (with-current-buffer buf
  10537. (save-excursion
  10538. (save-restriction
  10539. (goto-char pos)
  10540. (setq cnt (1+ cnt))
  10541. (org-toggle-tag tag (if off 'off 'on))
  10542. (setq newhead (org-get-heading)))))
  10543. (and agendap (org-agenda-change-all-lines newhead m))))
  10544. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10545. (defun org-tags-completion-function (string predicate &optional flag)
  10546. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10547. (confirm (lambda (x) (stringp (car x)))))
  10548. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10549. (setq s1 (match-string 1 string)
  10550. s2 (match-string 2 string))
  10551. (setq s1 "" s2 string))
  10552. (cond
  10553. ((eq flag nil)
  10554. ;; try completion
  10555. (setq rtn (try-completion s2 ctable confirm))
  10556. (if (stringp rtn)
  10557. (setq rtn
  10558. (concat s1 s2 (substring rtn (length s2))
  10559. (if (and org-add-colon-after-tag-completion
  10560. (assoc rtn ctable))
  10561. ":" ""))))
  10562. rtn)
  10563. ((eq flag t)
  10564. ;; all-completions
  10565. (all-completions s2 ctable confirm)
  10566. )
  10567. ((eq flag 'lambda)
  10568. ;; exact match?
  10569. (assoc s2 ctable)))
  10570. ))
  10571. (defun org-fast-tag-insert (kwd tags face &optional end)
  10572. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10573. (insert (format "%-12s" (concat kwd ":"))
  10574. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10575. (or end "")))
  10576. (defun org-fast-tag-show-exit (flag)
  10577. (save-excursion
  10578. (org-goto-line 3)
  10579. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10580. (replace-match ""))
  10581. (when flag
  10582. (end-of-line 1)
  10583. (org-move-to-column (- (window-width) 19) t)
  10584. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10585. (defun org-set-current-tags-overlay (current prefix)
  10586. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10587. (if (featurep 'xemacs)
  10588. (org-overlay-display org-tags-overlay (concat prefix s)
  10589. 'secondary-selection)
  10590. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10591. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10592. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10593. "Fast tag selection with single keys.
  10594. CURRENT is the current list of tags in the headline, INHERITED is the
  10595. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10596. possibly with grouping information. TODO-TABLE is a similar table with
  10597. TODO keywords, should these have keys assigned to them.
  10598. If the keys are nil, a-z are automatically assigned.
  10599. Returns the new tags string, or nil to not change the current settings."
  10600. (let* ((fulltable (append table todo-table))
  10601. (maxlen (apply 'max (mapcar
  10602. (lambda (x)
  10603. (if (stringp (car x)) (string-width (car x)) 0))
  10604. fulltable)))
  10605. (buf (current-buffer))
  10606. (expert (eq org-fast-tag-selection-single-key 'expert))
  10607. (buffer-tags nil)
  10608. (fwidth (+ maxlen 3 1 3))
  10609. (ncol (/ (- (window-width) 4) fwidth))
  10610. (i-face 'org-done)
  10611. (c-face 'org-todo)
  10612. tg cnt e c char c1 c2 ntable tbl rtn
  10613. ov-start ov-end ov-prefix
  10614. (exit-after-next org-fast-tag-selection-single-key)
  10615. (done-keywords org-done-keywords)
  10616. groups ingroup)
  10617. (save-excursion
  10618. (beginning-of-line 1)
  10619. (if (looking-at
  10620. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10621. (setq ov-start (match-beginning 1)
  10622. ov-end (match-end 1)
  10623. ov-prefix "")
  10624. (setq ov-start (1- (point-at-eol))
  10625. ov-end (1+ ov-start))
  10626. (skip-chars-forward "^\n\r")
  10627. (setq ov-prefix
  10628. (concat
  10629. (buffer-substring (1- (point)) (point))
  10630. (if (> (current-column) org-tags-column)
  10631. " "
  10632. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10633. (org-move-overlay org-tags-overlay ov-start ov-end)
  10634. (save-window-excursion
  10635. (if expert
  10636. (set-buffer (get-buffer-create " *Org tags*"))
  10637. (delete-other-windows)
  10638. (split-window-vertically)
  10639. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10640. (erase-buffer)
  10641. (org-set-local 'org-done-keywords done-keywords)
  10642. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10643. (org-fast-tag-insert "Current" current c-face "\n\n")
  10644. (org-fast-tag-show-exit exit-after-next)
  10645. (org-set-current-tags-overlay current ov-prefix)
  10646. (setq tbl fulltable char ?a cnt 0)
  10647. (while (setq e (pop tbl))
  10648. (cond
  10649. ((equal (car e) :startgroup)
  10650. (push '() groups) (setq ingroup t)
  10651. (when (not (= cnt 0))
  10652. (setq cnt 0)
  10653. (insert "\n"))
  10654. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  10655. ((equal (car e) :endgroup)
  10656. (setq ingroup nil cnt 0)
  10657. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  10658. ((equal e '(:newline))
  10659. (when (not (= cnt 0))
  10660. (setq cnt 0)
  10661. (insert "\n")
  10662. (setq e (car tbl))
  10663. (while (equal (car tbl) '(:newline))
  10664. (insert "\n")
  10665. (setq tbl (cdr tbl)))))
  10666. (t
  10667. (setq tg (copy-sequence (car e)) c2 nil)
  10668. (if (cdr e)
  10669. (setq c (cdr e))
  10670. ;; automatically assign a character.
  10671. (setq c1 (string-to-char
  10672. (downcase (substring
  10673. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10674. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10675. (while (or (rassoc char ntable) (rassoc char table))
  10676. (setq char (1+ char)))
  10677. (setq c2 c1))
  10678. (setq c (or c2 char)))
  10679. (if ingroup (push tg (car groups)))
  10680. (setq tg (org-add-props tg nil 'face
  10681. (cond
  10682. ((not (assoc tg table))
  10683. (org-get-todo-face tg))
  10684. ((member tg current) c-face)
  10685. ((member tg inherited) i-face)
  10686. (t nil))))
  10687. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10688. (insert "[" c "] " tg (make-string
  10689. (- fwidth 4 (length tg)) ?\ ))
  10690. (push (cons tg c) ntable)
  10691. (when (= (setq cnt (1+ cnt)) ncol)
  10692. (insert "\n")
  10693. (if ingroup (insert " "))
  10694. (setq cnt 0)))))
  10695. (setq ntable (nreverse ntable))
  10696. (insert "\n")
  10697. (goto-char (point-min))
  10698. (if (not expert) (org-fit-window-to-buffer))
  10699. (setq rtn
  10700. (catch 'exit
  10701. (while t
  10702. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  10703. (if (not groups) "no " "")
  10704. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10705. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10706. (cond
  10707. ((= c ?\r) (throw 'exit t))
  10708. ((= c ?!)
  10709. (setq groups (not groups))
  10710. (goto-char (point-min))
  10711. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10712. ((= c ?\C-c)
  10713. (if (not expert)
  10714. (org-fast-tag-show-exit
  10715. (setq exit-after-next (not exit-after-next)))
  10716. (setq expert nil)
  10717. (delete-other-windows)
  10718. (split-window-vertically)
  10719. (org-switch-to-buffer-other-window " *Org tags*")
  10720. (org-fit-window-to-buffer)))
  10721. ((or (= c ?\C-g)
  10722. (and (= c ?q) (not (rassoc c ntable))))
  10723. (org-detach-overlay org-tags-overlay)
  10724. (setq quit-flag t))
  10725. ((= c ?\ )
  10726. (setq current nil)
  10727. (if exit-after-next (setq exit-after-next 'now)))
  10728. ((= c ?\t)
  10729. (condition-case nil
  10730. (setq tg (org-icompleting-read
  10731. "Tag: "
  10732. (or buffer-tags
  10733. (with-current-buffer buf
  10734. (org-get-buffer-tags)))))
  10735. (quit (setq tg "")))
  10736. (when (string-match "\\S-" tg)
  10737. (add-to-list 'buffer-tags (list tg))
  10738. (if (member tg current)
  10739. (setq current (delete tg current))
  10740. (push tg current)))
  10741. (if exit-after-next (setq exit-after-next 'now)))
  10742. ((setq e (rassoc c todo-table) tg (car e))
  10743. (with-current-buffer buf
  10744. (save-excursion (org-todo tg)))
  10745. (if exit-after-next (setq exit-after-next 'now)))
  10746. ((setq e (rassoc c ntable) tg (car e))
  10747. (if (member tg current)
  10748. (setq current (delete tg current))
  10749. (loop for g in groups do
  10750. (if (member tg g)
  10751. (mapc (lambda (x)
  10752. (setq current (delete x current)))
  10753. g)))
  10754. (push tg current))
  10755. (if exit-after-next (setq exit-after-next 'now))))
  10756. ;; Create a sorted list
  10757. (setq current
  10758. (sort current
  10759. (lambda (a b)
  10760. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10761. (if (eq exit-after-next 'now) (throw 'exit t))
  10762. (goto-char (point-min))
  10763. (beginning-of-line 2)
  10764. (delete-region (point) (point-at-eol))
  10765. (org-fast-tag-insert "Current" current c-face)
  10766. (org-set-current-tags-overlay current ov-prefix)
  10767. (while (re-search-forward
  10768. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10769. (setq tg (match-string 1))
  10770. (add-text-properties
  10771. (match-beginning 1) (match-end 1)
  10772. (list 'face
  10773. (cond
  10774. ((member tg current) c-face)
  10775. ((member tg inherited) i-face)
  10776. (t (get-text-property (match-beginning 1) 'face))))))
  10777. (goto-char (point-min)))))
  10778. (org-detach-overlay org-tags-overlay)
  10779. (if rtn
  10780. (mapconcat 'identity current ":")
  10781. nil))))
  10782. (defun org-get-tags-string ()
  10783. "Get the TAGS string in the current headline."
  10784. (unless (org-on-heading-p t)
  10785. (error "Not on a heading"))
  10786. (save-excursion
  10787. (beginning-of-line 1)
  10788. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10789. (org-match-string-no-properties 1)
  10790. "")))
  10791. (defun org-get-tags ()
  10792. "Get the list of tags specified in the current headline."
  10793. (org-split-string (org-get-tags-string) ":"))
  10794. (defun org-get-buffer-tags ()
  10795. "Get a table of all tags used in the buffer, for completion."
  10796. (let (tags)
  10797. (save-excursion
  10798. (goto-char (point-min))
  10799. (while (re-search-forward
  10800. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10801. (when (equal (char-after (point-at-bol 0)) ?*)
  10802. (mapc (lambda (x) (add-to-list 'tags x))
  10803. (org-split-string (org-match-string-no-properties 1) ":")))))
  10804. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  10805. (mapcar 'list tags)))
  10806. ;;;; The mapping API
  10807. ;;;###autoload
  10808. (defun org-map-entries (func &optional match scope &rest skip)
  10809. "Call FUNC at each headline selected by MATCH in SCOPE.
  10810. FUNC is a function or a lisp form. The function will be called without
  10811. arguments, with the cursor positioned at the beginning of the headline.
  10812. The return values of all calls to the function will be collected and
  10813. returned as a list.
  10814. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10815. does not need to preserve point. After evaluation, the cursor will be
  10816. moved to the end of the line (presumably of the headline of the
  10817. processed entry) and search continues from there. Under some
  10818. circumstances, this may not produce the wanted results. For example,
  10819. if you have removed (e.g. archived) the current (sub)tree it could
  10820. mean that the next entry will be skipped entirely. In such cases, you
  10821. can specify the position from where search should continue by making
  10822. FUNC set the variable `org-map-continue-from' to the desired buffer
  10823. position.
  10824. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10825. Only headlines that are matched by this query will be considered during
  10826. the iteration. When MATCH is nil or t, all headlines will be
  10827. visited by the iteration.
  10828. SCOPE determines the scope of this command. It can be any of:
  10829. nil The current buffer, respecting the restriction if any
  10830. tree The subtree started with the entry at point
  10831. file The current buffer, without restriction
  10832. file-with-archives
  10833. The current buffer, and any archives associated with it
  10834. agenda All agenda files
  10835. agenda-with-archives
  10836. All agenda files with any archive files associated with them
  10837. \(file1 file2 ...)
  10838. If this is a list, all files in the list will be scanned
  10839. The remaining args are treated as settings for the skipping facilities of
  10840. the scanner. The following items can be given here:
  10841. archive skip trees with the archive tag.
  10842. comment skip trees with the COMMENT keyword
  10843. function or Emacs Lisp form:
  10844. will be used as value for `org-agenda-skip-function', so whenever
  10845. the function returns t, FUNC will not be called for that
  10846. entry and search will continue from the point where the
  10847. function leaves it.
  10848. If your function needs to retrieve the tags including inherited tags
  10849. at the *current* entry, you can use the value of the variable
  10850. `org-scanner-tags' which will be much faster than getting the value
  10851. with `org-get-tags-at'. If your function gets properties with
  10852. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10853. to t around the call to `org-entry-properties' to get the same speedup.
  10854. Note that if your function moves around to retrieve tags and properties at
  10855. a *different* entry, you cannot use these techniques."
  10856. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10857. (org-agenda-skip-archived-trees (memq 'archive skip))
  10858. (org-agenda-skip-comment-trees (memq 'comment skip))
  10859. (org-agenda-skip-function
  10860. (car (org-delete-all '(comment archive) skip)))
  10861. (org-tags-match-list-sublevels t)
  10862. matcher file res
  10863. org-todo-keywords-for-agenda
  10864. org-done-keywords-for-agenda
  10865. org-todo-keyword-alist-for-agenda
  10866. org-drawers-for-agenda
  10867. org-tag-alist-for-agenda)
  10868. (cond
  10869. ((eq match t) (setq matcher t))
  10870. ((eq match nil) (setq matcher t))
  10871. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10872. (save-excursion
  10873. (save-restriction
  10874. (when (eq scope 'tree)
  10875. (org-back-to-heading t)
  10876. (org-narrow-to-subtree)
  10877. (setq scope nil))
  10878. (if (not scope)
  10879. (progn
  10880. (org-prepare-agenda-buffers
  10881. (list (buffer-file-name (current-buffer))))
  10882. (setq res (org-scan-tags func matcher)))
  10883. ;; Get the right scope
  10884. (cond
  10885. ((and scope (listp scope) (symbolp (car scope)))
  10886. (setq scope (eval scope)))
  10887. ((eq scope 'agenda)
  10888. (setq scope (org-agenda-files t)))
  10889. ((eq scope 'agenda-with-archives)
  10890. (setq scope (org-agenda-files t))
  10891. (setq scope (org-add-archive-files scope)))
  10892. ((eq scope 'file)
  10893. (setq scope (list (buffer-file-name))))
  10894. ((eq scope 'file-with-archives)
  10895. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10896. (org-prepare-agenda-buffers scope)
  10897. (while (setq file (pop scope))
  10898. (with-current-buffer (org-find-base-buffer-visiting file)
  10899. (save-excursion
  10900. (save-restriction
  10901. (widen)
  10902. (goto-char (point-min))
  10903. (setq res (append res (org-scan-tags func matcher))))))))))
  10904. res))
  10905. ;;;; Properties
  10906. ;;; Setting and retrieving properties
  10907. (defconst org-special-properties
  10908. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10909. "TIMESTAMP" "TIMESTAMP_IA")
  10910. "The special properties valid in Org-mode.
  10911. These are properties that are not defined in the property drawer,
  10912. but in some other way.")
  10913. (defconst org-default-properties
  10914. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10915. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10916. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10917. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10918. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10919. "CLOCK_MODELINE_TOTAL" "STYLE")
  10920. "Some properties that are used by Org-mode for various purposes.
  10921. Being in this list makes sure that they are offered for completion.")
  10922. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10923. "Regular expression matching the first line of a property drawer.")
  10924. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10925. "Regular expression matching the first line of a property drawer.")
  10926. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10927. "Regular expression matching the first line of a property drawer.")
  10928. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10929. "Regular expression matching the first line of a property drawer.")
  10930. (defconst org-property-drawer-re
  10931. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10932. org-property-end-re "\\)\n?")
  10933. "Matches an entire property drawer.")
  10934. (defconst org-clock-drawer-re
  10935. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10936. org-property-end-re "\\)\n?")
  10937. "Matches an entire clock drawer.")
  10938. (defun org-property-action ()
  10939. "Do an action on properties."
  10940. (interactive)
  10941. (let (c)
  10942. (org-at-property-p)
  10943. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10944. (setq c (read-char-exclusive))
  10945. (cond
  10946. ((equal c ?s)
  10947. (call-interactively 'org-set-property))
  10948. ((equal c ?d)
  10949. (call-interactively 'org-delete-property))
  10950. ((equal c ?D)
  10951. (call-interactively 'org-delete-property-globally))
  10952. ((equal c ?c)
  10953. (call-interactively 'org-compute-property-at-point))
  10954. (t (error "No such property action %c" c)))))
  10955. (defun org-set-effort (&optional value)
  10956. "Set the effort property of the current entry.
  10957. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  10958. allowed value."
  10959. (interactive "P")
  10960. (if (equal value 0) (setq value 10))
  10961. (let* ((completion-ignore-case t)
  10962. (prop org-effort-property)
  10963. (cur (org-entry-get nil prop))
  10964. (allowed (org-property-get-allowed-values nil prop 'table))
  10965. (existing (mapcar 'list (org-property-values prop)))
  10966. (val (cond
  10967. ((stringp value) value)
  10968. ((and allowed (integerp value))
  10969. (or (car (nth (1- value) allowed))
  10970. (car (org-last allowed))))
  10971. (allowed
  10972. (message "Select 1-9,0, [RET%s]: %s"
  10973. (if cur (concat "=" cur) "")
  10974. (mapconcat 'car allowed " "))
  10975. (setq rpl (read-char-exclusive))
  10976. (if (equal rpl ?\r)
  10977. (setq val cur)
  10978. (setq rpl (- rpl ?0))
  10979. (if (equal rpl 0) (setq rpl 10))
  10980. (if (and (> rpl 0) (<= rpl (length allowed)))
  10981. (car (nth (1- rpl) allowed))
  10982. (org-completing-read "Value: " allowed nil))))
  10983. (t
  10984. (let (org-completion-use-ido org-completion-use-iswitchb)
  10985. (org-completing-read
  10986. (concat "Value " (if (and cur (string-match "\\S-" cur))
  10987. (concat "[" cur "]") "")
  10988. ": ")
  10989. existing nil nil "" nil cur))))))
  10990. (unless (equal (org-entry-get nil prop) val)
  10991. (org-entry-put nil prop val))
  10992. (message "%s is now %s" prop val)))
  10993. (defun org-at-property-p ()
  10994. "Is the cursor in a property line?"
  10995. ;; FIXME: Does not check if we are actually in the drawer.
  10996. ;; FIXME: also returns true on any drawers.....
  10997. ;; This is used by C-c C-c for property action.
  10998. (save-excursion
  10999. (beginning-of-line 1)
  11000. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  11001. (defun org-get-property-block (&optional beg end force)
  11002. "Return the (beg . end) range of the body of the property drawer.
  11003. BEG and END can be beginning and end of subtree, if not given
  11004. they will be found.
  11005. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11006. (catch 'exit
  11007. (save-excursion
  11008. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11009. (end (or end (progn (outline-next-heading) (point)))))
  11010. (goto-char beg)
  11011. (if (re-search-forward org-property-start-re end t)
  11012. (setq beg (1+ (match-end 0)))
  11013. (if force
  11014. (save-excursion
  11015. (org-insert-property-drawer)
  11016. (setq end (progn (outline-next-heading) (point))))
  11017. (throw 'exit nil))
  11018. (goto-char beg)
  11019. (if (re-search-forward org-property-start-re end t)
  11020. (setq beg (1+ (match-end 0)))))
  11021. (if (re-search-forward org-property-end-re end t)
  11022. (setq end (match-beginning 0))
  11023. (or force (throw 'exit nil))
  11024. (goto-char beg)
  11025. (setq end beg)
  11026. (org-indent-line-function)
  11027. (insert ":END:\n"))
  11028. (cons beg end)))))
  11029. (defun org-entry-properties (&optional pom which)
  11030. "Get all properties of the entry at point-or-marker POM.
  11031. This includes the TODO keyword, the tags, time strings for deadline,
  11032. scheduled, and clocking, and any additional properties defined in the
  11033. entry. The return value is an alist, keys may occur multiple times
  11034. if the property key was used several times.
  11035. POM may also be nil, in which case the current entry is used.
  11036. If WHICH is nil or `all', get all properties. If WHICH is
  11037. `special' or `standard', only get that subclass."
  11038. (setq which (or which 'all))
  11039. (org-with-point-at pom
  11040. (let ((clockstr (substring org-clock-string 0 -1))
  11041. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  11042. beg end range props sum-props key value string clocksum)
  11043. (save-excursion
  11044. (when (condition-case nil
  11045. (and (org-mode-p) (org-back-to-heading t))
  11046. (error nil))
  11047. (setq beg (point))
  11048. (setq sum-props (get-text-property (point) 'org-summaries))
  11049. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11050. (outline-next-heading)
  11051. (setq end (point))
  11052. (when (memq which '(all special))
  11053. ;; Get the special properties, like TODO and tags
  11054. (goto-char beg)
  11055. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  11056. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11057. (when (looking-at org-priority-regexp)
  11058. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11059. (when (and (setq value (org-get-tags-string))
  11060. (string-match "\\S-" value))
  11061. (push (cons "TAGS" value) props))
  11062. (when (setq value (org-get-tags-at))
  11063. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  11064. props))
  11065. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11066. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11067. string (if (equal key clockstr)
  11068. (org-no-properties
  11069. (org-trim
  11070. (buffer-substring
  11071. (match-beginning 3) (goto-char (point-at-eol)))))
  11072. (substring (org-match-string-no-properties 3) 1 -1)))
  11073. (unless key
  11074. (if (= (char-after (match-beginning 3)) ?\[)
  11075. (setq key "TIMESTAMP_IA")
  11076. (setq key "TIMESTAMP")))
  11077. (when (or (equal key clockstr) (not (assoc key props)))
  11078. (push (cons key string) props)))
  11079. )
  11080. (when (memq which '(all standard))
  11081. ;; Get the standard properties, like :PROP: ...
  11082. (setq range (org-get-property-block beg end))
  11083. (when range
  11084. (goto-char (car range))
  11085. (while (re-search-forward
  11086. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11087. (cdr range) t)
  11088. (setq key (org-match-string-no-properties 1)
  11089. value (org-trim (or (org-match-string-no-properties 2) "")))
  11090. (unless (member key excluded)
  11091. (push (cons key (or value "")) props)))))
  11092. (if clocksum
  11093. (push (cons "CLOCKSUM"
  11094. (org-columns-number-to-string (/ (float clocksum) 60.)
  11095. 'add_times))
  11096. props))
  11097. (unless (assoc "CATEGORY" props)
  11098. (setq value (or (org-get-category)
  11099. (progn (org-refresh-category-properties)
  11100. (org-get-category))))
  11101. (push (cons "CATEGORY" value) props))
  11102. (append sum-props (nreverse props)))))))
  11103. (defun org-entry-get (pom property &optional inherit)
  11104. "Get value of PROPERTY for entry at point-or-marker POM.
  11105. If INHERIT is non-nil and the entry does not have the property,
  11106. then also check higher levels of the hierarchy.
  11107. If INHERIT is the symbol `selective', use inheritance only if the setting
  11108. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11109. If the property is present but empty, the return value is the empty string.
  11110. If the property is not present at all, nil is returned."
  11111. (org-with-point-at pom
  11112. (if (and inherit (if (eq inherit 'selective)
  11113. (org-property-inherit-p property)
  11114. t))
  11115. (org-entry-get-with-inheritance property)
  11116. (if (member property org-special-properties)
  11117. ;; We need a special property. Use brute force, get all properties.
  11118. (cdr (assoc property (org-entry-properties nil 'special)))
  11119. (let ((range (org-get-property-block)))
  11120. (if (and range
  11121. (goto-char (car range))
  11122. (re-search-forward
  11123. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11124. (cdr range) t))
  11125. ;; Found the property, return it.
  11126. (if (match-end 1)
  11127. (org-match-string-no-properties 1)
  11128. "")))))))
  11129. (defun org-property-or-variable-value (var &optional inherit)
  11130. "Check if there is a property fixing the value of VAR.
  11131. If yes, return this value. If not, return the current value of the variable."
  11132. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11133. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11134. (read prop)
  11135. (symbol-value var))))
  11136. (defun org-entry-delete (pom property)
  11137. "Delete the property PROPERTY from entry at point-or-marker POM."
  11138. (org-with-point-at pom
  11139. (if (member property org-special-properties)
  11140. nil ; cannot delete these properties.
  11141. (let ((range (org-get-property-block)))
  11142. (if (and range
  11143. (goto-char (car range))
  11144. (re-search-forward
  11145. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11146. (cdr range) t))
  11147. (progn
  11148. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11149. t)
  11150. nil)))))
  11151. ;; Multi-values properties are properties that contain multiple values
  11152. ;; These values are assumed to be single words, separated by whitespace.
  11153. (defun org-entry-add-to-multivalued-property (pom property value)
  11154. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11155. (let* ((old (org-entry-get pom property))
  11156. (values (and old (org-split-string old "[ \t]"))))
  11157. (setq value (org-entry-protect-space value))
  11158. (unless (member value values)
  11159. (setq values (cons value values))
  11160. (org-entry-put pom property
  11161. (mapconcat 'identity values " ")))))
  11162. (defun org-entry-remove-from-multivalued-property (pom property value)
  11163. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11164. (let* ((old (org-entry-get pom property))
  11165. (values (and old (org-split-string old "[ \t]"))))
  11166. (setq value (org-entry-protect-space value))
  11167. (when (member value values)
  11168. (setq values (delete value values))
  11169. (org-entry-put pom property
  11170. (mapconcat 'identity values " ")))))
  11171. (defun org-entry-member-in-multivalued-property (pom property value)
  11172. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11173. (let* ((old (org-entry-get pom property))
  11174. (values (and old (org-split-string old "[ \t]"))))
  11175. (setq value (org-entry-protect-space value))
  11176. (member value values)))
  11177. (defun org-entry-get-multivalued-property (pom property)
  11178. "Return a list of values in a multivalued property."
  11179. (let* ((value (org-entry-get pom property))
  11180. (values (and value (org-split-string value "[ \t]"))))
  11181. (mapcar 'org-entry-restore-space values)))
  11182. (defun org-entry-put-multivalued-property (pom property &rest values)
  11183. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11184. VALUES should be a list of strings. Spaces will be protected."
  11185. (org-entry-put pom property
  11186. (mapconcat 'org-entry-protect-space values " "))
  11187. (let* ((value (org-entry-get pom property))
  11188. (values (and value (org-split-string value "[ \t]"))))
  11189. (mapcar 'org-entry-restore-space values)))
  11190. (defun org-entry-protect-space (s)
  11191. "Protect spaces and newline in string S."
  11192. (while (string-match " " s)
  11193. (setq s (replace-match "%20" t t s)))
  11194. (while (string-match "\n" s)
  11195. (setq s (replace-match "%0A" t t s)))
  11196. s)
  11197. (defun org-entry-restore-space (s)
  11198. "Restore spaces and newline in string S."
  11199. (while (string-match "%20" s)
  11200. (setq s (replace-match " " t t s)))
  11201. (while (string-match "%0A" s)
  11202. (setq s (replace-match "\n" t t s)))
  11203. s)
  11204. (defvar org-entry-property-inherited-from (make-marker)
  11205. "Marker pointing to the entry from where a property was inherited.
  11206. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11207. location of the entry where the inheritance search matched. If there was
  11208. no match, the marker will point nowhere.
  11209. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11210. is set.")
  11211. (defun org-entry-get-with-inheritance (property)
  11212. "Get entry property, and search higher levels if not present."
  11213. (move-marker org-entry-property-inherited-from nil)
  11214. (let (tmp)
  11215. (save-excursion
  11216. (save-restriction
  11217. (widen)
  11218. (catch 'ex
  11219. (while t
  11220. (when (setq tmp (org-entry-get nil property))
  11221. (org-back-to-heading t)
  11222. (move-marker org-entry-property-inherited-from (point))
  11223. (throw 'ex tmp))
  11224. (or (org-up-heading-safe) (throw 'ex nil)))))
  11225. (or tmp
  11226. (cdr (assoc property org-file-properties))
  11227. (cdr (assoc property org-global-properties))
  11228. (cdr (assoc property org-global-properties-fixed))))))
  11229. (defun org-entry-put (pom property value)
  11230. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11231. (org-with-point-at pom
  11232. (org-back-to-heading t)
  11233. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11234. range)
  11235. (cond
  11236. ((equal property "TODO")
  11237. (when (and (stringp value) (string-match "\\S-" value)
  11238. (not (member value org-todo-keywords-1)))
  11239. (error "\"%s\" is not a valid TODO state" value))
  11240. (if (or (not value)
  11241. (not (string-match "\\S-" value)))
  11242. (setq value 'none))
  11243. (org-todo value)
  11244. (org-set-tags nil 'align))
  11245. ((equal property "PRIORITY")
  11246. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11247. (string-to-char value) ?\ ))
  11248. (org-set-tags nil 'align))
  11249. ((equal property "SCHEDULED")
  11250. (if (re-search-forward org-scheduled-time-regexp end t)
  11251. (cond
  11252. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11253. ((eq value 'later) (org-timestamp-change 1 'day))
  11254. (t (call-interactively 'org-schedule)))
  11255. (call-interactively 'org-schedule)))
  11256. ((equal property "DEADLINE")
  11257. (if (re-search-forward org-deadline-time-regexp end t)
  11258. (cond
  11259. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11260. ((eq value 'later) (org-timestamp-change 1 'day))
  11261. (t (call-interactively 'org-deadline)))
  11262. (call-interactively 'org-deadline)))
  11263. ((member property org-special-properties)
  11264. (error "The %s property can not yet be set with `org-entry-put'"
  11265. property))
  11266. (t ; a non-special property
  11267. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11268. (setq range (org-get-property-block beg end 'force))
  11269. (goto-char (car range))
  11270. (if (re-search-forward
  11271. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11272. (progn
  11273. (delete-region (match-beginning 1) (match-end 1))
  11274. (goto-char (match-beginning 1)))
  11275. (goto-char (cdr range))
  11276. (insert "\n")
  11277. (backward-char 1)
  11278. (org-indent-line-function)
  11279. (insert ":" property ":"))
  11280. (and value (insert " " value))
  11281. (org-indent-line-function)))))))
  11282. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11283. "Get all property keys in the current buffer.
  11284. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11285. like tags and TODO state.
  11286. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11287. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11288. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11289. formats in the current buffer."
  11290. (let (rtn range cfmt s p)
  11291. (save-excursion
  11292. (save-restriction
  11293. (widen)
  11294. (goto-char (point-min))
  11295. (while (re-search-forward org-property-start-re nil t)
  11296. (setq range (org-get-property-block))
  11297. (goto-char (car range))
  11298. (while (re-search-forward
  11299. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11300. (cdr range) t)
  11301. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11302. (outline-next-heading))))
  11303. (when include-specials
  11304. (setq rtn (append org-special-properties rtn)))
  11305. (when include-defaults
  11306. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11307. (add-to-list 'rtn org-effort-property))
  11308. (when include-columns
  11309. (save-excursion
  11310. (save-restriction
  11311. (widen)
  11312. (goto-char (point-min))
  11313. (while (re-search-forward
  11314. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11315. nil t)
  11316. (setq cfmt (match-string 2) s 0)
  11317. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11318. cfmt s)
  11319. (setq s (match-end 0)
  11320. p (match-string 1 cfmt))
  11321. (unless (or (equal p "ITEM")
  11322. (member p org-special-properties))
  11323. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11324. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11325. (defun org-property-values (key)
  11326. "Return a list of all values of property KEY."
  11327. (save-excursion
  11328. (save-restriction
  11329. (widen)
  11330. (goto-char (point-min))
  11331. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11332. values)
  11333. (while (re-search-forward re nil t)
  11334. (add-to-list 'values (org-trim (match-string 1))))
  11335. (delete "" values)))))
  11336. (defun org-insert-property-drawer ()
  11337. "Insert a property drawer into the current entry."
  11338. (interactive)
  11339. (org-back-to-heading t)
  11340. (looking-at outline-regexp)
  11341. (let ((indent (if org-adapt-indentation
  11342. (- (match-end 0)(match-beginning 0))
  11343. 0))
  11344. (beg (point))
  11345. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11346. end hiddenp)
  11347. (outline-next-heading)
  11348. (setq end (point))
  11349. (goto-char beg)
  11350. (while (re-search-forward re end t))
  11351. (setq hiddenp (org-invisible-p))
  11352. (end-of-line 1)
  11353. (and (equal (char-after) ?\n) (forward-char 1))
  11354. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11355. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11356. ;; just skip this line
  11357. (beginning-of-line 2)
  11358. ;; Drawer start, find the end
  11359. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11360. (beginning-of-line 1)))
  11361. (org-skip-over-state-notes)
  11362. (skip-chars-backward " \t\n\r")
  11363. (if (eq (char-before) ?*) (forward-char 1))
  11364. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11365. (beginning-of-line 0)
  11366. (org-indent-to-column indent)
  11367. (beginning-of-line 2)
  11368. (org-indent-to-column indent)
  11369. (beginning-of-line 0)
  11370. (if hiddenp
  11371. (save-excursion
  11372. (org-back-to-heading t)
  11373. (hide-entry))
  11374. (org-flag-drawer t))))
  11375. (defun org-set-property (property value)
  11376. "In the current entry, set PROPERTY to VALUE.
  11377. When called interactively, this will prompt for a property name, offering
  11378. completion on existing and default properties. And then it will prompt
  11379. for a value, offering completion either on allowed values (via an inherited
  11380. xxx_ALL property) or on existing values in other instances of this property
  11381. in the current file."
  11382. (interactive
  11383. (let* ((completion-ignore-case t)
  11384. (keys (org-buffer-property-keys nil t t))
  11385. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11386. (prop (if (member prop0 keys)
  11387. prop0
  11388. (or (cdr (assoc (downcase prop0)
  11389. (mapcar (lambda (x) (cons (downcase x) x))
  11390. keys)))
  11391. prop0)))
  11392. (cur (org-entry-get nil prop))
  11393. (allowed (org-property-get-allowed-values nil prop 'table))
  11394. (existing (mapcar 'list (org-property-values prop)))
  11395. (val (if allowed
  11396. (org-completing-read "Value: " allowed nil 'req-match)
  11397. (let (org-completion-use-ido org-completion-use-iswitchb)
  11398. (org-completing-read
  11399. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11400. (concat "[" cur "]") "")
  11401. ": ")
  11402. existing nil nil "" nil cur)))))
  11403. (list prop (if (equal val "") cur val))))
  11404. (unless (equal (org-entry-get nil property) value)
  11405. (org-entry-put nil property value)))
  11406. (defun org-delete-property (property)
  11407. "In the current entry, delete PROPERTY."
  11408. (interactive
  11409. (let* ((completion-ignore-case t)
  11410. (prop (org-icompleting-read
  11411. "Property: " (org-entry-properties nil 'standard))))
  11412. (list prop)))
  11413. (message "Property %s %s" property
  11414. (if (org-entry-delete nil property)
  11415. "deleted"
  11416. "was not present in the entry")))
  11417. (defun org-delete-property-globally (property)
  11418. "Remove PROPERTY globally, from all entries."
  11419. (interactive
  11420. (let* ((completion-ignore-case t)
  11421. (prop (org-icompleting-read
  11422. "Globally remove property: "
  11423. (mapcar 'list (org-buffer-property-keys)))))
  11424. (list prop)))
  11425. (save-excursion
  11426. (save-restriction
  11427. (widen)
  11428. (goto-char (point-min))
  11429. (let ((cnt 0))
  11430. (while (re-search-forward
  11431. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11432. nil t)
  11433. (setq cnt (1+ cnt))
  11434. (replace-match ""))
  11435. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11436. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11437. (defun org-compute-property-at-point ()
  11438. "Compute the property at point.
  11439. This looks for an enclosing column format, extracts the operator and
  11440. then applies it to the property in the column format's scope."
  11441. (interactive)
  11442. (unless (org-at-property-p)
  11443. (error "Not at a property"))
  11444. (let ((prop (org-match-string-no-properties 2)))
  11445. (org-columns-get-format-and-top-level)
  11446. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11447. (error "No operator defined for property %s" prop))
  11448. (org-columns-compute prop)))
  11449. (defun org-property-get-allowed-values (pom property &optional table)
  11450. "Get allowed values for the property PROPERTY.
  11451. When TABLE is non-nil, return an alist that can directly be used for
  11452. completion."
  11453. (let (vals)
  11454. (cond
  11455. ((equal property "TODO")
  11456. (setq vals (org-with-point-at pom
  11457. (append org-todo-keywords-1 '("")))))
  11458. ((equal property "PRIORITY")
  11459. (let ((n org-lowest-priority))
  11460. (while (>= n org-highest-priority)
  11461. (push (char-to-string n) vals)
  11462. (setq n (1- n)))))
  11463. ((member property org-special-properties))
  11464. (t
  11465. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11466. (when (and vals (string-match "\\S-" vals))
  11467. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11468. (setq vals (mapcar (lambda (x)
  11469. (cond ((stringp x) x)
  11470. ((numberp x) (number-to-string x))
  11471. ((symbolp x) (symbol-name x))
  11472. (t "???")))
  11473. vals)))))
  11474. (if table (mapcar 'list vals) vals)))
  11475. (defun org-property-previous-allowed-value (&optional previous)
  11476. "Switch to the next allowed value for this property."
  11477. (interactive)
  11478. (org-property-next-allowed-value t))
  11479. (defun org-property-next-allowed-value (&optional previous)
  11480. "Switch to the next allowed value for this property."
  11481. (interactive)
  11482. (unless (org-at-property-p)
  11483. (error "Not at a property"))
  11484. (let* ((key (match-string 2))
  11485. (value (match-string 3))
  11486. (allowed (or (org-property-get-allowed-values (point) key)
  11487. (and (member value '("[ ]" "[-]" "[X]"))
  11488. '("[ ]" "[X]"))))
  11489. nval)
  11490. (unless allowed
  11491. (error "Allowed values for this property have not been defined"))
  11492. (if previous (setq allowed (reverse allowed)))
  11493. (if (member value allowed)
  11494. (setq nval (car (cdr (member value allowed)))))
  11495. (setq nval (or nval (car allowed)))
  11496. (if (equal nval value)
  11497. (error "Only one allowed value for this property"))
  11498. (org-at-property-p)
  11499. (replace-match (concat " :" key ": " nval) t t)
  11500. (org-indent-line-function)
  11501. (beginning-of-line 1)
  11502. (skip-chars-forward " \t")))
  11503. (defun org-find-entry-with-id (ident)
  11504. "Locate the entry that contains the ID property with exact value IDENT.
  11505. IDENT can be a string, a symbol or a number, this function will search for
  11506. the string representation of it.
  11507. Return the position where this entry starts, or nil if there is no such entry."
  11508. (interactive "sID: ")
  11509. (let ((id (cond
  11510. ((stringp ident) ident)
  11511. ((symbol-name ident) (symbol-name ident))
  11512. ((numberp ident) (number-to-string ident))
  11513. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11514. (case-fold-search nil))
  11515. (save-excursion
  11516. (save-restriction
  11517. (widen)
  11518. (goto-char (point-min))
  11519. (when (re-search-forward
  11520. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11521. nil t)
  11522. (org-back-to-heading t)
  11523. (point))))))
  11524. ;;;; Timestamps
  11525. (defvar org-last-changed-timestamp nil)
  11526. (defvar org-last-inserted-timestamp nil
  11527. "The last time stamp inserted with `org-insert-time-stamp'.")
  11528. (defvar org-time-was-given) ; dynamically scoped parameter
  11529. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11530. (defvar org-ts-what) ; dynamically scoped parameter
  11531. (defun org-time-stamp (arg &optional inactive)
  11532. "Prompt for a date/time and insert a time stamp.
  11533. If the user specifies a time like HH:MM, or if this command is called
  11534. with a prefix argument, the time stamp will contain date and time.
  11535. Otherwise, only the date will be included. All parts of a date not
  11536. specified by the user will be filled in from the current date/time.
  11537. So if you press just return without typing anything, the time stamp
  11538. will represent the current date/time. If there is already a timestamp
  11539. at the cursor, it will be modified."
  11540. (interactive "P")
  11541. (let* ((ts nil)
  11542. (default-time
  11543. ;; Default time is either today, or, when entering a range,
  11544. ;; the range start.
  11545. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11546. (save-excursion
  11547. (re-search-backward
  11548. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11549. (- (point) 20) t)))
  11550. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11551. (current-time)))
  11552. (default-input (and ts (org-get-compact-tod ts)))
  11553. org-time-was-given org-end-time-was-given time)
  11554. (cond
  11555. ((and (org-at-timestamp-p t)
  11556. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11557. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11558. (insert "--")
  11559. (setq time (let ((this-command this-command))
  11560. (org-read-date arg 'totime nil nil
  11561. default-time default-input)))
  11562. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11563. ((org-at-timestamp-p t)
  11564. (setq time (let ((this-command this-command))
  11565. (org-read-date arg 'totime nil nil default-time default-input)))
  11566. (when (org-at-timestamp-p t) ; just to get the match data
  11567. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11568. (replace-match "")
  11569. (setq org-last-changed-timestamp
  11570. (org-insert-time-stamp
  11571. time (or org-time-was-given arg)
  11572. inactive nil nil (list org-end-time-was-given))))
  11573. (message "Timestamp updated"))
  11574. (t
  11575. (setq time (let ((this-command this-command))
  11576. (org-read-date arg 'totime nil nil default-time default-input)))
  11577. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11578. nil nil (list org-end-time-was-given))))))
  11579. ;; FIXME: can we use this for something else, like computing time differences?
  11580. (defun org-get-compact-tod (s)
  11581. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11582. (let* ((t1 (match-string 1 s))
  11583. (h1 (string-to-number (match-string 2 s)))
  11584. (m1 (string-to-number (match-string 3 s)))
  11585. (t2 (and (match-end 4) (match-string 5 s)))
  11586. (h2 (and t2 (string-to-number (match-string 6 s))))
  11587. (m2 (and t2 (string-to-number (match-string 7 s))))
  11588. dh dm)
  11589. (if (not t2)
  11590. t1
  11591. (setq dh (- h2 h1) dm (- m2 m1))
  11592. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11593. (concat t1 "+" (number-to-string dh)
  11594. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11595. (defun org-time-stamp-inactive (&optional arg)
  11596. "Insert an inactive time stamp.
  11597. An inactive time stamp is enclosed in square brackets instead of angle
  11598. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11599. does not link to the calendar and cannot be changed with the S-cursor keys.
  11600. So these are more for recording a certain time/date."
  11601. (interactive "P")
  11602. (org-time-stamp arg 'inactive))
  11603. (defvar org-date-ovl (org-make-overlay 1 1))
  11604. (org-overlay-put org-date-ovl 'face 'org-warning)
  11605. (org-detach-overlay org-date-ovl)
  11606. (defvar org-ans1) ; dynamically scoped parameter
  11607. (defvar org-ans2) ; dynamically scoped parameter
  11608. (defvar org-plain-time-of-day-regexp) ; defined below
  11609. (defvar org-overriding-default-time nil) ; dynamically scoped
  11610. (defvar org-read-date-overlay nil)
  11611. (defvar org-dcst nil) ; dynamically scoped
  11612. (defvar org-read-date-history nil)
  11613. (defvar org-read-date-final-answer nil)
  11614. (defun org-read-date (&optional with-time to-time from-string prompt
  11615. default-time default-input)
  11616. "Read a date, possibly a time, and make things smooth for the user.
  11617. The prompt will suggest to enter an ISO date, but you can also enter anything
  11618. which will at least partially be understood by `parse-time-string'.
  11619. Unrecognized parts of the date will default to the current day, month, year,
  11620. hour and minute. If this command is called to replace a timestamp at point,
  11621. of to enter the second timestamp of a range, the default time is taken from the
  11622. existing stamp. For example,
  11623. 3-2-5 --> 2003-02-05
  11624. feb 15 --> currentyear-02-15
  11625. sep 12 9 --> 2009-09-12
  11626. 12:45 --> today 12:45
  11627. 22 sept 0:34 --> currentyear-09-22 0:34
  11628. 12 --> currentyear-currentmonth-12
  11629. Fri --> nearest Friday (today or later)
  11630. etc.
  11631. Furthermore you can specify a relative date by giving, as the *first* thing
  11632. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11633. change in days weeks, months, years.
  11634. With a single plus or minus, the date is relative to today. With a double
  11635. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11636. +4d --> four days from today
  11637. +4 --> same as above
  11638. +2w --> two weeks from today
  11639. ++5 --> five days from default date
  11640. The function understands only English month and weekday abbreviations,
  11641. but this can be configured with the variables `parse-time-months' and
  11642. `parse-time-weekdays'.
  11643. While prompting, a calendar is popped up - you can also select the
  11644. date with the mouse (button 1). The calendar shows a period of three
  11645. months. To scroll it to other months, use the keys `>' and `<'.
  11646. If you don't like the calendar, turn it off with
  11647. \(setq org-read-date-popup-calendar nil)
  11648. With optional argument TO-TIME, the date will immediately be converted
  11649. to an internal time.
  11650. With an optional argument WITH-TIME, the prompt will suggest to also
  11651. insert a time. Note that when WITH-TIME is not set, you can still
  11652. enter a time, and this function will inform the calling routine about
  11653. this change. The calling routine may then choose to change the format
  11654. used to insert the time stamp into the buffer to include the time.
  11655. With optional argument FROM-STRING, read from this string instead from
  11656. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11657. the time/date that is used for everything that is not specified by the
  11658. user."
  11659. (require 'parse-time)
  11660. (let* ((org-time-stamp-rounding-minutes
  11661. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11662. (org-dcst org-display-custom-times)
  11663. (ct (org-current-time))
  11664. (def (or org-overriding-default-time default-time ct))
  11665. (defdecode (decode-time def))
  11666. (dummy (progn
  11667. (when (< (nth 2 defdecode) org-extend-today-until)
  11668. (setcar (nthcdr 2 defdecode) -1)
  11669. (setcar (nthcdr 1 defdecode) 59)
  11670. (setq def (apply 'encode-time defdecode)
  11671. defdecode (decode-time def)))))
  11672. (calendar-frame-setup nil)
  11673. (calendar-move-hook nil)
  11674. (calendar-view-diary-initially-flag nil)
  11675. (view-diary-entries-initially nil)
  11676. (calendar-view-holidays-initially-flag nil)
  11677. (view-calendar-holidays-initially nil)
  11678. (timestr (format-time-string
  11679. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11680. (prompt (concat (if prompt (concat prompt " ") "")
  11681. (format "Date+time [%s]: " timestr)))
  11682. ans (org-ans0 "") org-ans1 org-ans2 final)
  11683. (cond
  11684. (from-string (setq ans from-string))
  11685. (org-read-date-popup-calendar
  11686. (save-excursion
  11687. (save-window-excursion
  11688. (calendar)
  11689. (calendar-forward-day (- (time-to-days def)
  11690. (calendar-absolute-from-gregorian
  11691. (calendar-current-date))))
  11692. (org-eval-in-calendar nil t)
  11693. (let* ((old-map (current-local-map))
  11694. (map (copy-keymap calendar-mode-map))
  11695. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11696. (org-defkey map (kbd "RET") 'org-calendar-select)
  11697. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11698. 'org-calendar-select-mouse)
  11699. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11700. 'org-calendar-select-mouse)
  11701. (org-defkey minibuffer-local-map [(meta shift left)]
  11702. (lambda () (interactive)
  11703. (org-eval-in-calendar '(calendar-backward-month 1))))
  11704. (org-defkey minibuffer-local-map [(meta shift right)]
  11705. (lambda () (interactive)
  11706. (org-eval-in-calendar '(calendar-forward-month 1))))
  11707. (org-defkey minibuffer-local-map [(meta shift up)]
  11708. (lambda () (interactive)
  11709. (org-eval-in-calendar '(calendar-backward-year 1))))
  11710. (org-defkey minibuffer-local-map [(meta shift down)]
  11711. (lambda () (interactive)
  11712. (org-eval-in-calendar '(calendar-forward-year 1))))
  11713. (org-defkey minibuffer-local-map [?\e (shift left)]
  11714. (lambda () (interactive)
  11715. (org-eval-in-calendar '(calendar-backward-month 1))))
  11716. (org-defkey minibuffer-local-map [?\e (shift right)]
  11717. (lambda () (interactive)
  11718. (org-eval-in-calendar '(calendar-forward-month 1))))
  11719. (org-defkey minibuffer-local-map [?\e (shift up)]
  11720. (lambda () (interactive)
  11721. (org-eval-in-calendar '(calendar-backward-year 1))))
  11722. (org-defkey minibuffer-local-map [?\e (shift down)]
  11723. (lambda () (interactive)
  11724. (org-eval-in-calendar '(calendar-forward-year 1))))
  11725. (org-defkey minibuffer-local-map [(shift up)]
  11726. (lambda () (interactive)
  11727. (org-eval-in-calendar '(calendar-backward-week 1))))
  11728. (org-defkey minibuffer-local-map [(shift down)]
  11729. (lambda () (interactive)
  11730. (org-eval-in-calendar '(calendar-forward-week 1))))
  11731. (org-defkey minibuffer-local-map [(shift left)]
  11732. (lambda () (interactive)
  11733. (org-eval-in-calendar '(calendar-backward-day 1))))
  11734. (org-defkey minibuffer-local-map [(shift right)]
  11735. (lambda () (interactive)
  11736. (org-eval-in-calendar '(calendar-forward-day 1))))
  11737. (org-defkey minibuffer-local-map ">"
  11738. (lambda () (interactive)
  11739. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11740. (org-defkey minibuffer-local-map "<"
  11741. (lambda () (interactive)
  11742. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11743. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11744. (unwind-protect
  11745. (progn
  11746. (use-local-map map)
  11747. (add-hook 'post-command-hook 'org-read-date-display)
  11748. (setq org-ans0 (read-string prompt default-input
  11749. 'org-read-date-history nil))
  11750. ;; org-ans0: from prompt
  11751. ;; org-ans1: from mouse click
  11752. ;; org-ans2: from calendar motion
  11753. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11754. (remove-hook 'post-command-hook 'org-read-date-display)
  11755. (use-local-map old-map)
  11756. (when org-read-date-overlay
  11757. (org-delete-overlay org-read-date-overlay)
  11758. (setq org-read-date-overlay nil)))))))
  11759. (t ; Naked prompt only
  11760. (unwind-protect
  11761. (setq ans (read-string prompt default-input
  11762. 'org-read-date-history timestr))
  11763. (when org-read-date-overlay
  11764. (org-delete-overlay org-read-date-overlay)
  11765. (setq org-read-date-overlay nil)))))
  11766. (setq final (org-read-date-analyze ans def defdecode))
  11767. (setq org-read-date-final-answer ans)
  11768. (if to-time
  11769. (apply 'encode-time final)
  11770. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11771. (format "%04d-%02d-%02d %02d:%02d"
  11772. (nth 5 final) (nth 4 final) (nth 3 final)
  11773. (nth 2 final) (nth 1 final))
  11774. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11775. (defvar def)
  11776. (defvar defdecode)
  11777. (defvar with-time)
  11778. (defvar org-read-date-analyze-futurep nil)
  11779. (defun org-read-date-display ()
  11780. "Display the current date prompt interpretation in the minibuffer."
  11781. (when org-read-date-display-live
  11782. (when org-read-date-overlay
  11783. (org-delete-overlay org-read-date-overlay))
  11784. (let ((p (point)))
  11785. (end-of-line 1)
  11786. (while (not (equal (buffer-substring
  11787. (max (point-min) (- (point) 4)) (point))
  11788. " "))
  11789. (insert " "))
  11790. (goto-char p))
  11791. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11792. " " (or org-ans1 org-ans2)))
  11793. (org-end-time-was-given nil)
  11794. (f (org-read-date-analyze ans def defdecode))
  11795. (fmts (if org-dcst
  11796. org-time-stamp-custom-formats
  11797. org-time-stamp-formats))
  11798. (fmt (if (or with-time
  11799. (and (boundp 'org-time-was-given) org-time-was-given))
  11800. (cdr fmts)
  11801. (car fmts)))
  11802. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11803. (when (and org-end-time-was-given
  11804. (string-match org-plain-time-of-day-regexp txt))
  11805. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11806. org-end-time-was-given
  11807. (substring txt (match-end 0)))))
  11808. (when org-read-date-analyze-futurep
  11809. (setq txt (concat txt " (=>F)")))
  11810. (setq org-read-date-overlay
  11811. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11812. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11813. (defun org-read-date-analyze (ans def defdecode)
  11814. "Analyse the combined answer of the date prompt."
  11815. ;; FIXME: cleanup and comment
  11816. (let (delta deltan deltaw deltadef year month day
  11817. hour minute second wday pm h2 m2 tl wday1
  11818. iso-year iso-weekday iso-week iso-year iso-date futurep)
  11819. (setq org-read-date-analyze-futurep nil)
  11820. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11821. (setq ans "+0"))
  11822. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11823. (setq ans (replace-match "" t t ans)
  11824. deltan (car delta)
  11825. deltaw (nth 1 delta)
  11826. deltadef (nth 2 delta)))
  11827. ;; Check if there is an iso week date in there
  11828. ;; If yes, sore the info and postpone interpreting it until the rest
  11829. ;; of the parsing is done
  11830. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11831. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11832. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11833. iso-week (string-to-number (match-string 2 ans)))
  11834. (setq ans (replace-match "" t t ans)))
  11835. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11836. (when (string-match
  11837. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11838. (setq year (if (match-end 2)
  11839. (string-to-number (match-string 2 ans))
  11840. (string-to-number (format-time-string "%Y")))
  11841. month (string-to-number (match-string 3 ans))
  11842. day (string-to-number (match-string 4 ans)))
  11843. (if (< year 100) (setq year (+ 2000 year)))
  11844. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11845. t nil ans)))
  11846. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11847. ;; If there is a time with am/pm, and *no* time without it, we convert
  11848. ;; so that matching will be successful.
  11849. (loop for i from 1 to 2 do ; twice, for end time as well
  11850. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11851. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11852. (setq hour (string-to-number (match-string 1 ans))
  11853. minute (if (match-end 3)
  11854. (string-to-number (match-string 3 ans))
  11855. 0)
  11856. pm (equal ?p
  11857. (string-to-char (downcase (match-string 4 ans)))))
  11858. (if (and (= hour 12) (not pm))
  11859. (setq hour 0)
  11860. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11861. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11862. t t ans))))
  11863. ;; Check if a time range is given as a duration
  11864. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11865. (setq hour (string-to-number (match-string 1 ans))
  11866. h2 (+ hour (string-to-number (match-string 3 ans)))
  11867. minute (string-to-number (match-string 2 ans))
  11868. m2 (+ minute (if (match-end 5) (string-to-number
  11869. (match-string 5 ans))0)))
  11870. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11871. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11872. t t ans)))
  11873. ;; Check if there is a time range
  11874. (when (boundp 'org-end-time-was-given)
  11875. (setq org-time-was-given nil)
  11876. (when (and (string-match org-plain-time-of-day-regexp ans)
  11877. (match-end 8))
  11878. (setq org-end-time-was-given (match-string 8 ans))
  11879. (setq ans (concat (substring ans 0 (match-beginning 7))
  11880. (substring ans (match-end 7))))))
  11881. (setq tl (parse-time-string ans)
  11882. day (or (nth 3 tl) (nth 3 defdecode))
  11883. month (or (nth 4 tl)
  11884. (if (and org-read-date-prefer-future
  11885. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11886. (prog1 (1+ (nth 4 defdecode)) (setq futurep t))
  11887. (nth 4 defdecode)))
  11888. year (or (nth 5 tl)
  11889. (if (and org-read-date-prefer-future
  11890. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11891. (prog1 (1+ (nth 5 defdecode)) (setq futurep t))
  11892. (nth 5 defdecode)))
  11893. hour (or (nth 2 tl) (nth 2 defdecode))
  11894. minute (or (nth 1 tl) (nth 1 defdecode))
  11895. second (or (nth 0 tl) 0)
  11896. wday (nth 6 tl))
  11897. (when (and (eq org-read-date-prefer-future 'time)
  11898. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  11899. (equal day (nth 3 defdecode))
  11900. (equal month (nth 4 defdecode))
  11901. (equal year (nth 5 defdecode))
  11902. (nth 2 tl)
  11903. (or (< (nth 2 tl) (nth 2 defdecode))
  11904. (and (= (nth 2 tl) (nth 2 defdecode))
  11905. (nth 1 tl)
  11906. (< (nth 1 tl) (nth 1 defdecode)))))
  11907. (setq day (1+ day)
  11908. futurep t))
  11909. ;; Special date definitions below
  11910. (cond
  11911. (iso-week
  11912. ;; There was an iso week
  11913. (setq futurep nil)
  11914. (setq year (or iso-year year)
  11915. day (or iso-weekday wday 1)
  11916. wday nil ; to make sure that the trigger below does not match
  11917. iso-date (calendar-gregorian-from-absolute
  11918. (calendar-absolute-from-iso
  11919. (list iso-week day year))))
  11920. ; FIXME: Should we also push ISO weeks into the future?
  11921. ; (when (and org-read-date-prefer-future
  11922. ; (not iso-year)
  11923. ; (< (calendar-absolute-from-gregorian iso-date)
  11924. ; (time-to-days (current-time))))
  11925. ; (setq year (1+ year)
  11926. ; iso-date (calendar-gregorian-from-absolute
  11927. ; (calendar-absolute-from-iso
  11928. ; (list iso-week day year)))))
  11929. (setq month (car iso-date)
  11930. year (nth 2 iso-date)
  11931. day (nth 1 iso-date)))
  11932. (deltan
  11933. (setq futurep nil)
  11934. (unless deltadef
  11935. (let ((now (decode-time (current-time))))
  11936. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11937. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11938. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11939. ((equal deltaw "m") (setq month (+ month deltan)))
  11940. ((equal deltaw "y") (setq year (+ year deltan)))))
  11941. ((and wday (not (nth 3 tl)))
  11942. (setq futurep nil)
  11943. ;; Weekday was given, but no day, so pick that day in the week
  11944. ;; on or after the derived date.
  11945. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11946. (unless (equal wday wday1)
  11947. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11948. (if (and (boundp 'org-time-was-given)
  11949. (nth 2 tl))
  11950. (setq org-time-was-given t))
  11951. (if (< year 100) (setq year (+ 2000 year)))
  11952. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11953. (setq org-read-date-analyze-futurep futurep)
  11954. (list second minute hour day month year)))
  11955. (defvar parse-time-weekdays)
  11956. (defun org-read-date-get-relative (s today default)
  11957. "Check string S for special relative date string.
  11958. TODAY and DEFAULT are internal times, for today and for a default.
  11959. Return shift list (N what def-flag)
  11960. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11961. N is the number of WHATs to shift.
  11962. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11963. the DEFAULT date rather than TODAY."
  11964. (when (and
  11965. (string-match
  11966. (concat
  11967. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11968. "\\([0-9]+\\)?"
  11969. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11970. "\\([ \t]\\|$\\)") s)
  11971. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11972. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11973. (string-to-char (substring (match-string 1 s) -1))
  11974. ?+))
  11975. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11976. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11977. (what (if (match-end 3) (match-string 3 s) "d"))
  11978. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11979. (date (if rel default today))
  11980. (wday (nth 6 (decode-time date)))
  11981. delta)
  11982. (if wday1
  11983. (progn
  11984. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11985. (if (= dir ?-) (setq delta (- delta 7)))
  11986. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11987. (list delta "d" rel))
  11988. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11989. (defun org-eval-in-calendar (form &optional keepdate)
  11990. "Eval FORM in the calendar window and return to current window.
  11991. Also, store the cursor date in variable org-ans2."
  11992. (let ((sf (selected-frame))
  11993. (sw (selected-window)))
  11994. (select-window (get-buffer-window "*Calendar*" t))
  11995. (eval form)
  11996. (when (and (not keepdate) (calendar-cursor-to-date))
  11997. (let* ((date (calendar-cursor-to-date))
  11998. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11999. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12000. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12001. (select-window sw)
  12002. (org-select-frame-set-input-focus sf)))
  12003. (defun org-calendar-select ()
  12004. "Return to `org-read-date' with the date currently selected.
  12005. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12006. (interactive)
  12007. (when (calendar-cursor-to-date)
  12008. (let* ((date (calendar-cursor-to-date))
  12009. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12010. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12011. (if (active-minibuffer-window) (exit-minibuffer))))
  12012. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12013. "Insert a date stamp for the date given by the internal TIME.
  12014. WITH-HM means, use the stamp format that includes the time of the day.
  12015. INACTIVE means use square brackets instead of angular ones, so that the
  12016. stamp will not contribute to the agenda.
  12017. PRE and POST are optional strings to be inserted before and after the
  12018. stamp.
  12019. The command returns the inserted time stamp."
  12020. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12021. stamp)
  12022. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12023. (insert-before-markers (or pre ""))
  12024. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12025. (when (listp extra)
  12026. (setq extra (car extra))
  12027. (if (and (stringp extra)
  12028. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12029. (setq extra (format "-%02d:%02d"
  12030. (string-to-number (match-string 1 extra))
  12031. (string-to-number (match-string 2 extra))))
  12032. (setq extra nil)))
  12033. (when extra
  12034. (backward-char 1)
  12035. (insert-before-markers extra)
  12036. (forward-char 1))
  12037. (insert-before-markers (or post ""))
  12038. (setq org-last-inserted-timestamp stamp)))
  12039. (defun org-toggle-time-stamp-overlays ()
  12040. "Toggle the use of custom time stamp formats."
  12041. (interactive)
  12042. (setq org-display-custom-times (not org-display-custom-times))
  12043. (unless org-display-custom-times
  12044. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12045. (while (setq p (next-single-property-change p 'display))
  12046. (if (and (get-text-property p 'display)
  12047. (eq (get-text-property p 'face) 'org-date))
  12048. (remove-text-properties
  12049. p (setq p (next-single-property-change p 'display))
  12050. '(display t))))
  12051. (set-buffer-modified-p bmp)))
  12052. (if (featurep 'xemacs)
  12053. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12054. (org-restart-font-lock)
  12055. (setq org-table-may-need-update t)
  12056. (if org-display-custom-times
  12057. (message "Time stamps are overlayed with custom format")
  12058. (message "Time stamp overlays removed")))
  12059. (defun org-display-custom-time (beg end)
  12060. "Overlay modified time stamp format over timestamp between BEG and END."
  12061. (let* ((ts (buffer-substring beg end))
  12062. t1 w1 with-hm tf time str w2 (off 0))
  12063. (save-match-data
  12064. (setq t1 (org-parse-time-string ts t))
  12065. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12066. (setq off (- (match-end 0) (match-beginning 0)))))
  12067. (setq end (- end off))
  12068. (setq w1 (- end beg)
  12069. with-hm (and (nth 1 t1) (nth 2 t1))
  12070. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12071. time (org-fix-decoded-time t1)
  12072. str (org-add-props
  12073. (format-time-string
  12074. (substring tf 1 -1) (apply 'encode-time time))
  12075. nil 'mouse-face 'highlight)
  12076. w2 (length str))
  12077. (if (not (= w2 w1))
  12078. (add-text-properties (1+ beg) (+ 2 beg)
  12079. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12080. (if (featurep 'xemacs)
  12081. (progn
  12082. (put-text-property beg end 'invisible t)
  12083. (put-text-property beg end 'end-glyph (make-glyph str)))
  12084. (put-text-property beg end 'display str))))
  12085. (defun org-translate-time (string)
  12086. "Translate all timestamps in STRING to custom format.
  12087. But do this only if the variable `org-display-custom-times' is set."
  12088. (when org-display-custom-times
  12089. (save-match-data
  12090. (let* ((start 0)
  12091. (re org-ts-regexp-both)
  12092. t1 with-hm inactive tf time str beg end)
  12093. (while (setq start (string-match re string start))
  12094. (setq beg (match-beginning 0)
  12095. end (match-end 0)
  12096. t1 (save-match-data
  12097. (org-parse-time-string (substring string beg end) t))
  12098. with-hm (and (nth 1 t1) (nth 2 t1))
  12099. inactive (equal (substring string beg (1+ beg)) "[")
  12100. tf (funcall (if with-hm 'cdr 'car)
  12101. org-time-stamp-custom-formats)
  12102. time (org-fix-decoded-time t1)
  12103. str (format-time-string
  12104. (concat
  12105. (if inactive "[" "<") (substring tf 1 -1)
  12106. (if inactive "]" ">"))
  12107. (apply 'encode-time time))
  12108. string (replace-match str t t string)
  12109. start (+ start (length str)))))))
  12110. string)
  12111. (defun org-fix-decoded-time (time)
  12112. "Set 0 instead of nil for the first 6 elements of time.
  12113. Don't touch the rest."
  12114. (let ((n 0))
  12115. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12116. (defun org-days-to-time (timestamp-string)
  12117. "Difference between TIMESTAMP-STRING and now in days."
  12118. (- (time-to-days (org-time-string-to-time timestamp-string))
  12119. (time-to-days (current-time))))
  12120. (defun org-deadline-close (timestamp-string &optional ndays)
  12121. "Is the time in TIMESTAMP-STRING close to the current date?"
  12122. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12123. (and (< (org-days-to-time timestamp-string) ndays)
  12124. (not (org-entry-is-done-p))))
  12125. (defun org-get-wdays (ts)
  12126. "Get the deadline lead time appropriate for timestring TS."
  12127. (cond
  12128. ((<= org-deadline-warning-days 0)
  12129. ;; 0 or negative, enforce this value no matter what
  12130. (- org-deadline-warning-days))
  12131. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12132. ;; lead time is specified.
  12133. (floor (* (string-to-number (match-string 1 ts))
  12134. (cdr (assoc (match-string 2 ts)
  12135. '(("d" . 1) ("w" . 7)
  12136. ("m" . 30.4) ("y" . 365.25)))))))
  12137. ;; go for the default.
  12138. (t org-deadline-warning-days)))
  12139. (defun org-calendar-select-mouse (ev)
  12140. "Return to `org-read-date' with the date currently selected.
  12141. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12142. (interactive "e")
  12143. (mouse-set-point ev)
  12144. (when (calendar-cursor-to-date)
  12145. (let* ((date (calendar-cursor-to-date))
  12146. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12147. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12148. (if (active-minibuffer-window) (exit-minibuffer))))
  12149. (defun org-check-deadlines (ndays)
  12150. "Check if there are any deadlines due or past due.
  12151. A deadline is considered due if it happens within `org-deadline-warning-days'
  12152. days from today's date. If the deadline appears in an entry marked DONE,
  12153. it is not shown. The prefix arg NDAYS can be used to test that many
  12154. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12155. (interactive "P")
  12156. (let* ((org-warn-days
  12157. (cond
  12158. ((equal ndays '(4)) 100000)
  12159. (ndays (prefix-numeric-value ndays))
  12160. (t (abs org-deadline-warning-days))))
  12161. (case-fold-search nil)
  12162. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12163. (callback
  12164. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12165. (message "%d deadlines past-due or due within %d days"
  12166. (org-occur regexp nil callback)
  12167. org-warn-days)))
  12168. (defun org-check-before-date (date)
  12169. "Check if there are deadlines or scheduled entries before DATE."
  12170. (interactive (list (org-read-date)))
  12171. (let ((case-fold-search nil)
  12172. (regexp (concat "\\<\\(" org-deadline-string
  12173. "\\|" org-scheduled-string
  12174. "\\) *<\\([^>]+\\)>"))
  12175. (callback
  12176. (lambda () (time-less-p
  12177. (org-time-string-to-time (match-string 2))
  12178. (org-time-string-to-time date)))))
  12179. (message "%d entries before %s"
  12180. (org-occur regexp nil callback) date)))
  12181. (defun org-check-after-date (date)
  12182. "Check if there are deadlines or scheduled entries after DATE."
  12183. (interactive (list (org-read-date)))
  12184. (let ((case-fold-search nil)
  12185. (regexp (concat "\\<\\(" org-deadline-string
  12186. "\\|" org-scheduled-string
  12187. "\\) *<\\([^>]+\\)>"))
  12188. (callback
  12189. (lambda () (not
  12190. (time-less-p
  12191. (org-time-string-to-time (match-string 2))
  12192. (org-time-string-to-time date))))))
  12193. (message "%d entries after %s"
  12194. (org-occur regexp nil callback) date)))
  12195. (defun org-evaluate-time-range (&optional to-buffer)
  12196. "Evaluate a time range by computing the difference between start and end.
  12197. Normally the result is just printed in the echo area, but with prefix arg
  12198. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12199. If the time range is actually in a table, the result is inserted into the
  12200. next column.
  12201. For time difference computation, a year is assumed to be exactly 365
  12202. days in order to avoid rounding problems."
  12203. (interactive "P")
  12204. (or
  12205. (org-clock-update-time-maybe)
  12206. (save-excursion
  12207. (unless (org-at-date-range-p t)
  12208. (goto-char (point-at-bol))
  12209. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12210. (if (not (org-at-date-range-p t))
  12211. (error "Not at a time-stamp range, and none found in current line")))
  12212. (let* ((ts1 (match-string 1))
  12213. (ts2 (match-string 2))
  12214. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12215. (match-end (match-end 0))
  12216. (time1 (org-time-string-to-time ts1))
  12217. (time2 (org-time-string-to-time ts2))
  12218. (t1 (org-float-time time1))
  12219. (t2 (org-float-time time2))
  12220. (diff (abs (- t2 t1)))
  12221. (negative (< (- t2 t1) 0))
  12222. ;; (ys (floor (* 365 24 60 60)))
  12223. (ds (* 24 60 60))
  12224. (hs (* 60 60))
  12225. (fy "%dy %dd %02d:%02d")
  12226. (fy1 "%dy %dd")
  12227. (fd "%dd %02d:%02d")
  12228. (fd1 "%dd")
  12229. (fh "%02d:%02d")
  12230. y d h m align)
  12231. (if havetime
  12232. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12233. y 0
  12234. d (floor (/ diff ds)) diff (mod diff ds)
  12235. h (floor (/ diff hs)) diff (mod diff hs)
  12236. m (floor (/ diff 60)))
  12237. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12238. y 0
  12239. d (floor (+ (/ diff ds) 0.5))
  12240. h 0 m 0))
  12241. (if (not to-buffer)
  12242. (message "%s" (org-make-tdiff-string y d h m))
  12243. (if (org-at-table-p)
  12244. (progn
  12245. (goto-char match-end)
  12246. (setq align t)
  12247. (and (looking-at " *|") (goto-char (match-end 0))))
  12248. (goto-char match-end))
  12249. (if (looking-at
  12250. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12251. (replace-match ""))
  12252. (if negative (insert " -"))
  12253. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12254. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12255. (insert " " (format fh h m))))
  12256. (if align (org-table-align))
  12257. (message "Time difference inserted")))))
  12258. (defun org-make-tdiff-string (y d h m)
  12259. (let ((fmt "")
  12260. (l nil))
  12261. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12262. l (push y l)))
  12263. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12264. l (push d l)))
  12265. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12266. l (push h l)))
  12267. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12268. l (push m l)))
  12269. (apply 'format fmt (nreverse l))))
  12270. (defun org-time-string-to-time (s)
  12271. (apply 'encode-time (org-parse-time-string s)))
  12272. (defun org-time-string-to-seconds (s)
  12273. (org-float-time (org-time-string-to-time s)))
  12274. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12275. "Convert a time stamp to an absolute day number.
  12276. If there is a specifyer for a cyclic time stamp, get the closest date to
  12277. DAYNR.
  12278. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12279. the variable date is bound by the calendar when this is called."
  12280. (cond
  12281. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12282. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12283. daynr
  12284. (+ daynr 1000)))
  12285. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12286. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12287. (time-to-days (current-time))) (match-string 0 s)
  12288. prefer show-all))
  12289. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12290. (defun org-days-to-iso-week (days)
  12291. "Return the iso week number."
  12292. (require 'cal-iso)
  12293. (car (calendar-iso-from-absolute days)))
  12294. (defun org-small-year-to-year (year)
  12295. "Convert 2-digit years into 4-digit years.
  12296. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12297. The year 2000 cannot be abbreviated. Any year larger than 99
  12298. is returned unchanged."
  12299. (if (< year 38)
  12300. (setq year (+ 2000 year))
  12301. (if (< year 100)
  12302. (setq year (+ 1900 year))))
  12303. year)
  12304. (defun org-time-from-absolute (d)
  12305. "Return the time corresponding to date D.
  12306. D may be an absolute day number, or a calendar-type list (month day year)."
  12307. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12308. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12309. (defun org-calendar-holiday ()
  12310. "List of holidays, for Diary display in Org-mode."
  12311. (require 'holidays)
  12312. (let ((hl (funcall
  12313. (if (fboundp 'calendar-check-holidays)
  12314. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12315. (if hl (mapconcat 'identity hl "; "))))
  12316. (defun org-diary-sexp-entry (sexp entry date)
  12317. "Process a SEXP diary ENTRY for DATE."
  12318. (require 'diary-lib)
  12319. (let ((result (if calendar-debug-sexp
  12320. (let ((stack-trace-on-error t))
  12321. (eval (car (read-from-string sexp))))
  12322. (condition-case nil
  12323. (eval (car (read-from-string sexp)))
  12324. (error
  12325. (beep)
  12326. (message "Bad sexp at line %d in %s: %s"
  12327. (org-current-line)
  12328. (buffer-file-name) sexp)
  12329. (sleep-for 2))))))
  12330. (cond ((stringp result) result)
  12331. ((and (consp result)
  12332. (stringp (cdr result))) (cdr result))
  12333. (result entry)
  12334. (t nil))))
  12335. (defun org-diary-to-ical-string (frombuf)
  12336. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12337. This uses the icalendar.el library."
  12338. (let* ((tmpdir (if (featurep 'xemacs)
  12339. (temp-directory)
  12340. temporary-file-directory))
  12341. (tmpfile (make-temp-name
  12342. (expand-file-name "orgics" tmpdir)))
  12343. buf rtn b e)
  12344. (save-excursion
  12345. (set-buffer frombuf)
  12346. (icalendar-export-region (point-min) (point-max) tmpfile)
  12347. (setq buf (find-buffer-visiting tmpfile))
  12348. (set-buffer buf)
  12349. (goto-char (point-min))
  12350. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12351. (setq b (match-beginning 0)))
  12352. (goto-char (point-max))
  12353. (if (re-search-backward "^END:VEVENT" nil t)
  12354. (setq e (match-end 0)))
  12355. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12356. (kill-buffer buf)
  12357. (delete-file tmpfile)
  12358. rtn))
  12359. (defun org-closest-date (start current change prefer show-all)
  12360. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12361. When PREFER is `past' return a date that is either CURRENT or past.
  12362. When PREFER is `future', return a date that is either CURRENT or future.
  12363. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12364. ;; Make the proper lists from the dates
  12365. (catch 'exit
  12366. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12367. dn dw sday cday n1 n2 n0
  12368. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12369. (setq start (org-date-to-gregorian start)
  12370. current (org-date-to-gregorian
  12371. (if show-all
  12372. current
  12373. (time-to-days (current-time))))
  12374. sday (calendar-absolute-from-gregorian start)
  12375. cday (calendar-absolute-from-gregorian current))
  12376. (if (<= cday sday) (throw 'exit sday))
  12377. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12378. (setq dn (string-to-number (match-string 1 change))
  12379. dw (cdr (assoc (match-string 2 change) a1)))
  12380. (error "Invalid change specifyer: %s" change))
  12381. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12382. (cond
  12383. ((eq dw 'day)
  12384. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12385. n2 (+ n1 dn)))
  12386. ((eq dw 'year)
  12387. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12388. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12389. (setq date1 (list m d y1)
  12390. n1 (calendar-absolute-from-gregorian date1)
  12391. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12392. n2 (calendar-absolute-from-gregorian date2)))
  12393. ((eq dw 'month)
  12394. ;; approx number of month between the two dates
  12395. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12396. ;; How often does dn fit in there?
  12397. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12398. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12399. m (+ m nm)
  12400. ny (floor (/ m 12))
  12401. y (+ y ny)
  12402. m (- m (* ny 12)))
  12403. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12404. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12405. (setq m2 (+ m dn) y2 y)
  12406. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12407. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12408. (while (<= n2 cday)
  12409. (setq n1 n2 m m2 y y2)
  12410. (setq m2 (+ m dn) y2 y)
  12411. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12412. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12413. ;; Make sure n1 is the earlier date
  12414. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12415. (if show-all
  12416. (cond
  12417. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12418. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12419. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12420. (cond
  12421. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12422. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12423. (t (if (= cday n1) n1 n2)))))))
  12424. (defun org-date-to-gregorian (date)
  12425. "Turn any specification of DATE into a gregorian date for the calendar."
  12426. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12427. ((and (listp date) (= (length date) 3)) date)
  12428. ((stringp date)
  12429. (setq date (org-parse-time-string date))
  12430. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12431. ((listp date)
  12432. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12433. (defun org-parse-time-string (s &optional nodefault)
  12434. "Parse the standard Org-mode time string.
  12435. This should be a lot faster than the normal `parse-time-string'.
  12436. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12437. hour and minute fields will be nil if not given."
  12438. (if (string-match org-ts-regexp0 s)
  12439. (list 0
  12440. (if (or (match-beginning 8) (not nodefault))
  12441. (string-to-number (or (match-string 8 s) "0")))
  12442. (if (or (match-beginning 7) (not nodefault))
  12443. (string-to-number (or (match-string 7 s) "0")))
  12444. (string-to-number (match-string 4 s))
  12445. (string-to-number (match-string 3 s))
  12446. (string-to-number (match-string 2 s))
  12447. nil nil nil)
  12448. (error "Not a standard Org-mode time string: %s" s)))
  12449. (defun org-timestamp-up (&optional arg)
  12450. "Increase the date item at the cursor by one.
  12451. If the cursor is on the year, change the year. If it is on the month or
  12452. the day, change that.
  12453. With prefix ARG, change by that many units."
  12454. (interactive "p")
  12455. (org-timestamp-change (prefix-numeric-value arg)))
  12456. (defun org-timestamp-down (&optional arg)
  12457. "Decrease the date item at the cursor by one.
  12458. If the cursor is on the year, change the year. If it is on the month or
  12459. the day, change that.
  12460. With prefix ARG, change by that many units."
  12461. (interactive "p")
  12462. (org-timestamp-change (- (prefix-numeric-value arg))))
  12463. (defun org-timestamp-up-day (&optional arg)
  12464. "Increase the date in the time stamp by one day.
  12465. With prefix ARG, change that many days."
  12466. (interactive "p")
  12467. (if (and (not (org-at-timestamp-p t))
  12468. (org-on-heading-p))
  12469. (org-todo 'up)
  12470. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12471. (defun org-timestamp-down-day (&optional arg)
  12472. "Decrease the date in the time stamp by one day.
  12473. With prefix ARG, change that many days."
  12474. (interactive "p")
  12475. (if (and (not (org-at-timestamp-p t))
  12476. (org-on-heading-p))
  12477. (org-todo 'down)
  12478. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12479. (defun org-at-timestamp-p (&optional inactive-ok)
  12480. "Determine if the cursor is in or at a timestamp."
  12481. (interactive)
  12482. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12483. (pos (point))
  12484. (ans (or (looking-at tsr)
  12485. (save-excursion
  12486. (skip-chars-backward "^[<\n\r\t")
  12487. (if (> (point) (point-min)) (backward-char 1))
  12488. (and (looking-at tsr)
  12489. (> (- (match-end 0) pos) -1))))))
  12490. (and ans
  12491. (boundp 'org-ts-what)
  12492. (setq org-ts-what
  12493. (cond
  12494. ((= pos (match-beginning 0)) 'bracket)
  12495. ((= pos (1- (match-end 0))) 'bracket)
  12496. ((org-pos-in-match-range pos 2) 'year)
  12497. ((org-pos-in-match-range pos 3) 'month)
  12498. ((org-pos-in-match-range pos 7) 'hour)
  12499. ((org-pos-in-match-range pos 8) 'minute)
  12500. ((or (org-pos-in-match-range pos 4)
  12501. (org-pos-in-match-range pos 5)) 'day)
  12502. ((and (> pos (or (match-end 8) (match-end 5)))
  12503. (< pos (match-end 0)))
  12504. (- pos (or (match-end 8) (match-end 5))))
  12505. (t 'day))))
  12506. ans))
  12507. (defun org-toggle-timestamp-type ()
  12508. "Toggle the type (<active> or [inactive]) of a time stamp."
  12509. (interactive)
  12510. (when (org-at-timestamp-p t)
  12511. (let ((beg (match-beginning 0)) (end (match-end 0))
  12512. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12513. (save-excursion
  12514. (goto-char beg)
  12515. (while (re-search-forward "[][<>]" end t)
  12516. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12517. t t)))
  12518. (message "Timestamp is now %sactive"
  12519. (if (equal (char-after beg) ?<) "" "in")))))
  12520. (defun org-timestamp-change (n &optional what)
  12521. "Change the date in the time stamp at point.
  12522. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12523. `year', `minute', `second'. If WHAT is not given, the cursor position
  12524. in the timestamp determines what will be changed."
  12525. (let ((pos (point))
  12526. with-hm inactive
  12527. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12528. org-ts-what
  12529. extra rem
  12530. ts time time0)
  12531. (if (not (org-at-timestamp-p t))
  12532. (error "Not at a timestamp"))
  12533. (if (and (not what) (eq org-ts-what 'bracket))
  12534. (org-toggle-timestamp-type)
  12535. (if (and (not what) (not (eq org-ts-what 'day))
  12536. org-display-custom-times
  12537. (get-text-property (point) 'display)
  12538. (not (get-text-property (1- (point)) 'display)))
  12539. (setq org-ts-what 'day))
  12540. (setq org-ts-what (or what org-ts-what)
  12541. inactive (= (char-after (match-beginning 0)) ?\[)
  12542. ts (match-string 0))
  12543. (replace-match "")
  12544. (if (string-match
  12545. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  12546. ts)
  12547. (setq extra (match-string 1 ts)))
  12548. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12549. (setq with-hm t))
  12550. (setq time0 (org-parse-time-string ts))
  12551. (when (and (eq org-ts-what 'minute)
  12552. (eq current-prefix-arg nil))
  12553. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12554. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12555. (setcar (cdr time0) (+ (nth 1 time0)
  12556. (if (> n 0) (- rem) (- dm rem))))))
  12557. (setq time
  12558. (encode-time (or (car time0) 0)
  12559. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12560. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12561. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12562. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12563. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12564. (nthcdr 6 time0)))
  12565. (when (and (member org-ts-what '(hour minute))
  12566. extra
  12567. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12568. (setq extra (org-modify-ts-extra
  12569. extra
  12570. (if (eq org-ts-what 'hour) 2 5)
  12571. n dm)))
  12572. (when (integerp org-ts-what)
  12573. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12574. (if (eq what 'calendar)
  12575. (let ((cal-date (org-get-date-from-calendar)))
  12576. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12577. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12578. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12579. (setcar time0 (or (car time0) 0))
  12580. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12581. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12582. (setq time (apply 'encode-time time0))))
  12583. (setq org-last-changed-timestamp
  12584. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12585. (org-clock-update-time-maybe)
  12586. (goto-char pos)
  12587. ;; Try to recenter the calendar window, if any
  12588. (if (and org-calendar-follow-timestamp-change
  12589. (get-buffer-window "*Calendar*" t)
  12590. (memq org-ts-what '(day month year)))
  12591. (org-recenter-calendar (time-to-days time))))))
  12592. (defun org-modify-ts-extra (s pos n dm)
  12593. "Change the different parts of the lead-time and repeat fields in timestamp."
  12594. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12595. ng h m new rem)
  12596. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12597. (cond
  12598. ((or (org-pos-in-match-range pos 2)
  12599. (org-pos-in-match-range pos 3))
  12600. (setq m (string-to-number (match-string 3 s))
  12601. h (string-to-number (match-string 2 s)))
  12602. (if (org-pos-in-match-range pos 2)
  12603. (setq h (+ h n))
  12604. (setq n (* dm (org-no-warnings (signum n))))
  12605. (when (not (= 0 (setq rem (% m dm))))
  12606. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12607. (setq m (+ m n)))
  12608. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12609. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12610. (setq h (min 24 (max 0 h)))
  12611. (setq ng 1 new (format "-%02d:%02d" h m)))
  12612. ((org-pos-in-match-range pos 6)
  12613. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12614. ((org-pos-in-match-range pos 5)
  12615. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12616. ((org-pos-in-match-range pos 9)
  12617. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12618. ((org-pos-in-match-range pos 8)
  12619. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12620. (when ng
  12621. (setq s (concat
  12622. (substring s 0 (match-beginning ng))
  12623. new
  12624. (substring s (match-end ng))))))
  12625. s))
  12626. (defun org-recenter-calendar (date)
  12627. "If the calendar is visible, recenter it to DATE."
  12628. (let* ((win (selected-window))
  12629. (cwin (get-buffer-window "*Calendar*" t))
  12630. (calendar-move-hook nil))
  12631. (when cwin
  12632. (select-window cwin)
  12633. (calendar-goto-date (if (listp date) date
  12634. (calendar-gregorian-from-absolute date)))
  12635. (select-window win))))
  12636. (defun org-goto-calendar (&optional arg)
  12637. "Go to the Emacs calendar at the current date.
  12638. If there is a time stamp in the current line, go to that date.
  12639. A prefix ARG can be used to force the current date."
  12640. (interactive "P")
  12641. (let ((tsr org-ts-regexp) diff
  12642. (calendar-move-hook nil)
  12643. (calendar-view-holidays-initially-flag nil)
  12644. (view-calendar-holidays-initially nil)
  12645. (calendar-view-diary-initially-flag nil)
  12646. (view-diary-entries-initially nil))
  12647. (if (or (org-at-timestamp-p)
  12648. (save-excursion
  12649. (beginning-of-line 1)
  12650. (looking-at (concat ".*" tsr))))
  12651. (let ((d1 (time-to-days (current-time)))
  12652. (d2 (time-to-days
  12653. (org-time-string-to-time (match-string 1)))))
  12654. (setq diff (- d2 d1))))
  12655. (calendar)
  12656. (calendar-goto-today)
  12657. (if (and diff (not arg)) (calendar-forward-day diff))))
  12658. (defun org-get-date-from-calendar ()
  12659. "Return a list (month day year) of date at point in calendar."
  12660. (with-current-buffer "*Calendar*"
  12661. (save-match-data
  12662. (calendar-cursor-to-date))))
  12663. (defun org-date-from-calendar ()
  12664. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12665. If there is already a time stamp at the cursor position, update it."
  12666. (interactive)
  12667. (if (org-at-timestamp-p t)
  12668. (org-timestamp-change 0 'calendar)
  12669. (let ((cal-date (org-get-date-from-calendar)))
  12670. (org-insert-time-stamp
  12671. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12672. (defun org-minutes-to-hh:mm-string (m)
  12673. "Compute H:MM from a number of minutes."
  12674. (let ((h (/ m 60)))
  12675. (setq m (- m (* 60 h)))
  12676. (format org-time-clocksum-format h m)))
  12677. (defun org-hh:mm-string-to-minutes (s)
  12678. "Convert a string H:MM to a number of minutes.
  12679. If the string is just a number, interprete it as minutes.
  12680. In fact, the first hh:mm or number in the string will be taken,
  12681. there can be extra stuff in the string.
  12682. If no number is found, the return value is 0."
  12683. (cond
  12684. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12685. (+ (* (string-to-number (match-string 1 s)) 60)
  12686. (string-to-number (match-string 2 s))))
  12687. ((string-match "\\([0-9]+\\)" s)
  12688. (string-to-number (match-string 1 s)))
  12689. (t 0)))
  12690. ;;;; Files
  12691. (defun org-save-all-org-buffers ()
  12692. "Save all Org-mode buffers without user confirmation."
  12693. (interactive)
  12694. (message "Saving all Org-mode buffers...")
  12695. (save-some-buffers t 'org-mode-p)
  12696. (when (featurep 'org-id) (org-id-locations-save))
  12697. (message "Saving all Org-mode buffers... done"))
  12698. (defun org-revert-all-org-buffers ()
  12699. "Revert all Org-mode buffers.
  12700. Prompt for confirmation when there are unsaved changes.
  12701. Be sure you know what you are doing before letting this function
  12702. overwrite your changes.
  12703. This function is useful in a setup where one tracks org files
  12704. with a version control system, to revert on one machine after pulling
  12705. changes from another. I believe the procedure must be like this:
  12706. 1. M-x org-save-all-org-buffers
  12707. 2. Pull changes from the other machine, resolve conflicts
  12708. 3. M-x org-revert-all-org-buffers"
  12709. (interactive)
  12710. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12711. (error "Abort"))
  12712. (save-excursion
  12713. (save-window-excursion
  12714. (mapc
  12715. (lambda (b)
  12716. (when (and (with-current-buffer b (org-mode-p))
  12717. (with-current-buffer b buffer-file-name))
  12718. (switch-to-buffer b)
  12719. (revert-buffer t 'no-confirm)))
  12720. (buffer-list))
  12721. (when (and (featurep 'org-id) org-id-track-globally)
  12722. (org-id-locations-load)))))
  12723. ;;;; Agenda files
  12724. ;;;###autoload
  12725. (defun org-iswitchb (&optional arg)
  12726. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12727. With a prefix argument, restrict available to files.
  12728. With two prefix arguments, restrict available buffers to agenda files."
  12729. (interactive "P")
  12730. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12731. ((equal arg '(16)) (org-buffer-list 'agenda))
  12732. (t (org-buffer-list)))))
  12733. (switch-to-buffer
  12734. (org-icompleting-read "Org buffer: "
  12735. (mapcar 'list (mapcar 'buffer-name blist))
  12736. nil t))))
  12737. ;;;###autoload
  12738. (defalias 'org-ido-switchb 'org-iswitchb)
  12739. (defun org-buffer-list (&optional predicate exclude-tmp)
  12740. "Return a list of Org buffers.
  12741. PREDICATE can be `export', `files' or `agenda'.
  12742. export restrict the list to Export buffers.
  12743. files restrict the list to buffers visiting Org files.
  12744. agenda restrict the list to buffers visiting agenda files.
  12745. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12746. (let* ((bfn nil)
  12747. (agenda-files (and (eq predicate 'agenda)
  12748. (mapcar 'file-truename (org-agenda-files t))))
  12749. (filter
  12750. (cond
  12751. ((eq predicate 'files)
  12752. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12753. ((eq predicate 'export)
  12754. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12755. ((eq predicate 'agenda)
  12756. (lambda (b)
  12757. (with-current-buffer b
  12758. (and (eq major-mode 'org-mode)
  12759. (setq bfn (buffer-file-name b))
  12760. (member (file-truename bfn) agenda-files)))))
  12761. (t (lambda (b) (with-current-buffer b
  12762. (or (eq major-mode 'org-mode)
  12763. (string-match "\*Org .*Export"
  12764. (buffer-name b)))))))))
  12765. (delq nil
  12766. (mapcar
  12767. (lambda(b)
  12768. (if (and (funcall filter b)
  12769. (or (not exclude-tmp)
  12770. (not (string-match "tmp" (buffer-name b)))))
  12771. b
  12772. nil))
  12773. (buffer-list)))))
  12774. (defun org-agenda-files (&optional unrestricted archives)
  12775. "Get the list of agenda files.
  12776. Optional UNRESTRICTED means return the full list even if a restriction
  12777. is currently in place.
  12778. When ARCHIVES is t, include all archive files hat are really being
  12779. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12780. `org-agenda-archives-mode' is t."
  12781. (let ((files
  12782. (cond
  12783. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12784. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12785. ((listp org-agenda-files) org-agenda-files)
  12786. (t (error "Invalid value of `org-agenda-files'")))))
  12787. (setq files (apply 'append
  12788. (mapcar (lambda (f)
  12789. (if (file-directory-p f)
  12790. (directory-files
  12791. f t org-agenda-file-regexp)
  12792. (list f)))
  12793. files)))
  12794. (when org-agenda-skip-unavailable-files
  12795. (setq files (delq nil
  12796. (mapcar (function
  12797. (lambda (file)
  12798. (and (file-readable-p file) file)))
  12799. files))))
  12800. (when (or (eq archives t)
  12801. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12802. (setq files (org-add-archive-files files)))
  12803. files))
  12804. (defun org-edit-agenda-file-list ()
  12805. "Edit the list of agenda files.
  12806. Depending on setup, this either uses customize to edit the variable
  12807. `org-agenda-files', or it visits the file that is holding the list. In the
  12808. latter case, the buffer is set up in a way that saving it automatically kills
  12809. the buffer and restores the previous window configuration."
  12810. (interactive)
  12811. (if (stringp org-agenda-files)
  12812. (let ((cw (current-window-configuration)))
  12813. (find-file org-agenda-files)
  12814. (org-set-local 'org-window-configuration cw)
  12815. (org-add-hook 'after-save-hook
  12816. (lambda ()
  12817. (set-window-configuration
  12818. (prog1 org-window-configuration
  12819. (kill-buffer (current-buffer))))
  12820. (org-install-agenda-files-menu)
  12821. (message "New agenda file list installed"))
  12822. nil 'local)
  12823. (message "%s" (substitute-command-keys
  12824. "Edit list and finish with \\[save-buffer]")))
  12825. (customize-variable 'org-agenda-files)))
  12826. (defun org-store-new-agenda-file-list (list)
  12827. "Set new value for the agenda file list and save it correctly."
  12828. (if (stringp org-agenda-files)
  12829. (let ((f org-agenda-files) b)
  12830. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12831. (with-temp-file f
  12832. (insert (mapconcat 'identity list "\n") "\n")))
  12833. (let ((org-mode-hook nil) (org-inhibit-startup t)
  12834. (org-insert-mode-line-in-empty-file nil))
  12835. (setq org-agenda-files list)
  12836. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12837. (defun org-read-agenda-file-list ()
  12838. "Read the list of agenda files from a file."
  12839. (when (file-directory-p org-agenda-files)
  12840. (error "`org-agenda-files' cannot be a single directory"))
  12841. (when (stringp org-agenda-files)
  12842. (with-temp-buffer
  12843. (insert-file-contents org-agenda-files)
  12844. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12845. ;;;###autoload
  12846. (defun org-cycle-agenda-files ()
  12847. "Cycle through the files in `org-agenda-files'.
  12848. If the current buffer visits an agenda file, find the next one in the list.
  12849. If the current buffer does not, find the first agenda file."
  12850. (interactive)
  12851. (let* ((fs (org-agenda-files t))
  12852. (files (append fs (list (car fs))))
  12853. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12854. file)
  12855. (unless files (error "No agenda files"))
  12856. (catch 'exit
  12857. (while (setq file (pop files))
  12858. (if (equal (file-truename file) tcf)
  12859. (when (car files)
  12860. (find-file (car files))
  12861. (throw 'exit t))))
  12862. (find-file (car fs)))
  12863. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12864. (defun org-agenda-file-to-front (&optional to-end)
  12865. "Move/add the current file to the top of the agenda file list.
  12866. If the file is not present in the list, it is added to the front. If it is
  12867. present, it is moved there. With optional argument TO-END, add/move to the
  12868. end of the list."
  12869. (interactive "P")
  12870. (let ((org-agenda-skip-unavailable-files nil)
  12871. (file-alist (mapcar (lambda (x)
  12872. (cons (file-truename x) x))
  12873. (org-agenda-files t)))
  12874. (ctf (file-truename buffer-file-name))
  12875. x had)
  12876. (setq x (assoc ctf file-alist) had x)
  12877. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12878. (if to-end
  12879. (setq file-alist (append (delq x file-alist) (list x)))
  12880. (setq file-alist (cons x (delq x file-alist))))
  12881. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12882. (org-install-agenda-files-menu)
  12883. (message "File %s to %s of agenda file list"
  12884. (if had "moved" "added") (if to-end "end" "front"))))
  12885. (defun org-remove-file (&optional file)
  12886. "Remove current file from the list of files in variable `org-agenda-files'.
  12887. These are the files which are being checked for agenda entries.
  12888. Optional argument FILE means, use this file instead of the current."
  12889. (interactive)
  12890. (let* ((org-agenda-skip-unavailable-files nil)
  12891. (file (or file buffer-file-name))
  12892. (true-file (file-truename file))
  12893. (afile (abbreviate-file-name file))
  12894. (files (delq nil (mapcar
  12895. (lambda (x)
  12896. (if (equal true-file
  12897. (file-truename x))
  12898. nil x))
  12899. (org-agenda-files t)))))
  12900. (if (not (= (length files) (length (org-agenda-files t))))
  12901. (progn
  12902. (org-store-new-agenda-file-list files)
  12903. (org-install-agenda-files-menu)
  12904. (message "Removed file: %s" afile))
  12905. (message "File was not in list: %s (not removed)" afile))))
  12906. (defun org-file-menu-entry (file)
  12907. (vector file (list 'find-file file) t))
  12908. (defun org-check-agenda-file (file)
  12909. "Make sure FILE exists. If not, ask user what to do."
  12910. (when (not (file-exists-p file))
  12911. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  12912. (abbreviate-file-name file))
  12913. (let ((r (downcase (read-char-exclusive))))
  12914. (cond
  12915. ((equal r ?r)
  12916. (org-remove-file file)
  12917. (throw 'nextfile t))
  12918. (t (error "Abort"))))))
  12919. (defun org-get-agenda-file-buffer (file)
  12920. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12921. it to the list of buffers which might be released later."
  12922. (let ((buf (org-find-base-buffer-visiting file)))
  12923. (if buf
  12924. buf ; just return it
  12925. ;; Make a new buffer and remember it
  12926. (setq buf (find-file-noselect file))
  12927. (if buf (push buf org-agenda-new-buffers))
  12928. buf)))
  12929. (defun org-release-buffers (blist)
  12930. "Release all buffers in list, asking the user for confirmation when needed.
  12931. When a buffer is unmodified, it is just killed. When modified, it is saved
  12932. \(if the user agrees) and then killed."
  12933. (let (buf file)
  12934. (while (setq buf (pop blist))
  12935. (setq file (buffer-file-name buf))
  12936. (when (and (buffer-modified-p buf)
  12937. file
  12938. (y-or-n-p (format "Save file %s? " file)))
  12939. (with-current-buffer buf (save-buffer)))
  12940. (kill-buffer buf))))
  12941. (defun org-prepare-agenda-buffers (files)
  12942. "Create buffers for all agenda files, protect archived trees and comments."
  12943. (interactive)
  12944. (let ((pa '(:org-archived t))
  12945. (pc '(:org-comment t))
  12946. (pall '(:org-archived t :org-comment t))
  12947. (inhibit-read-only t)
  12948. (rea (concat ":" org-archive-tag ":"))
  12949. bmp file re)
  12950. (save-excursion
  12951. (save-restriction
  12952. (while (setq file (pop files))
  12953. (catch 'nextfile
  12954. (if (bufferp file)
  12955. (set-buffer file)
  12956. (org-check-agenda-file file)
  12957. (set-buffer (org-get-agenda-file-buffer file)))
  12958. (widen)
  12959. (setq bmp (buffer-modified-p))
  12960. (org-refresh-category-properties)
  12961. (setq org-todo-keywords-for-agenda
  12962. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12963. (setq org-done-keywords-for-agenda
  12964. (append org-done-keywords-for-agenda org-done-keywords))
  12965. (setq org-todo-keyword-alist-for-agenda
  12966. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12967. (setq org-drawers-for-agenda
  12968. (append org-drawers-for-agenda org-drawers))
  12969. (setq org-tag-alist-for-agenda
  12970. (append org-tag-alist-for-agenda org-tag-alist))
  12971. (save-excursion
  12972. (remove-text-properties (point-min) (point-max) pall)
  12973. (when org-agenda-skip-archived-trees
  12974. (goto-char (point-min))
  12975. (while (re-search-forward rea nil t)
  12976. (if (org-on-heading-p t)
  12977. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12978. (goto-char (point-min))
  12979. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12980. (while (re-search-forward re nil t)
  12981. (add-text-properties
  12982. (match-beginning 0) (org-end-of-subtree t) pc)))
  12983. (set-buffer-modified-p bmp)))))
  12984. (setq org-todo-keyword-alist-for-agenda
  12985. (org-uniquify org-todo-keyword-alist-for-agenda)
  12986. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12987. ;;;; Embedded LaTeX
  12988. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12989. "Keymap for the minor `org-cdlatex-mode'.")
  12990. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12991. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12992. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12993. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12994. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12995. (defvar org-cdlatex-texmathp-advice-is-done nil
  12996. "Flag remembering if we have applied the advice to texmathp already.")
  12997. (define-minor-mode org-cdlatex-mode
  12998. "Toggle the minor `org-cdlatex-mode'.
  12999. This mode supports entering LaTeX environment and math in LaTeX fragments
  13000. in Org-mode.
  13001. \\{org-cdlatex-mode-map}"
  13002. nil " OCDL" nil
  13003. (when org-cdlatex-mode (require 'cdlatex))
  13004. (unless org-cdlatex-texmathp-advice-is-done
  13005. (setq org-cdlatex-texmathp-advice-is-done t)
  13006. (defadvice texmathp (around org-math-always-on activate)
  13007. "Always return t in org-mode buffers.
  13008. This is because we want to insert math symbols without dollars even outside
  13009. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13010. an embedded LaTeX fragment, let texmathp do its job.
  13011. \\[org-cdlatex-mode-map]"
  13012. (interactive)
  13013. (let (p)
  13014. (cond
  13015. ((not (org-mode-p)) ad-do-it)
  13016. ((eq this-command 'cdlatex-math-symbol)
  13017. (setq ad-return-value t
  13018. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13019. (t
  13020. (let ((p (org-inside-LaTeX-fragment-p)))
  13021. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13022. (setq ad-return-value t
  13023. texmathp-why '("Org-mode embedded math" . 0))
  13024. (if p ad-do-it)))))))))
  13025. (defun turn-on-org-cdlatex ()
  13026. "Unconditionally turn on `org-cdlatex-mode'."
  13027. (org-cdlatex-mode 1))
  13028. (defun org-inside-LaTeX-fragment-p ()
  13029. "Test if point is inside a LaTeX fragment.
  13030. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13031. sequence appearing also before point.
  13032. Even though the matchers for math are configurable, this function assumes
  13033. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13034. delimiters are skipped when they have been removed by customization.
  13035. The return value is nil, or a cons cell with the delimiter and
  13036. and the position of this delimiter.
  13037. This function does a reasonably good job, but can locally be fooled by
  13038. for example currency specifications. For example it will assume being in
  13039. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13040. fragments that are properly closed, but during editing, we have to live
  13041. with the uncertainty caused by missing closing delimiters. This function
  13042. looks only before point, not after."
  13043. (catch 'exit
  13044. (let ((pos (point))
  13045. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13046. (lim (progn
  13047. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13048. (point)))
  13049. dd-on str (start 0) m re)
  13050. (goto-char pos)
  13051. (when dodollar
  13052. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13053. re (nth 1 (assoc "$" org-latex-regexps)))
  13054. (while (string-match re str start)
  13055. (cond
  13056. ((= (match-end 0) (length str))
  13057. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13058. ((= (match-end 0) (- (length str) 5))
  13059. (throw 'exit nil))
  13060. (t (setq start (match-end 0))))))
  13061. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13062. (goto-char pos)
  13063. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13064. (and (match-beginning 2) (throw 'exit nil))
  13065. ;; count $$
  13066. (while (re-search-backward "\\$\\$" lim t)
  13067. (setq dd-on (not dd-on)))
  13068. (goto-char pos)
  13069. (if dd-on (cons "$$" m))))))
  13070. (defun org-try-cdlatex-tab ()
  13071. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13072. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13073. - inside a LaTeX fragment, or
  13074. - after the first word in a line, where an abbreviation expansion could
  13075. insert a LaTeX environment."
  13076. (when org-cdlatex-mode
  13077. (cond
  13078. ((save-excursion
  13079. (skip-chars-backward "a-zA-Z0-9*")
  13080. (skip-chars-backward " \t")
  13081. (bolp))
  13082. (cdlatex-tab) t)
  13083. ((org-inside-LaTeX-fragment-p)
  13084. (cdlatex-tab) t)
  13085. (t nil))))
  13086. (defun org-cdlatex-underscore-caret (&optional arg)
  13087. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13088. Revert to the normal definition outside of these fragments."
  13089. (interactive "P")
  13090. (if (org-inside-LaTeX-fragment-p)
  13091. (call-interactively 'cdlatex-sub-superscript)
  13092. (let (org-cdlatex-mode)
  13093. (call-interactively (key-binding (vector last-input-event))))))
  13094. (defun org-cdlatex-math-modify (&optional arg)
  13095. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13096. Revert to the normal definition outside of these fragments."
  13097. (interactive "P")
  13098. (if (org-inside-LaTeX-fragment-p)
  13099. (call-interactively 'cdlatex-math-modify)
  13100. (let (org-cdlatex-mode)
  13101. (call-interactively (key-binding (vector last-input-event))))))
  13102. (defvar org-latex-fragment-image-overlays nil
  13103. "List of overlays carrying the images of latex fragments.")
  13104. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13105. (defun org-remove-latex-fragment-image-overlays ()
  13106. "Remove all overlays with LaTeX fragment images in current buffer."
  13107. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13108. (setq org-latex-fragment-image-overlays nil))
  13109. (defun org-preview-latex-fragment (&optional subtree)
  13110. "Preview the LaTeX fragment at point, or all locally or globally.
  13111. If the cursor is in a LaTeX fragment, create the image and overlay
  13112. it over the source code. If there is no fragment at point, display
  13113. all fragments in the current text, from one headline to the next. With
  13114. prefix SUBTREE, display all fragments in the current subtree. With a
  13115. double prefix `C-u C-u', or when the cursor is before the first headline,
  13116. display all fragments in the buffer.
  13117. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13118. (interactive "P")
  13119. (org-remove-latex-fragment-image-overlays)
  13120. (save-excursion
  13121. (save-restriction
  13122. (let (beg end at msg)
  13123. (cond
  13124. ((or (equal subtree '(16))
  13125. (not (save-excursion
  13126. (re-search-backward (concat "^" outline-regexp) nil t))))
  13127. (setq beg (point-min) end (point-max)
  13128. msg "Creating images for buffer...%s"))
  13129. ((equal subtree '(4))
  13130. (org-back-to-heading)
  13131. (setq beg (point) end (org-end-of-subtree t)
  13132. msg "Creating images for subtree...%s"))
  13133. (t
  13134. (if (setq at (org-inside-LaTeX-fragment-p))
  13135. (goto-char (max (point-min) (- (cdr at) 2)))
  13136. (org-back-to-heading))
  13137. (setq beg (point) end (progn (outline-next-heading) (point))
  13138. msg (if at "Creating image...%s"
  13139. "Creating images for entry...%s"))))
  13140. (message msg "")
  13141. (narrow-to-region beg end)
  13142. (goto-char beg)
  13143. (org-format-latex
  13144. (concat "ltxpng/" (file-name-sans-extension
  13145. (file-name-nondirectory
  13146. buffer-file-name)))
  13147. default-directory 'overlays msg at 'forbuffer)
  13148. (message msg "done. Use `C-c C-c' to remove images.")))))
  13149. (defvar org-latex-regexps
  13150. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13151. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13152. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13153. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13154. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13155. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13156. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13157. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13158. "Regular expressions for matching embedded LaTeX.")
  13159. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13160. "Replace LaTeX fragments with links to an image, and produce images.
  13161. Some of the options can be changed using the variable
  13162. `org-format-latex-options'."
  13163. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13164. (let* ((prefixnodir (file-name-nondirectory prefix))
  13165. (absprefix (expand-file-name prefix dir))
  13166. (todir (file-name-directory absprefix))
  13167. (opt org-format-latex-options)
  13168. (matchers (plist-get opt :matchers))
  13169. (re-list org-latex-regexps)
  13170. (cnt 0) txt link beg end re e checkdir
  13171. executables-checked
  13172. m n block linkfile movefile ov)
  13173. ;; Check if there are old images files with this prefix, and remove them
  13174. (when (file-directory-p todir)
  13175. (mapc 'delete-file
  13176. (directory-files
  13177. todir 'full
  13178. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  13179. ;; Check the different regular expressions
  13180. (while (setq e (pop re-list))
  13181. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13182. block (if (nth 3 e) "\n\n" ""))
  13183. (when (member m matchers)
  13184. (goto-char (point-min))
  13185. (while (re-search-forward re nil t)
  13186. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13187. (not (get-text-property (match-beginning n)
  13188. 'org-protected))
  13189. (or (not overlays)
  13190. (not (eq (get-char-property (match-beginning n)
  13191. 'org-overlay-type)
  13192. 'org-latex-overlay))))
  13193. (setq txt (match-string n)
  13194. beg (match-beginning n) end (match-end n)
  13195. cnt (1+ cnt)
  13196. linkfile (format "%s_%04d.png" prefix cnt)
  13197. movefile (format "%s_%04d.png" absprefix cnt)
  13198. link (concat block "[[file:" linkfile "]]" block))
  13199. (if msg (message msg cnt))
  13200. (goto-char beg)
  13201. (unless checkdir ; make sure the directory exists
  13202. (setq checkdir t)
  13203. (or (file-directory-p todir) (make-directory todir)))
  13204. (unless executables-checked
  13205. (org-check-external-command
  13206. "latex" "needed to convert LaTeX fragments to images")
  13207. (org-check-external-command
  13208. "dvipng" "needed to convert LaTeX fragments to images")
  13209. (setq executables-checked t))
  13210. (org-create-formula-image
  13211. txt movefile opt forbuffer)
  13212. (if overlays
  13213. (progn
  13214. (mapc (lambda (o)
  13215. (if (eq (org-overlay-get o 'org-overlay-type)
  13216. 'org-latex-overlay)
  13217. (org-delete-overlay o)))
  13218. (org-overlays-in beg end))
  13219. (setq ov (org-make-overlay beg end))
  13220. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13221. (if (featurep 'xemacs)
  13222. (progn
  13223. (org-overlay-put ov 'invisible t)
  13224. (org-overlay-put
  13225. ov 'end-glyph
  13226. (make-glyph (vector 'png :file movefile))))
  13227. (org-overlay-put
  13228. ov 'display
  13229. (list 'image :type 'png :file movefile :ascent 'center)))
  13230. (push ov org-latex-fragment-image-overlays)
  13231. (goto-char end))
  13232. (delete-region beg end)
  13233. (insert link))))))))
  13234. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  13235. ;; This function borrows from Ganesh Swami's latex2png.el
  13236. (defun org-create-formula-image (string tofile options buffer)
  13237. "This calls dvipng."
  13238. (require 'org-latex)
  13239. (let* ((tmpdir (if (featurep 'xemacs)
  13240. (temp-directory)
  13241. temporary-file-directory))
  13242. (texfilebase (make-temp-name
  13243. (expand-file-name "orgtex" tmpdir)))
  13244. (texfile (concat texfilebase ".tex"))
  13245. (dvifile (concat texfilebase ".dvi"))
  13246. (pngfile (concat texfilebase ".png"))
  13247. (fnh (if (featurep 'xemacs)
  13248. (font-height (get-face-font 'default))
  13249. (face-attribute 'default :height nil)))
  13250. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13251. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13252. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13253. "Black"))
  13254. (bg (or (plist-get options (if buffer :background :html-background))
  13255. "Transparent")))
  13256. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13257. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13258. (with-temp-file texfile
  13259. (insert org-format-latex-header
  13260. (if org-export-latex-packages-alist
  13261. (concat "\n"
  13262. (mapconcat (lambda(p)
  13263. (if (equal "" (car p))
  13264. (format "\\usepackage{%s}" (cadr p))
  13265. (format "\\usepackage[%s]{%s}"
  13266. (car p) (cadr p))))
  13267. org-export-latex-packages-alist "\n"))
  13268. "")
  13269. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13270. (let ((dir default-directory))
  13271. (condition-case nil
  13272. (progn
  13273. (cd tmpdir)
  13274. (call-process "latex" nil nil nil texfile))
  13275. (error nil))
  13276. (cd dir))
  13277. (if (not (file-exists-p dvifile))
  13278. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13279. (condition-case nil
  13280. (call-process "dvipng" nil nil nil
  13281. "-fg" fg "-bg" bg
  13282. "-D" dpi
  13283. ;;"-x" scale "-y" scale
  13284. "-T" "tight"
  13285. "-o" pngfile
  13286. dvifile)
  13287. (error nil))
  13288. (if (not (file-exists-p pngfile))
  13289. (progn (message "Failed to create png file from %s" texfile) nil)
  13290. ;; Use the requested file name and clean up
  13291. (copy-file pngfile tofile 'replace)
  13292. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13293. (delete-file (concat texfilebase e)))
  13294. pngfile))))
  13295. (defun org-dvipng-color (attr)
  13296. "Return an rgb color specification for dvipng."
  13297. (apply 'format "rgb %s %s %s"
  13298. (mapcar 'org-normalize-color
  13299. (color-values (face-attribute 'default attr nil)))))
  13300. (defun org-normalize-color (value)
  13301. "Return string to be used as color value for an RGB component."
  13302. (format "%g" (/ value 65535.0)))
  13303. ;;;; Key bindings
  13304. ;; Make `C-c C-x' a prefix key
  13305. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13306. ;; TAB key with modifiers
  13307. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13308. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13309. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13310. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13311. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13312. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13313. ;; The following line is necessary under Suse GNU/Linux
  13314. (unless (featurep 'xemacs)
  13315. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13316. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13317. (define-key org-mode-map [backtab] 'org-shifttab)
  13318. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13319. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13320. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13321. ;; Cursor keys with modifiers
  13322. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13323. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13324. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13325. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13326. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13327. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13328. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13329. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13330. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13331. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13332. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13333. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13334. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13335. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13336. ;;; Extra keys for tty access.
  13337. ;; We only set them when really needed because otherwise the
  13338. ;; menus don't show the simple keys
  13339. (when (or org-use-extra-keys
  13340. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13341. (not window-system))
  13342. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13343. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13344. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13345. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13346. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13347. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13348. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13349. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13350. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13351. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13352. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13353. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13354. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13355. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13356. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13357. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13358. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13359. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13360. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13361. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13362. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13363. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13364. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13365. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13366. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13367. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13368. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13369. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13370. ;; All the other keys
  13371. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13372. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13373. (if (boundp 'narrow-map)
  13374. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13375. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13376. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13377. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13378. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13379. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13380. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  13381. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13382. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13383. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13384. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13385. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13386. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13387. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13388. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13389. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13390. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13391. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13392. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13393. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13394. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13395. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13396. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13397. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13398. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13399. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13400. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13401. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13402. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13403. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13404. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13405. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13406. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13407. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13408. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13409. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13410. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13411. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13412. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13413. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13414. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13415. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13416. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13417. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13418. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13419. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13420. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13421. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13422. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13423. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13424. (org-defkey org-mode-map "\C-m" 'org-return)
  13425. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13426. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13427. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13428. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13429. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13430. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13431. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13432. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13433. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13434. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13435. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13436. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13437. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13438. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13439. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13440. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13441. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13442. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  13443. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  13444. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13445. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13446. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13447. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13448. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13449. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13450. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13451. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13452. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13453. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13454. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13455. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13456. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13457. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13458. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13459. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13460. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13461. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13462. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13463. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13464. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13465. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13466. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13467. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13468. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13469. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13470. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13471. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13472. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13473. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13474. (when (featurep 'xemacs)
  13475. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13476. (defconst org-speed-commands-default
  13477. '(
  13478. ("a" . org-agenda)
  13479. ("/" . org-sparse-tree)
  13480. (";" . org-set-tags-command)
  13481. ("I" . org-clock-in)
  13482. ("O" . org-clock-out)
  13483. ("c" . org-cycle)
  13484. ("C" . org-shifttab)
  13485. ("n" . outline-next-visible-heading)
  13486. ("p" . outline-previous-visible-heading)
  13487. ("f" . org-forward-same-level)
  13488. ("b" . org-backward-same-level)
  13489. ("u" . outline-up-heading)
  13490. ("U" . org-shiftmetaup)
  13491. ("D" . org-shiftmetadown)
  13492. ("r" . org-metaright)
  13493. ("l" . org-metaleft)
  13494. ("R" . org-shiftmetaright)
  13495. ("L" . org-shiftmetaleft)
  13496. ("i" . (progn (forward-char 1) (call-interactively
  13497. 'org-insert-heading-respect-content)))
  13498. ("o" . org-open-at-point)
  13499. ("t" . org-todo)
  13500. ("j" . org-goto)
  13501. ("g" . (org-refile t))
  13502. ("e" . org-set-effort)
  13503. ("0" . (org-priority ?\ ))
  13504. ("1" . (org-priority ?A))
  13505. ("2" . (org-priority ?B))
  13506. ("3" . (org-priority ?C))
  13507. ("." . outline-mark-subtree)
  13508. ("^" . org-sort)
  13509. ("w" . org-refile)
  13510. ("a" . org-archive-subtree-default-with-confirmation)
  13511. ("/" . org-sparse-tree)
  13512. ("?" . org-speed-command-help)
  13513. )
  13514. "The default speed commands.")
  13515. (defun org-print-speed-command (e)
  13516. (princ (car e))
  13517. (princ " ")
  13518. (if (symbolp (cdr e))
  13519. (princ (symbol-name (cdr e)))
  13520. (prin1 (cdr e)))
  13521. (princ "\n"))
  13522. (defun org-speed-command-help ()
  13523. "Show the available speed commands."
  13524. (interactive)
  13525. (if (not org-use-speed-commands)
  13526. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  13527. (with-output-to-temp-buffer "*Help*"
  13528. (princ "Speed commands\n==============\n")
  13529. (mapc 'org-print-speed-command org-speed-commands-user)
  13530. (princ "\n")
  13531. (mapc 'org-print-speed-command org-speed-commands-default))))
  13532. (defvar org-self-insert-command-undo-counter 0)
  13533. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13534. (defvar org-speed-command nil)
  13535. (defun org-self-insert-command (N)
  13536. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13537. If the cursor is in a table looking at whitespace, the whitespace is
  13538. overwritten, and the table is not marked as requiring realignment."
  13539. (interactive "p")
  13540. (cond
  13541. ((and org-use-speed-commands
  13542. (bolp)
  13543. (looking-at outline-regexp)
  13544. (setq
  13545. org-speed-command
  13546. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  13547. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  13548. (cond
  13549. ((commandp org-speed-command)
  13550. (setq this-command org-speed-command)
  13551. (call-interactively org-speed-command))
  13552. ((functionp org-speed-command)
  13553. (funcall org-speed-command))
  13554. ((and org-speed-command (listp org-speed-command))
  13555. (eval org-speed-command))
  13556. (t (let (org-use-speed-commands)
  13557. (call-interactively 'org-self-insert-command)))))
  13558. ((and
  13559. (org-table-p)
  13560. (progn
  13561. ;; check if we blank the field, and if that triggers align
  13562. (and (featurep 'org-table) org-table-auto-blank-field
  13563. (member last-command
  13564. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13565. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13566. ;; got extra space, this field does not determine column width
  13567. (let (org-table-may-need-update) (org-table-blank-field))
  13568. ;; no extra space, this field may determine column width
  13569. (org-table-blank-field)))
  13570. t)
  13571. (eq N 1)
  13572. (looking-at "[^|\n]* |"))
  13573. (let (org-table-may-need-update)
  13574. (goto-char (1- (match-end 0)))
  13575. (delete-backward-char 1)
  13576. (goto-char (match-beginning 0))
  13577. (self-insert-command N)))
  13578. (t
  13579. (setq org-table-may-need-update t)
  13580. (self-insert-command N)
  13581. (org-fix-tags-on-the-fly)
  13582. (if org-self-insert-cluster-for-undo
  13583. (if (not (eq last-command 'org-self-insert-command))
  13584. (setq org-self-insert-command-undo-counter 1)
  13585. (if (>= org-self-insert-command-undo-counter 20)
  13586. (setq org-self-insert-command-undo-counter 1)
  13587. (and (> org-self-insert-command-undo-counter 0)
  13588. buffer-undo-list
  13589. (not (cadr buffer-undo-list)) ; remove nil entry
  13590. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13591. (setq org-self-insert-command-undo-counter
  13592. (1+ org-self-insert-command-undo-counter))))))))
  13593. (defun org-fix-tags-on-the-fly ()
  13594. (when (and (equal (char-after (point-at-bol)) ?*)
  13595. (org-on-heading-p))
  13596. (org-align-tags-here org-tags-column)))
  13597. (defun org-delete-backward-char (N)
  13598. "Like `delete-backward-char', insert whitespace at field end in tables.
  13599. When deleting backwards, in tables this function will insert whitespace in
  13600. front of the next \"|\" separator, to keep the table aligned. The table will
  13601. still be marked for re-alignment if the field did fill the entire column,
  13602. because, in this case the deletion might narrow the column."
  13603. (interactive "p")
  13604. (if (and (org-table-p)
  13605. (eq N 1)
  13606. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13607. (looking-at ".*?|"))
  13608. (let ((pos (point))
  13609. (noalign (looking-at "[^|\n\r]* |"))
  13610. (c org-table-may-need-update))
  13611. (backward-delete-char N)
  13612. (skip-chars-forward "^|")
  13613. (insert " ")
  13614. (goto-char (1- pos))
  13615. ;; noalign: if there were two spaces at the end, this field
  13616. ;; does not determine the width of the column.
  13617. (if noalign (setq org-table-may-need-update c)))
  13618. (backward-delete-char N)
  13619. (org-fix-tags-on-the-fly)))
  13620. (defun org-delete-char (N)
  13621. "Like `delete-char', but insert whitespace at field end in tables.
  13622. When deleting characters, in tables this function will insert whitespace in
  13623. front of the next \"|\" separator, to keep the table aligned. The table will
  13624. still be marked for re-alignment if the field did fill the entire column,
  13625. because, in this case the deletion might narrow the column."
  13626. (interactive "p")
  13627. (if (and (org-table-p)
  13628. (not (bolp))
  13629. (not (= (char-after) ?|))
  13630. (eq N 1))
  13631. (if (looking-at ".*?|")
  13632. (let ((pos (point))
  13633. (noalign (looking-at "[^|\n\r]* |"))
  13634. (c org-table-may-need-update))
  13635. (replace-match (concat
  13636. (substring (match-string 0) 1 -1)
  13637. " |"))
  13638. (goto-char pos)
  13639. ;; noalign: if there were two spaces at the end, this field
  13640. ;; does not determine the width of the column.
  13641. (if noalign (setq org-table-may-need-update c)))
  13642. (delete-char N))
  13643. (delete-char N)
  13644. (org-fix-tags-on-the-fly)))
  13645. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13646. (put 'org-self-insert-command 'delete-selection t)
  13647. (put 'orgtbl-self-insert-command 'delete-selection t)
  13648. (put 'org-delete-char 'delete-selection 'supersede)
  13649. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13650. (put 'org-yank 'delete-selection 'yank)
  13651. ;; Make `flyspell-mode' delay after some commands
  13652. (put 'org-self-insert-command 'flyspell-delayed t)
  13653. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13654. (put 'org-delete-char 'flyspell-delayed t)
  13655. (put 'org-delete-backward-char 'flyspell-delayed t)
  13656. ;; Make pabbrev-mode expand after org-mode commands
  13657. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13658. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13659. ;; How to do this: Measure non-white length of current string
  13660. ;; If equal to column width, we should realign.
  13661. (defun org-remap (map &rest commands)
  13662. "In MAP, remap the functions given in COMMANDS.
  13663. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13664. (let (new old)
  13665. (while commands
  13666. (setq old (pop commands) new (pop commands))
  13667. (if (fboundp 'command-remapping)
  13668. (org-defkey map (vector 'remap old) new)
  13669. (substitute-key-definition old new map global-map)))))
  13670. (when (eq org-enable-table-editor 'optimized)
  13671. ;; If the user wants maximum table support, we need to hijack
  13672. ;; some standard editing functions
  13673. (org-remap org-mode-map
  13674. 'self-insert-command 'org-self-insert-command
  13675. 'delete-char 'org-delete-char
  13676. 'delete-backward-char 'org-delete-backward-char)
  13677. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13678. (defvar org-ctrl-c-ctrl-c-hook nil
  13679. "Hook for functions attaching themselves to `C-c C-c'.
  13680. This can be used to add additional functionality to the C-c C-c key which
  13681. executes context-dependent commands.
  13682. Each function will be called with no arguments. The function must check
  13683. if the context is appropriate for it to act. If yes, it should do its
  13684. thing and then return a non-nil value. If the context is wrong,
  13685. just do nothing and return nil.")
  13686. (defvar org-tab-first-hook nil
  13687. "Hook for functions to attach themselves to TAB.
  13688. See `org-ctrl-c-ctrl-c-hook' for more information.
  13689. This hook runs as the first action when TAB is pressed, even before
  13690. `org-cycle' messes around with the `outline-regexp' to cater for
  13691. inline tasks and plain list item folding.
  13692. If any function in this hook returns t, not other actions like table
  13693. field motion visibility cycling will be done.")
  13694. (defvar org-tab-after-check-for-table-hook nil
  13695. "Hook for functions to attach themselves to TAB.
  13696. See `org-ctrl-c-ctrl-c-hook' for more information.
  13697. This hook runs after it has been established that the cursor is not in a
  13698. table, but before checking if the cursor is in a headline or if global cycling
  13699. should be done.
  13700. If any function in this hook returns t, not other actions like visibility
  13701. cycling will be done.")
  13702. (defvar org-tab-after-check-for-cycling-hook nil
  13703. "Hook for functions to attach themselves to TAB.
  13704. See `org-ctrl-c-ctrl-c-hook' for more information.
  13705. This hook runs after it has been established that not table field motion and
  13706. not visibility should be done because of current context. This is probably
  13707. the place where a package like yasnippets can hook in.")
  13708. (defvar org-tab-before-tab-emulation-hook nil
  13709. "Hook for functions to attach themselves to TAB.
  13710. See `org-ctrl-c-ctrl-c-hook' for more information.
  13711. This hook runs after every other options for TAB have been exhausted, but
  13712. before indentation and \t insertion takes place.")
  13713. (defvar org-metaleft-hook nil
  13714. "Hook for functions attaching themselves to `M-left'.
  13715. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13716. (defvar org-metaright-hook nil
  13717. "Hook for functions attaching themselves to `M-right'.
  13718. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13719. (defvar org-metaup-hook nil
  13720. "Hook for functions attaching themselves to `M-up'.
  13721. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13722. (defvar org-metadown-hook nil
  13723. "Hook for functions attaching themselves to `M-down'.
  13724. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13725. (defvar org-shiftmetaleft-hook nil
  13726. "Hook for functions attaching themselves to `M-S-left'.
  13727. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13728. (defvar org-shiftmetaright-hook nil
  13729. "Hook for functions attaching themselves to `M-S-right'.
  13730. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13731. (defvar org-shiftmetaup-hook nil
  13732. "Hook for functions attaching themselves to `M-S-up'.
  13733. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13734. (defvar org-shiftmetadown-hook nil
  13735. "Hook for functions attaching themselves to `M-S-down'.
  13736. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13737. (defvar org-metareturn-hook nil
  13738. "Hook for functions attaching themselves to `M-RET'.
  13739. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13740. (defun org-modifier-cursor-error ()
  13741. "Throw an error, a modified cursor command was applied in wrong context."
  13742. (error "This command is active in special context like tables, headlines or items"))
  13743. (defun org-shiftselect-error ()
  13744. "Throw an error because Shift-Cursor command was applied in wrong context."
  13745. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13746. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  13747. (error "This command works only in special context like headlines or timestamps")))
  13748. (defun org-call-for-shift-select (cmd)
  13749. (let ((this-command-keys-shift-translated t))
  13750. (call-interactively cmd)))
  13751. (defun org-shifttab (&optional arg)
  13752. "Global visibility cycling or move to previous table field.
  13753. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13754. on context.
  13755. See the individual commands for more information."
  13756. (interactive "P")
  13757. (cond
  13758. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13759. ((integerp arg)
  13760. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  13761. (message "Content view to level: %d" arg)
  13762. (org-content (prefix-numeric-value arg2))
  13763. (setq org-cycle-global-status 'overview)))
  13764. (t (call-interactively 'org-global-cycle))))
  13765. (defun org-shiftmetaleft ()
  13766. "Promote subtree or delete table column.
  13767. Calls `org-promote-subtree', `org-outdent-item',
  13768. or `org-table-delete-column', depending on context.
  13769. See the individual commands for more information."
  13770. (interactive)
  13771. (cond
  13772. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13773. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13774. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13775. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13776. (t (org-modifier-cursor-error))))
  13777. (defun org-shiftmetaright ()
  13778. "Demote subtree or insert table column.
  13779. Calls `org-demote-subtree', `org-indent-item',
  13780. or `org-table-insert-column', depending on context.
  13781. See the individual commands for more information."
  13782. (interactive)
  13783. (cond
  13784. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13785. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13786. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13787. ((org-at-item-p) (call-interactively 'org-indent-item))
  13788. (t (org-modifier-cursor-error))))
  13789. (defun org-shiftmetaup (&optional arg)
  13790. "Move subtree up or kill table row.
  13791. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13792. `org-move-item-up' depending on context. See the individual commands
  13793. for more information."
  13794. (interactive "P")
  13795. (cond
  13796. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13797. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13798. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13799. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13800. (t (org-modifier-cursor-error))))
  13801. (defun org-shiftmetadown (&optional arg)
  13802. "Move subtree down or insert table row.
  13803. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13804. `org-move-item-down', depending on context. See the individual
  13805. commands for more information."
  13806. (interactive "P")
  13807. (cond
  13808. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13809. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13810. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13811. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13812. (t (org-modifier-cursor-error))))
  13813. (defun org-metaleft (&optional arg)
  13814. "Promote heading or move table column to left.
  13815. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13816. With no specific context, calls the Emacs default `backward-word'.
  13817. See the individual commands for more information."
  13818. (interactive "P")
  13819. (cond
  13820. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13821. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13822. ((or (org-on-heading-p)
  13823. (and (org-region-active-p)
  13824. (save-excursion
  13825. (goto-char (region-beginning))
  13826. (org-on-heading-p))))
  13827. (call-interactively 'org-do-promote))
  13828. ((or (org-at-item-p)
  13829. (and (org-region-active-p)
  13830. (save-excursion
  13831. (goto-char (region-beginning))
  13832. (org-at-item-p))))
  13833. (call-interactively 'org-outdent-item))
  13834. (t (call-interactively 'backward-word))))
  13835. (defun org-metaright (&optional arg)
  13836. "Demote subtree or move table column to right.
  13837. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13838. With no specific context, calls the Emacs default `forward-word'.
  13839. See the individual commands for more information."
  13840. (interactive "P")
  13841. (cond
  13842. ((run-hook-with-args-until-success 'org-metaright-hook))
  13843. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13844. ((or (org-on-heading-p)
  13845. (and (org-region-active-p)
  13846. (save-excursion
  13847. (goto-char (region-beginning))
  13848. (org-on-heading-p))))
  13849. (call-interactively 'org-do-demote))
  13850. ((or (org-at-item-p)
  13851. (and (org-region-active-p)
  13852. (save-excursion
  13853. (goto-char (region-beginning))
  13854. (org-at-item-p))))
  13855. (call-interactively 'org-indent-item))
  13856. (t (call-interactively 'forward-word))))
  13857. (defun org-metaup (&optional arg)
  13858. "Move subtree up or move table row up.
  13859. Calls `org-move-subtree-up' or `org-table-move-row' or
  13860. `org-move-item-up', depending on context. See the individual commands
  13861. for more information."
  13862. (interactive "P")
  13863. (cond
  13864. ((run-hook-with-args-until-success 'org-metaup-hook))
  13865. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13866. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13867. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13868. (t (transpose-lines 1) (beginning-of-line -1))))
  13869. (defun org-metadown (&optional arg)
  13870. "Move subtree down or move table row down.
  13871. Calls `org-move-subtree-down' or `org-table-move-row' or
  13872. `org-move-item-down', depending on context. See the individual
  13873. commands for more information."
  13874. (interactive "P")
  13875. (cond
  13876. ((run-hook-with-args-until-success 'org-metadown-hook))
  13877. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13878. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13879. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13880. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13881. (defun org-shiftup (&optional arg)
  13882. "Increase item in timestamp or increase priority of current headline.
  13883. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13884. depending on context. See the individual commands for more information."
  13885. (interactive "P")
  13886. (cond
  13887. ((and org-support-shift-select (org-region-active-p))
  13888. (org-call-for-shift-select 'previous-line))
  13889. ((org-at-timestamp-p t)
  13890. (call-interactively (if org-edit-timestamp-down-means-later
  13891. 'org-timestamp-down 'org-timestamp-up)))
  13892. ((and (not (eq org-support-shift-select 'always))
  13893. org-enable-priority-commands
  13894. (org-on-heading-p))
  13895. (call-interactively 'org-priority-up))
  13896. ((and (not org-support-shift-select) (org-at-item-p))
  13897. (call-interactively 'org-previous-item))
  13898. ((org-clocktable-try-shift 'up arg))
  13899. (org-support-shift-select
  13900. (org-call-for-shift-select 'previous-line))
  13901. (t (org-shiftselect-error))))
  13902. (defun org-shiftdown (&optional arg)
  13903. "Decrease item in timestamp or decrease priority of current headline.
  13904. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13905. depending on context. See the individual commands for more information."
  13906. (interactive "P")
  13907. (cond
  13908. ((and org-support-shift-select (org-region-active-p))
  13909. (org-call-for-shift-select 'next-line))
  13910. ((org-at-timestamp-p t)
  13911. (call-interactively (if org-edit-timestamp-down-means-later
  13912. 'org-timestamp-up 'org-timestamp-down)))
  13913. ((and (not (eq org-support-shift-select 'always))
  13914. org-enable-priority-commands
  13915. (org-on-heading-p))
  13916. (call-interactively 'org-priority-down))
  13917. ((and (not org-support-shift-select) (org-at-item-p))
  13918. (call-interactively 'org-next-item))
  13919. ((org-clocktable-try-shift 'down arg))
  13920. (org-support-shift-select
  13921. (org-call-for-shift-select 'next-line))
  13922. (t (org-shiftselect-error))))
  13923. (defun org-shiftright (&optional arg)
  13924. "Cycle the thing at point or in the current line, depending on context.
  13925. Depending on context, this does one of the following:
  13926. - switch a timestamp at point one day into the future
  13927. - on a headline, switch to the next TODO keyword.
  13928. - on an item, switch entire list to the next bullet type
  13929. - on a property line, switch to the next allowed value
  13930. - on a clocktable definition line, move time block into the future"
  13931. (interactive "P")
  13932. (cond
  13933. ((and org-support-shift-select (org-region-active-p))
  13934. (org-call-for-shift-select 'forward-char))
  13935. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13936. ((and (not (eq org-support-shift-select 'always))
  13937. (org-on-heading-p))
  13938. (let ((org-inhibit-logging
  13939. (not org-treat-S-cursor-todo-selection-as-state-change))
  13940. (org-inhibit-blocking
  13941. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13942. (org-call-with-arg 'org-todo 'right)))
  13943. ((or (and org-support-shift-select
  13944. (not (eq org-support-shift-select 'always))
  13945. (org-at-item-bullet-p))
  13946. (and (not org-support-shift-select) (org-at-item-p)))
  13947. (org-call-with-arg 'org-cycle-list-bullet nil))
  13948. ((and (not (eq org-support-shift-select 'always))
  13949. (org-at-property-p))
  13950. (call-interactively 'org-property-next-allowed-value))
  13951. ((org-clocktable-try-shift 'right arg))
  13952. (org-support-shift-select
  13953. (org-call-for-shift-select 'forward-char))
  13954. (t (org-shiftselect-error))))
  13955. (defun org-shiftleft (&optional arg)
  13956. "Cycle the thing at point or in the current line, depending on context.
  13957. Depending on context, this does one of the following:
  13958. - switch a timestamp at point one day into the past
  13959. - on a headline, switch to the previous TODO keyword.
  13960. - on an item, switch entire list to the previous bullet type
  13961. - on a property line, switch to the previous allowed value
  13962. - on a clocktable definition line, move time block into the past"
  13963. (interactive "P")
  13964. (cond
  13965. ((and org-support-shift-select (org-region-active-p))
  13966. (org-call-for-shift-select 'backward-char))
  13967. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13968. ((and (not (eq org-support-shift-select 'always))
  13969. (org-on-heading-p))
  13970. (let ((org-inhibit-logging
  13971. (not org-treat-S-cursor-todo-selection-as-state-change))
  13972. (org-inhibit-blocking
  13973. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13974. (org-call-with-arg 'org-todo 'left)))
  13975. ((or (and org-support-shift-select
  13976. (not (eq org-support-shift-select 'always))
  13977. (org-at-item-bullet-p))
  13978. (and (not org-support-shift-select) (org-at-item-p)))
  13979. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13980. ((and (not (eq org-support-shift-select 'always))
  13981. (org-at-property-p))
  13982. (call-interactively 'org-property-previous-allowed-value))
  13983. ((org-clocktable-try-shift 'left arg))
  13984. (org-support-shift-select
  13985. (org-call-for-shift-select 'backward-char))
  13986. (t (org-shiftselect-error))))
  13987. (defun org-shiftcontrolright ()
  13988. "Switch to next TODO set."
  13989. (interactive)
  13990. (cond
  13991. ((and org-support-shift-select (org-region-active-p))
  13992. (org-call-for-shift-select 'forward-word))
  13993. ((and (not (eq org-support-shift-select 'always))
  13994. (org-on-heading-p))
  13995. (org-call-with-arg 'org-todo 'nextset))
  13996. (org-support-shift-select
  13997. (org-call-for-shift-select 'forward-word))
  13998. (t (org-shiftselect-error))))
  13999. (defun org-shiftcontrolleft ()
  14000. "Switch to previous TODO set."
  14001. (interactive)
  14002. (cond
  14003. ((and org-support-shift-select (org-region-active-p))
  14004. (org-call-for-shift-select 'backward-word))
  14005. ((and (not (eq org-support-shift-select 'always))
  14006. (org-on-heading-p))
  14007. (org-call-with-arg 'org-todo 'previousset))
  14008. (org-support-shift-select
  14009. (org-call-for-shift-select 'backward-word))
  14010. (t (org-shiftselect-error))))
  14011. (defun org-ctrl-c-ret ()
  14012. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14013. (interactive)
  14014. (cond
  14015. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14016. (t (call-interactively 'org-insert-heading))))
  14017. (defun org-copy-special ()
  14018. "Copy region in table or copy current subtree.
  14019. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14020. See the individual commands for more information."
  14021. (interactive)
  14022. (call-interactively
  14023. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14024. (defun org-cut-special ()
  14025. "Cut region in table or cut current subtree.
  14026. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14027. See the individual commands for more information."
  14028. (interactive)
  14029. (call-interactively
  14030. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14031. (defun org-paste-special (arg)
  14032. "Paste rectangular region into table, or past subtree relative to level.
  14033. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14034. See the individual commands for more information."
  14035. (interactive "P")
  14036. (if (org-at-table-p)
  14037. (org-table-paste-rectangle)
  14038. (org-paste-subtree arg)))
  14039. (defun org-edit-special ()
  14040. "Call a special editor for the stuff at point.
  14041. When at a table, call the formula editor with `org-table-edit-formulas'.
  14042. When at the first line of an src example, call `org-edit-src-code'.
  14043. When in an #+include line, visit the include file. Otherwise call
  14044. `ffap' to visit the file at point."
  14045. (interactive)
  14046. (cond
  14047. ((org-at-table-p)
  14048. (call-interactively 'org-table-edit-formulas))
  14049. ((save-excursion
  14050. (beginning-of-line 1)
  14051. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14052. (find-file (org-trim (match-string 1))))
  14053. ((org-edit-src-code))
  14054. ((org-edit-fixed-width-region))
  14055. (t (call-interactively 'ffap))))
  14056. (defun org-ctrl-c-ctrl-c (&optional arg)
  14057. "Set tags in headline, or update according to changed information at point.
  14058. This command does many different things, depending on context:
  14059. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14060. this is what we do.
  14061. - If the cursor is on a statistics cookie, update it.
  14062. - If the cursor is in a headline, prompt for tags and insert them
  14063. into the current line, aligned to `org-tags-column'. When called
  14064. with prefix arg, realign all tags in the current buffer.
  14065. - If the cursor is in one of the special #+KEYWORD lines, this
  14066. triggers scanning the buffer for these lines and updating the
  14067. information.
  14068. - If the cursor is inside a table, realign the table. This command
  14069. works even if the automatic table editor has been turned off.
  14070. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14071. the entire table.
  14072. - If the cursor is at a footnote reference or definition, jump to
  14073. the corresponding definition or references, respectively.
  14074. - If the cursor is a the beginning of a dynamic block, update it.
  14075. - If the cursor is inside a table created by the table.el package,
  14076. activate that table.
  14077. - If the current buffer is a remember buffer, close note and file
  14078. it. A prefix argument of 1 files to the default location
  14079. without further interaction. A prefix argument of 2 files to
  14080. the currently clocking task.
  14081. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14082. links in this buffer.
  14083. - If the cursor is on a numbered item in a plain list, renumber the
  14084. ordered list.
  14085. - If the cursor is on a checkbox, toggle it."
  14086. (interactive "P")
  14087. (let ((org-enable-table-editor t))
  14088. (cond
  14089. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14090. org-occur-highlights
  14091. org-latex-fragment-image-overlays)
  14092. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14093. (org-remove-occur-highlights)
  14094. (org-remove-latex-fragment-image-overlays)
  14095. (message "Temporary highlights/overlays removed from current buffer"))
  14096. ((and (local-variable-p 'org-finish-function (current-buffer))
  14097. (fboundp org-finish-function))
  14098. (funcall org-finish-function))
  14099. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14100. ((org-at-property-p)
  14101. (call-interactively 'org-property-action))
  14102. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14103. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14104. (or (org-on-heading-p) (org-at-item-p)))
  14105. (call-interactively 'org-update-statistics-cookies))
  14106. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14107. ((org-at-table.el-p)
  14108. (require 'table)
  14109. (beginning-of-line 1)
  14110. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14111. (call-interactively 'table-recognize-table))
  14112. ((org-at-table-p)
  14113. (org-table-maybe-eval-formula)
  14114. (if arg
  14115. (call-interactively 'org-table-recalculate)
  14116. (org-table-maybe-recalculate-line))
  14117. (call-interactively 'org-table-align))
  14118. ((or (org-footnote-at-reference-p)
  14119. (org-footnote-at-definition-p))
  14120. (call-interactively 'org-footnote-action))
  14121. ((org-at-item-checkbox-p)
  14122. (call-interactively 'org-toggle-checkbox))
  14123. ((org-at-item-p)
  14124. (if arg
  14125. (call-interactively 'org-toggle-checkbox)
  14126. (call-interactively 'org-maybe-renumber-ordered-list)))
  14127. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14128. ;; Dynamic block
  14129. (beginning-of-line 1)
  14130. (save-excursion (org-update-dblock)))
  14131. ((save-excursion
  14132. (beginning-of-line 1)
  14133. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14134. (cond
  14135. ((equal (match-string 1) "TBLFM")
  14136. ;; Recalculate the table before this line
  14137. (save-excursion
  14138. (beginning-of-line 1)
  14139. (skip-chars-backward " \r\n\t")
  14140. (if (org-at-table-p)
  14141. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14142. (t
  14143. ; (org-set-regexps-and-options)
  14144. ; (org-restart-font-lock)
  14145. (let ((org-inhibit-startup t)) (org-mode-restart))
  14146. (message "Local setup has been refreshed"))))
  14147. ((org-clock-update-time-maybe))
  14148. (t (error "C-c C-c can do nothing useful at this location")))))
  14149. (defun org-mode-restart ()
  14150. "Restart Org-mode, to scan again for special lines.
  14151. Also updates the keyword regular expressions."
  14152. (interactive)
  14153. (org-mode)
  14154. (message "Org-mode restarted"))
  14155. (defun org-kill-note-or-show-branches ()
  14156. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14157. (interactive)
  14158. (if (not org-finish-function)
  14159. (call-interactively 'show-branches)
  14160. (let ((org-note-abort t))
  14161. (funcall org-finish-function))))
  14162. (defun org-return (&optional indent)
  14163. "Goto next table row or insert a newline.
  14164. Calls `org-table-next-row' or `newline', depending on context.
  14165. See the individual commands for more information."
  14166. (interactive)
  14167. (cond
  14168. ((bobp) (if indent (newline-and-indent) (newline)))
  14169. ((org-at-table-p)
  14170. (org-table-justify-field-maybe)
  14171. (call-interactively 'org-table-next-row))
  14172. ((and org-return-follows-link
  14173. (eq (get-text-property (point) 'face) 'org-link))
  14174. (call-interactively 'org-open-at-point))
  14175. ((and (org-at-heading-p)
  14176. (looking-at
  14177. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14178. (org-show-entry)
  14179. (end-of-line 1)
  14180. (newline))
  14181. (t (if indent (newline-and-indent) (newline)))))
  14182. (defun org-return-indent ()
  14183. "Goto next table row or insert a newline and indent.
  14184. Calls `org-table-next-row' or `newline-and-indent', depending on
  14185. context. See the individual commands for more information."
  14186. (interactive)
  14187. (org-return t))
  14188. (defun org-ctrl-c-star ()
  14189. "Compute table, or change heading status of lines.
  14190. Calls `org-table-recalculate' or `org-toggle-heading',
  14191. depending on context."
  14192. (interactive)
  14193. (cond
  14194. ((org-at-table-p)
  14195. (call-interactively 'org-table-recalculate))
  14196. (t
  14197. ;; Convert all lines in region to list items
  14198. (call-interactively 'org-toggle-heading))))
  14199. (defun org-ctrl-c-minus ()
  14200. "Insert separator line in table or modify bullet status of line.
  14201. Also turns a plain line or a region of lines into list items.
  14202. Calls `org-table-insert-hline', `org-toggle-item', or
  14203. `org-cycle-list-bullet', depending on context."
  14204. (interactive)
  14205. (cond
  14206. ((org-at-table-p)
  14207. (call-interactively 'org-table-insert-hline))
  14208. ((org-region-active-p)
  14209. (call-interactively 'org-toggle-item))
  14210. ((org-in-item-p)
  14211. (call-interactively 'org-cycle-list-bullet))
  14212. (t
  14213. (call-interactively 'org-toggle-item))))
  14214. (defun org-toggle-item ()
  14215. "Convert headings or normal lines to items, items to normal lines.
  14216. If there is no active region, only the current line is considered.
  14217. If the first line in the region is a headline, convert all headlines to items.
  14218. If the first line in the region is an item, convert all items to normal lines.
  14219. If the first line is normal text, add an item bullet to each line."
  14220. (interactive)
  14221. (let (l2 l beg end)
  14222. (if (org-region-active-p)
  14223. (setq beg (region-beginning) end (region-end))
  14224. (setq beg (point-at-bol)
  14225. end (min (1+ (point-at-eol)) (point-max))))
  14226. (save-excursion
  14227. (goto-char end)
  14228. (setq l2 (org-current-line))
  14229. (goto-char beg)
  14230. (beginning-of-line 1)
  14231. (setq l (1- (org-current-line)))
  14232. (if (org-at-item-p)
  14233. ;; We already have items, de-itemize
  14234. (while (< (setq l (1+ l)) l2)
  14235. (when (org-at-item-p)
  14236. (goto-char (match-beginning 2))
  14237. (delete-region (match-beginning 2) (match-end 2))
  14238. (and (looking-at "[ \t]+") (replace-match "")))
  14239. (beginning-of-line 2))
  14240. (if (org-on-heading-p)
  14241. ;; Headings, convert to items
  14242. (while (< (setq l (1+ l)) l2)
  14243. (if (looking-at org-outline-regexp)
  14244. (replace-match "- " t t))
  14245. (beginning-of-line 2))
  14246. ;; normal lines, turn them into items
  14247. (while (< (setq l (1+ l)) l2)
  14248. (unless (org-at-item-p)
  14249. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14250. (replace-match "\\1- \\2")))
  14251. (beginning-of-line 2)))))))
  14252. (defun org-toggle-heading (&optional nstars)
  14253. "Convert headings to normal text, or items or text to headings.
  14254. If there is no active region, only the current line is considered.
  14255. If the first line is a heading, remove the stars from all headlines
  14256. in the region.
  14257. If the first line is a plain list item, turn all plain list items
  14258. into headings.
  14259. If the first line is a normal line, turn each and every line in the
  14260. region into a heading.
  14261. When converting a line into a heading, the number of stars is chosen
  14262. such that the lines become children of the current entry. However,
  14263. when a prefix argument is given, its value determines the number of
  14264. stars to add."
  14265. (interactive "P")
  14266. (let (l2 l itemp beg end)
  14267. (if (org-region-active-p)
  14268. (setq beg (region-beginning) end (region-end))
  14269. (setq beg (point-at-bol)
  14270. end (min (1+ (point-at-eol)) (point-max))))
  14271. (save-excursion
  14272. (goto-char end)
  14273. (setq l2 (org-current-line))
  14274. (goto-char beg)
  14275. (beginning-of-line 1)
  14276. (setq l (1- (org-current-line)))
  14277. (if (org-on-heading-p)
  14278. ;; We already have headlines, de-star them
  14279. (while (< (setq l (1+ l)) l2)
  14280. (when (org-on-heading-p t)
  14281. (and (looking-at outline-regexp) (replace-match "")))
  14282. (beginning-of-line 2))
  14283. (setq itemp (org-at-item-p))
  14284. (let* ((stars
  14285. (if nstars
  14286. (make-string (prefix-numeric-value current-prefix-arg)
  14287. ?*)
  14288. (save-excursion
  14289. (if (re-search-backward org-complex-heading-regexp nil t)
  14290. (match-string 1) ""))))
  14291. (add-stars (cond (nstars "")
  14292. ((equal stars "") "*")
  14293. (org-odd-levels-only "**")
  14294. (t "*")))
  14295. (rpl (concat stars add-stars " ")))
  14296. (while (< (setq l (1+ l)) l2)
  14297. (if itemp
  14298. (and (org-at-item-p) (replace-match rpl t t))
  14299. (unless (org-on-heading-p)
  14300. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14301. (replace-match (concat rpl (match-string 2))))))
  14302. (beginning-of-line 2)))))))
  14303. (defun org-meta-return (&optional arg)
  14304. "Insert a new heading or wrap a region in a table.
  14305. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14306. See the individual commands for more information."
  14307. (interactive "P")
  14308. (cond
  14309. ((run-hook-with-args-until-success 'org-metareturn-hook))
  14310. ((org-at-table-p)
  14311. (call-interactively 'org-table-wrap-region))
  14312. (t (call-interactively 'org-insert-heading))))
  14313. ;;; Menu entries
  14314. ;; Define the Org-mode menus
  14315. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14316. '("Tbl"
  14317. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  14318. ["Next Field" org-cycle (org-at-table-p)]
  14319. ["Previous Field" org-shifttab (org-at-table-p)]
  14320. ["Next Row" org-return (org-at-table-p)]
  14321. "--"
  14322. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14323. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14324. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14325. "--"
  14326. ("Column"
  14327. ["Move Column Left" org-metaleft (org-at-table-p)]
  14328. ["Move Column Right" org-metaright (org-at-table-p)]
  14329. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14330. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  14331. ("Row"
  14332. ["Move Row Up" org-metaup (org-at-table-p)]
  14333. ["Move Row Down" org-metadown (org-at-table-p)]
  14334. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14335. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14336. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14337. "--"
  14338. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  14339. ("Rectangle"
  14340. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14341. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14342. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14343. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14344. "--"
  14345. ("Calculate"
  14346. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14347. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14348. ["Edit Formulas" org-edit-special (org-at-table-p)]
  14349. "--"
  14350. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14351. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14352. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  14353. "--"
  14354. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14355. "--"
  14356. ["Sum Column/Rectangle" org-table-sum
  14357. (or (org-at-table-p) (org-region-active-p))]
  14358. ["Which Column?" org-table-current-column (org-at-table-p)])
  14359. ["Debug Formulas"
  14360. org-table-toggle-formula-debugger
  14361. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  14362. ["Show Col/Row Numbers"
  14363. org-table-toggle-coordinate-overlays
  14364. :style toggle
  14365. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14366. "--"
  14367. ["Create" org-table-create (and (not (org-at-table-p))
  14368. org-enable-table-editor)]
  14369. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14370. ["Import from File" org-table-import (not (org-at-table-p))]
  14371. ["Export to File" org-table-export (org-at-table-p)]
  14372. "--"
  14373. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14374. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14375. '("Org"
  14376. ("Show/Hide"
  14377. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14378. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14379. ["Sparse Tree..." org-sparse-tree t]
  14380. ["Reveal Context" org-reveal t]
  14381. ["Show All" show-all t]
  14382. "--"
  14383. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14384. "--"
  14385. ["New Heading" org-insert-heading t]
  14386. ("Navigate Headings"
  14387. ["Up" outline-up-heading t]
  14388. ["Next" outline-next-visible-heading t]
  14389. ["Previous" outline-previous-visible-heading t]
  14390. ["Next Same Level" outline-forward-same-level t]
  14391. ["Previous Same Level" outline-backward-same-level t]
  14392. "--"
  14393. ["Jump" org-goto t])
  14394. ("Edit Structure"
  14395. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14396. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14397. "--"
  14398. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14399. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14400. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14401. "--"
  14402. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14403. "--"
  14404. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14405. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14406. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14407. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14408. "--"
  14409. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14410. "--"
  14411. ["Convert to odd levels" org-convert-to-odd-levels t]
  14412. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14413. ("Editing"
  14414. ["Emphasis..." org-emphasize t]
  14415. ["Edit Source Example" org-edit-special t]
  14416. "--"
  14417. ["Footnote new/jump" org-footnote-action t]
  14418. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14419. ("Archive"
  14420. ["Archive (default method)" org-archive-subtree-default t]
  14421. "--"
  14422. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  14423. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14424. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  14425. )
  14426. "--"
  14427. ("Hyperlinks"
  14428. ["Store Link (Global)" org-store-link t]
  14429. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14430. ["Insert Link" org-insert-link t]
  14431. ["Follow Link" org-open-at-point t]
  14432. "--"
  14433. ["Next link" org-next-link t]
  14434. ["Previous link" org-previous-link t]
  14435. "--"
  14436. ["Descriptive Links"
  14437. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14438. :style radio
  14439. :selected (member '(org-link) buffer-invisibility-spec)]
  14440. ["Literal Links"
  14441. (progn
  14442. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14443. :style radio
  14444. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14445. "--"
  14446. ("TODO Lists"
  14447. ["TODO/DONE/-" org-todo t]
  14448. ("Select keyword"
  14449. ["Next keyword" org-shiftright (org-on-heading-p)]
  14450. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14451. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14452. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14453. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14454. ["Show TODO Tree" org-show-todo-tree t]
  14455. ["Global TODO list" org-todo-list t]
  14456. "--"
  14457. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14458. :selected org-enforce-todo-dependencies :style toggle :active t]
  14459. "Settings for tree at point"
  14460. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14461. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14462. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14463. ["Do Children parallel" org-toggle-ordered-property :style radio
  14464. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14465. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14466. "--"
  14467. ["Set Priority" org-priority t]
  14468. ["Priority Up" org-shiftup t]
  14469. ["Priority Down" org-shiftdown t]
  14470. "--"
  14471. ["Get news from all feeds" org-feed-update-all t]
  14472. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14473. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14474. ("TAGS and Properties"
  14475. ["Set Tags" org-set-tags-command t]
  14476. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14477. "--"
  14478. ["Set property" org-set-property t]
  14479. ["Column view of properties" org-columns t]
  14480. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14481. ("Dates and Scheduling"
  14482. ["Timestamp" org-time-stamp t]
  14483. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14484. ("Change Date"
  14485. ["1 Day Later" org-shiftright t]
  14486. ["1 Day Earlier" org-shiftleft t]
  14487. ["1 ... Later" org-shiftup t]
  14488. ["1 ... Earlier" org-shiftdown t])
  14489. ["Compute Time Range" org-evaluate-time-range t]
  14490. ["Schedule Item" org-schedule t]
  14491. ["Deadline" org-deadline t]
  14492. "--"
  14493. ["Custom time format" org-toggle-time-stamp-overlays
  14494. :style radio :selected org-display-custom-times]
  14495. "--"
  14496. ["Goto Calendar" org-goto-calendar t]
  14497. ["Date from Calendar" org-date-from-calendar t]
  14498. "--"
  14499. ["Start/Restart Timer" org-timer-start t]
  14500. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14501. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14502. ["Insert Timer String" org-timer t]
  14503. ["Insert Timer Item" org-timer-item t])
  14504. ("Logging work"
  14505. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14506. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14507. ["Clock out" org-clock-out t]
  14508. ["Clock cancel" org-clock-cancel t]
  14509. "--"
  14510. ["Mark as default task" org-clock-mark-default-task t]
  14511. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14512. ["Goto running clock" org-clock-goto t]
  14513. "--"
  14514. ["Display times" org-clock-display t]
  14515. ["Create clock table" org-clock-report t]
  14516. "--"
  14517. ["Record DONE time"
  14518. (progn (setq org-log-done (not org-log-done))
  14519. (message "Switching to %s will %s record a timestamp"
  14520. (car org-done-keywords)
  14521. (if org-log-done "automatically" "not")))
  14522. :style toggle :selected org-log-done])
  14523. "--"
  14524. ["Agenda Command..." org-agenda t]
  14525. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14526. ("File List for Agenda")
  14527. ("Special views current file"
  14528. ["TODO Tree" org-show-todo-tree t]
  14529. ["Check Deadlines" org-check-deadlines t]
  14530. ["Timeline" org-timeline t]
  14531. ["Tags/Property tree" org-match-sparse-tree t])
  14532. "--"
  14533. ["Export/Publish..." org-export t]
  14534. ("LaTeX"
  14535. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14536. :selected org-cdlatex-mode]
  14537. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14538. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14539. ["Modify math symbol" org-cdlatex-math-modify
  14540. (org-inside-LaTeX-fragment-p)]
  14541. ["Insert citation" org-reftex-citation t]
  14542. "--"
  14543. ["Export LaTeX fragments as images"
  14544. (if (featurep 'org-exp)
  14545. (setq org-export-with-LaTeX-fragments
  14546. (not org-export-with-LaTeX-fragments))
  14547. (require 'org-exp))
  14548. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14549. org-export-with-LaTeX-fragments)])
  14550. "--"
  14551. ("MobileOrg"
  14552. ["Push Files and Views" org-mobile-push t]
  14553. ["Get Captured and Flagged" org-mobile-pull t]
  14554. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  14555. "--"
  14556. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  14557. "--"
  14558. ("Documentation"
  14559. ["Show Version" org-version t]
  14560. ["Info Documentation" org-info t])
  14561. ("Customize"
  14562. ["Browse Org Group" org-customize t]
  14563. "--"
  14564. ["Expand This Menu" org-create-customize-menu
  14565. (fboundp 'customize-menu-create)])
  14566. ["Send bug report" org-submit-bug-report t]
  14567. "--"
  14568. ("Refresh/Reload"
  14569. ["Refresh setup current buffer" org-mode-restart t]
  14570. ["Reload Org (after update)" org-reload t]
  14571. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14572. ))
  14573. (defun org-info (&optional node)
  14574. "Read documentation for Org-mode in the info system.
  14575. With optional NODE, go directly to that node."
  14576. (interactive)
  14577. (info (format "(org)%s" (or node ""))))
  14578. ;;;###autoload
  14579. (defun org-submit-bug-report ()
  14580. "Submit a bug report on Org-mode via mail.
  14581. Don't hesitate to report any problems or inaccurate documentation.
  14582. If you don't have setup sending mail from (X)Emacs, please copy the
  14583. output buffer into your mail program, as it gives us important
  14584. information about your Org-mode version and configuration."
  14585. (interactive)
  14586. (require 'reporter)
  14587. (org-load-modules-maybe)
  14588. (org-require-autoloaded-modules)
  14589. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14590. (reporter-submit-bug-report
  14591. "emacs-orgmode@gnu.org"
  14592. (org-version)
  14593. (let (list)
  14594. (save-window-excursion
  14595. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14596. (delete-other-windows)
  14597. (erase-buffer)
  14598. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14599. We would like to add your full Org-mode and Outline configuration to the
  14600. bug report. This greatly simplifies the work of the maintainer and
  14601. other experts on the mailing list.
  14602. HOWEVER, some variables you have customized may contain private
  14603. information. The names of customers, colleagues, or friends, might
  14604. appear in the form of file names, tags, todo states, or search strings.
  14605. If you answer yes to the prompt, you might want to check and remove
  14606. such private information before sending the email.")
  14607. (add-text-properties (point-min) (point-max) '(face org-warning))
  14608. (when (yes-or-no-p "Include your Org-mode configuration ")
  14609. (mapatoms
  14610. (lambda (v)
  14611. (and (boundp v)
  14612. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14613. (or (and (symbol-value v)
  14614. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14615. (and
  14616. (get v 'custom-type) (get v 'standard-value)
  14617. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14618. (push v list)))))
  14619. (kill-buffer (get-buffer "*Warn about privacy*"))
  14620. list))
  14621. nil nil
  14622. "Remember to cover the basics, that is, what you expected to happen and
  14623. what in fact did happen. You don't know how to make a good report? See
  14624. http://orgmode.org/manual/Feedback.html#Feedback
  14625. Your bug report will be posted to the Org-mode mailing list.
  14626. ------------------------------------------------------------------------")))
  14627. (defun org-install-agenda-files-menu ()
  14628. (let ((bl (buffer-list)))
  14629. (save-excursion
  14630. (while bl
  14631. (set-buffer (pop bl))
  14632. (if (org-mode-p) (setq bl nil)))
  14633. (when (org-mode-p)
  14634. (easy-menu-change
  14635. '("Org") "File List for Agenda"
  14636. (append
  14637. (list
  14638. ["Edit File List" (org-edit-agenda-file-list) t]
  14639. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14640. ["Remove Current File from List" org-remove-file t]
  14641. ["Cycle through agenda files" org-cycle-agenda-files t]
  14642. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14643. "--")
  14644. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14645. ;;;; Documentation
  14646. ;;;###autoload
  14647. (defun org-require-autoloaded-modules ()
  14648. (interactive)
  14649. (mapc 'require
  14650. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14651. org-docbook org-exp org-html org-icalendar
  14652. org-id org-latex
  14653. org-publish org-remember org-table
  14654. org-timer org-xoxo)))
  14655. ;;;###autoload
  14656. (defun org-reload (&optional uncompiled)
  14657. "Reload all org lisp files.
  14658. With prefix arg UNCOMPILED, load the uncompiled versions."
  14659. (interactive "P")
  14660. (require 'find-func)
  14661. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14662. (dir-org (file-name-directory (org-find-library-name "org")))
  14663. (dir-org-contrib (ignore-errors
  14664. (file-name-directory
  14665. (org-find-library-name "org-contribdir"))))
  14666. (files
  14667. (append (directory-files dir-org t file-re)
  14668. (and dir-org-contrib
  14669. (directory-files dir-org-contrib t file-re))))
  14670. (remove-re (concat (if (featurep 'xemacs)
  14671. "org-colview" "org-colview-xemacs")
  14672. "\\'")))
  14673. (setq files (mapcar 'file-name-sans-extension files))
  14674. (setq files (mapcar
  14675. (lambda (x) (if (string-match remove-re x) nil x))
  14676. files))
  14677. (setq files (delq nil files))
  14678. (mapc
  14679. (lambda (f)
  14680. (when (featurep (intern (file-name-nondirectory f)))
  14681. (if (and (not uncompiled)
  14682. (file-exists-p (concat f ".elc")))
  14683. (load (concat f ".elc") nil nil t)
  14684. (load (concat f ".el") nil nil t))))
  14685. files))
  14686. (org-version))
  14687. ;;;###autoload
  14688. (defun org-customize ()
  14689. "Call the customize function with org as argument."
  14690. (interactive)
  14691. (org-load-modules-maybe)
  14692. (org-require-autoloaded-modules)
  14693. (customize-browse 'org))
  14694. (defun org-create-customize-menu ()
  14695. "Create a full customization menu for Org-mode, insert it into the menu."
  14696. (interactive)
  14697. (org-load-modules-maybe)
  14698. (org-require-autoloaded-modules)
  14699. (if (fboundp 'customize-menu-create)
  14700. (progn
  14701. (easy-menu-change
  14702. '("Org") "Customize"
  14703. `(["Browse Org group" org-customize t]
  14704. "--"
  14705. ,(customize-menu-create 'org)
  14706. ["Set" Custom-set t]
  14707. ["Save" Custom-save t]
  14708. ["Reset to Current" Custom-reset-current t]
  14709. ["Reset to Saved" Custom-reset-saved t]
  14710. ["Reset to Standard Settings" Custom-reset-standard t]))
  14711. (message "\"Org\"-menu now contains full customization menu"))
  14712. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14713. ;;;; Miscellaneous stuff
  14714. ;;; Generally useful functions
  14715. (defun org-get-at-bol (property)
  14716. "Get text property PROPERTY at beginning of line."
  14717. (get-text-property (point-at-bol) property))
  14718. (defun org-find-text-property-in-string (prop s)
  14719. "Return the first non-nil value of property PROP in string S."
  14720. (or (get-text-property 0 prop s)
  14721. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14722. prop s)))
  14723. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14724. "Display the given MESSAGE as a warning."
  14725. (if (fboundp 'display-warning)
  14726. (display-warning 'org message
  14727. (if (featurep 'xemacs)
  14728. 'warning
  14729. :warning))
  14730. (let ((buf (get-buffer-create "*Org warnings*")))
  14731. (with-current-buffer buf
  14732. (goto-char (point-max))
  14733. (insert "Warning (Org): " message)
  14734. (unless (bolp)
  14735. (newline)))
  14736. (display-buffer buf)
  14737. (sit-for 0))))
  14738. (defun org-in-commented-line ()
  14739. "Is point in a line starting with `#'?"
  14740. (equal (char-after (point-at-bol)) ?#))
  14741. (defun org-in-verbatim-emphasis ()
  14742. (save-match-data
  14743. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  14744. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14745. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14746. (if (and marker (marker-buffer marker)
  14747. (buffer-live-p (marker-buffer marker)))
  14748. (progn
  14749. (switch-to-buffer (marker-buffer marker))
  14750. (if (or (> marker (point-max)) (< marker (point-min)))
  14751. (widen))
  14752. (goto-char marker)
  14753. (org-show-context 'org-goto))
  14754. (if bookmark
  14755. (bookmark-jump bookmark)
  14756. (error "Cannot find location"))))
  14757. (defun org-quote-csv-field (s)
  14758. "Quote field for inclusion in CSV material."
  14759. (if (string-match "[\",]" s)
  14760. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14761. s))
  14762. (defun org-plist-delete (plist property)
  14763. "Delete PROPERTY from PLIST.
  14764. This is in contrast to merely setting it to 0."
  14765. (let (p)
  14766. (while plist
  14767. (if (not (eq property (car plist)))
  14768. (setq p (plist-put p (car plist) (nth 1 plist))))
  14769. (setq plist (cddr plist)))
  14770. p))
  14771. (defun org-force-self-insert (N)
  14772. "Needed to enforce self-insert under remapping."
  14773. (interactive "p")
  14774. (self-insert-command N))
  14775. (defun org-string-width (s)
  14776. "Compute width of string, ignoring invisible characters.
  14777. This ignores character with invisibility property `org-link', and also
  14778. characters with property `org-cwidth', because these will become invisible
  14779. upon the next fontification round."
  14780. (let (b l)
  14781. (when (or (eq t buffer-invisibility-spec)
  14782. (assq 'org-link buffer-invisibility-spec))
  14783. (while (setq b (text-property-any 0 (length s)
  14784. 'invisible 'org-link s))
  14785. (setq s (concat (substring s 0 b)
  14786. (substring s (or (next-single-property-change
  14787. b 'invisible s) (length s)))))))
  14788. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14789. (setq s (concat (substring s 0 b)
  14790. (substring s (or (next-single-property-change
  14791. b 'org-cwidth s) (length s))))))
  14792. (setq l (string-width s) b -1)
  14793. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14794. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14795. l))
  14796. (defun org-get-indentation (&optional line)
  14797. "Get the indentation of the current line, interpreting tabs.
  14798. When LINE is given, assume it represents a line and compute its indentation."
  14799. (if line
  14800. (if (string-match "^ *" (org-remove-tabs line))
  14801. (match-end 0))
  14802. (save-excursion
  14803. (beginning-of-line 1)
  14804. (skip-chars-forward " \t")
  14805. (current-column))))
  14806. (defun org-remove-tabs (s &optional width)
  14807. "Replace tabulators in S with spaces.
  14808. Assumes that s is a single line, starting in column 0."
  14809. (setq width (or width tab-width))
  14810. (while (string-match "\t" s)
  14811. (setq s (replace-match
  14812. (make-string
  14813. (- (* width (/ (+ (match-beginning 0) width) width))
  14814. (match-beginning 0)) ?\ )
  14815. t t s)))
  14816. s)
  14817. (defun org-fix-indentation (line ind)
  14818. "Fix indentation in LINE.
  14819. IND is a cons cell with target and minimum indentation.
  14820. If the current indentation in LINE is smaller than the minimum,
  14821. leave it alone. If it is larger than ind, set it to the target."
  14822. (let* ((l (org-remove-tabs line))
  14823. (i (org-get-indentation l))
  14824. (i1 (car ind)) (i2 (cdr ind)))
  14825. (if (>= i i2) (setq l (substring line i2)))
  14826. (if (> i1 0)
  14827. (concat (make-string i1 ?\ ) l)
  14828. l)))
  14829. (defun org-remove-indentation (code &optional n)
  14830. "Remove the maximum common indentation from the lines in CODE.
  14831. N may optionally be the number of spaces to remove."
  14832. (with-temp-buffer
  14833. (insert code)
  14834. (org-do-remove-indentation n)
  14835. (buffer-string)))
  14836. (defun org-do-remove-indentation (&optional n)
  14837. "Remove the maximum common indentation from the buffer."
  14838. (untabify (point-min) (point-max))
  14839. (let ((min 10000) re)
  14840. (if n
  14841. (setq min n)
  14842. (goto-char (point-min))
  14843. (while (re-search-forward "^ *[^ \n]" nil t)
  14844. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14845. (unless (or (= min 0) (= min 10000))
  14846. (setq re (format "^ \\{%d\\}" min))
  14847. (goto-char (point-min))
  14848. (while (re-search-forward re nil t)
  14849. (replace-match "")
  14850. (end-of-line 1))
  14851. min)))
  14852. (defun org-fill-template (template alist)
  14853. "Find each %key of ALIST in TEMPLATE and replace it."
  14854. (let (entry key value)
  14855. (setq alist (sort (copy-sequence alist)
  14856. (lambda (a b) (< (length (car a)) (length (car b))))))
  14857. (while (setq entry (pop alist))
  14858. (setq template
  14859. (replace-regexp-in-string
  14860. (concat "%" (regexp-quote (car entry)))
  14861. (cdr entry) template t t)))
  14862. template))
  14863. (defun org-base-buffer (buffer)
  14864. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14865. (if (not buffer)
  14866. buffer
  14867. (or (buffer-base-buffer buffer)
  14868. buffer)))
  14869. (defun org-trim (s)
  14870. "Remove whitespace at beginning and end of string."
  14871. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14872. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14873. s)
  14874. (defun org-wrap (string &optional width lines)
  14875. "Wrap string to either a number of lines, or a width in characters.
  14876. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14877. that costs. If there is a word longer than WIDTH, the text is actually
  14878. wrapped to the length of that word.
  14879. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14880. many lines, whatever width that takes.
  14881. The return value is a list of lines, without newlines at the end."
  14882. (let* ((words (org-split-string string "[ \t\n]+"))
  14883. (maxword (apply 'max (mapcar 'org-string-width words)))
  14884. w ll)
  14885. (cond (width
  14886. (org-do-wrap words (max maxword width)))
  14887. (lines
  14888. (setq w maxword)
  14889. (setq ll (org-do-wrap words maxword))
  14890. (if (<= (length ll) lines)
  14891. ll
  14892. (setq ll words)
  14893. (while (> (length ll) lines)
  14894. (setq w (1+ w))
  14895. (setq ll (org-do-wrap words w)))
  14896. ll))
  14897. (t (error "Cannot wrap this")))))
  14898. (defun org-do-wrap (words width)
  14899. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14900. (let (lines line)
  14901. (while words
  14902. (setq line (pop words))
  14903. (while (and words (< (+ (length line) (length (car words))) width))
  14904. (setq line (concat line " " (pop words))))
  14905. (setq lines (push line lines)))
  14906. (nreverse lines)))
  14907. (defun org-split-string (string &optional separators)
  14908. "Splits STRING into substrings at SEPARATORS.
  14909. No empty strings are returned if there are matches at the beginning
  14910. and end of string."
  14911. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14912. (start 0)
  14913. notfirst
  14914. (list nil))
  14915. (while (and (string-match rexp string
  14916. (if (and notfirst
  14917. (= start (match-beginning 0))
  14918. (< start (length string)))
  14919. (1+ start) start))
  14920. (< (match-beginning 0) (length string)))
  14921. (setq notfirst t)
  14922. (or (eq (match-beginning 0) 0)
  14923. (and (eq (match-beginning 0) (match-end 0))
  14924. (eq (match-beginning 0) start))
  14925. (setq list
  14926. (cons (substring string start (match-beginning 0))
  14927. list)))
  14928. (setq start (match-end 0)))
  14929. (or (eq start (length string))
  14930. (setq list
  14931. (cons (substring string start)
  14932. list)))
  14933. (nreverse list)))
  14934. (defun org-quote-vert (s)
  14935. "Replace \"|\" with \"\\vert\"."
  14936. (while (string-match "|" s)
  14937. (setq s (replace-match "\\vert" t t s)))
  14938. s)
  14939. (defun org-uuidgen-p (s)
  14940. "Is S an ID created by UUIDGEN?"
  14941. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14942. (defun org-context ()
  14943. "Return a list of contexts of the current cursor position.
  14944. If several contexts apply, all are returned.
  14945. Each context entry is a list with a symbol naming the context, and
  14946. two positions indicating start and end of the context. Possible
  14947. contexts are:
  14948. :headline anywhere in a headline
  14949. :headline-stars on the leading stars in a headline
  14950. :todo-keyword on a TODO keyword (including DONE) in a headline
  14951. :tags on the TAGS in a headline
  14952. :priority on the priority cookie in a headline
  14953. :item on the first line of a plain list item
  14954. :item-bullet on the bullet/number of a plain list item
  14955. :checkbox on the checkbox in a plain list item
  14956. :table in an org-mode table
  14957. :table-special on a special filed in a table
  14958. :table-table in a table.el table
  14959. :link on a hyperlink
  14960. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14961. :target on a <<target>>
  14962. :radio-target on a <<<radio-target>>>
  14963. :latex-fragment on a LaTeX fragment
  14964. :latex-preview on a LaTeX fragment with overlayed preview image
  14965. This function expects the position to be visible because it uses font-lock
  14966. faces as a help to recognize the following contexts: :table-special, :link,
  14967. and :keyword."
  14968. (let* ((f (get-text-property (point) 'face))
  14969. (faces (if (listp f) f (list f)))
  14970. (p (point)) clist o)
  14971. ;; First the large context
  14972. (cond
  14973. ((org-on-heading-p t)
  14974. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14975. (when (progn
  14976. (beginning-of-line 1)
  14977. (looking-at org-todo-line-tags-regexp))
  14978. (push (org-point-in-group p 1 :headline-stars) clist)
  14979. (push (org-point-in-group p 2 :todo-keyword) clist)
  14980. (push (org-point-in-group p 4 :tags) clist))
  14981. (goto-char p)
  14982. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  14983. (if (looking-at "\\[#[A-Z0-9]\\]")
  14984. (push (org-point-in-group p 0 :priority) clist)))
  14985. ((org-at-item-p)
  14986. (push (org-point-in-group p 2 :item-bullet) clist)
  14987. (push (list :item (point-at-bol)
  14988. (save-excursion (org-end-of-item) (point)))
  14989. clist)
  14990. (and (org-at-item-checkbox-p)
  14991. (push (org-point-in-group p 0 :checkbox) clist)))
  14992. ((org-at-table-p)
  14993. (push (list :table (org-table-begin) (org-table-end)) clist)
  14994. (if (memq 'org-formula faces)
  14995. (push (list :table-special
  14996. (previous-single-property-change p 'face)
  14997. (next-single-property-change p 'face)) clist)))
  14998. ((org-at-table-p 'any)
  14999. (push (list :table-table) clist)))
  15000. (goto-char p)
  15001. ;; Now the small context
  15002. (cond
  15003. ((org-at-timestamp-p)
  15004. (push (org-point-in-group p 0 :timestamp) clist))
  15005. ((memq 'org-link faces)
  15006. (push (list :link
  15007. (previous-single-property-change p 'face)
  15008. (next-single-property-change p 'face)) clist))
  15009. ((memq 'org-special-keyword faces)
  15010. (push (list :keyword
  15011. (previous-single-property-change p 'face)
  15012. (next-single-property-change p 'face)) clist))
  15013. ((org-on-target-p)
  15014. (push (org-point-in-group p 0 :target) clist)
  15015. (goto-char (1- (match-beginning 0)))
  15016. (if (looking-at org-radio-target-regexp)
  15017. (push (org-point-in-group p 0 :radio-target) clist))
  15018. (goto-char p))
  15019. ((setq o (car (delq nil
  15020. (mapcar
  15021. (lambda (x)
  15022. (if (memq x org-latex-fragment-image-overlays) x))
  15023. (org-overlays-at (point))))))
  15024. (push (list :latex-fragment
  15025. (org-overlay-start o) (org-overlay-end o)) clist)
  15026. (push (list :latex-preview
  15027. (org-overlay-start o) (org-overlay-end o)) clist))
  15028. ((org-inside-LaTeX-fragment-p)
  15029. ;; FIXME: positions wrong.
  15030. (push (list :latex-fragment (point) (point)) clist)))
  15031. (setq clist (nreverse (delq nil clist)))
  15032. clist))
  15033. ;; FIXME: Compare with at-regexp-p Do we need both?
  15034. (defun org-in-regexp (re &optional nlines visually)
  15035. "Check if point is inside a match of regexp.
  15036. Normally only the current line is checked, but you can include NLINES extra
  15037. lines both before and after point into the search.
  15038. If VISUALLY is set, require that the cursor is not after the match but
  15039. really on, so that the block visually is on the match."
  15040. (catch 'exit
  15041. (let ((pos (point))
  15042. (eol (point-at-eol (+ 1 (or nlines 0))))
  15043. (inc (if visually 1 0)))
  15044. (save-excursion
  15045. (beginning-of-line (- 1 (or nlines 0)))
  15046. (while (re-search-forward re eol t)
  15047. (if (and (<= (match-beginning 0) pos)
  15048. (>= (+ inc (match-end 0)) pos))
  15049. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15050. (defun org-at-regexp-p (regexp)
  15051. "Is point inside a match of REGEXP in the current line?"
  15052. (catch 'exit
  15053. (save-excursion
  15054. (let ((pos (point)) (end (point-at-eol)))
  15055. (beginning-of-line 1)
  15056. (while (re-search-forward regexp end t)
  15057. (if (and (<= (match-beginning 0) pos)
  15058. (>= (match-end 0) pos))
  15059. (throw 'exit t)))
  15060. nil))))
  15061. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15062. "Call `multi-occur' with buffers for all agenda files."
  15063. (interactive "sOrg-files matching: \np")
  15064. (let* ((files (org-agenda-files))
  15065. (tnames (mapcar 'file-truename files))
  15066. (extra org-agenda-text-search-extra-files)
  15067. f)
  15068. (when (eq (car extra) 'agenda-archives)
  15069. (setq extra (cdr extra))
  15070. (setq files (org-add-archive-files files)))
  15071. (while (setq f (pop extra))
  15072. (unless (member (file-truename f) tnames)
  15073. (add-to-list 'files f 'append)
  15074. (add-to-list 'tnames (file-truename f) 'append)))
  15075. (multi-occur
  15076. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  15077. regexp)))
  15078. (if (boundp 'occur-mode-find-occurrence-hook)
  15079. ;; Emacs 23
  15080. (add-hook 'occur-mode-find-occurrence-hook
  15081. (lambda ()
  15082. (when (org-mode-p)
  15083. (org-reveal))))
  15084. ;; Emacs 22
  15085. (defadvice occur-mode-goto-occurrence
  15086. (after org-occur-reveal activate)
  15087. (and (org-mode-p) (org-reveal)))
  15088. (defadvice occur-mode-goto-occurrence-other-window
  15089. (after org-occur-reveal activate)
  15090. (and (org-mode-p) (org-reveal)))
  15091. (defadvice occur-mode-display-occurrence
  15092. (after org-occur-reveal activate)
  15093. (when (org-mode-p)
  15094. (let ((pos (occur-mode-find-occurrence)))
  15095. (with-current-buffer (marker-buffer pos)
  15096. (save-excursion
  15097. (goto-char pos)
  15098. (org-reveal)))))))
  15099. (defun org-occur-link-in-agenda-files ()
  15100. "Create a link and search for it in the agendas.
  15101. The link is not stored in `org-stored-links', it is just created
  15102. for the search purpose."
  15103. (interactive)
  15104. (let ((link (condition-case nil
  15105. (org-store-link nil)
  15106. (error "Unable to create a link to here"))))
  15107. (org-occur-in-agenda-files (regexp-quote link))))
  15108. (defun org-uniquify (list)
  15109. "Remove duplicate elements from LIST."
  15110. (let (res)
  15111. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15112. res))
  15113. (defun org-delete-all (elts list)
  15114. "Remove all elements in ELTS from LIST."
  15115. (while elts
  15116. (setq list (delete (pop elts) list)))
  15117. list)
  15118. (defun org-back-over-empty-lines ()
  15119. "Move backwards over whitespace, to the beginning of the first empty line.
  15120. Returns the number of empty lines passed."
  15121. (let ((pos (point)))
  15122. (skip-chars-backward " \t\n\r")
  15123. (beginning-of-line 2)
  15124. (goto-char (min (point) pos))
  15125. (count-lines (point) pos)))
  15126. (defun org-skip-whitespace ()
  15127. (skip-chars-forward " \t\n\r"))
  15128. (defun org-point-in-group (point group &optional context)
  15129. "Check if POINT is in match-group GROUP.
  15130. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15131. match. If the match group does ot exist or point is not inside it,
  15132. return nil."
  15133. (and (match-beginning group)
  15134. (>= point (match-beginning group))
  15135. (<= point (match-end group))
  15136. (if context
  15137. (list context (match-beginning group) (match-end group))
  15138. t)))
  15139. (defun org-switch-to-buffer-other-window (&rest args)
  15140. "Switch to buffer in a second window on the current frame.
  15141. In particular, do not allow pop-up frames."
  15142. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15143. special-display-function)
  15144. (apply 'switch-to-buffer-other-window args)))
  15145. (defun org-combine-plists (&rest plists)
  15146. "Create a single property list from all plists in PLISTS.
  15147. The process starts by copying the first list, and then setting properties
  15148. from the other lists. Settings in the last list are the most significant
  15149. ones and overrule settings in the other lists."
  15150. (let ((rtn (copy-sequence (pop plists)))
  15151. p v ls)
  15152. (while plists
  15153. (setq ls (pop plists))
  15154. (while ls
  15155. (setq p (pop ls) v (pop ls))
  15156. (setq rtn (plist-put rtn p v))))
  15157. rtn))
  15158. (defun org-move-line-down (arg)
  15159. "Move the current line down. With prefix argument, move it past ARG lines."
  15160. (interactive "p")
  15161. (let ((col (current-column))
  15162. beg end pos)
  15163. (beginning-of-line 1) (setq beg (point))
  15164. (beginning-of-line 2) (setq end (point))
  15165. (beginning-of-line (+ 1 arg))
  15166. (setq pos (move-marker (make-marker) (point)))
  15167. (insert (delete-and-extract-region beg end))
  15168. (goto-char pos)
  15169. (org-move-to-column col)))
  15170. (defun org-move-line-up (arg)
  15171. "Move the current line up. With prefix argument, move it past ARG lines."
  15172. (interactive "p")
  15173. (let ((col (current-column))
  15174. beg end pos)
  15175. (beginning-of-line 1) (setq beg (point))
  15176. (beginning-of-line 2) (setq end (point))
  15177. (beginning-of-line (- arg))
  15178. (setq pos (move-marker (make-marker) (point)))
  15179. (insert (delete-and-extract-region beg end))
  15180. (goto-char pos)
  15181. (org-move-to-column col)))
  15182. (defun org-replace-escapes (string table)
  15183. "Replace %-escapes in STRING with values in TABLE.
  15184. TABLE is an association list with keys like \"%a\" and string values.
  15185. The sequences in STRING may contain normal field width and padding information,
  15186. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15187. so values can contain further %-escapes if they are define later in TABLE."
  15188. (let ((case-fold-search nil)
  15189. e re rpl)
  15190. (while (setq e (pop table))
  15191. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15192. (while (string-match re string)
  15193. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15194. (cdr e)))
  15195. (setq string (replace-match rpl t t string))))
  15196. string))
  15197. (defun org-sublist (list start end)
  15198. "Return a section of LIST, from START to END.
  15199. Counting starts at 1."
  15200. (let (rtn (c start))
  15201. (setq list (nthcdr (1- start) list))
  15202. (while (and list (<= c end))
  15203. (push (pop list) rtn)
  15204. (setq c (1+ c)))
  15205. (nreverse rtn)))
  15206. (defun org-find-base-buffer-visiting (file)
  15207. "Like `find-buffer-visiting' but always return the base buffer and
  15208. not an indirect buffer."
  15209. (let ((buf (or (get-file-buffer file)
  15210. (find-buffer-visiting file))))
  15211. (if buf
  15212. (or (buffer-base-buffer buf) buf)
  15213. nil)))
  15214. (defun org-image-file-name-regexp (&optional extensions)
  15215. "Return regexp matching the file names of images.
  15216. If EXTENSIONS is given, only match these."
  15217. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15218. (image-file-name-regexp)
  15219. (let ((image-file-name-extensions
  15220. (or extensions
  15221. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15222. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15223. (concat "\\."
  15224. (regexp-opt (nconc (mapcar 'upcase
  15225. image-file-name-extensions)
  15226. image-file-name-extensions)
  15227. t)
  15228. "\\'"))))
  15229. (defun org-file-image-p (file &optional extensions)
  15230. "Return non-nil if FILE is an image."
  15231. (save-match-data
  15232. (string-match (org-image-file-name-regexp extensions) file)))
  15233. (defun org-get-cursor-date ()
  15234. "Return the date at cursor in as a time.
  15235. This works in the calendar and in the agenda, anywhere else it just
  15236. returns the current time."
  15237. (let (date day defd)
  15238. (cond
  15239. ((eq major-mode 'calendar-mode)
  15240. (setq date (calendar-cursor-to-date)
  15241. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15242. ((eq major-mode 'org-agenda-mode)
  15243. (setq day (get-text-property (point) 'day))
  15244. (if day
  15245. (setq date (calendar-gregorian-from-absolute day)
  15246. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15247. (nth 2 date))))))
  15248. (or defd (current-time))))
  15249. (defvar org-agenda-action-marker (make-marker)
  15250. "Marker pointing to the entry for the next agenda action.")
  15251. (defun org-mark-entry-for-agenda-action ()
  15252. "Mark the current entry as target of an agenda action.
  15253. Agenda actions are actions executed from the agenda with the key `k',
  15254. which make use of the date at the cursor."
  15255. (interactive)
  15256. (move-marker org-agenda-action-marker
  15257. (save-excursion (org-back-to-heading t) (point))
  15258. (current-buffer))
  15259. (message
  15260. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15261. ;;; Paragraph filling stuff.
  15262. ;; We want this to be just right, so use the full arsenal.
  15263. (defun org-indent-line-function ()
  15264. "Indent line like previous, but further if previous was headline or item."
  15265. (interactive)
  15266. (let* ((pos (point))
  15267. (itemp (org-at-item-p))
  15268. (case-fold-search t)
  15269. (org-drawer-regexp (or org-drawer-regexp "\000"))
  15270. column bpos bcol tpos tcol bullet btype bullet-type)
  15271. ;; Find the previous relevant line
  15272. (beginning-of-line 1)
  15273. (cond
  15274. ((looking-at "#") (setq column 0))
  15275. ((looking-at "\\*+ ") (setq column 0))
  15276. ((and (looking-at "[ \t]*:END:")
  15277. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  15278. (save-excursion
  15279. (goto-char (1- (match-beginning 1)))
  15280. (setq column (current-column))))
  15281. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  15282. (save-excursion
  15283. (re-search-backward
  15284. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  15285. (setq column (org-get-indentation (match-string 0))))
  15286. (t
  15287. (beginning-of-line 0)
  15288. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  15289. (not (looking-at "[ \t]*:END:"))
  15290. (not (looking-at org-drawer-regexp)))
  15291. (beginning-of-line 0))
  15292. (cond
  15293. ((looking-at "\\*+[ \t]+")
  15294. (if (not org-adapt-indentation)
  15295. (setq column 0)
  15296. (goto-char (match-end 0))
  15297. (setq column (current-column))))
  15298. ((looking-at org-drawer-regexp)
  15299. (goto-char (1- (match-beginning 1)))
  15300. (setq column (current-column)))
  15301. ((looking-at "\\([ \t]*\\):END:")
  15302. (goto-char (match-end 1))
  15303. (setq column (current-column)))
  15304. ((org-in-item-p)
  15305. (org-beginning-of-item)
  15306. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  15307. (setq bpos (match-beginning 1) tpos (match-end 0)
  15308. bcol (progn (goto-char bpos) (current-column))
  15309. tcol (progn (goto-char tpos) (current-column))
  15310. bullet (match-string 1)
  15311. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  15312. (if (> tcol (+ bcol org-description-max-indent))
  15313. (setq tcol (+ bcol 5)))
  15314. (if (not itemp)
  15315. (setq column tcol)
  15316. (goto-char pos)
  15317. (beginning-of-line 1)
  15318. (if (looking-at "\\S-")
  15319. (progn
  15320. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  15321. (setq bullet (match-string 1)
  15322. btype (if (string-match "[0-9]" bullet) "n" bullet))
  15323. (setq column (if (equal btype bullet-type) bcol tcol)))
  15324. (setq column (org-get-indentation)))))
  15325. (t (setq column (org-get-indentation))))))
  15326. (goto-char pos)
  15327. (if (<= (current-column) (current-indentation))
  15328. (org-indent-line-to column)
  15329. (save-excursion (org-indent-line-to column)))
  15330. (setq column (current-column))
  15331. (beginning-of-line 1)
  15332. (if (looking-at
  15333. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  15334. (replace-match (concat (match-string 1)
  15335. (format org-property-format
  15336. (match-string 2) (match-string 3)))
  15337. t t))
  15338. (org-move-to-column column)))
  15339. (defun org-set-autofill-regexps ()
  15340. (interactive)
  15341. ;; In the paragraph separator we include headlines, because filling
  15342. ;; text in a line directly attached to a headline would otherwise
  15343. ;; fill the headline as well.
  15344. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15345. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  15346. ;; The paragraph starter includes hand-formatted lists.
  15347. (org-set-local
  15348. 'paragraph-start
  15349. (concat
  15350. "\f" "\\|"
  15351. "[ ]*$" "\\|"
  15352. "\\*+ " "\\|"
  15353. "[ \t]*#" "\\|"
  15354. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  15355. "[ \t]*[:|]" "\\|"
  15356. "\\$\\$" "\\|"
  15357. "\\\\\\(begin\\|end\\|[][]\\)"))
  15358. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15359. ;; But only if the user has not turned off tables or fixed-width regions
  15360. (org-set-local
  15361. 'auto-fill-inhibit-regexp
  15362. (concat "\\*+ \\|#\\+"
  15363. "\\|[ \t]*" org-keyword-time-regexp
  15364. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15365. (concat
  15366. "\\|[ \t]*["
  15367. (if org-enable-table-editor "|" "")
  15368. (if org-enable-fixed-width-editor ":" "")
  15369. "]"))))
  15370. ;; We use our own fill-paragraph function, to make sure that tables
  15371. ;; and fixed-width regions are not wrapped. That function will pass
  15372. ;; through to `fill-paragraph' when appropriate.
  15373. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15374. ; Adaptive filling: To get full control, first make sure that
  15375. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15376. (org-set-local 'adaptive-fill-regexp "\000")
  15377. (org-set-local 'adaptive-fill-function
  15378. 'org-adaptive-fill-function)
  15379. (org-set-local
  15380. 'align-mode-rules-list
  15381. '((org-in-buffer-settings
  15382. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15383. (modes . '(org-mode))))))
  15384. (defun org-fill-paragraph (&optional justify)
  15385. "Re-align a table, pass through to fill-paragraph if no table."
  15386. (let ((table-p (org-at-table-p))
  15387. (table.el-p (org-at-table.el-p)))
  15388. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15389. (save-excursion (goto-char (point-at-bol))
  15390. (looking-at outline-regexp)))
  15391. t) ; skip headlines
  15392. (table.el-p t) ; skip table.el tables
  15393. (table-p (org-table-align) t) ; align org-mode tables
  15394. (t nil)))) ; call paragraph-fill
  15395. ;; For reference, this is the default value of adaptive-fill-regexp
  15396. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15397. (defun org-adaptive-fill-function ()
  15398. "Return a fill prefix for org-mode files.
  15399. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15400. work correctly."
  15401. (cond ((looking-at "#[ \t]+")
  15402. (match-string 0))
  15403. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15404. (save-excursion
  15405. (if (> (match-end 1) (+ (match-beginning 1)
  15406. org-description-max-indent))
  15407. (goto-char (+ (match-beginning 1) 5))
  15408. (goto-char (match-end 0)))
  15409. (make-string (current-column) ?\ )))
  15410. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15411. (save-excursion
  15412. (goto-char (match-end 0))
  15413. (make-string (current-column) ?\ )))
  15414. (t nil)))
  15415. ;;; Other stuff.
  15416. (defun org-toggle-fixed-width-section (arg)
  15417. "Toggle the fixed-width export.
  15418. If there is no active region, the QUOTE keyword at the current headline is
  15419. inserted or removed. When present, it causes the text between this headline
  15420. and the next to be exported as fixed-width text, and unmodified.
  15421. If there is an active region, this command adds or removes a colon as the
  15422. first character of this line. If the first character of a line is a colon,
  15423. this line is also exported in fixed-width font."
  15424. (interactive "P")
  15425. (let* ((cc 0)
  15426. (regionp (org-region-active-p))
  15427. (beg (if regionp (region-beginning) (point)))
  15428. (end (if regionp (region-end)))
  15429. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15430. (case-fold-search nil)
  15431. (re "[ \t]*\\(: \\)")
  15432. off)
  15433. (if regionp
  15434. (save-excursion
  15435. (goto-char beg)
  15436. (setq cc (current-column))
  15437. (beginning-of-line 1)
  15438. (setq off (looking-at re))
  15439. (while (> nlines 0)
  15440. (setq nlines (1- nlines))
  15441. (beginning-of-line 1)
  15442. (cond
  15443. (arg
  15444. (org-move-to-column cc t)
  15445. (insert ": \n")
  15446. (forward-line -1))
  15447. ((and off (looking-at re))
  15448. (replace-match "" t t nil 1))
  15449. ((not off) (org-move-to-column cc t) (insert ": ")))
  15450. (forward-line 1)))
  15451. (save-excursion
  15452. (org-back-to-heading)
  15453. (if (looking-at (concat outline-regexp
  15454. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15455. (replace-match "" t t nil 1)
  15456. (if (looking-at outline-regexp)
  15457. (progn
  15458. (goto-char (match-end 0))
  15459. (insert org-quote-string " "))))))))
  15460. (defun org-reftex-citation ()
  15461. "Use reftex-citation to insert a citation into the buffer.
  15462. This looks for a line like
  15463. #+BIBLIOGRAPHY: foo plain option:-d
  15464. and derives from it that foo.bib is the bbliography file relevant
  15465. for this document. It then installs the necessary environment for RefTeX
  15466. to work in this buffer and calls `reftex-citation' to insert a citation
  15467. into the buffer.
  15468. Export of such citations to both LaTeX and HTML is handled by the contributed
  15469. package org-exp-bibtex by Taru Karttunen."
  15470. (interactive)
  15471. (let ((reftex-docstruct-symbol 'rds)
  15472. (reftex-cite-format "\\cite{%l}")
  15473. rds bib)
  15474. (save-excursion
  15475. (save-restriction
  15476. (widen)
  15477. (let ((case-fold-search t)
  15478. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15479. (if (not (save-excursion
  15480. (or (re-search-forward re nil t)
  15481. (re-search-backward re nil t))))
  15482. (error "No bibliography defined in file")
  15483. (setq bib (concat (match-string 1) ".bib")
  15484. rds (list (list 'bib bib)))))))
  15485. (call-interactively 'reftex-citation)))
  15486. ;;;; Functions extending outline functionality
  15487. (defun org-beginning-of-line (&optional arg)
  15488. "Go to the beginning of the current line. If that is invisible, continue
  15489. to a visible line beginning. This makes the function of C-a more intuitive.
  15490. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15491. first attempt, and only move to after the tags when the cursor is already
  15492. beyond the end of the headline."
  15493. (interactive "P")
  15494. (let ((pos (point))
  15495. (special (if (consp org-special-ctrl-a/e)
  15496. (car org-special-ctrl-a/e)
  15497. org-special-ctrl-a/e))
  15498. refpos)
  15499. (if (org-bound-and-true-p line-move-visual)
  15500. (beginning-of-visual-line 1)
  15501. (beginning-of-line 1))
  15502. (if (and arg (fboundp 'move-beginning-of-line))
  15503. (call-interactively 'move-beginning-of-line)
  15504. (if (bobp)
  15505. nil
  15506. (backward-char 1)
  15507. (if (org-invisible-p)
  15508. (while (and (not (bobp)) (org-invisible-p))
  15509. (backward-char 1)
  15510. (beginning-of-line 1))
  15511. (forward-char 1))))
  15512. (when special
  15513. (cond
  15514. ((and (looking-at org-complex-heading-regexp)
  15515. (= (char-after (match-end 1)) ?\ ))
  15516. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15517. (point-at-eol)))
  15518. (goto-char
  15519. (if (eq special t)
  15520. (cond ((> pos refpos) refpos)
  15521. ((= pos (point)) refpos)
  15522. (t (point)))
  15523. (cond ((> pos (point)) (point))
  15524. ((not (eq last-command this-command)) (point))
  15525. (t refpos)))))
  15526. ((org-at-item-p)
  15527. (goto-char
  15528. (if (eq special t)
  15529. (cond ((> pos (match-end 4)) (match-end 4))
  15530. ((= pos (point)) (match-end 4))
  15531. (t (point)))
  15532. (cond ((> pos (point)) (point))
  15533. ((not (eq last-command this-command)) (point))
  15534. (t (match-end 4))))))))
  15535. (org-no-warnings
  15536. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15537. (defun org-end-of-line (&optional arg)
  15538. "Go to the end of the line.
  15539. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15540. first attempt, and only move to after the tags when the cursor is already
  15541. beyond the end of the headline."
  15542. (interactive "P")
  15543. (let ((special (if (consp org-special-ctrl-a/e)
  15544. (cdr org-special-ctrl-a/e)
  15545. org-special-ctrl-a/e)))
  15546. (if (or (not special)
  15547. (not (org-on-heading-p))
  15548. arg)
  15549. (call-interactively
  15550. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15551. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15552. (t 'end-of-line)))
  15553. (let ((pos (point)))
  15554. (beginning-of-line 1)
  15555. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  15556. (if (eq special t)
  15557. (if (or (< pos (match-beginning 1))
  15558. (= pos (match-end 0)))
  15559. (goto-char (match-beginning 1))
  15560. (goto-char (match-end 0)))
  15561. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15562. (goto-char (match-end 0))
  15563. (goto-char (match-beginning 1))))
  15564. (call-interactively (if (fboundp 'move-end-of-line)
  15565. 'move-end-of-line
  15566. 'end-of-line)))))
  15567. (org-no-warnings
  15568. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15569. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15570. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15571. (define-key org-mode-map [home] 'org-beginning-of-line)
  15572. (define-key org-mode-map [end] 'org-end-of-line)
  15573. (defun org-backward-sentence (&optional arg)
  15574. "Go to beginning of sentence, or beginning of table field.
  15575. This will call `backward-sentence' or `org-table-beginning-of-field',
  15576. depending on context."
  15577. (interactive "P")
  15578. (cond
  15579. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15580. (t (call-interactively 'backward-sentence))))
  15581. (defun org-forward-sentence (&optional arg)
  15582. "Go to end of sentence, or end of table field.
  15583. This will call `forward-sentence' or `org-table-end-of-field',
  15584. depending on context."
  15585. (interactive "P")
  15586. (cond
  15587. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15588. (t (call-interactively 'forward-sentence))))
  15589. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15590. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15591. (defun org-kill-line (&optional arg)
  15592. "Kill line, to tags or end of line."
  15593. (interactive "P")
  15594. (cond
  15595. ((or (not org-special-ctrl-k)
  15596. (bolp)
  15597. (not (org-on-heading-p)))
  15598. (call-interactively 'kill-line))
  15599. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15600. (kill-region (point) (match-beginning 1))
  15601. (org-set-tags nil t))
  15602. (t (kill-region (point) (point-at-eol)))))
  15603. (define-key org-mode-map "\C-k" 'org-kill-line)
  15604. (defun org-yank (&optional arg)
  15605. "Yank. If the kill is a subtree, treat it specially.
  15606. This command will look at the current kill and check if is a single
  15607. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15608. cursor is at the beginning of a line or after the stars of a currently
  15609. empty headline, then the yank is handled specially. How exactly depends
  15610. on the value of the following variables, both set by default.
  15611. org-yank-folded-subtrees
  15612. When set, the subtree(s) will be folded after insertion, but only
  15613. if doing so would now swallow text after the yanked text.
  15614. org-yank-adjusted-subtrees
  15615. When set, the subtree will be promoted or demoted in order to
  15616. fit into the local outline tree structure, which means that the level
  15617. will be adjusted so that it becomes the smaller one of the two
  15618. *visible* surrounding headings.
  15619. Any prefix to this command will cause `yank' to be called directly with
  15620. no special treatment. In particular, a simple `C-u' prefix will just
  15621. plainly yank the text as it is.
  15622. \[1] The test checks if the first non-white line is a heading
  15623. and if there are no other headings with fewer stars."
  15624. (interactive "P")
  15625. (org-yank-generic 'yank arg))
  15626. (defun org-yank-generic (command arg)
  15627. "Perform some yank-like command.
  15628. This function implements the behavior described in the `org-yank'
  15629. documentation. However, it has been generalized to work for any
  15630. interactive command with similar behavior."
  15631. ;; pretend to be command COMMAND
  15632. (setq this-command command)
  15633. (if arg
  15634. (call-interactively command)
  15635. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15636. (and (org-kill-is-subtree-p)
  15637. (or (bolp)
  15638. (and (looking-at "[ \t]*$")
  15639. (string-match
  15640. "\\`\\*+\\'"
  15641. (buffer-substring (point-at-bol) (point)))))))
  15642. swallowp)
  15643. (cond
  15644. ((and subtreep org-yank-folded-subtrees)
  15645. (let ((beg (point))
  15646. end)
  15647. (if (and subtreep org-yank-adjusted-subtrees)
  15648. (org-paste-subtree nil nil 'for-yank)
  15649. (call-interactively command))
  15650. (setq end (point))
  15651. (goto-char beg)
  15652. (when (and (bolp) subtreep
  15653. (not (setq swallowp
  15654. (org-yank-folding-would-swallow-text beg end))))
  15655. (or (looking-at outline-regexp)
  15656. (re-search-forward (concat "^" outline-regexp) end t))
  15657. (while (and (< (point) end) (looking-at outline-regexp))
  15658. (hide-subtree)
  15659. (org-cycle-show-empty-lines 'folded)
  15660. (condition-case nil
  15661. (outline-forward-same-level 1)
  15662. (error (goto-char end)))))
  15663. (when swallowp
  15664. (message
  15665. "Inserted text not folded because that would swallow text"))
  15666. (goto-char end)
  15667. (skip-chars-forward " \t\n\r")
  15668. (beginning-of-line 1)
  15669. (push-mark beg 'nomsg)))
  15670. ((and subtreep org-yank-adjusted-subtrees)
  15671. (let ((beg (point-at-bol)))
  15672. (org-paste-subtree nil nil 'for-yank)
  15673. (push-mark beg 'nomsg)))
  15674. (t
  15675. (call-interactively command))))))
  15676. (defun org-yank-folding-would-swallow-text (beg end)
  15677. "Would hide-subtree at BEG swallow any text after END?"
  15678. (let (level)
  15679. (save-excursion
  15680. (goto-char beg)
  15681. (when (or (looking-at outline-regexp)
  15682. (re-search-forward (concat "^" outline-regexp) end t))
  15683. (setq level (org-outline-level)))
  15684. (goto-char end)
  15685. (skip-chars-forward " \t\r\n\v\f")
  15686. (if (or (eobp)
  15687. (and (bolp) (looking-at org-outline-regexp)
  15688. (<= (org-outline-level) level)))
  15689. nil ; Nothing would be swallowed
  15690. t)))) ; something would swallow
  15691. (define-key org-mode-map "\C-y" 'org-yank)
  15692. (defun org-invisible-p ()
  15693. "Check if point is at a character currently not visible."
  15694. ;; Early versions of noutline don't have `outline-invisible-p'.
  15695. (if (fboundp 'outline-invisible-p)
  15696. (outline-invisible-p)
  15697. (get-char-property (point) 'invisible)))
  15698. (defun org-invisible-p2 ()
  15699. "Check if point is at a character currently not visible."
  15700. (save-excursion
  15701. (if (and (eolp) (not (bobp))) (backward-char 1))
  15702. ;; Early versions of noutline don't have `outline-invisible-p'.
  15703. (if (fboundp 'outline-invisible-p)
  15704. (outline-invisible-p)
  15705. (get-char-property (point) 'invisible))))
  15706. (defun org-back-to-heading (&optional invisible-ok)
  15707. "Call `outline-back-to-heading', but provide a better error message."
  15708. (condition-case nil
  15709. (outline-back-to-heading invisible-ok)
  15710. (error (error "Before first headline at position %d in buffer %s"
  15711. (point) (current-buffer)))))
  15712. (defun org-before-first-heading-p ()
  15713. "Before first heading?"
  15714. (save-excursion
  15715. (null (re-search-backward "^\\*+ " nil t))))
  15716. (defun org-on-heading-p (&optional ignored)
  15717. (outline-on-heading-p t))
  15718. (defun org-at-heading-p (&optional ignored)
  15719. (outline-on-heading-p t))
  15720. (defun org-at-heading-or-item-p ()
  15721. (or (org-on-heading-p) (org-at-item-p)))
  15722. (defun org-on-target-p ()
  15723. (or (org-in-regexp org-radio-target-regexp)
  15724. (org-in-regexp org-target-regexp)))
  15725. (defun org-up-heading-all (arg)
  15726. "Move to the heading line of which the present line is a subheading.
  15727. This function considers both visible and invisible heading lines.
  15728. With argument, move up ARG levels."
  15729. (if (fboundp 'outline-up-heading-all)
  15730. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15731. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15732. (defun org-up-heading-safe ()
  15733. "Move to the heading line of which the present line is a subheading.
  15734. This version will not throw an error. It will return the level of the
  15735. headline found, or nil if no higher level is found.
  15736. Also, this function will be a lot faster than `outline-up-heading',
  15737. because it relies on stars being the outline starters. This can really
  15738. make a significant difference in outlines with very many siblings."
  15739. (let (start-level re)
  15740. (org-back-to-heading t)
  15741. (setq start-level (funcall outline-level))
  15742. (if (equal start-level 1)
  15743. nil
  15744. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15745. (if (re-search-backward re nil t)
  15746. (funcall outline-level)))))
  15747. (defun org-first-sibling-p ()
  15748. "Is this heading the first child of its parents?"
  15749. (interactive)
  15750. (let ((re (concat "^" outline-regexp))
  15751. level l)
  15752. (unless (org-at-heading-p t)
  15753. (error "Not at a heading"))
  15754. (setq level (funcall outline-level))
  15755. (save-excursion
  15756. (if (not (re-search-backward re nil t))
  15757. t
  15758. (setq l (funcall outline-level))
  15759. (< l level)))))
  15760. (defun org-goto-sibling (&optional previous)
  15761. "Goto the next sibling, even if it is invisible.
  15762. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15763. when a sibling was found. When none is found, return nil and don't
  15764. move point."
  15765. (let ((fun (if previous 're-search-backward 're-search-forward))
  15766. (pos (point))
  15767. (re (concat "^" outline-regexp))
  15768. level l)
  15769. (when (condition-case nil (org-back-to-heading t) (error nil))
  15770. (setq level (funcall outline-level))
  15771. (catch 'exit
  15772. (or previous (forward-char 1))
  15773. (while (funcall fun re nil t)
  15774. (setq l (funcall outline-level))
  15775. (when (< l level) (goto-char pos) (throw 'exit nil))
  15776. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15777. (goto-char pos)
  15778. nil))))
  15779. (defun org-show-siblings ()
  15780. "Show all siblings of the current headline."
  15781. (save-excursion
  15782. (while (org-goto-sibling) (org-flag-heading nil)))
  15783. (save-excursion
  15784. (while (org-goto-sibling 'previous)
  15785. (org-flag-heading nil))))
  15786. (defun org-show-hidden-entry ()
  15787. "Show an entry where even the heading is hidden."
  15788. (save-excursion
  15789. (org-show-entry)))
  15790. (defun org-flag-heading (flag &optional entry)
  15791. "Flag the current heading. FLAG non-nil means make invisible.
  15792. When ENTRY is non-nil, show the entire entry."
  15793. (save-excursion
  15794. (org-back-to-heading t)
  15795. ;; Check if we should show the entire entry
  15796. (if entry
  15797. (progn
  15798. (org-show-entry)
  15799. (save-excursion
  15800. (and (outline-next-heading)
  15801. (org-flag-heading nil))))
  15802. (outline-flag-region (max (point-min) (1- (point)))
  15803. (save-excursion (outline-end-of-heading) (point))
  15804. flag))))
  15805. (defun org-get-next-sibling ()
  15806. "Move to next heading of the same level, and return point.
  15807. If there is no such heading, return nil.
  15808. This is like outline-next-sibling, but invisible headings are ok."
  15809. (let ((level (funcall outline-level)))
  15810. (outline-next-heading)
  15811. (while (and (not (eobp)) (> (funcall outline-level) level))
  15812. (outline-next-heading))
  15813. (if (or (eobp) (< (funcall outline-level) level))
  15814. nil
  15815. (point))))
  15816. (defun org-get-last-sibling ()
  15817. "Move to previous heading of the same level, and return point.
  15818. If there is no such heading, return nil."
  15819. (let ((opoint (point))
  15820. (level (funcall outline-level)))
  15821. (outline-previous-heading)
  15822. (when (and (/= (point) opoint) (outline-on-heading-p t))
  15823. (while (and (> (funcall outline-level) level)
  15824. (not (bobp)))
  15825. (outline-previous-heading))
  15826. (if (< (funcall outline-level) level)
  15827. nil
  15828. (point)))))
  15829. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15830. ;; This contains an exact copy of the original function, but it uses
  15831. ;; `org-back-to-heading', to make it work also in invisible
  15832. ;; trees. And is uses an invisible-OK argument.
  15833. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15834. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15835. ;; with many children and grandchildren etc, this can be much faster
  15836. ;; than the outline version.
  15837. (org-back-to-heading invisible-OK)
  15838. (let ((first t)
  15839. (level (funcall outline-level)))
  15840. (if (and (org-mode-p) (< level 1000))
  15841. ;; A true heading (not a plain list item), in Org-mode
  15842. ;; This means we can easily find the end by looking
  15843. ;; only for the right number of stars. Using a regexp to do
  15844. ;; this is so much faster than using a Lisp loop.
  15845. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15846. (forward-char 1)
  15847. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15848. ;; something else, do it the slow way
  15849. (while (and (not (eobp))
  15850. (or first (> (funcall outline-level) level)))
  15851. (setq first nil)
  15852. (outline-next-heading)))
  15853. (unless to-heading
  15854. (if (memq (preceding-char) '(?\n ?\^M))
  15855. (progn
  15856. ;; Go to end of line before heading
  15857. (forward-char -1)
  15858. (if (memq (preceding-char) '(?\n ?\^M))
  15859. ;; leave blank line before heading
  15860. (forward-char -1))))))
  15861. (point))
  15862. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15863. "Use Org version in org-mode, for dramatic speed-up."
  15864. (if (eq major-mode 'org-mode)
  15865. (progn
  15866. (org-end-of-subtree nil t)
  15867. (unless (eobp) (backward-char 1)))
  15868. ad-do-it))
  15869. (defun org-forward-same-level (arg &optional invisible-ok)
  15870. "Move forward to the arg'th subheading at same level as this one.
  15871. Stop at the first and last subheadings of a superior heading."
  15872. (interactive "p")
  15873. (org-back-to-heading invisible-ok)
  15874. (org-on-heading-p)
  15875. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15876. (re (format "^\\*\\{1,%d\\} " level))
  15877. l)
  15878. (forward-char 1)
  15879. (while (> arg 0)
  15880. (while (and (re-search-forward re nil 'move)
  15881. (setq l (- (match-end 0) (match-beginning 0) 1))
  15882. (= l level)
  15883. (not invisible-ok)
  15884. (org-invisible-p))
  15885. (if (< l level) (setq arg 1)))
  15886. (setq arg (1- arg)))
  15887. (beginning-of-line 1)))
  15888. (defun org-backward-same-level (arg &optional invisible-ok)
  15889. "Move backward to the arg'th subheading at same level as this one.
  15890. Stop at the first and last subheadings of a superior heading."
  15891. (interactive "p")
  15892. (org-back-to-heading)
  15893. (org-on-heading-p)
  15894. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15895. (re (format "^\\*\\{1,%d\\} " level))
  15896. l)
  15897. (while (> arg 0)
  15898. (while (and (re-search-backward re nil 'move)
  15899. (setq l (- (match-end 0) (match-beginning 0) 1))
  15900. (= l level)
  15901. (not invisible-ok)
  15902. (org-invisible-p))
  15903. (if (< l level) (setq arg 1)))
  15904. (setq arg (1- arg)))))
  15905. (defun org-show-subtree ()
  15906. "Show everything after this heading at deeper levels."
  15907. (outline-flag-region
  15908. (point)
  15909. (save-excursion
  15910. (org-end-of-subtree t t))
  15911. nil))
  15912. (defun org-show-entry ()
  15913. "Show the body directly following this heading.
  15914. Show the heading too, if it is currently invisible."
  15915. (interactive)
  15916. (save-excursion
  15917. (condition-case nil
  15918. (progn
  15919. (org-back-to-heading t)
  15920. (outline-flag-region
  15921. (max (point-min) (1- (point)))
  15922. (save-excursion
  15923. (if (re-search-forward
  15924. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15925. (match-beginning 1)
  15926. (point-max)))
  15927. nil)
  15928. (org-cycle-hide-drawers 'children))
  15929. (error nil))))
  15930. (defun org-make-options-regexp (kwds &optional extra)
  15931. "Make a regular expression for keyword lines."
  15932. (concat
  15933. "^"
  15934. "#?[ \t]*\\+\\("
  15935. (mapconcat 'regexp-quote kwds "\\|")
  15936. (if extra (concat "\\|" extra))
  15937. "\\):[ \t]*"
  15938. "\\(.*\\)"))
  15939. ;; Make isearch reveal the necessary context
  15940. (defun org-isearch-end ()
  15941. "Reveal context after isearch exits."
  15942. (when isearch-success ; only if search was successful
  15943. (if (featurep 'xemacs)
  15944. ;; Under XEmacs, the hook is run in the correct place,
  15945. ;; we directly show the context.
  15946. (org-show-context 'isearch)
  15947. ;; In Emacs the hook runs *before* restoring the overlays.
  15948. ;; So we have to use a one-time post-command-hook to do this.
  15949. ;; (Emacs 22 has a special variable, see function `org-mode')
  15950. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15951. isearch-mode-end-hook-quit)
  15952. ;; Only when the isearch was not quitted.
  15953. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15954. 'append 'local)))))
  15955. (defun org-isearch-post-command ()
  15956. "Remove self from hook, and show context."
  15957. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15958. (org-show-context 'isearch))
  15959. ;;;; Integration with and fixes for other packages
  15960. ;;; Imenu support
  15961. (defvar org-imenu-markers nil
  15962. "All markers currently used by Imenu.")
  15963. (make-variable-buffer-local 'org-imenu-markers)
  15964. (defun org-imenu-new-marker (&optional pos)
  15965. "Return a new marker for use by Imenu, and remember the marker."
  15966. (let ((m (make-marker)))
  15967. (move-marker m (or pos (point)))
  15968. (push m org-imenu-markers)
  15969. m))
  15970. (defun org-imenu-get-tree ()
  15971. "Produce the index for Imenu."
  15972. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15973. (setq org-imenu-markers nil)
  15974. (let* ((n org-imenu-depth)
  15975. (re (concat "^" outline-regexp))
  15976. (subs (make-vector (1+ n) nil))
  15977. (last-level 0)
  15978. m level head)
  15979. (save-excursion
  15980. (save-restriction
  15981. (widen)
  15982. (goto-char (point-max))
  15983. (while (re-search-backward re nil t)
  15984. (setq level (org-reduced-level (funcall outline-level)))
  15985. (when (<= level n)
  15986. (looking-at org-complex-heading-regexp)
  15987. (setq head (org-link-display-format
  15988. (org-match-string-no-properties 4))
  15989. m (org-imenu-new-marker))
  15990. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15991. (if (>= level last-level)
  15992. (push (cons head m) (aref subs level))
  15993. (push (cons head (aref subs (1+ level))) (aref subs level))
  15994. (loop for i from (1+ level) to n do (aset subs i nil)))
  15995. (setq last-level level)))))
  15996. (aref subs 1)))
  15997. (eval-after-load "imenu"
  15998. '(progn
  15999. (add-hook 'imenu-after-jump-hook
  16000. (lambda ()
  16001. (if (eq major-mode 'org-mode)
  16002. (org-show-context 'org-goto))))))
  16003. (defun org-link-display-format (link)
  16004. "Replace a link with either the description, or the link target
  16005. if no description is present"
  16006. (save-match-data
  16007. (if (string-match org-bracket-link-analytic-regexp link)
  16008. (replace-match (or (match-string 5 link)
  16009. (concat (match-string 1 link)
  16010. (match-string 3 link)))
  16011. nil nil link)
  16012. link)))
  16013. ;; Speedbar support
  16014. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16015. "Overlay marking the agenda restriction line in speedbar.")
  16016. (org-overlay-put org-speedbar-restriction-lock-overlay
  16017. 'face 'org-agenda-restriction-lock)
  16018. (org-overlay-put org-speedbar-restriction-lock-overlay
  16019. 'help-echo "Agendas are currently limited to this item.")
  16020. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16021. (defun org-speedbar-set-agenda-restriction ()
  16022. "Restrict future agenda commands to the location at point in speedbar.
  16023. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16024. (interactive)
  16025. (require 'org-agenda)
  16026. (let (p m tp np dir txt)
  16027. (cond
  16028. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16029. 'org-imenu t))
  16030. (setq m (get-text-property p 'org-imenu-marker))
  16031. (save-excursion
  16032. (save-restriction
  16033. (set-buffer (marker-buffer m))
  16034. (goto-char m)
  16035. (org-agenda-set-restriction-lock 'subtree))))
  16036. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16037. 'speedbar-function 'speedbar-find-file))
  16038. (setq tp (previous-single-property-change
  16039. (1+ p) 'speedbar-function)
  16040. np (next-single-property-change
  16041. tp 'speedbar-function)
  16042. dir (speedbar-line-directory)
  16043. txt (buffer-substring-no-properties (or tp (point-min))
  16044. (or np (point-max))))
  16045. (save-excursion
  16046. (save-restriction
  16047. (set-buffer (find-file-noselect
  16048. (let ((default-directory dir))
  16049. (expand-file-name txt))))
  16050. (unless (org-mode-p)
  16051. (error "Cannot restrict to non-Org-mode file"))
  16052. (org-agenda-set-restriction-lock 'file))))
  16053. (t (error "Don't know how to restrict Org-mode's agenda")))
  16054. (org-move-overlay org-speedbar-restriction-lock-overlay
  16055. (point-at-bol) (point-at-eol))
  16056. (setq current-prefix-arg nil)
  16057. (org-agenda-maybe-redo)))
  16058. (eval-after-load "speedbar"
  16059. '(progn
  16060. (speedbar-add-supported-extension ".org")
  16061. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16062. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16063. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16064. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16065. (add-hook 'speedbar-visiting-tag-hook
  16066. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16067. ;;; Fixes and Hacks for problems with other packages
  16068. ;; Make flyspell not check words in links, to not mess up our keymap
  16069. (defun org-mode-flyspell-verify ()
  16070. "Don't let flyspell put overlays at active buttons."
  16071. (and (not (get-text-property (point) 'keymap))
  16072. (not (get-text-property (point) 'org-no-flyspell))))
  16073. (defun org-remove-flyspell-overlays-in (beg end)
  16074. "Remove flyspell overlays in region."
  16075. (and (org-bound-and-true-p flyspell-mode)
  16076. (fboundp 'flyspell-delete-region-overlays)
  16077. (flyspell-delete-region-overlays beg end))
  16078. (add-text-properties beg end '(org-no-flyspell t)))
  16079. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16080. (eval-after-load "bookmark"
  16081. '(if (boundp 'bookmark-after-jump-hook)
  16082. ;; We can use the hook
  16083. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16084. ;; Hook not available, use advice
  16085. (defadvice bookmark-jump (after org-make-visible activate)
  16086. "Make the position visible."
  16087. (org-bookmark-jump-unhide))))
  16088. ;; Make sure saveplace shows the location if it was hidden
  16089. (eval-after-load "saveplace"
  16090. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16091. "Make the position visible."
  16092. (org-bookmark-jump-unhide)))
  16093. ;; Make sure ecb shows the location if it was hidden
  16094. (eval-after-load "ecb"
  16095. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16096. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16097. (if (eq major-mode 'org-mode)
  16098. (org-show-context))))
  16099. (defun org-bookmark-jump-unhide ()
  16100. "Unhide the current position, to show the bookmark location."
  16101. (and (org-mode-p)
  16102. (or (org-invisible-p)
  16103. (save-excursion (goto-char (max (point-min) (1- (point))))
  16104. (org-invisible-p)))
  16105. (org-show-context 'bookmark-jump)))
  16106. ;; Make session.el ignore our circular variable
  16107. (eval-after-load "session"
  16108. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16109. ;;;; Experimental code
  16110. (defun org-closed-in-range ()
  16111. "Sparse tree of items closed in a certain time range.
  16112. Still experimental, may disappear in the future."
  16113. (interactive)
  16114. ;; Get the time interval from the user.
  16115. (let* ((time1 (org-float-time
  16116. (org-read-date nil 'to-time nil "Starting date: ")))
  16117. (time2 (org-float-time
  16118. (org-read-date nil 'to-time nil "End date:")))
  16119. ;; callback function
  16120. (callback (lambda ()
  16121. (let ((time
  16122. (org-float-time
  16123. (apply 'encode-time
  16124. (org-parse-time-string
  16125. (match-string 1))))))
  16126. ;; check if time in interval
  16127. (and (>= time time1) (<= time time2))))))
  16128. ;; make tree, check each match with the callback
  16129. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16130. ;;;; Finish up
  16131. (provide 'org)
  16132. (run-hooks 'org-load-hook)
  16133. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16134. ;;; org.el ends here