org.el 663 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806
  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.33a
  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.33a"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let* ((origin default-directory)
  96. (version org-version)
  97. (git-version)
  98. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  99. (when (and (file-exists-p (expand-file-name ".git" dir))
  100. (executable-find "git"))
  101. (unwind-protect
  102. (progn
  103. (cd dir)
  104. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  105. (with-current-buffer "*Shell Command Output*"
  106. (goto-char (point-min))
  107. (setq git-version (buffer-substring (point) (point-at-eol))))
  108. (subst-char-in-string ?- ?. git-version t)
  109. (when (string-match "\\S-"
  110. (shell-command-to-string
  111. "git diff-index --name-only HEAD --"))
  112. (setq git-version (concat git-version ".dirty")))
  113. (setq version (concat version " (" git-version ")"))))
  114. (cd origin)))
  115. (setq version (format "Org-mode version %s" version))
  116. (if here (insert version))
  117. (message version)))
  118. ;;; Compatibility constants
  119. ;;; The custom variables
  120. (defgroup org nil
  121. "Outline-based notes management and organizer."
  122. :tag "Org"
  123. :group 'outlines
  124. :group 'hypermedia
  125. :group 'calendar)
  126. (defcustom org-mode-hook nil
  127. "Mode hook for Org-mode, run after the mode was turned on."
  128. :group 'org
  129. :type 'hook)
  130. (defcustom org-load-hook nil
  131. "Hook that is run after org.el has been loaded."
  132. :group 'org
  133. :type 'hook)
  134. (defvar org-modules) ; defined below
  135. (defvar org-modules-loaded nil
  136. "Have the modules been loaded already?")
  137. (defun org-load-modules-maybe (&optional force)
  138. "Load all extensions listed in `org-modules'."
  139. (when (or force (not org-modules-loaded))
  140. (mapc (lambda (ext)
  141. (condition-case nil (require ext)
  142. (error (message "Problems while trying to load feature `%s'" ext))))
  143. org-modules)
  144. (setq org-modules-loaded t)))
  145. (defun org-set-modules (var value)
  146. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  147. (set var value)
  148. (when (featurep 'org)
  149. (org-load-modules-maybe 'force)))
  150. (when (org-bound-and-true-p org-modules)
  151. (let ((a (member 'org-infojs org-modules)))
  152. (and a (setcar a 'org-jsinfo))))
  153. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  154. "Modules that should always be loaded together with org.el.
  155. If a description starts with <C>, the file is not part of Emacs
  156. and loading it will require that you have downloaded and properly installed
  157. the org-mode distribution.
  158. You can also use this system to load external packages (i.e. neither Org
  159. core modules, nor modules from the CONTRIB directory). Just add symbols
  160. to the end of the list. If the package is called org-xyz.el, then you need
  161. to add the symbol `xyz', and the package must have a call to
  162. (provide 'org-xyz)"
  163. :group 'org
  164. :set 'org-set-modules
  165. :type
  166. '(set :greedy t
  167. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  168. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  169. (const :tag " crypt: Encryption of subtrees" org-crypt)
  170. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  171. (const :tag " id: Global IDs for identifying entries" org-id)
  172. (const :tag " info: Links to Info nodes" org-info)
  173. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  174. (const :tag " habit: Track your consistency with habits" org-habit)
  175. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  176. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  177. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  178. (const :tag " mew Links to Mew folders/messages" org-mew)
  179. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  180. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  181. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  182. (const :tag " vm: Links to VM folders/messages" org-vm)
  183. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  184. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  185. (const :tag " mouse: Additional mouse support" org-mouse)
  186. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  187. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  188. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  189. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  190. (const :tag "C collector: Collect properties into tables" org-collector)
  191. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  192. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  193. (const :tag "C eval: Include command output as text" org-eval)
  194. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  195. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  196. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  197. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  198. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  199. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  200. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  201. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  202. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  203. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  204. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  205. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  206. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  207. (const :tag "C R: Computation using the R language" org-R)
  208. (const :tag "C registry: A registry for Org-mode links" org-registry)
  209. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  210. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  211. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  212. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  213. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  214. (const :tag "C track: Keep up with Org-mode development" org-track)
  215. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  216. (defcustom org-support-shift-select nil
  217. "Non-nil means, make shift-cursor commands select text when possible.
  218. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  219. selecting a region, or enlarge thusly regions started in this way.
  220. In Org-mode, in special contexts, these same keys are used for other
  221. purposes, important enough to compete with shift selection. Org tries
  222. to balance these needs by supporting `shift-select-mode' outside these
  223. special contexts, under control of this variable.
  224. The default of this variable is nil, to avoid confusing behavior. Shifted
  225. cursor keys will then execute Org commands in the following contexts:
  226. - on a headline, changing TODO state (left/right) and priority (up/down)
  227. - on a time stamp, changing the time
  228. - in a plain list item, changing the bullet type
  229. - in a property definition line, switching between allowed values
  230. - in the BEGIN line of a clock table (changing the time block).
  231. Outside these contexts, the commands will throw an error.
  232. When this variable is t and the cursor is not in a special context,
  233. Org-mode will support shift-selection for making and enlarging regions.
  234. To make this more effective, the bullet cycling will no longer happen
  235. anywhere in an item line, but only if the cursor is exactly on the bullet.
  236. If you set this variable to the symbol `always', then the keys
  237. will not be special in headlines, property lines, and item lines, to make
  238. shift selection work there as well. If this is what you want, you can
  239. use the following alternative commands: `C-c C-t' and `C-c ,' to
  240. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  241. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  242. edited by hand or in column view.
  243. However, when the cursor is on a timestamp, shift-cursor commands
  244. will still edit the time stamp - this is just too good to give up.
  245. XEmacs user should have this variable set to nil, because shift-select-mode
  246. is Emacs 23 only."
  247. :group 'org
  248. :type '(choice
  249. (const :tag "Never" nil)
  250. (const :tag "When outside special context" t)
  251. (const :tag "Everywhere except timestamps" always)))
  252. (defgroup org-startup nil
  253. "Options concerning startup of Org-mode."
  254. :tag "Org Startup"
  255. :group 'org)
  256. (defcustom org-startup-folded t
  257. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  258. This can also be configured on a per-file basis by adding one of
  259. the following lines anywhere in the buffer:
  260. #+STARTUP: fold (or `overview', this is equivalent)
  261. #+STARTUP: nofold (or `showall', this is equivalent)
  262. #+STARTUP: content
  263. #+STARTUP: showeverything"
  264. :group 'org-startup
  265. :type '(choice
  266. (const :tag "nofold: show all" nil)
  267. (const :tag "fold: overview" t)
  268. (const :tag "content: all headlines" content)
  269. (const :tag "show everything, even drawers" showeverything)))
  270. (defcustom org-startup-truncated t
  271. "Non-nil means, entering Org-mode will set `truncate-lines'.
  272. This is useful since some lines containing links can be very long and
  273. uninteresting. Also tables look terrible when wrapped."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-startup-indented nil
  277. "Non-nil means, turn on `org-indent-mode' on startup.
  278. This can also be configured on a per-file basis by adding one of
  279. the following lines anywhere in the buffer:
  280. #+STARTUP: indent
  281. #+STARTUP: noindent"
  282. :group 'org-structure
  283. :type '(choice
  284. (const :tag "Not" nil)
  285. (const :tag "Globally (slow on startup in large files)" t)))
  286. (defcustom org-startup-align-all-tables nil
  287. "Non-nil means, align all tables when visiting a file.
  288. This is useful when the column width in tables is forced with <N> cookies
  289. in table fields. Such tables will look correct only after the first re-align.
  290. This can also be configured on a per-file basis by adding one of
  291. the following lines anywhere in the buffer:
  292. #+STARTUP: align
  293. #+STARTUP: noalign"
  294. :group 'org-startup
  295. :type 'boolean)
  296. (defcustom org-insert-mode-line-in-empty-file nil
  297. "Non-nil means insert the first line setting Org-mode in empty files.
  298. When the function `org-mode' is called interactively in an empty file, this
  299. normally means that the file name does not automatically trigger Org-mode.
  300. To ensure that the file will always be in Org-mode in the future, a
  301. line enforcing Org-mode will be inserted into the buffer, if this option
  302. has been set."
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-replace-disputed-keys nil
  306. "Non-nil means use alternative key bindings for some keys.
  307. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  308. These keys are also used by other packages like shift-selection-mode'
  309. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  310. If you want to use Org-mode together with one of these other modes,
  311. or more generally if you would like to move some Org-mode commands to
  312. other keys, set this variable and configure the keys with the variable
  313. `org-disputed-keys'.
  314. This option is only relevant at load-time of Org-mode, and must be set
  315. *before* org.el is loaded. Changing it requires a restart of Emacs to
  316. become effective."
  317. :group 'org-startup
  318. :type 'boolean)
  319. (defcustom org-use-extra-keys nil
  320. "Non-nil means use extra key sequence definitions for certain
  321. commands. This happens automatically if you run XEmacs or if
  322. window-system is nil. This variable lets you do the same
  323. manually. You must set it before loading org.
  324. Example: on Carbon Emacs 22 running graphically, with an external
  325. keyboard on a Powerbook, the default way of setting M-left might
  326. not work for either Alt or ESC. Setting this variable will make
  327. it work for ESC."
  328. :group 'org-startup
  329. :type 'boolean)
  330. (if (fboundp 'defvaralias)
  331. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  332. (defcustom org-disputed-keys
  333. '(([(shift up)] . [(meta p)])
  334. ([(shift down)] . [(meta n)])
  335. ([(shift left)] . [(meta -)])
  336. ([(shift right)] . [(meta +)])
  337. ([(control shift right)] . [(meta shift +)])
  338. ([(control shift left)] . [(meta shift -)]))
  339. "Keys for which Org-mode and other modes compete.
  340. This is an alist, cars are the default keys, second element specifies
  341. the alternative to use when `org-replace-disputed-keys' is t.
  342. Keys can be specified in any syntax supported by `define-key'.
  343. The value of this option takes effect only at Org-mode's startup,
  344. therefore you'll have to restart Emacs to apply it after changing."
  345. :group 'org-startup
  346. :type 'alist)
  347. (defun org-key (key)
  348. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  349. Or return the original if not disputed."
  350. (if org-replace-disputed-keys
  351. (let* ((nkey (key-description key))
  352. (x (org-find-if (lambda (x)
  353. (equal (key-description (car x)) nkey))
  354. org-disputed-keys)))
  355. (if x (cdr x) key))
  356. key))
  357. (defun org-find-if (predicate seq)
  358. (catch 'exit
  359. (while seq
  360. (if (funcall predicate (car seq))
  361. (throw 'exit (car seq))
  362. (pop seq)))))
  363. (defun org-defkey (keymap key def)
  364. "Define a key, possibly translated, as returned by `org-key'."
  365. (define-key keymap (org-key key) def))
  366. (defcustom org-ellipsis nil
  367. "The ellipsis to use in the Org-mode outline.
  368. When nil, just use the standard three dots. When a string, use that instead,
  369. When a face, use the standard 3 dots, but with the specified face.
  370. The change affects only Org-mode (which will then use its own display table).
  371. Changing this requires executing `M-x org-mode' in a buffer to become
  372. effective."
  373. :group 'org-startup
  374. :type '(choice (const :tag "Default" nil)
  375. (face :tag "Face" :value org-warning)
  376. (string :tag "String" :value "...#")))
  377. (defvar org-display-table nil
  378. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  379. (defgroup org-keywords nil
  380. "Keywords in Org-mode."
  381. :tag "Org Keywords"
  382. :group 'org)
  383. (defcustom org-deadline-string "DEADLINE:"
  384. "String to mark deadline entries.
  385. A deadline is this string, followed by a time stamp. Should be a word,
  386. terminated by a colon. You can insert a schedule keyword and
  387. a timestamp with \\[org-deadline].
  388. Changes become only effective after restarting Emacs."
  389. :group 'org-keywords
  390. :type 'string)
  391. (defcustom org-scheduled-string "SCHEDULED:"
  392. "String to mark scheduled TODO entries.
  393. A schedule is this string, followed by a time stamp. Should be a word,
  394. terminated by a colon. You can insert a schedule keyword and
  395. a timestamp with \\[org-schedule].
  396. Changes become only effective after restarting Emacs."
  397. :group 'org-keywords
  398. :type 'string)
  399. (defcustom org-closed-string "CLOSED:"
  400. "String used as the prefix for timestamps logging closing a TODO entry."
  401. :group 'org-keywords
  402. :type 'string)
  403. (defcustom org-clock-string "CLOCK:"
  404. "String used as prefix for timestamps clocking work hours on an item."
  405. :group 'org-keywords
  406. :type 'string)
  407. (defcustom org-comment-string "COMMENT"
  408. "Entries starting with this keyword will never be exported.
  409. An entry can be toggled between COMMENT and normal with
  410. \\[org-toggle-comment].
  411. Changes become only effective after restarting Emacs."
  412. :group 'org-keywords
  413. :type 'string)
  414. (defcustom org-quote-string "QUOTE"
  415. "Entries starting with this keyword will be exported in fixed-width font.
  416. Quoting applies only to the text in the entry following the headline, and does
  417. not extend beyond the next headline, even if that is lower level.
  418. An entry can be toggled between QUOTE and normal with
  419. \\[org-toggle-fixed-width-section]."
  420. :group 'org-keywords
  421. :type 'string)
  422. (defconst org-repeat-re
  423. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  424. "Regular expression for specifying repeated events.
  425. After a match, group 1 contains the repeat expression.")
  426. (defgroup org-structure nil
  427. "Options concerning the general structure of Org-mode files."
  428. :tag "Org Structure"
  429. :group 'org)
  430. (defgroup org-reveal-location nil
  431. "Options about how to make context of a location visible."
  432. :tag "Org Reveal Location"
  433. :group 'org-structure)
  434. (defconst org-context-choice
  435. '(choice
  436. (const :tag "Always" t)
  437. (const :tag "Never" nil)
  438. (repeat :greedy t :tag "Individual contexts"
  439. (cons
  440. (choice :tag "Context"
  441. (const agenda)
  442. (const org-goto)
  443. (const occur-tree)
  444. (const tags-tree)
  445. (const link-search)
  446. (const mark-goto)
  447. (const bookmark-jump)
  448. (const isearch)
  449. (const default))
  450. (boolean))))
  451. "Contexts for the reveal options.")
  452. (defcustom org-show-hierarchy-above '((default . t))
  453. "Non-nil means, show full hierarchy when revealing a location.
  454. Org-mode often shows locations in an org-mode file which might have
  455. been invisible before. When this is set, the hierarchy of headings
  456. above the exposed location is shown.
  457. Turning this off for example for sparse trees makes them very compact.
  458. Instead of t, this can also be an alist specifying this option for different
  459. contexts. Valid contexts are
  460. agenda when exposing an entry from the agenda
  461. org-goto when using the command `org-goto' on key C-c C-j
  462. occur-tree when using the command `org-occur' on key C-c /
  463. tags-tree when constructing a sparse tree based on tags matches
  464. link-search when exposing search matches associated with a link
  465. mark-goto when exposing the jump goal of a mark
  466. bookmark-jump when exposing a bookmark location
  467. isearch when exiting from an incremental search
  468. default default for all contexts not set explicitly"
  469. :group 'org-reveal-location
  470. :type org-context-choice)
  471. (defcustom org-show-following-heading '((default . nil))
  472. "Non-nil means, show following heading when revealing a location.
  473. Org-mode often shows locations in an org-mode file which might have
  474. been invisible before. When this is set, the heading following the
  475. match is shown.
  476. Turning this off for example for sparse trees makes them very compact,
  477. but makes it harder to edit the location of the match. In such a case,
  478. use the command \\[org-reveal] to show more context.
  479. Instead of t, this can also be an alist specifying this option for different
  480. contexts. See `org-show-hierarchy-above' for valid contexts."
  481. :group 'org-reveal-location
  482. :type org-context-choice)
  483. (defcustom org-show-siblings '((default . nil) (isearch t))
  484. "Non-nil means, show all sibling heading when revealing a location.
  485. Org-mode often shows locations in an org-mode file which might have
  486. been invisible before. When this is set, the sibling of the current entry
  487. heading are all made visible. If `org-show-hierarchy-above' is t,
  488. the same happens on each level of the hierarchy above the current entry.
  489. By default this is on for the isearch context, off for all other contexts.
  490. Turning this off for example for sparse trees makes them very compact,
  491. but makes it harder to edit the location of the match. In such a case,
  492. use the command \\[org-reveal] to show more context.
  493. Instead of t, this can also be an alist specifying this option for different
  494. contexts. See `org-show-hierarchy-above' for valid contexts."
  495. :group 'org-reveal-location
  496. :type org-context-choice)
  497. (defcustom org-show-entry-below '((default . nil))
  498. "Non-nil means, show the entry below a headline when revealing a location.
  499. Org-mode often shows locations in an org-mode file which might have
  500. been invisible before. When this is set, the text below the headline that is
  501. exposed is also shown.
  502. By default this is off for all contexts.
  503. Instead of t, this can also be an alist specifying this option for different
  504. contexts. See `org-show-hierarchy-above' for valid contexts."
  505. :group 'org-reveal-location
  506. :type org-context-choice)
  507. (defcustom org-indirect-buffer-display 'other-window
  508. "How should indirect tree buffers be displayed?
  509. This applies to indirect buffers created with the commands
  510. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  511. Valid values are:
  512. current-window Display in the current window
  513. other-window Just display in another window.
  514. dedicated-frame Create one new frame, and re-use it each time.
  515. new-frame Make a new frame each time. Note that in this case
  516. previously-made indirect buffers are kept, and you need to
  517. kill these buffers yourself."
  518. :group 'org-structure
  519. :group 'org-agenda-windows
  520. :type '(choice
  521. (const :tag "In current window" current-window)
  522. (const :tag "In current frame, other window" other-window)
  523. (const :tag "Each time a new frame" new-frame)
  524. (const :tag "One dedicated frame" dedicated-frame)))
  525. (defcustom org-use-speed-commands nil
  526. "Non-nil means, activate single letter commands at beginning of a headline.
  527. This may also be a function to test for appropriate locations where speed
  528. commands should be active."
  529. :group 'org-structure
  530. :type '(choice
  531. (const :tag "Never" nil)
  532. (const :tag "At beginning of headline stars" t)
  533. (function)))
  534. (defcustom org-speed-commands-user nil
  535. "Alist of additional speed commands.
  536. This list will be checked before `org-speed-commands-default'
  537. when the variable `org-use-speed-commands' is non-nil
  538. and when the cursor is at the beginning of a headline.
  539. The car if each entry is a string with a single letter, which must
  540. be assigned to `self-insert-command' in the global map.
  541. The cdr is either a command to be called interactively, a function
  542. to be called, or a form to be evaluated."
  543. :group 'org-structure
  544. :type '(repeat
  545. (cons
  546. (string "Command letter")
  547. (choice
  548. (function)
  549. (sexp)))))
  550. (defgroup org-cycle nil
  551. "Options concerning visibility cycling in Org-mode."
  552. :tag "Org Cycle"
  553. :group 'org-structure)
  554. (defcustom org-cycle-skip-children-state-if-no-children t
  555. "Non-nil means, skip CHILDREN state in entries that don't have any."
  556. :group 'org-cycle
  557. :type 'boolean)
  558. (defcustom org-cycle-max-level nil
  559. "Maximum level which should still be subject to visibility cycling.
  560. Levels higher than this will, for cycling, be treated as text, not a headline.
  561. When `org-odd-levels-only' is set, a value of N in this variable actually
  562. means 2N-1 stars as the limiting headline.
  563. When nil, cycle all levels.
  564. Note that the limiting level of cycling is also influenced by
  565. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  566. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  567. than its value."
  568. :group 'org-cycle
  569. :type '(choice
  570. (const :tag "No limit" nil)
  571. (integer :tag "Maximum level")))
  572. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  573. "Names of drawers. Drawers are not opened by cycling on the headline above.
  574. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  575. this:
  576. :DRAWERNAME:
  577. .....
  578. :END:
  579. The drawer \"PROPERTIES\" is special for capturing properties through
  580. the property API.
  581. Drawers can be defined on the per-file basis with a line like:
  582. #+DRAWERS: HIDDEN STATE PROPERTIES"
  583. :group 'org-structure
  584. :group 'org-cycle
  585. :type '(repeat (string :tag "Drawer Name")))
  586. (defcustom org-hide-block-startup nil
  587. "Non-nil means, , entering Org-mode will fold all blocks.
  588. This can also be set in on a per-file basis with
  589. #+STARTUP: hideblocks
  590. #+STARTUP: showblocks"
  591. :group 'org-startup
  592. :group 'org-cycle
  593. :type 'boolean)
  594. (defcustom org-cycle-global-at-bob nil
  595. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  596. This makes it possible to do global cycling without having to use S-TAB or
  597. C-u TAB. For this special case to work, the first line of the buffer
  598. must not be a headline - it may be empty or some other text. When used in
  599. this way, `org-cycle-hook' is disables temporarily, to make sure the
  600. cursor stays at the beginning of the buffer.
  601. When this option is nil, don't do anything special at the beginning
  602. of the buffer."
  603. :group 'org-cycle
  604. :type 'boolean)
  605. (defcustom org-cycle-level-after-item/entry-creation t
  606. "Non-nil means, cycle entry level or item indentation in new empty entries.
  607. When the cursor is at the end of an empty headline, i.e with only stars
  608. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  609. and then all possible anchestor states, before returning to the original state.
  610. This makes data entry extremely fast: M-RET to create a new headline,
  611. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  612. When the cursor is at the end of an empty plain list item, one TAB will
  613. make it a subitem, two or more tabs will back up to make this an item
  614. higher up in the item hierarchy."
  615. :group 'org-cycle
  616. :type 'boolean)
  617. (defcustom org-cycle-emulate-tab t
  618. "Where should `org-cycle' emulate TAB.
  619. nil Never
  620. white Only in completely white lines
  621. whitestart Only at the beginning of lines, before the first non-white char
  622. t Everywhere except in headlines
  623. exc-hl-bol Everywhere except at the start of a headline
  624. If TAB is used in a place where it does not emulate TAB, the current subtree
  625. visibility is cycled."
  626. :group 'org-cycle
  627. :type '(choice (const :tag "Never" nil)
  628. (const :tag "Only in completely white lines" white)
  629. (const :tag "Before first char in a line" whitestart)
  630. (const :tag "Everywhere except in headlines" t)
  631. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  632. ))
  633. (defcustom org-cycle-separator-lines 2
  634. "Number of empty lines needed to keep an empty line between collapsed trees.
  635. If you leave an empty line between the end of a subtree and the following
  636. headline, this empty line is hidden when the subtree is folded.
  637. Org-mode will leave (exactly) one empty line visible if the number of
  638. empty lines is equal or larger to the number given in this variable.
  639. So the default 2 means, at least 2 empty lines after the end of a subtree
  640. are needed to produce free space between a collapsed subtree and the
  641. following headline.
  642. If the number is negative, and the number of empty lines is at least -N,
  643. all empty lines are shown.
  644. Special case: when 0, never leave empty lines in collapsed view."
  645. :group 'org-cycle
  646. :type 'integer)
  647. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  648. (defcustom org-pre-cycle-hook nil
  649. "Hook that is run before visibility cycling is happening.
  650. The function(s) in this hook must accept a single argument which indicates
  651. the new state that will be set right after running this hook. The
  652. argument is a symbol. Before a global state change, it can have the values
  653. `overview', `content', or `all'. Before a local state change, it can have
  654. the values `folded', `children', or `subtree'."
  655. :group 'org-cycle
  656. :type 'hook)
  657. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  658. org-cycle-hide-drawers
  659. org-cycle-show-empty-lines
  660. org-optimize-window-after-visibility-change)
  661. "Hook that is run after `org-cycle' has changed the buffer visibility.
  662. The function(s) in this hook must accept a single argument which indicates
  663. the new state that was set by the most recent `org-cycle' command. The
  664. argument is a symbol. After a global state change, it can have the values
  665. `overview', `content', or `all'. After a local state change, it can have
  666. the values `folded', `children', or `subtree'."
  667. :group 'org-cycle
  668. :type 'hook)
  669. (defgroup org-edit-structure nil
  670. "Options concerning structure editing in Org-mode."
  671. :tag "Org Edit Structure"
  672. :group 'org-structure)
  673. (defcustom org-odd-levels-only nil
  674. "Non-nil means, skip even levels and only use odd levels for the outline.
  675. This has the effect that two stars are being added/taken away in
  676. promotion/demotion commands. It also influences how levels are
  677. handled by the exporters.
  678. Changing it requires restart of `font-lock-mode' to become effective
  679. for fontification also in regions already fontified.
  680. You may also set this on a per-file basis by adding one of the following
  681. lines to the buffer:
  682. #+STARTUP: odd
  683. #+STARTUP: oddeven"
  684. :group 'org-edit-structure
  685. :group 'org-font-lock
  686. :type 'boolean)
  687. (defcustom org-adapt-indentation t
  688. "Non-nil means, adapt indentation to outline node level.
  689. When this variable is set, Org assumes that you write outlines by
  690. indenting text in each node to align with the headline (after the stars).
  691. The following issues are influenced by this variable:
  692. - When this is set and the *entire* text in an entry is indented, the
  693. indentation is increased by one space in a demotion command, and
  694. decreased by one in a promotion command. If any line in the entry
  695. body starts with text at column 0, indentation is not changed at all.
  696. - Property drawers and planning information is inserted indented when
  697. this variable s set. When nil, they will not be indented.
  698. - TAB indents a line relative to context. The lines below a headline
  699. will be indented when this variable is set.
  700. Note that this is all about true indentation, by adding and removing
  701. space characters. See also `org-indent.el' which does level-dependent
  702. indentation in a virtual way, i.e. at display time in Emacs."
  703. :group 'org-edit-structure
  704. :type 'boolean)
  705. (defcustom org-special-ctrl-a/e nil
  706. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  707. When t, `C-a' will bring back the cursor to the beginning of the
  708. headline text, i.e. after the stars and after a possible TODO keyword.
  709. In an item, this will be the position after the bullet.
  710. When the cursor is already at that position, another `C-a' will bring
  711. it to the beginning of the line.
  712. `C-e' will jump to the end of the headline, ignoring the presence of tags
  713. in the headline. A second `C-e' will then jump to the true end of the
  714. line, after any tags. This also means that, when this variable is
  715. non-nil, `C-e' also will never jump beyond the end of the heading of a
  716. folded section, i.e. not after the ellipses.
  717. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  718. going to the true line boundary first. Only a directly following, identical
  719. keypress will bring the cursor to the special positions.
  720. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  721. set separately."
  722. :group 'org-edit-structure
  723. :type '(choice
  724. (const :tag "off" nil)
  725. (const :tag "on: after stars/bullet and before tags first" t)
  726. (const :tag "reversed: true line boundary first" reversed)
  727. (cons :tag "Set C-a and C-e separately"
  728. (choice :tag "Special C-a"
  729. (const :tag "off" nil)
  730. (const :tag "on: after stars/bullet first" t)
  731. (const :tag "reversed: before stars/bullet first" reversed))
  732. (choice :tag "Special C-e"
  733. (const :tag "off" nil)
  734. (const :tag "on: before tags first" t)
  735. (const :tag "reversed: after tags first" reversed)))))
  736. (if (fboundp 'defvaralias)
  737. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  738. (defcustom org-special-ctrl-k nil
  739. "Non-nil means `C-k' will behave specially in headlines.
  740. When nil, `C-k' will call the default `kill-line' command.
  741. When t, the following will happen while the cursor is in the headline:
  742. - When the cursor is at the beginning of a headline, kill the entire
  743. line and possible the folded subtree below the line.
  744. - When in the middle of the headline text, kill the headline up to the tags.
  745. - When after the headline text, kill the tags."
  746. :group 'org-edit-structure
  747. :type 'boolean)
  748. (defcustom org-yank-folded-subtrees t
  749. "Non-nil means, when yanking subtrees, fold them.
  750. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  751. it starts with a heading and all other headings in it are either children
  752. or siblings, then fold all the subtrees. However, do this only if no
  753. text after the yank would be swallowed into a folded tree by this action."
  754. :group 'org-edit-structure
  755. :type 'boolean)
  756. (defcustom org-yank-adjusted-subtrees nil
  757. "Non-nil means, when yanking subtrees, adjust the level.
  758. With this setting, `org-paste-subtree' is used to insert the subtree, see
  759. this function for details."
  760. :group 'org-edit-structure
  761. :type 'boolean)
  762. (defcustom org-M-RET-may-split-line '((default . t))
  763. "Non-nil means, M-RET will split the line at the cursor position.
  764. When nil, it will go to the end of the line before making a
  765. new line.
  766. You may also set this option in a different way for different
  767. contexts. Valid contexts are:
  768. headline when creating a new headline
  769. item when creating a new item
  770. table in a table field
  771. default the value to be used for all contexts not explicitly
  772. customized"
  773. :group 'org-structure
  774. :group 'org-table
  775. :type '(choice
  776. (const :tag "Always" t)
  777. (const :tag "Never" nil)
  778. (repeat :greedy t :tag "Individual contexts"
  779. (cons
  780. (choice :tag "Context"
  781. (const headline)
  782. (const item)
  783. (const table)
  784. (const default))
  785. (boolean)))))
  786. (defcustom org-insert-heading-respect-content nil
  787. "Non-nil means, insert new headings after the current subtree.
  788. When nil, the new heading is created directly after the current line.
  789. The commands \\[org-insert-heading-respect-content] and
  790. \\[org-insert-todo-heading-respect-content] turn this variable on
  791. for the duration of the command."
  792. :group 'org-structure
  793. :type 'boolean)
  794. (defcustom org-blank-before-new-entry '((heading . auto)
  795. (plain-list-item . auto))
  796. "Should `org-insert-heading' leave a blank line before new heading/item?
  797. The value is an alist, with `heading' and `plain-list-item' as car,
  798. and a boolean flag as cdr. For plain lists, if the variable
  799. `org-empty-line-terminates-plain-lists' is set, the setting here
  800. is ignored and no empty line is inserted, to keep the list in tact."
  801. :group 'org-edit-structure
  802. :type '(list
  803. (cons (const heading)
  804. (choice (const :tag "Never" nil)
  805. (const :tag "Always" t)
  806. (const :tag "Auto" auto)))
  807. (cons (const plain-list-item)
  808. (choice (const :tag "Never" nil)
  809. (const :tag "Always" t)
  810. (const :tag "Auto" auto)))))
  811. (defcustom org-insert-heading-hook nil
  812. "Hook being run after inserting a new heading."
  813. :group 'org-edit-structure
  814. :type 'hook)
  815. (defcustom org-enable-fixed-width-editor t
  816. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  817. This currently only means, they are never auto-wrapped.
  818. When nil, such lines will be treated like ordinary lines.
  819. See also the QUOTE keyword."
  820. :group 'org-edit-structure
  821. :type 'boolean)
  822. (defcustom org-goto-auto-isearch t
  823. "Non-nil means, typing characters in org-goto starts incremental search."
  824. :group 'org-edit-structure
  825. :type 'boolean)
  826. (defgroup org-sparse-trees nil
  827. "Options concerning sparse trees in Org-mode."
  828. :tag "Org Sparse Trees"
  829. :group 'org-structure)
  830. (defcustom org-highlight-sparse-tree-matches t
  831. "Non-nil means, highlight all matches that define a sparse tree.
  832. The highlights will automatically disappear the next time the buffer is
  833. changed by an edit command."
  834. :group 'org-sparse-trees
  835. :type 'boolean)
  836. (defcustom org-remove-highlights-with-change t
  837. "Non-nil means, any change to the buffer will remove temporary highlights.
  838. Such highlights are created by `org-occur' and `org-clock-display'.
  839. When nil, `C-c C-c needs to be used to get rid of the highlights.
  840. The highlights created by `org-preview-latex-fragment' always need
  841. `C-c C-c' to be removed."
  842. :group 'org-sparse-trees
  843. :group 'org-time
  844. :type 'boolean)
  845. (defcustom org-occur-hook '(org-first-headline-recenter)
  846. "Hook that is run after `org-occur' has constructed a sparse tree.
  847. This can be used to recenter the window to show as much of the structure
  848. as possible."
  849. :group 'org-sparse-trees
  850. :type 'hook)
  851. (defgroup org-imenu-and-speedbar nil
  852. "Options concerning imenu and speedbar in Org-mode."
  853. :tag "Org Imenu and Speedbar"
  854. :group 'org-structure)
  855. (defcustom org-imenu-depth 2
  856. "The maximum level for Imenu access to Org-mode headlines.
  857. This also applied for speedbar access."
  858. :group 'org-imenu-and-speedbar
  859. :type 'integer)
  860. (defgroup org-table nil
  861. "Options concerning tables in Org-mode."
  862. :tag "Org Table"
  863. :group 'org)
  864. (defcustom org-enable-table-editor 'optimized
  865. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  866. When nil, such lines will be treated like ordinary lines.
  867. When equal to the symbol `optimized', the table editor will be optimized to
  868. do the following:
  869. - Automatic overwrite mode in front of whitespace in table fields.
  870. This makes the structure of the table stay in tact as long as the edited
  871. field does not exceed the column width.
  872. - Minimize the number of realigns. Normally, the table is aligned each time
  873. TAB or RET are pressed to move to another field. With optimization this
  874. happens only if changes to a field might have changed the column width.
  875. Optimization requires replacing the functions `self-insert-command',
  876. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  877. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  878. very good at guessing when a re-align will be necessary, but you can always
  879. force one with \\[org-ctrl-c-ctrl-c].
  880. If you would like to use the optimized version in Org-mode, but the
  881. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  882. This variable can be used to turn on and off the table editor during a session,
  883. but in order to toggle optimization, a restart is required.
  884. See also the variable `org-table-auto-blank-field'."
  885. :group 'org-table
  886. :type '(choice
  887. (const :tag "off" nil)
  888. (const :tag "on" t)
  889. (const :tag "on, optimized" optimized)))
  890. (defcustom org-self-insert-cluster-for-undo t
  891. "Non-nil means cluster self-insert commands for undo when possible.
  892. If this is set, then, like in the Emacs command loop, 20 consecutive
  893. characters will be undone together.
  894. This is configurable, because there is some impact on typing performance."
  895. :group 'org-table
  896. :type 'boolean)
  897. (defcustom org-table-tab-recognizes-table.el t
  898. "Non-nil means, TAB will automatically notice a table.el table.
  899. When it sees such a table, it moves point into it and - if necessary -
  900. calls `table-recognize-table'."
  901. :group 'org-table-editing
  902. :type 'boolean)
  903. (defgroup org-link nil
  904. "Options concerning links in Org-mode."
  905. :tag "Org Link"
  906. :group 'org)
  907. (defvar org-link-abbrev-alist-local nil
  908. "Buffer-local version of `org-link-abbrev-alist', which see.
  909. The value of this is taken from the #+LINK lines.")
  910. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  911. (defcustom org-link-abbrev-alist nil
  912. "Alist of link abbreviations.
  913. The car of each element is a string, to be replaced at the start of a link.
  914. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  915. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  916. [[linkkey:tag][description]]
  917. The 'linkkey' must be a word word, starting with a letter, followed
  918. by letters, numbers, '-' or '_'.
  919. If REPLACE is a string, the tag will simply be appended to create the link.
  920. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  921. the placeholder \"%h\" will cause a url-encoded version of the tag to
  922. be inserted at that point (see the function `url-hexify-string').
  923. REPLACE may also be a function that will be called with the tag as the
  924. only argument to create the link, which should be returned as a string.
  925. See the manual for examples."
  926. :group 'org-link
  927. :type '(repeat
  928. (cons
  929. (string :tag "Protocol")
  930. (choice
  931. (string :tag "Format")
  932. (function)))))
  933. (defcustom org-descriptive-links t
  934. "Non-nil means, hide link part and only show description of bracket links.
  935. Bracket links are like [[link][description]]. This variable sets the initial
  936. state in new org-mode buffers. The setting can then be toggled on a
  937. per-buffer basis from the Org->Hyperlinks menu."
  938. :group 'org-link
  939. :type 'boolean)
  940. (defcustom org-link-file-path-type 'adaptive
  941. "How the path name in file links should be stored.
  942. Valid values are:
  943. relative Relative to the current directory, i.e. the directory of the file
  944. into which the link is being inserted.
  945. absolute Absolute path, if possible with ~ for home directory.
  946. noabbrev Absolute path, no abbreviation of home directory.
  947. adaptive Use relative path for files in the current directory and sub-
  948. directories of it. For other files, use an absolute path."
  949. :group 'org-link
  950. :type '(choice
  951. (const relative)
  952. (const absolute)
  953. (const noabbrev)
  954. (const adaptive)))
  955. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  956. "Types of links that should be activated in Org-mode files.
  957. This is a list of symbols, each leading to the activation of a certain link
  958. type. In principle, it does not hurt to turn on most link types - there may
  959. be a small gain when turning off unused link types. The types are:
  960. bracket The recommended [[link][description]] or [[link]] links with hiding.
  961. angular Links in angular brackets that may contain whitespace like
  962. <bbdb:Carsten Dominik>.
  963. plain Plain links in normal text, no whitespace, like http://google.com.
  964. radio Text that is matched by a radio target, see manual for details.
  965. tag Tag settings in a headline (link to tag search).
  966. date Time stamps (link to calendar).
  967. footnote Footnote labels.
  968. Changing this variable requires a restart of Emacs to become effective."
  969. :group 'org-link
  970. :type '(set :greedy t
  971. (const :tag "Double bracket links (new style)" bracket)
  972. (const :tag "Angular bracket links (old style)" angular)
  973. (const :tag "Plain text links" plain)
  974. (const :tag "Radio target matches" radio)
  975. (const :tag "Tags" tag)
  976. (const :tag "Timestamps" date)
  977. (const :tag "Footnotes" footnote)))
  978. (defcustom org-make-link-description-function nil
  979. "Function to use to generate link descriptions from links. If
  980. nil the link location will be used. This function must take two
  981. parameters; the first is the link and the second the description
  982. org-insert-link has generated, and should return the description
  983. to use."
  984. :group 'org-link
  985. :type 'function)
  986. (defgroup org-link-store nil
  987. "Options concerning storing links in Org-mode."
  988. :tag "Org Store Link"
  989. :group 'org-link)
  990. (defcustom org-email-link-description-format "Email %c: %.30s"
  991. "Format of the description part of a link to an email or usenet message.
  992. The following %-escapes will be replaced by corresponding information:
  993. %F full \"From\" field
  994. %f name, taken from \"From\" field, address if no name
  995. %T full \"To\" field
  996. %t first name in \"To\" field, address if no name
  997. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  998. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  999. %s subject
  1000. %m message-id.
  1001. You may use normal field width specification between the % and the letter.
  1002. This is for example useful to limit the length of the subject.
  1003. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1004. :group 'org-link-store
  1005. :type 'string)
  1006. (defcustom org-from-is-user-regexp
  1007. (let (r1 r2)
  1008. (when (and user-mail-address (not (string= user-mail-address "")))
  1009. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1010. (when (and user-full-name (not (string= user-full-name "")))
  1011. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1012. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1013. "Regexp matched against the \"From:\" header of an email or usenet message.
  1014. It should match if the message is from the user him/herself."
  1015. :group 'org-link-store
  1016. :type 'regexp)
  1017. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1018. "Non-nil means, storing a link to an Org file will use entry IDs.
  1019. Note that before this variable is even considered, org-id must be loaded,
  1020. so please customize `org-modules' and turn it on.
  1021. The variable can have the following values:
  1022. t Create an ID if needed to make a link to the current entry.
  1023. create-if-interactive
  1024. If `org-store-link' is called directly (interactively, as a user
  1025. command), do create an ID to support the link. But when doing the
  1026. job for remember, only use the ID if it already exists. The
  1027. purpose of this setting is to avoid proliferation of unwanted
  1028. IDs, just because you happen to be in an Org file when you
  1029. call `org-remember' that automatically and preemptively
  1030. creates a link. If you do want to get an ID link in a remember
  1031. template to an entry not having an ID, create it first by
  1032. explicitly creating a link to it, using `C-c C-l' first.
  1033. create-if-interactive-and-no-custom-id
  1034. Like create-if-interactive, but do not create an ID if there is
  1035. a CUSTOM_ID property defined in the entry. This is the default.
  1036. use-existing
  1037. Use existing ID, do not create one.
  1038. nil Never use an ID to make a link, instead link using a text search for
  1039. the headline text."
  1040. :group 'org-link-store
  1041. :type '(choice
  1042. (const :tag "Create ID to make link" t)
  1043. (const :tag "Create if storing link interactively"
  1044. create-if-interactive)
  1045. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1046. create-if-interactive-and-no-custom-id)
  1047. (const :tag "Only use existing" use-existing)
  1048. (const :tag "Do not use ID to create link" nil)))
  1049. (defcustom org-context-in-file-links t
  1050. "Non-nil means, file links from `org-store-link' contain context.
  1051. A search string will be added to the file name with :: as separator and
  1052. used to find the context when the link is activated by the command
  1053. `org-open-at-point'.
  1054. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1055. negates this setting for the duration of the command."
  1056. :group 'org-link-store
  1057. :type 'boolean)
  1058. (defcustom org-keep-stored-link-after-insertion nil
  1059. "Non-nil means, keep link in list for entire session.
  1060. The command `org-store-link' adds a link pointing to the current
  1061. location to an internal list. These links accumulate during a session.
  1062. The command `org-insert-link' can be used to insert links into any
  1063. Org-mode file (offering completion for all stored links). When this
  1064. option is nil, every link which has been inserted once using \\[org-insert-link]
  1065. will be removed from the list, to make completing the unused links
  1066. more efficient."
  1067. :group 'org-link-store
  1068. :type 'boolean)
  1069. (defgroup org-link-follow nil
  1070. "Options concerning following links in Org-mode."
  1071. :tag "Org Follow Link"
  1072. :group 'org-link)
  1073. (defcustom org-link-translation-function nil
  1074. "Function to translate links with different syntax to Org syntax.
  1075. This can be used to translate links created for example by the Planner
  1076. or emacs-wiki packages to Org syntax.
  1077. The function must accept two parameters, a TYPE containing the link
  1078. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1079. which is everything after the link protocol. It should return a cons
  1080. with possibly modified values of type and path.
  1081. Org contains a function for this, so if you set this variable to
  1082. `org-translate-link-from-planner', you should be able follow many
  1083. links created by planner."
  1084. :group 'org-link-follow
  1085. :type 'function)
  1086. (defcustom org-follow-link-hook nil
  1087. "Hook that is run after a link has been followed."
  1088. :group 'org-link-follow
  1089. :type 'hook)
  1090. (defcustom org-tab-follows-link nil
  1091. "Non-nil means, on links TAB will follow the link.
  1092. Needs to be set before org.el is loaded.
  1093. This really should not be used, it does not make sense, and the
  1094. implementation is bad."
  1095. :group 'org-link-follow
  1096. :type 'boolean)
  1097. (defcustom org-return-follows-link nil
  1098. "Non-nil means, on links RET will follow the link.
  1099. Needs to be set before org.el is loaded."
  1100. :group 'org-link-follow
  1101. :type 'boolean)
  1102. (defcustom org-mouse-1-follows-link
  1103. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1104. "Non-nil means, mouse-1 on a link will follow the link.
  1105. A longer mouse click will still set point. Does not work on XEmacs.
  1106. Needs to be set before org.el is loaded."
  1107. :group 'org-link-follow
  1108. :type 'boolean)
  1109. (defcustom org-mark-ring-length 4
  1110. "Number of different positions to be recorded in the ring
  1111. Changing this requires a restart of Emacs to work correctly."
  1112. :group 'org-link-follow
  1113. :type 'integer)
  1114. (defcustom org-link-frame-setup
  1115. '((vm . vm-visit-folder-other-frame)
  1116. (gnus . gnus-other-frame)
  1117. (file . find-file-other-window))
  1118. "Setup the frame configuration for following links.
  1119. When following a link with Emacs, it may often be useful to display
  1120. this link in another window or frame. This variable can be used to
  1121. set this up for the different types of links.
  1122. For VM, use any of
  1123. `vm-visit-folder'
  1124. `vm-visit-folder-other-frame'
  1125. For Gnus, use any of
  1126. `gnus'
  1127. `gnus-other-frame'
  1128. `org-gnus-no-new-news'
  1129. For FILE, use any of
  1130. `find-file'
  1131. `find-file-other-window'
  1132. `find-file-other-frame'
  1133. For the calendar, use the variable `calendar-setup'.
  1134. For BBDB, it is currently only possible to display the matches in
  1135. another window."
  1136. :group 'org-link-follow
  1137. :type '(list
  1138. (cons (const vm)
  1139. (choice
  1140. (const vm-visit-folder)
  1141. (const vm-visit-folder-other-window)
  1142. (const vm-visit-folder-other-frame)))
  1143. (cons (const gnus)
  1144. (choice
  1145. (const gnus)
  1146. (const gnus-other-frame)
  1147. (const org-gnus-no-new-news)))
  1148. (cons (const file)
  1149. (choice
  1150. (const find-file)
  1151. (const find-file-other-window)
  1152. (const find-file-other-frame)))))
  1153. (defcustom org-display-internal-link-with-indirect-buffer nil
  1154. "Non-nil means, use indirect buffer to display infile links.
  1155. Activating internal links (from one location in a file to another location
  1156. in the same file) normally just jumps to the location. When the link is
  1157. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1158. another window. When this option is set, the other window actually displays
  1159. an indirect buffer clone of the current buffer, to avoid any visibility
  1160. changes to the current buffer."
  1161. :group 'org-link-follow
  1162. :type 'boolean)
  1163. (defcustom org-open-non-existing-files nil
  1164. "Non-nil means, `org-open-file' will open non-existing files.
  1165. When nil, an error will be generated.
  1166. This variable applies only to external applications because they
  1167. might choke on non-existing files. If the link is to a file that
  1168. will be opened in Emacs, the variable is ignored."
  1169. :group 'org-link-follow
  1170. :type 'boolean)
  1171. (defcustom org-open-directory-means-index-dot-org nil
  1172. "Non-nil means, a link to a directory really means to index.org.
  1173. When nil, following a directory link will run dired or open a finder/explorer
  1174. window on that directory."
  1175. :group 'org-link-follow
  1176. :type 'boolean)
  1177. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1178. "Function and arguments to call for following mailto links.
  1179. This is a list with the first element being a lisp function, and the
  1180. remaining elements being arguments to the function. In string arguments,
  1181. %a will be replaced by the address, and %s will be replaced by the subject
  1182. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1183. :group 'org-link-follow
  1184. :type '(choice
  1185. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1186. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1187. (const :tag "message-mail" (message-mail "%a" "%s"))
  1188. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1189. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1190. "Non-nil means, ask for confirmation before executing shell links.
  1191. Shell links can be dangerous: just think about a link
  1192. [[shell:rm -rf ~/*][Google Search]]
  1193. This link would show up in your Org-mode document as \"Google Search\",
  1194. but really it would remove your entire home directory.
  1195. Therefore we advise against setting this variable to nil.
  1196. Just change it to `y-or-n-p' if you want to confirm with a
  1197. single keystroke rather than having to type \"yes\"."
  1198. :group 'org-link-follow
  1199. :type '(choice
  1200. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1201. (const :tag "with y-or-n (faster)" y-or-n-p)
  1202. (const :tag "no confirmation (dangerous)" nil)))
  1203. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1204. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1205. Elisp links can be dangerous: just think about a link
  1206. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1207. This link would show up in your Org-mode document as \"Google Search\",
  1208. but really it would remove your entire home directory.
  1209. Therefore we advise against setting this variable to nil.
  1210. Just change it to `y-or-n-p' if you want to confirm with a
  1211. single keystroke rather than having to type \"yes\"."
  1212. :group 'org-link-follow
  1213. :type '(choice
  1214. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1215. (const :tag "with y-or-n (faster)" y-or-n-p)
  1216. (const :tag "no confirmation (dangerous)" nil)))
  1217. (defconst org-file-apps-defaults-gnu
  1218. '((remote . emacs)
  1219. (system . mailcap)
  1220. (t . mailcap))
  1221. "Default file applications on a UNIX or GNU/Linux system.
  1222. See `org-file-apps'.")
  1223. (defconst org-file-apps-defaults-macosx
  1224. '((remote . emacs)
  1225. (t . "open %s")
  1226. (system . "open %s")
  1227. ("ps.gz" . "gv %s")
  1228. ("eps.gz" . "gv %s")
  1229. ("dvi" . "xdvi %s")
  1230. ("fig" . "xfig %s"))
  1231. "Default file applications on a MacOS X system.
  1232. The system \"open\" is known as a default, but we use X11 applications
  1233. for some files for which the OS does not have a good default.
  1234. See `org-file-apps'.")
  1235. (defconst org-file-apps-defaults-windowsnt
  1236. (list
  1237. '(remote . emacs)
  1238. (cons t
  1239. (list (if (featurep 'xemacs)
  1240. 'mswindows-shell-execute
  1241. 'w32-shell-execute)
  1242. "open" 'file))
  1243. (cons 'system
  1244. (list (if (featurep 'xemacs)
  1245. 'mswindows-shell-execute
  1246. 'w32-shell-execute)
  1247. "open" 'file)))
  1248. "Default file applications on a Windows NT system.
  1249. The system \"open\" is used for most files.
  1250. See `org-file-apps'.")
  1251. (defcustom org-file-apps
  1252. '(
  1253. (auto-mode . emacs)
  1254. ("\\.mm\\'" . default)
  1255. ("\\.x?html?\\'" . default)
  1256. ("\\.pdf\\'" . default)
  1257. )
  1258. "External applications for opening `file:path' items in a document.
  1259. Org-mode uses system defaults for different file types, but
  1260. you can use this variable to set the application for a given file
  1261. extension. The entries in this list are cons cells where the car identifies
  1262. files and the cdr the corresponding command. Possible values for the
  1263. file identifier are
  1264. \"regex\" Regular expression matched against the file name. For backward
  1265. compatibility, this can also be a string with only alphanumeric
  1266. characters, which is then interpreted as an extension.
  1267. `directory' Matches a directory
  1268. `remote' Matches a remote file, accessible through tramp or efs.
  1269. Remote files most likely should be visited through Emacs
  1270. because external applications cannot handle such paths.
  1271. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1272. so all files Emacs knows how to handle. Using this with
  1273. command `emacs' will open most files in Emacs. Beware that this
  1274. will also open html files inside Emacs, unless you add
  1275. (\"html\" . default) to the list as well.
  1276. t Default for files not matched by any of the other options.
  1277. `system' The system command to open files, like `open' on Windows
  1278. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1279. that will be selected if you call `C-c C-o' with a double
  1280. `C-u C-u' prefix.
  1281. Possible values for the command are:
  1282. `emacs' The file will be visited by the current Emacs process.
  1283. `default' Use the default application for this file type, which is the
  1284. association for t in the list, most likely in the system-specific
  1285. part.
  1286. This can be used to overrule an unwanted setting in the
  1287. system-specific variable.
  1288. `system' Use the system command for opening files, like \"open\".
  1289. This command is specified by the entry whose car is `system'.
  1290. Most likely, the system-specific version of this variable
  1291. does define this command, but you can overrule/replace it
  1292. here.
  1293. string A command to be executed by a shell; %s will be replaced
  1294. by the path to the file.
  1295. sexp A Lisp form which will be evaluated. The file path will
  1296. be available in the Lisp variable `file'.
  1297. For more examples, see the system specific constants
  1298. `org-file-apps-defaults-macosx'
  1299. `org-file-apps-defaults-windowsnt'
  1300. `org-file-apps-defaults-gnu'."
  1301. :group 'org-link-follow
  1302. :type '(repeat
  1303. (cons (choice :value ""
  1304. (string :tag "Extension")
  1305. (const :tag "System command to open files" system)
  1306. (const :tag "Default for unrecognized files" t)
  1307. (const :tag "Remote file" remote)
  1308. (const :tag "Links to a directory" directory)
  1309. (const :tag "Any files that have Emacs modes"
  1310. auto-mode))
  1311. (choice :value ""
  1312. (const :tag "Visit with Emacs" emacs)
  1313. (const :tag "Use default" default)
  1314. (const :tag "Use the system command" system)
  1315. (string :tag "Command")
  1316. (sexp :tag "Lisp form")))))
  1317. (defgroup org-refile nil
  1318. "Options concerning refiling entries in Org-mode."
  1319. :tag "Org Refile"
  1320. :group 'org)
  1321. (defcustom org-directory "~/org"
  1322. "Directory with org files.
  1323. This is just a default location to look for Org files. There is no need
  1324. at all to put your files into this directory. It is only used in the
  1325. following situations:
  1326. 1. When a remember template specifies a target file that is not an
  1327. absolute path. The path will then be interpreted relative to
  1328. `org-directory'
  1329. 2. When a remember note is filed away in an interactive way (when exiting the
  1330. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1331. with `org-directory' as the default path."
  1332. :group 'org-refile
  1333. :group 'org-remember
  1334. :type 'directory)
  1335. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1336. "Default target for storing notes.
  1337. Used by the hooks for remember.el. This can be a string, or nil to mean
  1338. the value of `remember-data-file'.
  1339. You can set this on a per-template basis with the variable
  1340. `org-remember-templates'."
  1341. :group 'org-refile
  1342. :group 'org-remember
  1343. :type '(choice
  1344. (const :tag "Default from remember-data-file" nil)
  1345. file))
  1346. (defcustom org-goto-interface 'outline
  1347. "The default interface to be used for `org-goto'.
  1348. Allowed values are:
  1349. outline The interface shows an outline of the relevant file
  1350. and the correct heading is found by moving through
  1351. the outline or by searching with incremental search.
  1352. outline-path-completion Headlines in the current buffer are offered via
  1353. completion. This is the interface also used by
  1354. the refile command."
  1355. :group 'org-refile
  1356. :type '(choice
  1357. (const :tag "Outline" outline)
  1358. (const :tag "Outline-path-completion" outline-path-completion)))
  1359. (defcustom org-goto-max-level 5
  1360. "Maximum level to be considered when running org-goto with refile interface."
  1361. :group 'org-refile
  1362. :type 'integer)
  1363. (defcustom org-reverse-note-order nil
  1364. "Non-nil means, store new notes at the beginning of a file or entry.
  1365. When nil, new notes will be filed to the end of a file or entry.
  1366. This can also be a list with cons cells of regular expressions that
  1367. are matched against file names, and values."
  1368. :group 'org-remember
  1369. :group 'org-refile
  1370. :type '(choice
  1371. (const :tag "Reverse always" t)
  1372. (const :tag "Reverse never" nil)
  1373. (repeat :tag "By file name regexp"
  1374. (cons regexp boolean))))
  1375. (defcustom org-refile-targets nil
  1376. "Targets for refiling entries with \\[org-refile].
  1377. This is list of cons cells. Each cell contains:
  1378. - a specification of the files to be considered, either a list of files,
  1379. or a symbol whose function or variable value will be used to retrieve
  1380. a file name or a list of file names. If you use `org-agenda-files' for
  1381. that, all agenda files will be scanned for targets. Nil means, consider
  1382. headings in the current buffer.
  1383. - A specification of how to find candidate refile targets. This may be
  1384. any of:
  1385. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1386. This tag has to be present in all target headlines, inheritance will
  1387. not be considered.
  1388. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1389. todo keyword.
  1390. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1391. headlines that are refiling targets.
  1392. - a cons cell (:level . N). Any headline of level N is considered a target.
  1393. Note that, when `org-odd-levels-only' is set, level corresponds to
  1394. order in hierarchy, not to the number of stars.
  1395. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1396. Note that, when `org-odd-levels-only' is set, level corresponds to
  1397. order in hierarchy, not to the number of stars.
  1398. You can set the variable `org-refile-target-verify-function' to a function
  1399. to verify each headline found by the simple critery above.
  1400. When this variable is nil, all top-level headlines in the current buffer
  1401. are used, equivalent to the value `((nil . (:level . 1))'."
  1402. :group 'org-refile
  1403. :type '(repeat
  1404. (cons
  1405. (choice :value org-agenda-files
  1406. (const :tag "All agenda files" org-agenda-files)
  1407. (const :tag "Current buffer" nil)
  1408. (function) (variable) (file))
  1409. (choice :tag "Identify target headline by"
  1410. (cons :tag "Specific tag" (const :value :tag) (string))
  1411. (cons :tag "TODO keyword" (const :value :todo) (string))
  1412. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1413. (cons :tag "Level number" (const :value :level) (integer))
  1414. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1415. (defcustom org-refile-target-verify-function nil
  1416. "Function to verify if the headline at point should be a refile target.
  1417. The function will be called without arguments, with point at the
  1418. beginning of the headline. It should return t and leave point
  1419. where it is if the headline is a valid target for refiling.
  1420. If the target should not be selected, the function must return nil.
  1421. In addition to this, it may move point to a place from where the search
  1422. should be continued. For example, the function may decide that the entire
  1423. subtree of the current entry should be excluded and move point to the end
  1424. of the subtree."
  1425. :group 'org-refile
  1426. :type 'function)
  1427. (defcustom org-refile-use-outline-path nil
  1428. "Non-nil means, provide refile targets as paths.
  1429. So a level 3 headline will be available as level1/level2/level3.
  1430. When the value is `file', also include the file name (without directory)
  1431. into the path. In this case, you can also stop the completion after
  1432. the file name, to get entries inserted as top level in the file.
  1433. When `full-file-path', include the full file path."
  1434. :group 'org-refile
  1435. :type '(choice
  1436. (const :tag "Not" nil)
  1437. (const :tag "Yes" t)
  1438. (const :tag "Start with file name" file)
  1439. (const :tag "Start with full file path" full-file-path)))
  1440. (defcustom org-outline-path-complete-in-steps t
  1441. "Non-nil means, complete the outline path in hierarchical steps.
  1442. When Org-mode uses the refile interface to select an outline path
  1443. \(see variable `org-refile-use-outline-path'), the completion of
  1444. the path can be done is a single go, or if can be done in steps down
  1445. the headline hierarchy. Going in steps is probably the best if you
  1446. do not use a special completion package like `ido' or `icicles'.
  1447. However, when using these packages, going in one step can be very
  1448. fast, while still showing the whole path to the entry."
  1449. :group 'org-refile
  1450. :type 'boolean)
  1451. (defcustom org-refile-allow-creating-parent-nodes nil
  1452. "Non-nil means, allow to create new nodes as refile targets.
  1453. New nodes are then created by adding \"/new node name\" to the completion
  1454. of an existing node. When the value of this variable is `confirm',
  1455. new node creation must be confirmed by the user (recommended)
  1456. When nil, the completion must match an existing entry.
  1457. Note that, if the new heading is not seen by the criteria
  1458. listed in `org-refile-targets', multiple instances of the same
  1459. heading would be created by trying again to file under the new
  1460. heading."
  1461. :group 'org-refile
  1462. :type '(choice
  1463. (const :tag "Never" nil)
  1464. (const :tag "Always" t)
  1465. (const :tag "Prompt for confirmation" confirm)))
  1466. (defgroup org-todo nil
  1467. "Options concerning TODO items in Org-mode."
  1468. :tag "Org TODO"
  1469. :group 'org)
  1470. (defgroup org-progress nil
  1471. "Options concerning Progress logging in Org-mode."
  1472. :tag "Org Progress"
  1473. :group 'org-time)
  1474. (defvar org-todo-interpretation-widgets
  1475. '(
  1476. (:tag "Sequence (cycling hits every state)" sequence)
  1477. (:tag "Type (cycling directly to DONE)" type))
  1478. "The available interpretation symbols for customizing
  1479. `org-todo-keywords'.
  1480. Interested libraries should add to this list.")
  1481. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1482. "List of TODO entry keyword sequences and their interpretation.
  1483. \\<org-mode-map>This is a list of sequences.
  1484. Each sequence starts with a symbol, either `sequence' or `type',
  1485. indicating if the keywords should be interpreted as a sequence of
  1486. action steps, or as different types of TODO items. The first
  1487. keywords are states requiring action - these states will select a headline
  1488. for inclusion into the global TODO list Org-mode produces. If one of
  1489. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1490. signify that no further action is necessary. If \"|\" is not found,
  1491. the last keyword is treated as the only DONE state of the sequence.
  1492. The command \\[org-todo] cycles an entry through these states, and one
  1493. additional state where no keyword is present. For details about this
  1494. cycling, see the manual.
  1495. TODO keywords and interpretation can also be set on a per-file basis with
  1496. the special #+SEQ_TODO and #+TYP_TODO lines.
  1497. Each keyword can optionally specify a character for fast state selection
  1498. \(in combination with the variable `org-use-fast-todo-selection')
  1499. and specifiers for state change logging, using the same syntax
  1500. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1501. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1502. indicates to record a time stamp each time this state is selected.
  1503. Each keyword may also specify if a timestamp or a note should be
  1504. recorded when entering or leaving the state, by adding additional
  1505. characters in the parenthesis after the keyword. This looks like this:
  1506. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1507. record only the time of the state change. With X and Y being either
  1508. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1509. Y when leaving the state if and only if the *target* state does not
  1510. define X. You may omit any of the fast-selection key or X or /Y,
  1511. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1512. For backward compatibility, this variable may also be just a list
  1513. of keywords - in this case the interpretation (sequence or type) will be
  1514. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1515. :group 'org-todo
  1516. :group 'org-keywords
  1517. :type '(choice
  1518. (repeat :tag "Old syntax, just keywords"
  1519. (string :tag "Keyword"))
  1520. (repeat :tag "New syntax"
  1521. (cons
  1522. (choice
  1523. :tag "Interpretation"
  1524. ;;Quick and dirty way to see
  1525. ;;`org-todo-interpretations'. This takes the
  1526. ;;place of item arguments
  1527. :convert-widget
  1528. (lambda (widget)
  1529. (widget-put widget
  1530. :args (mapcar
  1531. #'(lambda (x)
  1532. (widget-convert
  1533. (cons 'const x)))
  1534. org-todo-interpretation-widgets))
  1535. widget))
  1536. (repeat
  1537. (string :tag "Keyword"))))))
  1538. (defvar org-todo-keywords-1 nil
  1539. "All TODO and DONE keywords active in a buffer.")
  1540. (make-variable-buffer-local 'org-todo-keywords-1)
  1541. (defvar org-todo-keywords-for-agenda nil)
  1542. (defvar org-done-keywords-for-agenda nil)
  1543. (defvar org-drawers-for-agenda nil)
  1544. (defvar org-todo-keyword-alist-for-agenda nil)
  1545. (defvar org-tag-alist-for-agenda nil)
  1546. (defvar org-agenda-contributing-files nil)
  1547. (defvar org-not-done-keywords nil)
  1548. (make-variable-buffer-local 'org-not-done-keywords)
  1549. (defvar org-done-keywords nil)
  1550. (make-variable-buffer-local 'org-done-keywords)
  1551. (defvar org-todo-heads nil)
  1552. (make-variable-buffer-local 'org-todo-heads)
  1553. (defvar org-todo-sets nil)
  1554. (make-variable-buffer-local 'org-todo-sets)
  1555. (defvar org-todo-log-states nil)
  1556. (make-variable-buffer-local 'org-todo-log-states)
  1557. (defvar org-todo-kwd-alist nil)
  1558. (make-variable-buffer-local 'org-todo-kwd-alist)
  1559. (defvar org-todo-key-alist nil)
  1560. (make-variable-buffer-local 'org-todo-key-alist)
  1561. (defvar org-todo-key-trigger nil)
  1562. (make-variable-buffer-local 'org-todo-key-trigger)
  1563. (defcustom org-todo-interpretation 'sequence
  1564. "Controls how TODO keywords are interpreted.
  1565. This variable is in principle obsolete and is only used for
  1566. backward compatibility, if the interpretation of todo keywords is
  1567. not given already in `org-todo-keywords'. See that variable for
  1568. more information."
  1569. :group 'org-todo
  1570. :group 'org-keywords
  1571. :type '(choice (const sequence)
  1572. (const type)))
  1573. (defcustom org-use-fast-todo-selection t
  1574. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1575. This variable describes if and under what circumstances the cycling
  1576. mechanism for TODO keywords will be replaced by a single-key, direct
  1577. selection scheme.
  1578. When nil, fast selection is never used.
  1579. When the symbol `prefix', it will be used when `org-todo' is called with
  1580. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1581. in an agenda buffer.
  1582. When t, fast selection is used by default. In this case, the prefix
  1583. argument forces cycling instead.
  1584. In all cases, the special interface is only used if access keys have actually
  1585. been assigned by the user, i.e. if keywords in the configuration are followed
  1586. by a letter in parenthesis, like TODO(t)."
  1587. :group 'org-todo
  1588. :type '(choice
  1589. (const :tag "Never" nil)
  1590. (const :tag "By default" t)
  1591. (const :tag "Only with C-u C-c C-t" prefix)))
  1592. (defcustom org-provide-todo-statistics t
  1593. "Non-nil means, update todo statistics after insert and toggle.
  1594. ALL-HEADLINES means update todo statistics by including headlines
  1595. with no TODO keyword as well, counting them as not done.
  1596. A list of TODO keywords means the same, but skip keywords that are
  1597. not in this list.
  1598. When this is set, todo statistics is updated in the parent of the
  1599. current entry each time a todo state is changed."
  1600. :group 'org-todo
  1601. :type '(choice
  1602. (const :tag "Yes, only for TODO entries" t)
  1603. (const :tag "Yes, including all entries" 'all-headlines)
  1604. (repeat :tag "Yes, for TODOs in this list"
  1605. (string :tag "TODO keyword"))
  1606. (other :tag "No TODO statistics" nil)))
  1607. (defcustom org-hierarchical-todo-statistics t
  1608. "Non-nil means, TODO statistics covers just direct children.
  1609. When nil, all entries in the subtree are considered.
  1610. This has only an effect if `org-provide-todo-statistics' is set.
  1611. To set this to nil for only a single subtree, use a COOKIE_DATA
  1612. property and include the word \"recursive\" into the value."
  1613. :group 'org-todo
  1614. :type 'boolean)
  1615. (defcustom org-after-todo-state-change-hook nil
  1616. "Hook which is run after the state of a TODO item was changed.
  1617. The new state (a string with a TODO keyword, or nil) is available in the
  1618. Lisp variable `state'."
  1619. :group 'org-todo
  1620. :type 'hook)
  1621. (defvar org-blocker-hook nil
  1622. "Hook for functions that are allowed to block a state change.
  1623. Each function gets as its single argument a property list, see
  1624. `org-trigger-hook' for more information about this list.
  1625. If any of the functions in this hook returns nil, the state change
  1626. is blocked.")
  1627. (defvar org-trigger-hook nil
  1628. "Hook for functions that are triggered by a state change.
  1629. Each function gets as its single argument a property list with at least
  1630. the following elements:
  1631. (:type type-of-change :position pos-at-entry-start
  1632. :from old-state :to new-state)
  1633. Depending on the type, more properties may be present.
  1634. This mechanism is currently implemented for:
  1635. TODO state changes
  1636. ------------------
  1637. :type todo-state-change
  1638. :from previous state (keyword as a string), or nil, or a symbol
  1639. 'todo' or 'done', to indicate the general type of state.
  1640. :to new state, like in :from")
  1641. (defcustom org-enforce-todo-dependencies nil
  1642. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1643. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1644. be blocked if any prior sibling is not yet done.
  1645. Finally, if the parent is blocked because of ordered siblings of its own,
  1646. the child will also be blocked.
  1647. This variable needs to be set before org.el is loaded, and you need to
  1648. restart Emacs after a change to make the change effective. The only way
  1649. to change is while Emacs is running is through the customize interface."
  1650. :set (lambda (var val)
  1651. (set var val)
  1652. (if val
  1653. (add-hook 'org-blocker-hook
  1654. 'org-block-todo-from-children-or-siblings-or-parent)
  1655. (remove-hook 'org-blocker-hook
  1656. 'org-block-todo-from-children-or-siblings-or-parent)))
  1657. :group 'org-todo
  1658. :type 'boolean)
  1659. (defcustom org-enforce-todo-checkbox-dependencies nil
  1660. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1661. When this is nil, checkboxes have no influence on switching TODO states.
  1662. When non-nil, you first need to check off all check boxes before the TODO
  1663. entry can be switched to DONE.
  1664. This variable needs to be set before org.el is loaded, and you need to
  1665. restart Emacs after a change to make the change effective. The only way
  1666. to change is while Emacs is running is through the customize interface."
  1667. :set (lambda (var val)
  1668. (set var val)
  1669. (if val
  1670. (add-hook 'org-blocker-hook
  1671. 'org-block-todo-from-checkboxes)
  1672. (remove-hook 'org-blocker-hook
  1673. 'org-block-todo-from-checkboxes)))
  1674. :group 'org-todo
  1675. :type 'boolean)
  1676. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1677. "Non-nil means, inserting a TODO heading is treated as state change.
  1678. So when the command \\[org-insert-todo-heading] is used, state change
  1679. logging will apply if appropriate. When nil, the new TODO item will
  1680. be inserted directly, and no logging will take place."
  1681. :group 'org-todo
  1682. :type 'boolean)
  1683. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1684. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1685. This is the default behavior. However, setting this to nil allows a
  1686. convenient way to select a TODO state and bypass any logging associated
  1687. with that."
  1688. :group 'org-todo
  1689. :type 'boolean)
  1690. (defcustom org-todo-state-tags-triggers nil
  1691. "Tag changes that should be triggered by TODO state changes.
  1692. This is a list. Each entry is
  1693. (state-change (tag . flag) .......)
  1694. State-change can be a string with a state, and empty string to indicate the
  1695. state that has no TODO keyword, or it can be one of the symbols `todo'
  1696. or `done', meaning any not-done or done state, respectively."
  1697. :group 'org-todo
  1698. :group 'org-tags
  1699. :type '(repeat
  1700. (cons (choice :tag "When changing to"
  1701. (const :tag "Not-done state" todo)
  1702. (const :tag "Done state" done)
  1703. (string :tag "State"))
  1704. (repeat
  1705. (cons :tag "Tag action"
  1706. (string :tag "Tag")
  1707. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1708. (defcustom org-log-done nil
  1709. "Information to record when a task moves to the DONE state.
  1710. Possible values are:
  1711. nil Don't add anything, just change the keyword
  1712. time Add a time stamp to the task
  1713. note Prompt for a note and add it with template `org-log-note-headings'
  1714. This option can also be set with on a per-file-basis with
  1715. #+STARTUP: nologdone
  1716. #+STARTUP: logdone
  1717. #+STARTUP: lognotedone
  1718. You can have local logging settings for a subtree by setting the LOGGING
  1719. property to one or more of these keywords."
  1720. :group 'org-todo
  1721. :group 'org-progress
  1722. :type '(choice
  1723. (const :tag "No logging" nil)
  1724. (const :tag "Record CLOSED timestamp" time)
  1725. (const :tag "Record CLOSED timestamp with note." note)))
  1726. ;; Normalize old uses of org-log-done.
  1727. (cond
  1728. ((eq org-log-done t) (setq org-log-done 'time))
  1729. ((and (listp org-log-done) (memq 'done org-log-done))
  1730. (setq org-log-done 'note)))
  1731. (defcustom org-log-reschedule nil
  1732. "Information to record when the scheduling date of a tasks is modified.
  1733. Possible values are:
  1734. nil Don't add anything, just change the date
  1735. time Add a time stamp to the task
  1736. note Prompt for a note and add it with template `org-log-note-headings'
  1737. This option can also be set with on a per-file-basis with
  1738. #+STARTUP: nologreschedule
  1739. #+STARTUP: logreschedule
  1740. #+STARTUP: lognotereschedule"
  1741. :group 'org-todo
  1742. :group 'org-progress
  1743. :type '(choice
  1744. (const :tag "No logging" nil)
  1745. (const :tag "Record timestamp" time)
  1746. (const :tag "Record timestamp with note." note)))
  1747. (defcustom org-log-redeadline nil
  1748. "Information to record when the deadline date of a tasks is modified.
  1749. Possible values are:
  1750. nil Don't add anything, just change the date
  1751. time Add a time stamp to the task
  1752. note Prompt for a note and add it with template `org-log-note-headings'
  1753. This option can also be set with on a per-file-basis with
  1754. #+STARTUP: nologredeadline
  1755. #+STARTUP: logredeadline
  1756. #+STARTUP: lognoteredeadline
  1757. You can have local logging settings for a subtree by setting the LOGGING
  1758. property to one or more of these keywords."
  1759. :group 'org-todo
  1760. :group 'org-progress
  1761. :type '(choice
  1762. (const :tag "No logging" nil)
  1763. (const :tag "Record timestamp" time)
  1764. (const :tag "Record timestamp with note." note)))
  1765. (defcustom org-log-note-clock-out nil
  1766. "Non-nil means, record a note when clocking out of an item.
  1767. This can also be configured on a per-file basis by adding one of
  1768. the following lines anywhere in the buffer:
  1769. #+STARTUP: lognoteclock-out
  1770. #+STARTUP: nolognoteclock-out"
  1771. :group 'org-todo
  1772. :group 'org-progress
  1773. :type 'boolean)
  1774. (defcustom org-log-done-with-time t
  1775. "Non-nil means, the CLOSED time stamp will contain date and time.
  1776. When nil, only the date will be recorded."
  1777. :group 'org-progress
  1778. :type 'boolean)
  1779. (defcustom org-log-note-headings
  1780. '((done . "CLOSING NOTE %t")
  1781. (state . "State %-12s from %-12S %t")
  1782. (note . "Note taken on %t")
  1783. (reschedule . "Rescheduled from %S on %t")
  1784. (redeadline . "New deadline from %S on %t")
  1785. (clock-out . ""))
  1786. "Headings for notes added to entries.
  1787. The value is an alist, with the car being a symbol indicating the note
  1788. context, and the cdr is the heading to be used. The heading may also be the
  1789. empty string.
  1790. %t in the heading will be replaced by a time stamp.
  1791. %s will be replaced by the new TODO state, in double quotes.
  1792. %S will be replaced by the old TODO state, in double quotes.
  1793. %u will be replaced by the user name.
  1794. %U will be replaced by the full user name."
  1795. :group 'org-todo
  1796. :group 'org-progress
  1797. :type '(list :greedy t
  1798. (cons (const :tag "Heading when closing an item" done) string)
  1799. (cons (const :tag
  1800. "Heading when changing todo state (todo sequence only)"
  1801. state) string)
  1802. (cons (const :tag "Heading when just taking a note" note) string)
  1803. (cons (const :tag "Heading when clocking out" clock-out) string)
  1804. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1805. (cons (const :tag "Heading when changing deadline" redeadline) string)))
  1806. (unless (assq 'note org-log-note-headings)
  1807. (push '(note . "%t") org-log-note-headings))
  1808. (defcustom org-log-into-drawer nil
  1809. "Non-nil means, insert state change notes and time stamps into a drawer.
  1810. When nil, state changes notes will be inserted after the headline and
  1811. any scheduling and clock lines, but not inside a drawer.
  1812. The value of this variable should be the name of the drawer to use.
  1813. LOGBOOK is proposed at the default drawer for this purpose, you can
  1814. also set this to a string to define the drawer of your choice.
  1815. A value of t is also allowed, representing \"LOGBOOK\".
  1816. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1817. will be ignored.
  1818. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1819. a subtree."
  1820. :group 'org-todo
  1821. :group 'org-progress
  1822. :type '(choice
  1823. (const :tag "Not into a drawer" nil)
  1824. (const :tag "LOGBOOK" t)
  1825. (string :tag "Other")))
  1826. (if (fboundp 'defvaralias)
  1827. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1828. (defun org-log-into-drawer ()
  1829. "Return the value of `org-log-into-drawer', but let properties overrule.
  1830. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1831. used instead of the default value."
  1832. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1833. (cond
  1834. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1835. ((equal p "t") "LOGBOOK")
  1836. (t p))))
  1837. (defcustom org-log-state-notes-insert-after-drawers nil
  1838. "Non-nil means, insert state change notes after any drawers in entry.
  1839. Only the drawers that *immediately* follow the headline and the
  1840. deadline/scheduled line are skipped.
  1841. When nil, insert notes right after the heading and perhaps the line
  1842. with deadline/scheduling if present.
  1843. This variable will have no effect if `org-log-into-drawer' is
  1844. set."
  1845. :group 'org-todo
  1846. :group 'org-progress
  1847. :type 'boolean)
  1848. (defcustom org-log-states-order-reversed t
  1849. "Non-nil means, the latest state change note will be directly after heading.
  1850. When nil, the notes will be orderer according to time."
  1851. :group 'org-todo
  1852. :group 'org-progress
  1853. :type 'boolean)
  1854. (defcustom org-log-repeat 'time
  1855. "Non-nil means, record moving through the DONE state when triggering repeat.
  1856. An auto-repeating task is immediately switched back to TODO when
  1857. marked DONE. If you are not logging state changes (by adding \"@\"
  1858. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1859. record a closing note, there will be no record of the task moving
  1860. through DONE. This variable forces taking a note anyway.
  1861. nil Don't force a record
  1862. time Record a time stamp
  1863. note Record a note
  1864. This option can also be set with on a per-file-basis with
  1865. #+STARTUP: logrepeat
  1866. #+STARTUP: lognoterepeat
  1867. #+STARTUP: nologrepeat
  1868. You can have local logging settings for a subtree by setting the LOGGING
  1869. property to one or more of these keywords."
  1870. :group 'org-todo
  1871. :group 'org-progress
  1872. :type '(choice
  1873. (const :tag "Don't force a record" nil)
  1874. (const :tag "Force recording the DONE state" time)
  1875. (const :tag "Force recording a note with the DONE state" note)))
  1876. (defgroup org-priorities nil
  1877. "Priorities in Org-mode."
  1878. :tag "Org Priorities"
  1879. :group 'org-todo)
  1880. (defcustom org-enable-priority-commands t
  1881. "Non-nil means, priority commands are active.
  1882. When nil, these commands will be disabled, so that you never accidentally
  1883. set a priority."
  1884. :group 'org-priorities
  1885. :type 'boolean)
  1886. (defcustom org-highest-priority ?A
  1887. "The highest priority of TODO items. A character like ?A, ?B etc.
  1888. Must have a smaller ASCII number than `org-lowest-priority'."
  1889. :group 'org-priorities
  1890. :type 'character)
  1891. (defcustom org-lowest-priority ?C
  1892. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1893. Must have a larger ASCII number than `org-highest-priority'."
  1894. :group 'org-priorities
  1895. :type 'character)
  1896. (defcustom org-default-priority ?B
  1897. "The default priority of TODO items.
  1898. This is the priority an item get if no explicit priority is given."
  1899. :group 'org-priorities
  1900. :type 'character)
  1901. (defcustom org-priority-start-cycle-with-default t
  1902. "Non-nil means, start with default priority when starting to cycle.
  1903. When this is nil, the first step in the cycle will be (depending on the
  1904. command used) one higher or lower that the default priority."
  1905. :group 'org-priorities
  1906. :type 'boolean)
  1907. (defgroup org-time nil
  1908. "Options concerning time stamps and deadlines in Org-mode."
  1909. :tag "Org Time"
  1910. :group 'org)
  1911. (defcustom org-insert-labeled-timestamps-at-point nil
  1912. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1913. When nil, these labeled time stamps are forces into the second line of an
  1914. entry, just after the headline. When scheduling from the global TODO list,
  1915. the time stamp will always be forced into the second line."
  1916. :group 'org-time
  1917. :type 'boolean)
  1918. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1919. "Formats for `format-time-string' which are used for time stamps.
  1920. It is not recommended to change this constant.")
  1921. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1922. "Number of minutes to round time stamps to.
  1923. These are two values, the first applies when first creating a time stamp.
  1924. The second applies when changing it with the commands `S-up' and `S-down'.
  1925. When changing the time stamp, this means that it will change in steps
  1926. of N minutes, as given by the second value.
  1927. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1928. numbers should be factors of 60, so for example 5, 10, 15.
  1929. When this is larger than 1, you can still force an exact time-stamp by using
  1930. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1931. and by using a prefix arg to `S-up/down' to specify the exact number
  1932. of minutes to shift."
  1933. :group 'org-time
  1934. :get '(lambda (var) ; Make sure all entries have 5 elements
  1935. (if (integerp (default-value var))
  1936. (list (default-value var) 5)
  1937. (default-value var)))
  1938. :type '(list
  1939. (integer :tag "when inserting times")
  1940. (integer :tag "when modifying times")))
  1941. ;; Normalize old customizations of this variable.
  1942. (when (integerp org-time-stamp-rounding-minutes)
  1943. (setq org-time-stamp-rounding-minutes
  1944. (list org-time-stamp-rounding-minutes
  1945. org-time-stamp-rounding-minutes)))
  1946. (defcustom org-display-custom-times nil
  1947. "Non-nil means, overlay custom formats over all time stamps.
  1948. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1949. To turn this on on a per-file basis, insert anywhere in the file:
  1950. #+STARTUP: customtime"
  1951. :group 'org-time
  1952. :set 'set-default
  1953. :type 'sexp)
  1954. (make-variable-buffer-local 'org-display-custom-times)
  1955. (defcustom org-time-stamp-custom-formats
  1956. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1957. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1958. These are overlayed over the default ISO format if the variable
  1959. `org-display-custom-times' is set. Time like %H:%M should be at the
  1960. end of the second format. The custom formats are also honored by export
  1961. commands, if custom time display is turned on at the time of export."
  1962. :group 'org-time
  1963. :type 'sexp)
  1964. (defun org-time-stamp-format (&optional long inactive)
  1965. "Get the right format for a time string."
  1966. (let ((f (if long (cdr org-time-stamp-formats)
  1967. (car org-time-stamp-formats))))
  1968. (if inactive
  1969. (concat "[" (substring f 1 -1) "]")
  1970. f)))
  1971. (defcustom org-time-clocksum-format "%d:%02d"
  1972. "The format string used when creating CLOCKSUM lines, or when
  1973. org-mode generates a time duration."
  1974. :group 'org-time
  1975. :type 'string)
  1976. (defcustom org-time-clocksum-use-fractional nil
  1977. "If non-nil, \\[org-clock-display] uses fractional times.
  1978. org-mode generates a time duration."
  1979. :group 'org-time
  1980. :type 'boolean)
  1981. (defcustom org-time-clocksum-fractional-format "%.2f"
  1982. "The format string used when creating CLOCKSUM lines, or when
  1983. org-mode generates a time duration."
  1984. :group 'org-time
  1985. :type 'string)
  1986. (defcustom org-deadline-warning-days 14
  1987. "No. of days before expiration during which a deadline becomes active.
  1988. This variable governs the display in sparse trees and in the agenda.
  1989. When 0 or negative, it means use this number (the absolute value of it)
  1990. even if a deadline has a different individual lead time specified.
  1991. Custom commands can set this variable in the options section."
  1992. :group 'org-time
  1993. :group 'org-agenda-daily/weekly
  1994. :type 'integer)
  1995. (defcustom org-read-date-prefer-future t
  1996. "Non-nil means, assume future for incomplete date input from user.
  1997. This affects the following situations:
  1998. 1. The user gives a month but not a year.
  1999. For example, if it is april and you enter \"feb 2\", this will be read
  2000. as feb 2, *next* year. \"May 5\", however, will be this year.
  2001. 2. The user gives a day, but no month.
  2002. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2003. read this as the third of *next* month. However, if you enter \"17\",
  2004. it will be considered as *this* month.
  2005. If you set this variable to the symbol `time', then also the following
  2006. will work:
  2007. 3. If the user gives a time, but no day. If the time is before now,
  2008. to will be interpreted as tomorrow.
  2009. Currently none of this works for ISO week specifications.
  2010. When this option is nil, the current day, month and year will always be
  2011. used as defaults."
  2012. :group 'org-time
  2013. :type '(choice
  2014. (const :tag "Never" nil)
  2015. (const :tag "Check month and day" t)
  2016. (const :tag "Check month, day, and time" time)))
  2017. (defcustom org-read-date-display-live t
  2018. "Non-nil means, display current interpretation of date prompt live.
  2019. This display will be in an overlay, in the minibuffer."
  2020. :group 'org-time
  2021. :type 'boolean)
  2022. (defcustom org-read-date-popup-calendar t
  2023. "Non-nil means, pop up a calendar when prompting for a date.
  2024. In the calendar, the date can be selected with mouse-1. However, the
  2025. minibuffer will also be active, and you can simply enter the date as well.
  2026. When nil, only the minibuffer will be available."
  2027. :group 'org-time
  2028. :type 'boolean)
  2029. (if (fboundp 'defvaralias)
  2030. (defvaralias 'org-popup-calendar-for-date-prompt
  2031. 'org-read-date-popup-calendar))
  2032. (defcustom org-read-date-minibuffer-setup-hook nil
  2033. "Hook to be used to set up keys for the date/time interface.
  2034. Add key definitions to `minibuffer-local-map', which will be a temporary
  2035. copy."
  2036. :group 'org-time
  2037. :type 'hook)
  2038. (defcustom org-extend-today-until 0
  2039. "The hour when your day really ends. Must be an integer.
  2040. This has influence for the following applications:
  2041. - When switching the agenda to \"today\". It it is still earlier than
  2042. the time given here, the day recognized as TODAY is actually yesterday.
  2043. - When a date is read from the user and it is still before the time given
  2044. here, the current date and time will be assumed to be yesterday, 23:59.
  2045. Also, timestamps inserted in remember templates follow this rule.
  2046. IMPORTANT: This is a feature whose implementation is and likely will
  2047. remain incomplete. Really, it is only here because past midnight seems to
  2048. be the favorite working time of John Wiegley :-)"
  2049. :group 'org-time
  2050. :type 'integer)
  2051. (defcustom org-edit-timestamp-down-means-later nil
  2052. "Non-nil means, S-down will increase the time in a time stamp.
  2053. When nil, S-up will increase."
  2054. :group 'org-time
  2055. :type 'boolean)
  2056. (defcustom org-calendar-follow-timestamp-change t
  2057. "Non-nil means, make the calendar window follow timestamp changes.
  2058. When a timestamp is modified and the calendar window is visible, it will be
  2059. moved to the new date."
  2060. :group 'org-time
  2061. :type 'boolean)
  2062. (defgroup org-tags nil
  2063. "Options concerning tags in Org-mode."
  2064. :tag "Org Tags"
  2065. :group 'org)
  2066. (defcustom org-tag-alist nil
  2067. "List of tags allowed in Org-mode files.
  2068. When this list is nil, Org-mode will base TAG input on what is already in the
  2069. buffer.
  2070. The value of this variable is an alist, the car of each entry must be a
  2071. keyword as a string, the cdr may be a character that is used to select
  2072. that tag through the fast-tag-selection interface.
  2073. See the manual for details."
  2074. :group 'org-tags
  2075. :type '(repeat
  2076. (choice
  2077. (cons (string :tag "Tag name")
  2078. (character :tag "Access char"))
  2079. (list :tag "Start radio group"
  2080. (const :startgroup)
  2081. (option (string :tag "Group description")))
  2082. (list :tag "End radio group"
  2083. (const :endgroup)
  2084. (option (string :tag "Group description")))
  2085. (const :tag "New line" (:newline)))))
  2086. (defcustom org-tag-persistent-alist nil
  2087. "List of tags that will always appear in all Org-mode files.
  2088. This is in addition to any in buffer settings or customizations
  2089. of `org-tag-alist'.
  2090. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2091. The value of this variable is an alist, the car of each entry must be a
  2092. keyword as a string, the cdr may be a character that is used to select
  2093. that tag through the fast-tag-selection interface.
  2094. See the manual for details.
  2095. To disable these tags on a per-file basis, insert anywhere in the file:
  2096. #+STARTUP: noptag"
  2097. :group 'org-tags
  2098. :type '(repeat
  2099. (choice
  2100. (cons (string :tag "Tag name")
  2101. (character :tag "Access char"))
  2102. (const :tag "Start radio group" (:startgroup))
  2103. (const :tag "End radio group" (:endgroup))
  2104. (const :tag "New line" (:newline)))))
  2105. (defvar org-file-tags nil
  2106. "List of tags that can be inherited by all entries in the file.
  2107. The tags will be inherited if the variable `org-use-tag-inheritance'
  2108. says they should be.
  2109. This variable is populated from #+FILETAGS lines.")
  2110. (defcustom org-use-fast-tag-selection 'auto
  2111. "Non-nil means, use fast tag selection scheme.
  2112. This is a special interface to select and deselect tags with single keys.
  2113. When nil, fast selection is never used.
  2114. When the symbol `auto', fast selection is used if and only if selection
  2115. characters for tags have been configured, either through the variable
  2116. `org-tag-alist' or through a #+TAGS line in the buffer.
  2117. When t, fast selection is always used and selection keys are assigned
  2118. automatically if necessary."
  2119. :group 'org-tags
  2120. :type '(choice
  2121. (const :tag "Always" t)
  2122. (const :tag "Never" nil)
  2123. (const :tag "When selection characters are configured" 'auto)))
  2124. (defcustom org-fast-tag-selection-single-key nil
  2125. "Non-nil means, fast tag selection exits after first change.
  2126. When nil, you have to press RET to exit it.
  2127. During fast tag selection, you can toggle this flag with `C-c'.
  2128. This variable can also have the value `expert'. In this case, the window
  2129. displaying the tags menu is not even shown, until you press C-c again."
  2130. :group 'org-tags
  2131. :type '(choice
  2132. (const :tag "No" nil)
  2133. (const :tag "Yes" t)
  2134. (const :tag "Expert" expert)))
  2135. (defvar org-fast-tag-selection-include-todo nil
  2136. "Non-nil means, fast tags selection interface will also offer TODO states.
  2137. This is an undocumented feature, you should not rely on it.")
  2138. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2139. "The column to which tags should be indented in a headline.
  2140. If this number is positive, it specifies the column. If it is negative,
  2141. it means that the tags should be flushright to that column. For example,
  2142. -80 works well for a normal 80 character screen."
  2143. :group 'org-tags
  2144. :type 'integer)
  2145. (defcustom org-auto-align-tags t
  2146. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2147. These operations change the length of a headline and therefore shift
  2148. the tags around. With this options turned on, after each such operation
  2149. the tags are again aligned to `org-tags-column'."
  2150. :group 'org-tags
  2151. :type 'boolean)
  2152. (defcustom org-use-tag-inheritance t
  2153. "Non-nil means, tags in levels apply also for sublevels.
  2154. When nil, only the tags directly given in a specific line apply there.
  2155. This may also be a list of tags that should be inherited, or a regexp that
  2156. matches tags that should be inherited. Additional control is possible
  2157. with the variable `org-tags-exclude-from-inheritance' which gives an
  2158. explicit list of tags to be excluded from inheritance., even if the value of
  2159. `org-use-tag-inheritance' would select it for inheritance.
  2160. If this option is t, a match early-on in a tree can lead to a large
  2161. number of matches in the subtree when constructing the agenda or creating
  2162. a sparse tree. If you only want to see the first match in a tree during
  2163. a search, check out the variable `org-tags-match-list-sublevels'."
  2164. :group 'org-tags
  2165. :type '(choice
  2166. (const :tag "Not" nil)
  2167. (const :tag "Always" t)
  2168. (repeat :tag "Specific tags" (string :tag "Tag"))
  2169. (regexp :tag "Tags matched by regexp")))
  2170. (defcustom org-tags-exclude-from-inheritance nil
  2171. "List of tags that should never be inherited.
  2172. This is a way to exclude a few tags from inheritance. For way to do
  2173. the opposite, to actively allow inheritance for selected tags,
  2174. see the variable `org-use-tag-inheritance'."
  2175. :group 'org-tags
  2176. :type '(repeat (string :tag "Tag")))
  2177. (defun org-tag-inherit-p (tag)
  2178. "Check if TAG is one that should be inherited."
  2179. (cond
  2180. ((member tag org-tags-exclude-from-inheritance) nil)
  2181. ((eq org-use-tag-inheritance t) t)
  2182. ((not org-use-tag-inheritance) nil)
  2183. ((stringp org-use-tag-inheritance)
  2184. (string-match org-use-tag-inheritance tag))
  2185. ((listp org-use-tag-inheritance)
  2186. (member tag org-use-tag-inheritance))
  2187. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2188. (defcustom org-tags-match-list-sublevels t
  2189. "Non-nil means list also sublevels of headlines matching a search.
  2190. This variable applies to tags/property searches, and also to stuck
  2191. projects because this search is based on a tags match as well.
  2192. When set to the symbol `indented', sublevels are indented with
  2193. leading dots.
  2194. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2195. the sublevels of a headline matching a tag search often also match
  2196. the same search. Listing all of them can create very long lists.
  2197. Setting this variable to nil causes subtrees of a match to be skipped.
  2198. This variable is semi-obsolete and probably should always be true. It
  2199. is better to limit inheritance to certain tags using the variables
  2200. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2201. :group 'org-tags
  2202. :type '(choice
  2203. (const :tag "No, don't list them" nil)
  2204. (const :tag "Yes, do list them" t)
  2205. (const :tag "List them, indented with leading dots" indented)))
  2206. (defcustom org-tags-sort-function nil
  2207. "When set, tags are sorted using this function as a comparator"
  2208. :group 'org-tags
  2209. :type '(choice
  2210. (const :tag "No sorting" nil)
  2211. (const :tag "Alphabetical" string<)
  2212. (const :tag "Reverse alphabetical" string>)
  2213. (function :tag "Custom function" nil)))
  2214. (defvar org-tags-history nil
  2215. "History of minibuffer reads for tags.")
  2216. (defvar org-last-tags-completion-table nil
  2217. "The last used completion table for tags.")
  2218. (defvar org-after-tags-change-hook nil
  2219. "Hook that is run after the tags in a line have changed.")
  2220. (defgroup org-properties nil
  2221. "Options concerning properties in Org-mode."
  2222. :tag "Org Properties"
  2223. :group 'org)
  2224. (defcustom org-property-format "%-10s %s"
  2225. "How property key/value pairs should be formatted by `indent-line'.
  2226. When `indent-line' hits a property definition, it will format the line
  2227. according to this format, mainly to make sure that the values are
  2228. lined-up with respect to each other."
  2229. :group 'org-properties
  2230. :type 'string)
  2231. (defcustom org-use-property-inheritance nil
  2232. "Non-nil means, properties apply also for sublevels.
  2233. This setting is chiefly used during property searches. Turning it on can
  2234. cause significant overhead when doing a search, which is why it is not
  2235. on by default.
  2236. When nil, only the properties directly given in the current entry count.
  2237. When t, every property is inherited. The value may also be a list of
  2238. properties that should have inheritance, or a regular expression matching
  2239. properties that should be inherited.
  2240. However, note that some special properties use inheritance under special
  2241. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2242. and the properties ending in \"_ALL\" when they are used as descriptor
  2243. for valid values of a property.
  2244. Note for programmers:
  2245. When querying an entry with `org-entry-get', you can control if inheritance
  2246. should be used. By default, `org-entry-get' looks only at the local
  2247. properties. You can request inheritance by setting the inherit argument
  2248. to t (to force inheritance) or to `selective' (to respect the setting
  2249. in this variable)."
  2250. :group 'org-properties
  2251. :type '(choice
  2252. (const :tag "Not" nil)
  2253. (const :tag "Always" t)
  2254. (repeat :tag "Specific properties" (string :tag "Property"))
  2255. (regexp :tag "Properties matched by regexp")))
  2256. (defun org-property-inherit-p (property)
  2257. "Check if PROPERTY is one that should be inherited."
  2258. (cond
  2259. ((eq org-use-property-inheritance t) t)
  2260. ((not org-use-property-inheritance) nil)
  2261. ((stringp org-use-property-inheritance)
  2262. (string-match org-use-property-inheritance property))
  2263. ((listp org-use-property-inheritance)
  2264. (member property org-use-property-inheritance))
  2265. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2266. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2267. "The default column format, if no other format has been defined.
  2268. This variable can be set on the per-file basis by inserting a line
  2269. #+COLUMNS: %25ITEM ....."
  2270. :group 'org-properties
  2271. :type 'string)
  2272. (defcustom org-columns-ellipses ".."
  2273. "The ellipses to be used when a field in column view is truncated.
  2274. When this is the empty string, as many characters as possible are shown,
  2275. but then there will be no visual indication that the field has been truncated.
  2276. When this is a string of length N, the last N characters of a truncated
  2277. field are replaced by this string. If the column is narrower than the
  2278. ellipses string, only part of the ellipses string will be shown."
  2279. :group 'org-properties
  2280. :type 'string)
  2281. (defcustom org-columns-modify-value-for-display-function nil
  2282. "Function that modifies values for display in column view.
  2283. For example, it can be used to cut out a certain part from a time stamp.
  2284. The function must take 2 arguments:
  2285. column-title The title of the column (*not* the property name)
  2286. value The value that should be modified.
  2287. The function should return the value that should be displayed,
  2288. or nil if the normal value should be used."
  2289. :group 'org-properties
  2290. :type 'function)
  2291. (defcustom org-effort-property "Effort"
  2292. "The property that is being used to keep track of effort estimates.
  2293. Effort estimates given in this property need to have the format H:MM."
  2294. :group 'org-properties
  2295. :group 'org-progress
  2296. :type '(string :tag "Property"))
  2297. (defconst org-global-properties-fixed
  2298. '(("VISIBILITY_ALL" . "folded children content all")
  2299. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2300. "List of property/value pairs that can be inherited by any entry.
  2301. These are fixed values, for the preset properties. The user variable
  2302. that can be used to add to this list is `org-global-properties'.
  2303. The entries in this list are cons cells where the car is a property
  2304. name and cdr is a string with the value. If the value represents
  2305. multiple items like an \"_ALL\" property, separate the items by
  2306. spaces.")
  2307. (defcustom org-global-properties nil
  2308. "List of property/value pairs that can be inherited by any entry.
  2309. This list will be combined with the constant `org-global-properties-fixed'.
  2310. The entries in this list are cons cells where the car is a property
  2311. name and cdr is a string with the value.
  2312. You can set buffer-local values for the same purpose in the variable
  2313. `org-file-properties' this by adding lines like
  2314. #+PROPERTY: NAME VALUE"
  2315. :group 'org-properties
  2316. :type '(repeat
  2317. (cons (string :tag "Property")
  2318. (string :tag "Value"))))
  2319. (defvar org-file-properties nil
  2320. "List of property/value pairs that can be inherited by any entry.
  2321. Valid for the current buffer.
  2322. This variable is populated from #+PROPERTY lines.")
  2323. (make-variable-buffer-local 'org-file-properties)
  2324. (defgroup org-agenda nil
  2325. "Options concerning agenda views in Org-mode."
  2326. :tag "Org Agenda"
  2327. :group 'org)
  2328. (defvar org-category nil
  2329. "Variable used by org files to set a category for agenda display.
  2330. Such files should use a file variable to set it, for example
  2331. # -*- mode: org; org-category: \"ELisp\"
  2332. or contain a special line
  2333. #+CATEGORY: ELisp
  2334. If the file does not specify a category, then file's base name
  2335. is used instead.")
  2336. (make-variable-buffer-local 'org-category)
  2337. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2338. (defcustom org-agenda-files nil
  2339. "The files to be used for agenda display.
  2340. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2341. \\[org-remove-file]. You can also use customize to edit the list.
  2342. If an entry is a directory, all files in that directory that are matched by
  2343. `org-agenda-file-regexp' will be part of the file list.
  2344. If the value of the variable is not a list but a single file name, then
  2345. the list of agenda files is actually stored and maintained in that file, one
  2346. agenda file per line."
  2347. :group 'org-agenda
  2348. :type '(choice
  2349. (repeat :tag "List of files and directories" file)
  2350. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2351. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2352. "Regular expression to match files for `org-agenda-files'.
  2353. If any element in the list in that variable contains a directory instead
  2354. of a normal file, all files in that directory that are matched by this
  2355. regular expression will be included."
  2356. :group 'org-agenda
  2357. :type 'regexp)
  2358. (defcustom org-agenda-text-search-extra-files nil
  2359. "List of extra files to be searched by text search commands.
  2360. These files will be search in addition to the agenda files by the
  2361. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2362. Note that these files will only be searched for text search commands,
  2363. not for the other agenda views like todo lists, tag searches or the weekly
  2364. agenda. This variable is intended to list notes and possibly archive files
  2365. that should also be searched by these two commands.
  2366. In fact, if the first element in the list is the symbol `agenda-archives',
  2367. than all archive files of all agenda files will be added to the search
  2368. scope."
  2369. :group 'org-agenda
  2370. :type '(set :greedy t
  2371. (const :tag "Agenda Archives" agenda-archives)
  2372. (repeat :inline t (file))))
  2373. (if (fboundp 'defvaralias)
  2374. (defvaralias 'org-agenda-multi-occur-extra-files
  2375. 'org-agenda-text-search-extra-files))
  2376. (defcustom org-agenda-skip-unavailable-files nil
  2377. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2378. A nil value means to remove them, after a query, from the list."
  2379. :group 'org-agenda
  2380. :type 'boolean)
  2381. (defcustom org-calendar-to-agenda-key [?c]
  2382. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2383. The command `org-calendar-goto-agenda' will be bound to this key. The
  2384. default is the character `c' because then `c' can be used to switch back and
  2385. forth between agenda and calendar."
  2386. :group 'org-agenda
  2387. :type 'sexp)
  2388. (defcustom org-calendar-agenda-action-key [?k]
  2389. "The key to be installed in `calendar-mode-map' for agenda-action.
  2390. The command `org-agenda-action' will be bound to this key. The
  2391. default is the character `k' because we use the same key in the agenda."
  2392. :group 'org-agenda
  2393. :type 'sexp)
  2394. (defcustom org-calendar-insert-diary-entry-key [?i]
  2395. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2396. This option is irrelevant until `org-agenda-diary-file' has been configured
  2397. to point to an Org-mode file. When that is the case, the command
  2398. `org-agenda-diary-entry' will be bound to the key given here, by default
  2399. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2400. if you want to continue doing this, you need to change this to a different
  2401. key."
  2402. :group 'org-agenda
  2403. :type 'sexp)
  2404. (defcustom org-agenda-diary-file 'diary-file
  2405. "File to which to add new entries with the `i' key in agenda and calendar.
  2406. When this is the symbol `diary-file', the functionality in the Emacs
  2407. calendar will be used to add entries to the `diary-file'. But when this
  2408. points to a file, `org-agenda-diary-entry' will be used instead."
  2409. :group 'org-agenda
  2410. :type '(choice
  2411. (const :tag "The standard Emacs diary file" diary-file)
  2412. (file :tag "Special Org file diary entries")))
  2413. (eval-after-load "calendar"
  2414. '(progn
  2415. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2416. 'org-calendar-goto-agenda)
  2417. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2418. 'org-agenda-action)
  2419. (add-hook 'calendar-mode-hook
  2420. (lambda ()
  2421. (unless (eq org-agenda-diary-file 'diary-file)
  2422. (define-key calendar-mode-map
  2423. org-calendar-insert-diary-entry-key
  2424. 'org-agenda-diary-entry))))))
  2425. (defgroup org-latex nil
  2426. "Options for embedding LaTeX code into Org-mode."
  2427. :tag "Org LaTeX"
  2428. :group 'org)
  2429. (defcustom org-format-latex-options
  2430. '(:foreground default :background default :scale 1.0
  2431. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2432. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2433. "Options for creating images from LaTeX fragments.
  2434. This is a property list with the following properties:
  2435. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2436. `default' means use the foreground of the default face.
  2437. :background the background color, or \"Transparent\".
  2438. `default' means use the background of the default face.
  2439. :scale a scaling factor for the size of the images.
  2440. :html-foreground, :html-background, :html-scale
  2441. the same numbers for HTML export.
  2442. :matchers a list indicating which matchers should be used to
  2443. find LaTeX fragments. Valid members of this list are:
  2444. \"begin\" find environments
  2445. \"$1\" find single characters surrounded by $.$
  2446. \"$\" find math expressions surrounded by $...$
  2447. \"$$\" find math expressions surrounded by $$....$$
  2448. \"\\(\" find math expressions surrounded by \\(...\\)
  2449. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2450. :group 'org-latex
  2451. :type 'plist)
  2452. (defcustom org-format-latex-header "\\documentclass{article}
  2453. \\usepackage{amssymb}
  2454. \\usepackage[usenames]{color}
  2455. \\usepackage{amsmath}
  2456. \\usepackage{latexsym}
  2457. \\usepackage[mathscr]{eucal}
  2458. \\pagestyle{empty} % do not remove
  2459. % The settings below are copied from fullpage.sty
  2460. \\setlength{\\textwidth}{\\paperwidth}
  2461. \\addtolength{\\textwidth}{-3cm}
  2462. \\setlength{\\oddsidemargin}{1.5cm}
  2463. \\addtolength{\\oddsidemargin}{-2.54cm}
  2464. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2465. \\setlength{\\textheight}{\\paperheight}
  2466. \\addtolength{\\textheight}{-\\headheight}
  2467. \\addtolength{\\textheight}{-\\headsep}
  2468. \\addtolength{\\textheight}{-\\footskip}
  2469. \\addtolength{\\textheight}{-3cm}
  2470. \\setlength{\\topmargin}{1.5cm}
  2471. \\addtolength{\\topmargin}{-2.54cm}"
  2472. "The document header used for processing LaTeX fragments.
  2473. It is imperative that this header make sure that no page number
  2474. appears on the page."
  2475. :group 'org-latex
  2476. :type 'string)
  2477. (defgroup org-font-lock nil
  2478. "Font-lock settings for highlighting in Org-mode."
  2479. :tag "Org Font Lock"
  2480. :group 'org)
  2481. (defcustom org-level-color-stars-only nil
  2482. "Non-nil means fontify only the stars in each headline.
  2483. When nil, the entire headline is fontified.
  2484. Changing it requires restart of `font-lock-mode' to become effective
  2485. also in regions already fontified."
  2486. :group 'org-font-lock
  2487. :type 'boolean)
  2488. (defcustom org-hide-leading-stars nil
  2489. "Non-nil means, hide the first N-1 stars in a headline.
  2490. This works by using the face `org-hide' for these stars. This
  2491. face is white for a light background, and black for a dark
  2492. background. You may have to customize the face `org-hide' to
  2493. make this work.
  2494. Changing it requires restart of `font-lock-mode' to become effective
  2495. also in regions already fontified.
  2496. You may also set this on a per-file basis by adding one of the following
  2497. lines to the buffer:
  2498. #+STARTUP: hidestars
  2499. #+STARTUP: showstars"
  2500. :group 'org-font-lock
  2501. :type 'boolean)
  2502. (defcustom org-fontify-done-headline nil
  2503. "Non-nil means, change the face of a headline if it is marked DONE.
  2504. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2505. When this is non-nil, the headline after the keyword is set to the
  2506. `org-headline-done' as an additional indication."
  2507. :group 'org-font-lock
  2508. :type 'boolean)
  2509. (defcustom org-fontify-emphasized-text t
  2510. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2511. Changing this variable requires a restart of Emacs to take effect."
  2512. :group 'org-font-lock
  2513. :type 'boolean)
  2514. (defcustom org-fontify-whole-heading-line nil
  2515. "Non-nil means fontify the whole line for headings.
  2516. This is useful when setting a background color for the
  2517. org-level-* faces."
  2518. :group 'org-font-lock
  2519. :type 'boolean)
  2520. (defcustom org-highlight-latex-fragments-and-specials nil
  2521. "Non-nil means, fontify what is treated specially by the exporters."
  2522. :group 'org-font-lock
  2523. :type 'boolean)
  2524. (defcustom org-hide-emphasis-markers nil
  2525. "Non-nil mean font-lock should hide the emphasis marker characters."
  2526. :group 'org-font-lock
  2527. :type 'boolean)
  2528. (defvar org-emph-re nil
  2529. "Regular expression for matching emphasis.")
  2530. (defvar org-verbatim-re nil
  2531. "Regular expression for matching verbatim text.")
  2532. (defvar org-emphasis-regexp-components) ; defined just below
  2533. (defvar org-emphasis-alist) ; defined just below
  2534. (defun org-set-emph-re (var val)
  2535. "Set variable and compute the emphasis regular expression."
  2536. (set var val)
  2537. (when (and (boundp 'org-emphasis-alist)
  2538. (boundp 'org-emphasis-regexp-components)
  2539. org-emphasis-alist org-emphasis-regexp-components)
  2540. (let* ((e org-emphasis-regexp-components)
  2541. (pre (car e))
  2542. (post (nth 1 e))
  2543. (border (nth 2 e))
  2544. (body (nth 3 e))
  2545. (nl (nth 4 e))
  2546. (body1 (concat body "*?"))
  2547. (markers (mapconcat 'car org-emphasis-alist ""))
  2548. (vmarkers (mapconcat
  2549. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2550. org-emphasis-alist "")))
  2551. ;; make sure special characters appear at the right position in the class
  2552. (if (string-match "\\^" markers)
  2553. (setq markers (concat (replace-match "" t t markers) "^")))
  2554. (if (string-match "-" markers)
  2555. (setq markers (concat (replace-match "" t t markers) "-")))
  2556. (if (string-match "\\^" vmarkers)
  2557. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2558. (if (string-match "-" vmarkers)
  2559. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2560. (if (> nl 0)
  2561. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2562. (int-to-string nl) "\\}")))
  2563. ;; Make the regexp
  2564. (setq org-emph-re
  2565. (concat "\\([" pre "]\\|^\\)"
  2566. "\\("
  2567. "\\([" markers "]\\)"
  2568. "\\("
  2569. "[^" border "]\\|"
  2570. "[^" border "]"
  2571. body1
  2572. "[^" border "]"
  2573. "\\)"
  2574. "\\3\\)"
  2575. "\\([" post "]\\|$\\)"))
  2576. (setq org-verbatim-re
  2577. (concat "\\([" pre "]\\|^\\)"
  2578. "\\("
  2579. "\\([" vmarkers "]\\)"
  2580. "\\("
  2581. "[^" border "]\\|"
  2582. "[^" border "]"
  2583. body1
  2584. "[^" border "]"
  2585. "\\)"
  2586. "\\3\\)"
  2587. "\\([" post "]\\|$\\)")))))
  2588. (defcustom org-emphasis-regexp-components
  2589. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2590. "Components used to build the regular expression for emphasis.
  2591. This is a list with 6 entries. Terminology: In an emphasis string
  2592. like \" *strong word* \", we call the initial space PREMATCH, the final
  2593. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2594. and \"trong wor\" is the body. The different components in this variable
  2595. specify what is allowed/forbidden in each part:
  2596. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2597. post Chars allowed as postmatch. End of line will be allowed too.
  2598. border The chars *forbidden* as border characters.
  2599. body-regexp A regexp like \".\" to match a body character. Don't use
  2600. non-shy groups here, and don't allow newline here.
  2601. newline The maximum number of newlines allowed in an emphasis exp.
  2602. Use customize to modify this, or restart Emacs after changing it."
  2603. :group 'org-font-lock
  2604. :set 'org-set-emph-re
  2605. :type '(list
  2606. (sexp :tag "Allowed chars in pre ")
  2607. (sexp :tag "Allowed chars in post ")
  2608. (sexp :tag "Forbidden chars in border ")
  2609. (sexp :tag "Regexp for body ")
  2610. (integer :tag "number of newlines allowed")
  2611. (option (boolean :tag "Please ignore this button"))))
  2612. (defcustom org-emphasis-alist
  2613. `(("*" bold "<b>" "</b>")
  2614. ("/" italic "<i>" "</i>")
  2615. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2616. ("=" org-code "<code>" "</code>" verbatim)
  2617. ("~" org-verbatim "<code>" "</code>" verbatim)
  2618. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2619. "<del>" "</del>")
  2620. )
  2621. "Special syntax for emphasized text.
  2622. Text starting and ending with a special character will be emphasized, for
  2623. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2624. characters, the face to be used by font-lock for highlighting in Org-mode
  2625. Emacs buffers, and the HTML tags to be used for this.
  2626. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2627. Use customize to modify this, or restart Emacs after changing it."
  2628. :group 'org-font-lock
  2629. :set 'org-set-emph-re
  2630. :type '(repeat
  2631. (list
  2632. (string :tag "Marker character")
  2633. (choice
  2634. (face :tag "Font-lock-face")
  2635. (plist :tag "Face property list"))
  2636. (string :tag "HTML start tag")
  2637. (string :tag "HTML end tag")
  2638. (option (const verbatim)))))
  2639. (defvar org-protecting-blocks
  2640. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2641. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2642. This is needed for font-lock setup.")
  2643. ;;; Miscellaneous options
  2644. (defgroup org-completion nil
  2645. "Completion in Org-mode."
  2646. :tag "Org Completion"
  2647. :group 'org)
  2648. (defcustom org-completion-use-ido nil
  2649. "Non-nil means, use ido completion wherever possible.
  2650. Note that `ido-mode' must be active for this variable to be relevant.
  2651. If you decide to turn this variable on, you might well want to turn off
  2652. `org-outline-path-complete-in-steps'.
  2653. See also `org-completion-use-iswitchb'."
  2654. :group 'org-completion
  2655. :type 'boolean)
  2656. (defcustom org-completion-use-iswitchb nil
  2657. "Non-nil means, use iswitchb completion wherever possible.
  2658. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2659. If you decide to turn this variable on, you might well want to turn off
  2660. `org-outline-path-complete-in-steps'.
  2661. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2662. :group 'org-completion
  2663. :type 'boolean)
  2664. (defcustom org-completion-fallback-command 'hippie-expand
  2665. "The expansion command called by \\[org-complete] in normal context.
  2666. Normal means, no org-mode-specific context."
  2667. :group 'org-completion
  2668. :type 'function)
  2669. ;;; Functions and variables from their packages
  2670. ;; Declared here to avoid compiler warnings
  2671. ;; XEmacs only
  2672. (defvar outline-mode-menu-heading)
  2673. (defvar outline-mode-menu-show)
  2674. (defvar outline-mode-menu-hide)
  2675. (defvar zmacs-regions) ; XEmacs regions
  2676. ;; Emacs only
  2677. (defvar mark-active)
  2678. ;; Various packages
  2679. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2680. (declare-function calendar-forward-day "cal-move" (arg))
  2681. (declare-function calendar-goto-date "cal-move" (date))
  2682. (declare-function calendar-goto-today "cal-move" ())
  2683. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2684. (defvar calc-embedded-close-formula)
  2685. (defvar calc-embedded-open-formula)
  2686. (declare-function cdlatex-tab "ext:cdlatex" ())
  2687. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2688. (defvar font-lock-unfontify-region-function)
  2689. (declare-function iswitchb-read-buffer "iswitchb"
  2690. (prompt &optional default require-match start matches-set))
  2691. (defvar iswitchb-temp-buflist)
  2692. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2693. (defvar org-agenda-tags-todo-honor-ignore-options)
  2694. (declare-function org-agenda-skip "org-agenda" ())
  2695. (declare-function org-format-agenda-item "org-agenda"
  2696. (extra txt &optional category tags dotime noprefix remove-re))
  2697. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2698. (declare-function org-agenda-change-all-lines "org-agenda"
  2699. (newhead hdmarker &optional fixface just-this))
  2700. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2701. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2702. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2703. (beg end))
  2704. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2705. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2706. "org-agenda" (&optional end))
  2707. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2708. (declare-function org-indent-mode "org-indent" (&optional arg))
  2709. (declare-function parse-time-string "parse-time" (string))
  2710. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2711. (defvar remember-data-file)
  2712. (defvar texmathp-why)
  2713. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2714. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2715. (defvar w3m-current-url)
  2716. (defvar w3m-current-title)
  2717. (defvar org-latex-regexps)
  2718. ;;; Autoload and prepare some org modules
  2719. ;; Some table stuff that needs to be defined here, because it is used
  2720. ;; by the functions setting up org-mode or checking for table context.
  2721. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2722. "Detects an org-type or table-type table.")
  2723. (defconst org-table-line-regexp "^[ \t]*|"
  2724. "Detects an org-type table line.")
  2725. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2726. "Detects an org-type table line.")
  2727. (defconst org-table-hline-regexp "^[ \t]*|-"
  2728. "Detects an org-type table hline.")
  2729. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2730. "Detects a table-type table hline.")
  2731. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2732. "Searching from within a table (any type) this finds the first line
  2733. outside the table.")
  2734. ;; Autoload the functions in org-table.el that are needed by functions here.
  2735. (eval-and-compile
  2736. (org-autoload "org-table"
  2737. '(org-table-align org-table-begin org-table-blank-field
  2738. org-table-convert org-table-convert-region org-table-copy-down
  2739. org-table-copy-region org-table-create
  2740. org-table-create-or-convert-from-region
  2741. org-table-create-with-table.el org-table-current-dline
  2742. org-table-cut-region org-table-delete-column org-table-edit-field
  2743. org-table-edit-formulas org-table-end org-table-eval-formula
  2744. org-table-export org-table-field-info
  2745. org-table-get-stored-formulas org-table-goto-column
  2746. org-table-hline-and-move org-table-import org-table-insert-column
  2747. org-table-insert-hline org-table-insert-row org-table-iterate
  2748. org-table-justify-field-maybe org-table-kill-row
  2749. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2750. org-table-move-column org-table-move-column-left
  2751. org-table-move-column-right org-table-move-row
  2752. org-table-move-row-down org-table-move-row-up
  2753. org-table-next-field org-table-next-row org-table-paste-rectangle
  2754. org-table-previous-field org-table-recalculate
  2755. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2756. org-table-toggle-coordinate-overlays
  2757. org-table-toggle-formula-debugger org-table-wrap-region
  2758. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2759. (defun org-at-table-p (&optional table-type)
  2760. "Return t if the cursor is inside an org-type table.
  2761. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2762. (if org-enable-table-editor
  2763. (save-excursion
  2764. (beginning-of-line 1)
  2765. (looking-at (if table-type org-table-any-line-regexp
  2766. org-table-line-regexp)))
  2767. nil))
  2768. (defsubst org-table-p () (org-at-table-p))
  2769. (defun org-at-table.el-p ()
  2770. "Return t if and only if we are at a table.el table."
  2771. (and (org-at-table-p 'any)
  2772. (save-excursion
  2773. (goto-char (org-table-begin 'any))
  2774. (looking-at org-table1-hline-regexp))))
  2775. (defun org-table-recognize-table.el ()
  2776. "If there is a table.el table nearby, recognize it and move into it."
  2777. (if org-table-tab-recognizes-table.el
  2778. (if (org-at-table.el-p)
  2779. (progn
  2780. (beginning-of-line 1)
  2781. (if (looking-at org-table-dataline-regexp)
  2782. nil
  2783. (if (looking-at org-table1-hline-regexp)
  2784. (progn
  2785. (beginning-of-line 2)
  2786. (if (looking-at org-table-any-border-regexp)
  2787. (beginning-of-line -1)))))
  2788. (if (re-search-forward "|" (org-table-end t) t)
  2789. (progn
  2790. (require 'table)
  2791. (if (table--at-cell-p (point))
  2792. t
  2793. (message "recognizing table.el table...")
  2794. (table-recognize-table)
  2795. (message "recognizing table.el table...done")))
  2796. (error "This should not happen..."))
  2797. t)
  2798. nil)
  2799. nil))
  2800. (defun org-at-table-hline-p ()
  2801. "Return t if the cursor is inside a hline in a table."
  2802. (if org-enable-table-editor
  2803. (save-excursion
  2804. (beginning-of-line 1)
  2805. (looking-at org-table-hline-regexp))
  2806. nil))
  2807. (defvar org-table-clean-did-remove-column nil)
  2808. (defun org-table-map-tables (function)
  2809. "Apply FUNCTION to the start of all tables in the buffer."
  2810. (save-excursion
  2811. (save-restriction
  2812. (widen)
  2813. (goto-char (point-min))
  2814. (while (re-search-forward org-table-any-line-regexp nil t)
  2815. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2816. (beginning-of-line 1)
  2817. (when (looking-at org-table-line-regexp)
  2818. (save-excursion (funcall function))
  2819. (or (looking-at org-table-line-regexp)
  2820. (forward-char 1)))
  2821. (re-search-forward org-table-any-border-regexp nil 1))))
  2822. (message "Mapping tables: done"))
  2823. ;; Declare and autoload functions from org-exp.el & Co
  2824. (declare-function org-default-export-plist "org-exp")
  2825. (declare-function org-infile-export-plist "org-exp")
  2826. (declare-function org-get-current-options "org-exp")
  2827. (eval-and-compile
  2828. (org-autoload "org-exp"
  2829. '(org-export org-export-visible
  2830. org-insert-export-options-template
  2831. org-table-clean-before-export))
  2832. (org-autoload "org-ascii"
  2833. '(org-export-as-ascii org-export-ascii-preprocess
  2834. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2835. org-export-region-as-ascii))
  2836. (org-autoload "org-html"
  2837. '(org-export-as-html-and-open
  2838. org-export-as-html-batch org-export-as-html-to-buffer
  2839. org-replace-region-by-html org-export-region-as-html
  2840. org-export-as-html))
  2841. (org-autoload "org-icalendar"
  2842. '(org-export-icalendar-this-file
  2843. org-export-icalendar-all-agenda-files
  2844. org-export-icalendar-combine-agenda-files))
  2845. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2846. ;; Declare and autoload functions from org-agenda.el
  2847. (eval-and-compile
  2848. (org-autoload "org-agenda"
  2849. '(org-agenda org-agenda-list org-search-view
  2850. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2851. org-diary org-agenda-to-appt
  2852. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2853. ;; Autoload org-remember
  2854. (eval-and-compile
  2855. (org-autoload "org-remember"
  2856. '(org-remember-insinuate org-remember-annotation
  2857. org-remember-apply-template org-remember org-remember-handler)))
  2858. ;; Autoload org-clock.el
  2859. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2860. (beg end))
  2861. (declare-function org-clock-update-mode-line "org-clock" ())
  2862. (declare-function org-resolve-clocks "org-clock"
  2863. (&optional also-non-dangling-p prompt last-valid))
  2864. (defvar org-clock-start-time)
  2865. (defvar org-clock-marker (make-marker)
  2866. "Marker recording the last clock-in.")
  2867. (defvar org-clock-hd-marker (make-marker)
  2868. "Marker recording the last clock-in, but the headline position.")
  2869. (defvar org-clock-heading ""
  2870. "The heading of the current clock entry.")
  2871. (defun org-clock-is-active ()
  2872. "Return non-nil if clock is currently running.
  2873. The return value is actually the clock marker."
  2874. (marker-buffer org-clock-marker))
  2875. (eval-and-compile
  2876. (org-autoload
  2877. "org-clock"
  2878. '(org-clock-in org-clock-out org-clock-cancel
  2879. org-clock-goto org-clock-sum org-clock-display
  2880. org-clock-remove-overlays org-clock-report
  2881. org-clocktable-shift org-dblock-write:clocktable
  2882. org-get-clocktable org-resolve-clocks)))
  2883. (defun org-clock-update-time-maybe ()
  2884. "If this is a CLOCK line, update it and return t.
  2885. Otherwise, return nil."
  2886. (interactive)
  2887. (save-excursion
  2888. (beginning-of-line 1)
  2889. (skip-chars-forward " \t")
  2890. (when (looking-at org-clock-string)
  2891. (let ((re (concat "[ \t]*" org-clock-string
  2892. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2893. "\\([ \t]*=>.*\\)?\\)?"))
  2894. ts te h m s neg)
  2895. (cond
  2896. ((not (looking-at re))
  2897. nil)
  2898. ((not (match-end 2))
  2899. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2900. (> org-clock-marker (point))
  2901. (<= org-clock-marker (point-at-eol)))
  2902. ;; The clock is running here
  2903. (setq org-clock-start-time
  2904. (apply 'encode-time
  2905. (org-parse-time-string (match-string 1))))
  2906. (org-clock-update-mode-line)))
  2907. (t
  2908. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2909. (end-of-line 1)
  2910. (setq ts (match-string 1)
  2911. te (match-string 3))
  2912. (setq s (- (org-float-time
  2913. (apply 'encode-time (org-parse-time-string te)))
  2914. (org-float-time
  2915. (apply 'encode-time (org-parse-time-string ts))))
  2916. neg (< s 0)
  2917. s (abs s)
  2918. h (floor (/ s 3600))
  2919. s (- s (* 3600 h))
  2920. m (floor (/ s 60))
  2921. s (- s (* 60 s)))
  2922. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2923. t))))))
  2924. (defun org-check-running-clock ()
  2925. "Check if the current buffer contains the running clock.
  2926. If yes, offer to stop it and to save the buffer with the changes."
  2927. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2928. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2929. (buffer-name))))
  2930. (org-clock-out)
  2931. (when (y-or-n-p "Save changed buffer?")
  2932. (save-buffer))))
  2933. (defun org-clocktable-try-shift (dir n)
  2934. "Check if this line starts a clock table, if yes, shift the time block."
  2935. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2936. (org-clocktable-shift dir n)))
  2937. ;; Autoload org-timer.el
  2938. (eval-and-compile
  2939. (org-autoload
  2940. "org-timer"
  2941. '(org-timer-start org-timer org-timer-item
  2942. org-timer-change-times-in-region
  2943. org-timer-set-timer
  2944. org-timer-reset-timers
  2945. org-timer-show-remaining-time)))
  2946. ;; Autoload org-feed.el
  2947. (eval-and-compile
  2948. (org-autoload
  2949. "org-feed"
  2950. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2951. ;; Autoload org-indent.el
  2952. ;; Define the variable already here, to make sure we have it.
  2953. (defvar org-indent-mode nil
  2954. "Non-nil if Org-Indent mode is enabled.
  2955. Use the command `org-indent-mode' to change this variable.")
  2956. (eval-and-compile
  2957. (org-autoload
  2958. "org-indent"
  2959. '(org-indent-mode)))
  2960. ;; Autoload org-mobile.el
  2961. (eval-and-compile
  2962. (org-autoload
  2963. "org-mobile"
  2964. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  2965. ;; Autoload archiving code
  2966. ;; The stuff that is needed for cycling and tags has to be defined here.
  2967. (defgroup org-archive nil
  2968. "Options concerning archiving in Org-mode."
  2969. :tag "Org Archive"
  2970. :group 'org-structure)
  2971. (defcustom org-archive-location "%s_archive::"
  2972. "The location where subtrees should be archived.
  2973. The value of this variable is a string, consisting of two parts,
  2974. separated by a double-colon. The first part is a filename and
  2975. the second part is a headline.
  2976. When the filename is omitted, archiving happens in the same file.
  2977. %s in the filename will be replaced by the current file
  2978. name (without the directory part). Archiving to a different file
  2979. is useful to keep archived entries from contributing to the
  2980. Org-mode Agenda.
  2981. The archived entries will be filed as subtrees of the specified
  2982. headline. When the headline is omitted, the subtrees are simply
  2983. filed away at the end of the file, as top-level entries. Also in
  2984. the heading you can use %s to represent the file name, this can be
  2985. useful when using the same archive for a number of different files.
  2986. Here are a few examples:
  2987. \"%s_archive::\"
  2988. If the current file is Projects.org, archive in file
  2989. Projects.org_archive, as top-level trees. This is the default.
  2990. \"::* Archived Tasks\"
  2991. Archive in the current file, under the top-level headline
  2992. \"* Archived Tasks\".
  2993. \"~/org/archive.org::\"
  2994. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2995. \"~/org/archive.org::From %s\"
  2996. Archive in file ~/org/archive.org (absolute path), under headlines
  2997. \"From FILENAME\" where file name is the current file name.
  2998. \"basement::** Finished Tasks\"
  2999. Archive in file ./basement (relative path), as level 3 trees
  3000. below the level 2 heading \"** Finished Tasks\".
  3001. You may set this option on a per-file basis by adding to the buffer a
  3002. line like
  3003. #+ARCHIVE: basement::** Finished Tasks
  3004. You may also define it locally for a subtree by setting an ARCHIVE property
  3005. in the entry. If such a property is found in an entry, or anywhere up
  3006. the hierarchy, it will be used."
  3007. :group 'org-archive
  3008. :type 'string)
  3009. (defcustom org-archive-tag "ARCHIVE"
  3010. "The tag that marks a subtree as archived.
  3011. An archived subtree does not open during visibility cycling, and does
  3012. not contribute to the agenda listings.
  3013. After changing this, font-lock must be restarted in the relevant buffers to
  3014. get the proper fontification."
  3015. :group 'org-archive
  3016. :group 'org-keywords
  3017. :type 'string)
  3018. (defcustom org-agenda-skip-archived-trees t
  3019. "Non-nil means, the agenda will skip any items located in archived trees.
  3020. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3021. variable is no longer recommended, you should leave it at the value t.
  3022. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3023. :group 'org-archive
  3024. :group 'org-agenda-skip
  3025. :type 'boolean)
  3026. (defcustom org-columns-skip-archived-trees t
  3027. "Non-nil means, ignore archived trees when creating column view."
  3028. :group 'org-archive
  3029. :group 'org-properties
  3030. :type 'boolean)
  3031. (defcustom org-cycle-open-archived-trees nil
  3032. "Non-nil means, `org-cycle' will open archived trees.
  3033. An archived tree is a tree marked with the tag ARCHIVE.
  3034. When nil, archived trees will stay folded. You can still open them with
  3035. normal outline commands like `show-all', but not with the cycling commands."
  3036. :group 'org-archive
  3037. :group 'org-cycle
  3038. :type 'boolean)
  3039. (defcustom org-sparse-tree-open-archived-trees nil
  3040. "Non-nil means sparse tree construction shows matches in archived trees.
  3041. When nil, matches in these trees are highlighted, but the trees are kept in
  3042. collapsed state."
  3043. :group 'org-archive
  3044. :group 'org-sparse-trees
  3045. :type 'boolean)
  3046. (defun org-cycle-hide-archived-subtrees (state)
  3047. "Re-hide all archived subtrees after a visibility state change."
  3048. (when (and (not org-cycle-open-archived-trees)
  3049. (not (memq state '(overview folded))))
  3050. (save-excursion
  3051. (let* ((globalp (memq state '(contents all)))
  3052. (beg (if globalp (point-min) (point)))
  3053. (end (if globalp (point-max) (org-end-of-subtree t))))
  3054. (org-hide-archived-subtrees beg end)
  3055. (goto-char beg)
  3056. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3057. (message "%s" (substitute-command-keys
  3058. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3059. (defun org-force-cycle-archived ()
  3060. "Cycle subtree even if it is archived."
  3061. (interactive)
  3062. (setq this-command 'org-cycle)
  3063. (let ((org-cycle-open-archived-trees t))
  3064. (call-interactively 'org-cycle)))
  3065. (defun org-hide-archived-subtrees (beg end)
  3066. "Re-hide all archived subtrees after a visibility state change."
  3067. (save-excursion
  3068. (let* ((re (concat ":" org-archive-tag ":")))
  3069. (goto-char beg)
  3070. (while (re-search-forward re end t)
  3071. (and (org-on-heading-p) (org-flag-subtree t))
  3072. (org-end-of-subtree t)))))
  3073. (defun org-flag-subtree (flag)
  3074. (save-excursion
  3075. (org-back-to-heading t)
  3076. (outline-end-of-heading)
  3077. (outline-flag-region (point)
  3078. (progn (org-end-of-subtree t) (point))
  3079. flag)))
  3080. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3081. (eval-and-compile
  3082. (org-autoload "org-archive"
  3083. '(org-add-archive-files org-archive-subtree
  3084. org-archive-to-archive-sibling org-toggle-archive-tag)))
  3085. ;; Autoload Column View Code
  3086. (declare-function org-columns-number-to-string "org-colview")
  3087. (declare-function org-columns-get-format-and-top-level "org-colview")
  3088. (declare-function org-columns-compute "org-colview")
  3089. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3090. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3091. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3092. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3093. ;; Autoload ID code
  3094. (declare-function org-id-store-link "org-id")
  3095. (declare-function org-id-locations-load "org-id")
  3096. (declare-function org-id-locations-save "org-id")
  3097. (defvar org-id-track-globally)
  3098. (org-autoload "org-id"
  3099. '(org-id-get-create org-id-new org-id-copy org-id-get
  3100. org-id-get-with-outline-path-completion
  3101. org-id-get-with-outline-drilling
  3102. org-id-goto org-id-find org-id-store-link))
  3103. ;; Autoload Plotting Code
  3104. (org-autoload "org-plot"
  3105. '(org-plot/gnuplot))
  3106. ;;; Variables for pre-computed regular expressions, all buffer local
  3107. (defvar org-drawer-regexp nil
  3108. "Matches first line of a hidden block.")
  3109. (make-variable-buffer-local 'org-drawer-regexp)
  3110. (defvar org-todo-regexp nil
  3111. "Matches any of the TODO state keywords.")
  3112. (make-variable-buffer-local 'org-todo-regexp)
  3113. (defvar org-not-done-regexp nil
  3114. "Matches any of the TODO state keywords except the last one.")
  3115. (make-variable-buffer-local 'org-not-done-regexp)
  3116. (defvar org-not-done-heading-regexp nil
  3117. "Matches a TODO headline that is not done.")
  3118. (make-variable-buffer-local 'org-not-done-regexp)
  3119. (defvar org-todo-line-regexp nil
  3120. "Matches a headline and puts TODO state into group 2 if present.")
  3121. (make-variable-buffer-local 'org-todo-line-regexp)
  3122. (defvar org-complex-heading-regexp nil
  3123. "Matches a headline and puts everything into groups:
  3124. group 1: the stars
  3125. group 2: The todo keyword, maybe
  3126. group 3: Priority cookie
  3127. group 4: True headline
  3128. group 5: Tags")
  3129. (make-variable-buffer-local 'org-complex-heading-regexp)
  3130. (defvar org-complex-heading-regexp-format nil)
  3131. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3132. (defvar org-todo-line-tags-regexp nil
  3133. "Matches a headline and puts TODO state into group 2 if present.
  3134. Also put tags into group 4 if tags are present.")
  3135. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3136. (defvar org-nl-done-regexp nil
  3137. "Matches newline followed by a headline with the DONE keyword.")
  3138. (make-variable-buffer-local 'org-nl-done-regexp)
  3139. (defvar org-looking-at-done-regexp nil
  3140. "Matches the DONE keyword a point.")
  3141. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3142. (defvar org-ds-keyword-length 12
  3143. "Maximum length of the Deadline and SCHEDULED keywords.")
  3144. (make-variable-buffer-local 'org-ds-keyword-length)
  3145. (defvar org-deadline-regexp nil
  3146. "Matches the DEADLINE keyword.")
  3147. (make-variable-buffer-local 'org-deadline-regexp)
  3148. (defvar org-deadline-time-regexp nil
  3149. "Matches the DEADLINE keyword together with a time stamp.")
  3150. (make-variable-buffer-local 'org-deadline-time-regexp)
  3151. (defvar org-deadline-line-regexp nil
  3152. "Matches the DEADLINE keyword and the rest of the line.")
  3153. (make-variable-buffer-local 'org-deadline-line-regexp)
  3154. (defvar org-scheduled-regexp nil
  3155. "Matches the SCHEDULED keyword.")
  3156. (make-variable-buffer-local 'org-scheduled-regexp)
  3157. (defvar org-scheduled-time-regexp nil
  3158. "Matches the SCHEDULED keyword together with a time stamp.")
  3159. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3160. (defvar org-closed-time-regexp nil
  3161. "Matches the CLOSED keyword together with a time stamp.")
  3162. (make-variable-buffer-local 'org-closed-time-regexp)
  3163. (defvar org-keyword-time-regexp nil
  3164. "Matches any of the 4 keywords, together with the time stamp.")
  3165. (make-variable-buffer-local 'org-keyword-time-regexp)
  3166. (defvar org-keyword-time-not-clock-regexp nil
  3167. "Matches any of the 3 keywords, together with the time stamp.")
  3168. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3169. (defvar org-maybe-keyword-time-regexp nil
  3170. "Matches a timestamp, possibly preceeded by a keyword.")
  3171. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3172. (defvar org-planning-or-clock-line-re nil
  3173. "Matches a line with planning or clock info.")
  3174. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3175. (defconst org-plain-time-of-day-regexp
  3176. (concat
  3177. "\\(\\<[012]?[0-9]"
  3178. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3179. "\\(--?"
  3180. "\\(\\<[012]?[0-9]"
  3181. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3182. "\\)?")
  3183. "Regular expression to match a plain time or time range.
  3184. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3185. groups carry important information:
  3186. 0 the full match
  3187. 1 the first time, range or not
  3188. 8 the second time, if it is a range.")
  3189. (defconst org-plain-time-extension-regexp
  3190. (concat
  3191. "\\(\\<[012]?[0-9]"
  3192. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3193. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3194. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3195. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3196. groups carry important information:
  3197. 0 the full match
  3198. 7 hours of duration
  3199. 9 minutes of duration")
  3200. (defconst org-stamp-time-of-day-regexp
  3201. (concat
  3202. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3203. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3204. "\\(--?"
  3205. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3206. "Regular expression to match a timestamp time or time range.
  3207. After a match, the following groups carry important information:
  3208. 0 the full match
  3209. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3210. 2 the first time, range or not
  3211. 4 the second time, if it is a range.")
  3212. (defconst org-startup-options
  3213. '(("fold" org-startup-folded t)
  3214. ("overview" org-startup-folded t)
  3215. ("nofold" org-startup-folded nil)
  3216. ("showall" org-startup-folded nil)
  3217. ("showeverything" org-startup-folded showeverything)
  3218. ("content" org-startup-folded content)
  3219. ("indent" org-startup-indented t)
  3220. ("noindent" org-startup-indented nil)
  3221. ("hidestars" org-hide-leading-stars t)
  3222. ("showstars" org-hide-leading-stars nil)
  3223. ("odd" org-odd-levels-only t)
  3224. ("oddeven" org-odd-levels-only nil)
  3225. ("align" org-startup-align-all-tables t)
  3226. ("noalign" org-startup-align-all-tables nil)
  3227. ("customtime" org-display-custom-times t)
  3228. ("logdone" org-log-done time)
  3229. ("lognotedone" org-log-done note)
  3230. ("nologdone" org-log-done nil)
  3231. ("lognoteclock-out" org-log-note-clock-out t)
  3232. ("nolognoteclock-out" org-log-note-clock-out nil)
  3233. ("logrepeat" org-log-repeat state)
  3234. ("lognoterepeat" org-log-repeat note)
  3235. ("nologrepeat" org-log-repeat nil)
  3236. ("logreschedule" org-log-reschedule time)
  3237. ("lognotereschedule" org-log-reschedule note)
  3238. ("nologreschedule" org-log-reschedule nil)
  3239. ("logredeadline" org-log-redeadline time)
  3240. ("lognoteredeadline" org-log-redeadline note)
  3241. ("nologredeadline" org-log-redeadline nil)
  3242. ("fninline" org-footnote-define-inline t)
  3243. ("nofninline" org-footnote-define-inline nil)
  3244. ("fnlocal" org-footnote-section nil)
  3245. ("fnauto" org-footnote-auto-label t)
  3246. ("fnprompt" org-footnote-auto-label nil)
  3247. ("fnconfirm" org-footnote-auto-label confirm)
  3248. ("fnplain" org-footnote-auto-label plain)
  3249. ("fnadjust" org-footnote-auto-adjust t)
  3250. ("nofnadjust" org-footnote-auto-adjust nil)
  3251. ("constcgs" constants-unit-system cgs)
  3252. ("constSI" constants-unit-system SI)
  3253. ("noptag" org-tag-persistent-alist nil)
  3254. ("hideblocks" org-hide-block-startup t)
  3255. ("nohideblocks" org-hide-block-startup nil))
  3256. "Variable associated with STARTUP options for org-mode.
  3257. Each element is a list of three items: The startup options as written
  3258. in the #+STARTUP line, the corresponding variable, and the value to
  3259. set this variable to if the option is found. An optional forth element PUSH
  3260. means to push this value onto the list in the variable.")
  3261. (defun org-set-regexps-and-options ()
  3262. "Precompute regular expressions for current buffer."
  3263. (when (org-mode-p)
  3264. (org-set-local 'org-todo-kwd-alist nil)
  3265. (org-set-local 'org-todo-key-alist nil)
  3266. (org-set-local 'org-todo-key-trigger nil)
  3267. (org-set-local 'org-todo-keywords-1 nil)
  3268. (org-set-local 'org-done-keywords nil)
  3269. (org-set-local 'org-todo-heads nil)
  3270. (org-set-local 'org-todo-sets nil)
  3271. (org-set-local 'org-todo-log-states nil)
  3272. (org-set-local 'org-file-properties nil)
  3273. (org-set-local 'org-file-tags nil)
  3274. (let ((re (org-make-options-regexp
  3275. '("CATEGORY" "TODO" "COLUMNS"
  3276. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3277. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3278. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3279. (splitre "[ \t]+")
  3280. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3281. tail sep kws1 prio props ftags drawers
  3282. ext-setup-or-nil setup-contents (start 0))
  3283. (save-excursion
  3284. (save-restriction
  3285. (widen)
  3286. (goto-char (point-min))
  3287. (while (or (and ext-setup-or-nil
  3288. (string-match re ext-setup-or-nil start)
  3289. (setq start (match-end 0)))
  3290. (and (setq ext-setup-or-nil nil start 0)
  3291. (re-search-forward re nil t)))
  3292. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3293. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3294. (cond
  3295. ((equal key "CATEGORY")
  3296. (if (string-match "[ \t]+$" value)
  3297. (setq value (replace-match "" t t value)))
  3298. (setq cat value))
  3299. ((member key '("SEQ_TODO" "TODO"))
  3300. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3301. ((equal key "TYP_TODO")
  3302. (push (cons 'type (org-split-string value splitre)) kwds))
  3303. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3304. ;; general TODO-like setup
  3305. (push (cons (intern (downcase (match-string 1 key)))
  3306. (org-split-string value splitre)) kwds))
  3307. ((equal key "TAGS")
  3308. (setq tags (append tags (if tags '("\\n") nil)
  3309. (org-split-string value splitre))))
  3310. ((equal key "COLUMNS")
  3311. (org-set-local 'org-columns-default-format value))
  3312. ((equal key "LINK")
  3313. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3314. (push (cons (match-string 1 value)
  3315. (org-trim (match-string 2 value)))
  3316. links)))
  3317. ((equal key "PRIORITIES")
  3318. (setq prio (org-split-string value " +")))
  3319. ((equal key "PROPERTY")
  3320. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3321. (push (cons (match-string 1 value) (match-string 2 value))
  3322. props)))
  3323. ((equal key "FILETAGS")
  3324. (when (string-match "\\S-" value)
  3325. (setq ftags
  3326. (append
  3327. ftags
  3328. (apply 'append
  3329. (mapcar (lambda (x) (org-split-string x ":"))
  3330. (org-split-string value)))))))
  3331. ((equal key "DRAWERS")
  3332. (setq drawers (org-split-string value splitre)))
  3333. ((equal key "CONSTANTS")
  3334. (setq const (append const (org-split-string value splitre))))
  3335. ((equal key "STARTUP")
  3336. (let ((opts (org-split-string value splitre))
  3337. l var val)
  3338. (while (setq l (pop opts))
  3339. (when (setq l (assoc l org-startup-options))
  3340. (setq var (nth 1 l) val (nth 2 l))
  3341. (if (not (nth 3 l))
  3342. (set (make-local-variable var) val)
  3343. (if (not (listp (symbol-value var)))
  3344. (set (make-local-variable var) nil))
  3345. (set (make-local-variable var) (symbol-value var))
  3346. (add-to-list var val))))))
  3347. ((equal key "ARCHIVE")
  3348. (string-match " *$" value)
  3349. (setq arch (replace-match "" t t value))
  3350. (remove-text-properties 0 (length arch)
  3351. '(face t fontified t) arch))
  3352. ((equal key "SETUPFILE")
  3353. (setq setup-contents (org-file-contents
  3354. (expand-file-name
  3355. (org-remove-double-quotes value))
  3356. 'noerror))
  3357. (if (not ext-setup-or-nil)
  3358. (setq ext-setup-or-nil setup-contents start 0)
  3359. (setq ext-setup-or-nil
  3360. (concat (substring ext-setup-or-nil 0 start)
  3361. "\n" setup-contents "\n"
  3362. (substring ext-setup-or-nil start)))))
  3363. ))))
  3364. (when cat
  3365. (org-set-local 'org-category (intern cat))
  3366. (push (cons "CATEGORY" cat) props))
  3367. (when prio
  3368. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3369. (setq prio (mapcar 'string-to-char prio))
  3370. (org-set-local 'org-highest-priority (nth 0 prio))
  3371. (org-set-local 'org-lowest-priority (nth 1 prio))
  3372. (org-set-local 'org-default-priority (nth 2 prio)))
  3373. (and props (org-set-local 'org-file-properties (nreverse props)))
  3374. (and ftags (org-set-local 'org-file-tags
  3375. (mapcar 'org-add-prop-inherited ftags)))
  3376. (and drawers (org-set-local 'org-drawers drawers))
  3377. (and arch (org-set-local 'org-archive-location arch))
  3378. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3379. ;; Process the TODO keywords
  3380. (unless kwds
  3381. ;; Use the global values as if they had been given locally.
  3382. (setq kwds (default-value 'org-todo-keywords))
  3383. (if (stringp (car kwds))
  3384. (setq kwds (list (cons org-todo-interpretation
  3385. (default-value 'org-todo-keywords)))))
  3386. (setq kwds (reverse kwds)))
  3387. (setq kwds (nreverse kwds))
  3388. (let (inter kws kw)
  3389. (while (setq kws (pop kwds))
  3390. (let ((kws (or
  3391. (run-hook-with-args-until-success
  3392. 'org-todo-setup-filter-hook kws)
  3393. kws)))
  3394. (setq inter (pop kws) sep (member "|" kws)
  3395. kws0 (delete "|" (copy-sequence kws))
  3396. kwsa nil
  3397. kws1 (mapcar
  3398. (lambda (x)
  3399. ;; 1 2
  3400. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3401. (progn
  3402. (setq kw (match-string 1 x)
  3403. key (and (match-end 2) (match-string 2 x))
  3404. log (org-extract-log-state-settings x))
  3405. (push (cons kw (and key (string-to-char key))) kwsa)
  3406. (and log (push log org-todo-log-states))
  3407. kw)
  3408. (error "Invalid TODO keyword %s" x)))
  3409. kws0)
  3410. kwsa (if kwsa (append '((:startgroup))
  3411. (nreverse kwsa)
  3412. '((:endgroup))))
  3413. hw (car kws1)
  3414. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3415. tail (list inter hw (car dws) (org-last dws))))
  3416. (add-to-list 'org-todo-heads hw 'append)
  3417. (push kws1 org-todo-sets)
  3418. (setq org-done-keywords (append org-done-keywords dws nil))
  3419. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3420. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3421. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3422. (setq org-todo-sets (nreverse org-todo-sets)
  3423. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3424. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3425. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3426. ;; Process the constants
  3427. (when const
  3428. (let (e cst)
  3429. (while (setq e (pop const))
  3430. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3431. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3432. (setq org-table-formula-constants-local cst)))
  3433. ;; Process the tags.
  3434. (when tags
  3435. (let (e tgs)
  3436. (while (setq e (pop tags))
  3437. (cond
  3438. ((equal e "{") (push '(:startgroup) tgs))
  3439. ((equal e "}") (push '(:endgroup) tgs))
  3440. ((equal e "\\n") (push '(:newline) tgs))
  3441. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3442. (push (cons (match-string 1 e)
  3443. (string-to-char (match-string 2 e)))
  3444. tgs))
  3445. (t (push (list e) tgs))))
  3446. (org-set-local 'org-tag-alist nil)
  3447. (while (setq e (pop tgs))
  3448. (or (and (stringp (car e))
  3449. (assoc (car e) org-tag-alist))
  3450. (push e org-tag-alist)))))
  3451. ;; Compute the regular expressions and other local variables
  3452. (if (not org-done-keywords)
  3453. (setq org-done-keywords (and org-todo-keywords-1
  3454. (list (org-last org-todo-keywords-1)))))
  3455. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3456. (length org-scheduled-string)
  3457. (length org-clock-string)
  3458. (length org-closed-string)))
  3459. org-drawer-regexp
  3460. (concat "^[ \t]*:\\("
  3461. (mapconcat 'regexp-quote org-drawers "\\|")
  3462. "\\):[ \t]*$")
  3463. org-not-done-keywords
  3464. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3465. org-todo-regexp
  3466. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3467. "\\|") "\\)\\>")
  3468. org-not-done-regexp
  3469. (concat "\\<\\("
  3470. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3471. "\\)\\>")
  3472. org-not-done-heading-regexp
  3473. (concat "^\\(\\*+\\)[ \t]+\\("
  3474. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3475. "\\)\\>")
  3476. org-todo-line-regexp
  3477. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3478. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3479. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3480. org-complex-heading-regexp
  3481. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3482. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3483. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3484. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3485. org-complex-heading-regexp-format
  3486. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3487. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3488. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3489. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3490. org-nl-done-regexp
  3491. (concat "\n\\*+[ \t]+"
  3492. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3493. "\\)" "\\>")
  3494. org-todo-line-tags-regexp
  3495. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3496. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3497. (org-re
  3498. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3499. org-looking-at-done-regexp
  3500. (concat "^" "\\(?:"
  3501. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3502. "\\>")
  3503. org-deadline-regexp (concat "\\<" org-deadline-string)
  3504. org-deadline-time-regexp
  3505. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3506. org-deadline-line-regexp
  3507. (concat "\\<\\(" org-deadline-string "\\).*")
  3508. org-scheduled-regexp
  3509. (concat "\\<" org-scheduled-string)
  3510. org-scheduled-time-regexp
  3511. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3512. org-closed-time-regexp
  3513. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3514. org-keyword-time-regexp
  3515. (concat "\\<\\(" org-scheduled-string
  3516. "\\|" org-deadline-string
  3517. "\\|" org-closed-string
  3518. "\\|" org-clock-string "\\)"
  3519. " *[[<]\\([^]>]+\\)[]>]")
  3520. org-keyword-time-not-clock-regexp
  3521. (concat "\\<\\(" org-scheduled-string
  3522. "\\|" org-deadline-string
  3523. "\\|" org-closed-string
  3524. "\\)"
  3525. " *[[<]\\([^]>]+\\)[]>]")
  3526. org-maybe-keyword-time-regexp
  3527. (concat "\\(\\<\\(" org-scheduled-string
  3528. "\\|" org-deadline-string
  3529. "\\|" org-closed-string
  3530. "\\|" org-clock-string "\\)\\)?"
  3531. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3532. org-planning-or-clock-line-re
  3533. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3534. "\\|" org-deadline-string
  3535. "\\|" org-closed-string "\\|" org-clock-string
  3536. "\\)\\>\\)")
  3537. )
  3538. (org-compute-latex-and-specials-regexp)
  3539. (org-set-font-lock-defaults))))
  3540. (defun org-file-contents (file &optional noerror)
  3541. "Return the contents of FILE, as a string."
  3542. (if (or (not file)
  3543. (not (file-readable-p file)))
  3544. (if noerror
  3545. (progn
  3546. (message "Cannot read file %s" file)
  3547. (ding) (sit-for 2)
  3548. "")
  3549. (error "Cannot read file %s" file))
  3550. (with-temp-buffer
  3551. (insert-file-contents file)
  3552. (buffer-string))))
  3553. (defun org-extract-log-state-settings (x)
  3554. "Extract the log state setting from a TODO keyword string.
  3555. This will extract info from a string like \"WAIT(w@/!)\"."
  3556. (let (kw key log1 log2)
  3557. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3558. (setq kw (match-string 1 x)
  3559. key (and (match-end 2) (match-string 2 x))
  3560. log1 (and (match-end 3) (match-string 3 x))
  3561. log2 (and (match-end 4) (match-string 4 x)))
  3562. (and (or log1 log2)
  3563. (list kw
  3564. (and log1 (if (equal log1 "!") 'time 'note))
  3565. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3566. (defun org-remove-keyword-keys (list)
  3567. "Remove a pair of parenthesis at the end of each string in LIST."
  3568. (mapcar (lambda (x)
  3569. (if (string-match "(.*)$" x)
  3570. (substring x 0 (match-beginning 0))
  3571. x))
  3572. list))
  3573. ;; FIXME: this could be done much better, using second characters etc.
  3574. (defun org-assign-fast-keys (alist)
  3575. "Assign fast keys to a keyword-key alist.
  3576. Respect keys that are already there."
  3577. (let (new e k c c1 c2 (char ?a))
  3578. (while (setq e (pop alist))
  3579. (cond
  3580. ((equal e '(:startgroup)) (push e new))
  3581. ((equal e '(:endgroup)) (push e new))
  3582. ((equal e '(:newline)) (push e new))
  3583. (t
  3584. (setq k (car e) c2 nil)
  3585. (if (cdr e)
  3586. (setq c (cdr e))
  3587. ;; automatically assign a character.
  3588. (setq c1 (string-to-char
  3589. (downcase (substring
  3590. k (if (= (string-to-char k) ?@) 1 0)))))
  3591. (if (or (rassoc c1 new) (rassoc c1 alist))
  3592. (while (or (rassoc char new) (rassoc char alist))
  3593. (setq char (1+ char)))
  3594. (setq c2 c1))
  3595. (setq c (or c2 char)))
  3596. (push (cons k c) new))))
  3597. (nreverse new)))
  3598. ;;; Some variables used in various places
  3599. (defvar org-window-configuration nil
  3600. "Used in various places to store a window configuration.")
  3601. (defvar org-selected-window nil
  3602. "Used in various places to store a window configuration.")
  3603. (defvar org-finish-function nil
  3604. "Function to be called when `C-c C-c' is used.
  3605. This is for getting out of special buffers like remember.")
  3606. ;; FIXME: Occasionally check by commenting these, to make sure
  3607. ;; no other functions uses these, forgetting to let-bind them.
  3608. (defvar entry)
  3609. (defvar last-state)
  3610. (defvar date)
  3611. ;; Defined somewhere in this file, but used before definition.
  3612. (defvar org-html-entities)
  3613. (defvar org-struct-menu)
  3614. (defvar org-org-menu)
  3615. (defvar org-tbl-menu)
  3616. ;;;; Define the Org-mode
  3617. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3618. (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."))
  3619. ;; We use a before-change function to check if a table might need
  3620. ;; an update.
  3621. (defvar org-table-may-need-update t
  3622. "Indicates that a table might need an update.
  3623. This variable is set by `org-before-change-function'.
  3624. `org-table-align' sets it back to nil.")
  3625. (defun org-before-change-function (beg end)
  3626. "Every change indicates that a table might need an update."
  3627. (setq org-table-may-need-update t))
  3628. (defvar org-mode-map)
  3629. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3630. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3631. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3632. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3633. (defvar org-table-buffer-is-an nil)
  3634. (defconst org-outline-regexp "\\*+ ")
  3635. ;;;###autoload
  3636. (define-derived-mode org-mode outline-mode "Org"
  3637. "Outline-based notes management and organizer, alias
  3638. \"Carsten's outline-mode for keeping track of everything.\"
  3639. Org-mode develops organizational tasks around a NOTES file which
  3640. contains information about projects as plain text. Org-mode is
  3641. implemented on top of outline-mode, which is ideal to keep the content
  3642. of large files well structured. It supports ToDo items, deadlines and
  3643. time stamps, which magically appear in the diary listing of the Emacs
  3644. calendar. Tables are easily created with a built-in table editor.
  3645. Plain text URL-like links connect to websites, emails (VM), Usenet
  3646. messages (Gnus), BBDB entries, and any files related to the project.
  3647. For printing and sharing of notes, an Org-mode file (or a part of it)
  3648. can be exported as a structured ASCII or HTML file.
  3649. The following commands are available:
  3650. \\{org-mode-map}"
  3651. ;; Get rid of Outline menus, they are not needed
  3652. ;; Need to do this here because define-derived-mode sets up
  3653. ;; the keymap so late. Still, it is a waste to call this each time
  3654. ;; we switch another buffer into org-mode.
  3655. (if (featurep 'xemacs)
  3656. (when (boundp 'outline-mode-menu-heading)
  3657. ;; Assume this is Greg's port, it used easymenu
  3658. (easy-menu-remove outline-mode-menu-heading)
  3659. (easy-menu-remove outline-mode-menu-show)
  3660. (easy-menu-remove outline-mode-menu-hide))
  3661. (define-key org-mode-map [menu-bar headings] 'undefined)
  3662. (define-key org-mode-map [menu-bar hide] 'undefined)
  3663. (define-key org-mode-map [menu-bar show] 'undefined))
  3664. (org-load-modules-maybe)
  3665. (easy-menu-add org-org-menu)
  3666. (easy-menu-add org-tbl-menu)
  3667. (org-install-agenda-files-menu)
  3668. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3669. (org-add-to-invisibility-spec '(org-cwidth))
  3670. (org-add-to-invisibility-spec '(org-hide-block . t))
  3671. (when (featurep 'xemacs)
  3672. (org-set-local 'line-move-ignore-invisible t))
  3673. (org-set-local 'outline-regexp org-outline-regexp)
  3674. (org-set-local 'outline-level 'org-outline-level)
  3675. (when (and org-ellipsis
  3676. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3677. (fboundp 'make-glyph-code))
  3678. (unless org-display-table
  3679. (setq org-display-table (make-display-table)))
  3680. (set-display-table-slot
  3681. org-display-table 4
  3682. (vconcat (mapcar
  3683. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3684. org-ellipsis)))
  3685. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3686. (setq buffer-display-table org-display-table))
  3687. (org-set-regexps-and-options)
  3688. (when (and org-tag-faces (not org-tags-special-faces-re))
  3689. ;; tag faces set outside customize.... force initialization.
  3690. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3691. ;; Calc embedded
  3692. (org-set-local 'calc-embedded-open-mode "# ")
  3693. (modify-syntax-entry ?# "<")
  3694. (modify-syntax-entry ?@ "w")
  3695. (if org-startup-truncated (setq truncate-lines t))
  3696. (org-set-local 'font-lock-unfontify-region-function
  3697. 'org-unfontify-region)
  3698. ;; Activate before-change-function
  3699. (org-set-local 'org-table-may-need-update t)
  3700. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3701. 'local)
  3702. ;; Check for running clock before killing a buffer
  3703. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3704. ;; Paragraphs and auto-filling
  3705. (org-set-autofill-regexps)
  3706. (setq indent-line-function 'org-indent-line-function)
  3707. (org-update-radio-target-regexp)
  3708. ;; Make sure dependence stuff works reliably, even for users who set it
  3709. ;; too late :-(
  3710. (if org-enforce-todo-dependencies
  3711. (add-hook 'org-blocker-hook
  3712. 'org-block-todo-from-children-or-siblings-or-parent)
  3713. (remove-hook 'org-blocker-hook
  3714. 'org-block-todo-from-children-or-siblings-or-parent))
  3715. (if org-enforce-todo-checkbox-dependencies
  3716. (add-hook 'org-blocker-hook
  3717. 'org-block-todo-from-checkboxes)
  3718. (remove-hook 'org-blocker-hook
  3719. 'org-block-todo-from-checkboxes))
  3720. ;; Comment characters
  3721. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3722. (org-set-local 'comment-padding " ")
  3723. ;; Align options lines
  3724. (org-set-local
  3725. 'align-mode-rules-list
  3726. '((org-in-buffer-settings
  3727. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3728. (modes . '(org-mode)))))
  3729. ;; Imenu
  3730. (org-set-local 'imenu-create-index-function
  3731. 'org-imenu-get-tree)
  3732. ;; Make isearch reveal context
  3733. (if (or (featurep 'xemacs)
  3734. (not (boundp 'outline-isearch-open-invisible-function)))
  3735. ;; Emacs 21 and XEmacs make use of the hook
  3736. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3737. ;; Emacs 22 deals with this through a special variable
  3738. (org-set-local 'outline-isearch-open-invisible-function
  3739. (lambda (&rest ignore) (org-show-context 'isearch))))
  3740. ;; If empty file that did not turn on org-mode automatically, make it to.
  3741. (if (and org-insert-mode-line-in-empty-file
  3742. (interactive-p)
  3743. (= (point-min) (point-max)))
  3744. (insert "# -*- mode: org -*-\n\n"))
  3745. (unless org-inhibit-startup
  3746. (when org-startup-align-all-tables
  3747. (let ((bmp (buffer-modified-p)))
  3748. (org-table-map-tables 'org-table-align)
  3749. (set-buffer-modified-p bmp)))
  3750. (when org-startup-indented
  3751. (require 'org-indent)
  3752. (org-indent-mode 1))
  3753. (org-set-startup-visibility)))
  3754. (when (fboundp 'abbrev-table-put)
  3755. (abbrev-table-put org-mode-abbrev-table
  3756. :parents (list text-mode-abbrev-table)))
  3757. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3758. (defun org-current-time ()
  3759. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3760. (if (> (car org-time-stamp-rounding-minutes) 1)
  3761. (let ((r (car org-time-stamp-rounding-minutes))
  3762. (time (decode-time)))
  3763. (apply 'encode-time
  3764. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3765. (nthcdr 2 time))))
  3766. (current-time)))
  3767. ;;;; Font-Lock stuff, including the activators
  3768. (defvar org-mouse-map (make-sparse-keymap))
  3769. (org-defkey org-mouse-map
  3770. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3771. (org-defkey org-mouse-map
  3772. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3773. (when org-mouse-1-follows-link
  3774. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3775. (when org-tab-follows-link
  3776. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3777. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3778. (require 'font-lock)
  3779. (defconst org-non-link-chars "]\t\n\r<>")
  3780. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3781. "shell" "elisp"))
  3782. (defvar org-link-types-re nil
  3783. "Matches a link that has a url-like prefix like \"http:\"")
  3784. (defvar org-link-re-with-space nil
  3785. "Matches a link with spaces, optional angular brackets around it.")
  3786. (defvar org-link-re-with-space2 nil
  3787. "Matches a link with spaces, optional angular brackets around it.")
  3788. (defvar org-link-re-with-space3 nil
  3789. "Matches a link with spaces, only for internal part in bracket links.")
  3790. (defvar org-angle-link-re nil
  3791. "Matches link with angular brackets, spaces are allowed.")
  3792. (defvar org-plain-link-re nil
  3793. "Matches plain link, without spaces.")
  3794. (defvar org-bracket-link-regexp nil
  3795. "Matches a link in double brackets.")
  3796. (defvar org-bracket-link-analytic-regexp nil
  3797. "Regular expression used to analyze links.
  3798. Here is what the match groups contain after a match:
  3799. 1: http:
  3800. 2: http
  3801. 3: path
  3802. 4: [desc]
  3803. 5: desc")
  3804. (defvar org-bracket-link-analytic-regexp++ nil
  3805. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3806. (defvar org-any-link-re nil
  3807. "Regular expression matching any link.")
  3808. (defun org-make-link-regexps ()
  3809. "Update the link regular expressions.
  3810. This should be called after the variable `org-link-types' has changed."
  3811. (setq org-link-types-re
  3812. (concat
  3813. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3814. org-link-re-with-space
  3815. (concat
  3816. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3817. "\\([^" org-non-link-chars " ]"
  3818. "[^" org-non-link-chars "]*"
  3819. "[^" org-non-link-chars " ]\\)>?")
  3820. org-link-re-with-space2
  3821. (concat
  3822. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3823. "\\([^" org-non-link-chars " ]"
  3824. "[^\t\n\r]*"
  3825. "[^" org-non-link-chars " ]\\)>?")
  3826. org-link-re-with-space3
  3827. (concat
  3828. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3829. "\\([^" org-non-link-chars " ]"
  3830. "[^\t\n\r]*\\)")
  3831. org-angle-link-re
  3832. (concat
  3833. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3834. "\\([^" org-non-link-chars " ]"
  3835. "[^" org-non-link-chars "]*"
  3836. "\\)>")
  3837. org-plain-link-re
  3838. (concat
  3839. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3840. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3841. org-bracket-link-regexp
  3842. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3843. org-bracket-link-analytic-regexp
  3844. (concat
  3845. "\\[\\["
  3846. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3847. "\\([^]]+\\)"
  3848. "\\]"
  3849. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3850. "\\]")
  3851. org-bracket-link-analytic-regexp++
  3852. (concat
  3853. "\\[\\["
  3854. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3855. "\\([^]]+\\)"
  3856. "\\]"
  3857. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3858. "\\]")
  3859. org-any-link-re
  3860. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3861. org-angle-link-re "\\)\\|\\("
  3862. org-plain-link-re "\\)")))
  3863. (org-make-link-regexps)
  3864. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3865. "Regular expression for fast time stamp matching.")
  3866. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3867. "Regular expression for fast time stamp matching.")
  3868. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3869. "Regular expression matching time strings for analysis.
  3870. This one does not require the space after the date, so it can be used
  3871. on a string that terminates immediately after the date.")
  3872. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3873. "Regular expression matching time strings for analysis.")
  3874. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3875. "Regular expression matching time stamps, with groups.")
  3876. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3877. "Regular expression matching time stamps (also [..]), with groups.")
  3878. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3879. "Regular expression matching a time stamp range.")
  3880. (defconst org-tr-regexp-both
  3881. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3882. "Regular expression matching a time stamp range.")
  3883. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3884. org-ts-regexp "\\)?")
  3885. "Regular expression matching a time stamp or time stamp range.")
  3886. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3887. org-ts-regexp-both "\\)?")
  3888. "Regular expression matching a time stamp or time stamp range.
  3889. The time stamps may be either active or inactive.")
  3890. (defvar org-emph-face nil)
  3891. (defun org-do-emphasis-faces (limit)
  3892. "Run through the buffer and add overlays to links."
  3893. (let (rtn a)
  3894. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3895. (if (not (= (char-after (match-beginning 3))
  3896. (char-after (match-beginning 4))))
  3897. (progn
  3898. (setq rtn t)
  3899. (setq a (assoc (match-string 3) org-emphasis-alist))
  3900. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3901. 'face
  3902. (nth 1 a))
  3903. (and (nth 4 a)
  3904. (org-remove-flyspell-overlays-in
  3905. (match-beginning 0) (match-end 0)))
  3906. (add-text-properties (match-beginning 2) (match-end 2)
  3907. '(font-lock-multiline t))
  3908. (when org-hide-emphasis-markers
  3909. (add-text-properties (match-end 4) (match-beginning 5)
  3910. '(invisible org-link))
  3911. (add-text-properties (match-beginning 3) (match-end 3)
  3912. '(invisible org-link)))))
  3913. (backward-char 1))
  3914. rtn))
  3915. (defun org-emphasize (&optional char)
  3916. "Insert or change an emphasis, i.e. a font like bold or italic.
  3917. If there is an active region, change that region to a new emphasis.
  3918. If there is no region, just insert the marker characters and position
  3919. the cursor between them.
  3920. CHAR should be either the marker character, or the first character of the
  3921. HTML tag associated with that emphasis. If CHAR is a space, the means
  3922. to remove the emphasis of the selected region.
  3923. If char is not given (for example in an interactive call) it
  3924. will be prompted for."
  3925. (interactive)
  3926. (let ((eal org-emphasis-alist) e det
  3927. (erc org-emphasis-regexp-components)
  3928. (prompt "")
  3929. (string "") beg end move tag c s)
  3930. (if (org-region-active-p)
  3931. (setq beg (region-beginning) end (region-end)
  3932. string (buffer-substring beg end))
  3933. (setq move t))
  3934. (while (setq e (pop eal))
  3935. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3936. c (aref tag 0))
  3937. (push (cons c (string-to-char (car e))) det)
  3938. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3939. (substring tag 1)))))
  3940. (setq det (nreverse det))
  3941. (unless char
  3942. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3943. (setq char (read-char-exclusive)))
  3944. (setq char (or (cdr (assoc char det)) char))
  3945. (if (equal char ?\ )
  3946. (setq s "" move nil)
  3947. (unless (assoc (char-to-string char) org-emphasis-alist)
  3948. (error "No such emphasis marker: \"%c\"" char))
  3949. (setq s (char-to-string char)))
  3950. (while (and (> (length string) 1)
  3951. (equal (substring string 0 1) (substring string -1))
  3952. (assoc (substring string 0 1) org-emphasis-alist))
  3953. (setq string (substring string 1 -1)))
  3954. (setq string (concat s string s))
  3955. (if beg (delete-region beg end))
  3956. (unless (or (bolp)
  3957. (string-match (concat "[" (nth 0 erc) "\n]")
  3958. (char-to-string (char-before (point)))))
  3959. (insert " "))
  3960. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3961. (char-to-string (char-after (point))))
  3962. (insert " ") (backward-char 1))
  3963. (insert string)
  3964. (and move (backward-char 1))))
  3965. (defconst org-nonsticky-props
  3966. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3967. (defsubst org-rear-nonsticky-at (pos)
  3968. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3969. (defun org-activate-plain-links (limit)
  3970. "Run through the buffer and add overlays to links."
  3971. (catch 'exit
  3972. (let (f)
  3973. (if (re-search-forward org-plain-link-re limit t)
  3974. (progn
  3975. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3976. (setq f (get-text-property (match-beginning 0) 'face))
  3977. (if (or (eq f 'org-tag)
  3978. (and (listp f) (memq 'org-tag f)))
  3979. nil
  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-code (limit)
  3986. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3987. (progn
  3988. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3989. (remove-text-properties (match-beginning 0) (match-end 0)
  3990. '(display t invisible t intangible t))
  3991. t)))
  3992. (defun org-fontify-meta-lines-and-blocks (limit)
  3993. "Fontify #+ lines and blocks, in the correct ways."
  3994. (let ((case-fold-search t))
  3995. (if (re-search-forward
  3996. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3997. limit t)
  3998. (let ((beg (match-beginning 0))
  3999. (beg1 (line-beginning-position 2))
  4000. (dc1 (downcase (match-string 2)))
  4001. (dc3 (downcase (match-string 3)))
  4002. end end1 quoting)
  4003. (cond
  4004. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4005. ;; a single line of backend-specific content
  4006. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4007. (remove-text-properties (match-beginning 0) (match-end 0)
  4008. '(display t invisible t intangible t))
  4009. (add-text-properties (match-beginning 1) (match-end 3)
  4010. '(font-lock-fontified t face org-meta-line))
  4011. (add-text-properties (match-beginning 6) (match-end 6)
  4012. '(font-lock-fontified t face org-block))
  4013. t)
  4014. ((and (match-end 4) (equal dc3 "begin"))
  4015. ;; Truely a block
  4016. (setq quoting (member (downcase (match-string 5))
  4017. org-protecting-blocks))
  4018. (when (re-search-forward
  4019. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4020. nil t) ;; on purpose, we look further than LIMIT
  4021. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4022. (when quoting
  4023. (remove-text-properties beg end
  4024. '(display t invisible t intangible t)))
  4025. (add-text-properties
  4026. beg end
  4027. '(font-lock-fontified t font-lock-multiline t))
  4028. (add-text-properties beg beg1 '(face org-meta-line))
  4029. (add-text-properties end1 end '(face org-meta-line))
  4030. (when quoting
  4031. (add-text-properties beg1 end1 '(face org-block)))
  4032. t))
  4033. ((not (member (char-after beg) '(?\ ?\t)))
  4034. ;; just any other in-buffer setting, but not indented
  4035. (add-text-properties
  4036. beg (match-end 0)
  4037. '(font-lock-fontified t face org-meta-line))
  4038. t)
  4039. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4040. "orgtbl:" "tblfm:" "tblname:"))
  4041. (and (match-end 4) (equal dc3 "attr")))
  4042. (add-text-properties
  4043. beg (match-end 0)
  4044. '(font-lock-fontified t face org-meta-line))
  4045. t)
  4046. ((member dc3 '(" " ""))
  4047. (add-text-properties
  4048. beg (match-end 0)
  4049. '(font-lock-fontified t face font-lock-comment-face)))
  4050. (t nil))))))
  4051. (defun org-activate-angle-links (limit)
  4052. "Run through the buffer and add overlays to links."
  4053. (if (re-search-forward org-angle-link-re limit t)
  4054. (progn
  4055. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4056. (add-text-properties (match-beginning 0) (match-end 0)
  4057. (list 'mouse-face 'highlight
  4058. 'keymap org-mouse-map))
  4059. (org-rear-nonsticky-at (match-end 0))
  4060. t)))
  4061. (defun org-activate-footnote-links (limit)
  4062. "Run through the buffer and add overlays to links."
  4063. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4064. limit t)
  4065. (progn
  4066. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4067. (add-text-properties (match-beginning 2) (match-end 2)
  4068. (list 'mouse-face 'highlight
  4069. 'keymap org-mouse-map
  4070. 'help-echo
  4071. (if (= (point-at-bol) (match-beginning 2))
  4072. "Footnote definition"
  4073. "Footnote reference")
  4074. ))
  4075. (org-rear-nonsticky-at (match-end 2))
  4076. t)))
  4077. (defun org-activate-bracket-links (limit)
  4078. "Run through the buffer and add overlays to bracketed links."
  4079. (if (re-search-forward org-bracket-link-regexp limit t)
  4080. (let* ((help (concat "LINK: "
  4081. (org-match-string-no-properties 1)))
  4082. ;; FIXME: above we should remove the escapes.
  4083. ;; but that requires another match, protecting match data,
  4084. ;; a lot of overhead for font-lock.
  4085. (ip (org-maybe-intangible
  4086. (list 'invisible 'org-link
  4087. 'keymap org-mouse-map 'mouse-face 'highlight
  4088. 'font-lock-multiline t 'help-echo help)))
  4089. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4090. 'font-lock-multiline t 'help-echo help)))
  4091. ;; We need to remove the invisible property here. Table narrowing
  4092. ;; may have made some of this invisible.
  4093. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4094. (remove-text-properties (match-beginning 0) (match-end 0)
  4095. '(invisible nil))
  4096. (if (match-end 3)
  4097. (progn
  4098. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4099. (org-rear-nonsticky-at (match-beginning 3))
  4100. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4101. (org-rear-nonsticky-at (match-end 3))
  4102. (add-text-properties (match-end 3) (match-end 0) ip)
  4103. (org-rear-nonsticky-at (match-end 0)))
  4104. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4105. (org-rear-nonsticky-at (match-beginning 1))
  4106. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4107. (org-rear-nonsticky-at (match-end 1))
  4108. (add-text-properties (match-end 1) (match-end 0) ip)
  4109. (org-rear-nonsticky-at (match-end 0)))
  4110. t)))
  4111. (defun org-activate-dates (limit)
  4112. "Run through the buffer and add overlays to dates."
  4113. (if (re-search-forward org-tsr-regexp-both limit t)
  4114. (progn
  4115. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4116. (add-text-properties (match-beginning 0) (match-end 0)
  4117. (list 'mouse-face 'highlight
  4118. 'keymap org-mouse-map))
  4119. (org-rear-nonsticky-at (match-end 0))
  4120. (when org-display-custom-times
  4121. (if (match-end 3)
  4122. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4123. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4124. t)))
  4125. (defvar org-target-link-regexp nil
  4126. "Regular expression matching radio targets in plain text.")
  4127. (make-variable-buffer-local 'org-target-link-regexp)
  4128. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4129. "Regular expression matching a link target.")
  4130. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4131. "Regular expression matching a radio target.")
  4132. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4133. "Regular expression matching any target.")
  4134. (defun org-activate-target-links (limit)
  4135. "Run through the buffer and add overlays to target matches."
  4136. (when org-target-link-regexp
  4137. (let ((case-fold-search t))
  4138. (if (re-search-forward org-target-link-regexp limit t)
  4139. (progn
  4140. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4141. (add-text-properties (match-beginning 0) (match-end 0)
  4142. (list 'mouse-face 'highlight
  4143. 'keymap org-mouse-map
  4144. 'help-echo "Radio target link"
  4145. 'org-linked-text t))
  4146. (org-rear-nonsticky-at (match-end 0))
  4147. t)))))
  4148. (defun org-update-radio-target-regexp ()
  4149. "Find all radio targets in this file and update the regular expression."
  4150. (interactive)
  4151. (when (memq 'radio org-activate-links)
  4152. (setq org-target-link-regexp
  4153. (org-make-target-link-regexp (org-all-targets 'radio)))
  4154. (org-restart-font-lock)))
  4155. (defun org-hide-wide-columns (limit)
  4156. (let (s e)
  4157. (setq s (text-property-any (point) (or limit (point-max))
  4158. 'org-cwidth t))
  4159. (when s
  4160. (setq e (next-single-property-change s 'org-cwidth))
  4161. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4162. (goto-char e)
  4163. t)))
  4164. (defvar org-latex-and-specials-regexp nil
  4165. "Regular expression for highlighting export special stuff.")
  4166. (defvar org-match-substring-regexp)
  4167. (defvar org-match-substring-with-braces-regexp)
  4168. ;; This should be with the exporter code, but we also use if for font-locking
  4169. (defconst org-export-html-special-string-regexps
  4170. '(("\\\\-" . "&shy;")
  4171. ("---\\([^-]\\)" . "&mdash;\\1")
  4172. ("--\\([^-]\\)" . "&ndash;\\1")
  4173. ("\\.\\.\\." . "&hellip;"))
  4174. "Regular expressions for special string conversion.")
  4175. (defun org-compute-latex-and-specials-regexp ()
  4176. "Compute regular expression for stuff treated specially by exporters."
  4177. (if (not org-highlight-latex-fragments-and-specials)
  4178. (org-set-local 'org-latex-and-specials-regexp nil)
  4179. (require 'org-exp)
  4180. (let*
  4181. ((matchers (plist-get org-format-latex-options :matchers))
  4182. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4183. org-latex-regexps)))
  4184. (options (org-combine-plists (org-default-export-plist)
  4185. (org-infile-export-plist)))
  4186. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4187. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4188. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4189. (org-export-html-expand (plist-get options :expand-quoted-html))
  4190. (org-export-with-special-strings (plist-get options :special-strings))
  4191. (re-sub
  4192. (cond
  4193. ((equal org-export-with-sub-superscripts '{})
  4194. (list org-match-substring-with-braces-regexp))
  4195. (org-export-with-sub-superscripts
  4196. (list org-match-substring-regexp))
  4197. (t nil)))
  4198. (re-latex
  4199. (if org-export-with-LaTeX-fragments
  4200. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4201. (re-macros
  4202. (if org-export-with-TeX-macros
  4203. (list (concat "\\\\"
  4204. (regexp-opt
  4205. (append (mapcar 'car org-html-entities)
  4206. (if (boundp 'org-latex-entities)
  4207. (mapcar (lambda (x)
  4208. (or (car-safe x) x))
  4209. org-latex-entities)
  4210. nil))
  4211. 'words))) ; FIXME
  4212. ))
  4213. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4214. (re-special (if org-export-with-special-strings
  4215. (mapcar (lambda (x) (car x))
  4216. org-export-html-special-string-regexps)))
  4217. (re-rest
  4218. (delq nil
  4219. (list
  4220. (if org-export-html-expand "@<[^>\n]+>")
  4221. ))))
  4222. (org-set-local
  4223. 'org-latex-and-specials-regexp
  4224. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4225. re-rest) "\\|")))))
  4226. (defun org-do-latex-and-special-faces (limit)
  4227. "Run through the buffer and add overlays to links."
  4228. (when org-latex-and-specials-regexp
  4229. (let (rtn d)
  4230. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4231. limit t))
  4232. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4233. 'face))
  4234. '(org-code org-verbatim underline)))
  4235. (progn
  4236. (setq rtn t
  4237. d (cond ((member (char-after (1+ (match-beginning 0)))
  4238. '(?_ ?^)) 1)
  4239. (t 0)))
  4240. (font-lock-prepend-text-property
  4241. (+ d (match-beginning 0)) (match-end 0)
  4242. 'face 'org-latex-and-export-specials)
  4243. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4244. '(font-lock-multiline t)))))
  4245. rtn)))
  4246. (defun org-restart-font-lock ()
  4247. "Restart font-lock-mode, to force refontification."
  4248. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4249. (font-lock-mode -1)
  4250. (font-lock-mode 1)))
  4251. (defun org-all-targets (&optional radio)
  4252. "Return a list of all targets in this file.
  4253. With optional argument RADIO, only find radio targets."
  4254. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4255. rtn)
  4256. (save-excursion
  4257. (goto-char (point-min))
  4258. (while (re-search-forward re nil t)
  4259. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4260. rtn)))
  4261. (defun org-make-target-link-regexp (targets)
  4262. "Make regular expression matching all strings in TARGETS.
  4263. The regular expression finds the targets also if there is a line break
  4264. between words."
  4265. (and targets
  4266. (concat
  4267. "\\<\\("
  4268. (mapconcat
  4269. (lambda (x)
  4270. (while (string-match " +" x)
  4271. (setq x (replace-match "\\s-+" t t x)))
  4272. x)
  4273. targets
  4274. "\\|")
  4275. "\\)\\>")))
  4276. (defun org-activate-tags (limit)
  4277. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4278. (progn
  4279. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4280. (add-text-properties (match-beginning 1) (match-end 1)
  4281. (list 'mouse-face 'highlight
  4282. 'keymap org-mouse-map))
  4283. (org-rear-nonsticky-at (match-end 1))
  4284. t)))
  4285. (defun org-outline-level ()
  4286. "Compute the outline level of the heading at point.
  4287. This function assumes that the cursor is at the beginning of a line matched
  4288. by outline-regexp. Otherwise it returns garbage.
  4289. If this is called at a normal headline, the level is the number of stars.
  4290. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4291. For plain list items, if they are matched by `outline-regexp', this returns
  4292. 1000 plus the line indentation."
  4293. (save-excursion
  4294. (looking-at outline-regexp)
  4295. (if (match-beginning 1)
  4296. (+ (org-get-string-indentation (match-string 1)) 1000)
  4297. (1- (- (match-end 0) (match-beginning 0))))))
  4298. (defvar org-font-lock-keywords nil)
  4299. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4300. "Regular expression matching a property line.")
  4301. (defvar org-font-lock-hook nil
  4302. "Functions to be called for special font lock stuff.")
  4303. (defun org-font-lock-hook (limit)
  4304. (run-hook-with-args 'org-font-lock-hook limit))
  4305. (defun org-set-font-lock-defaults ()
  4306. (let* ((em org-fontify-emphasized-text)
  4307. (lk org-activate-links)
  4308. (org-font-lock-extra-keywords
  4309. (list
  4310. ;; Call the hook
  4311. '(org-font-lock-hook)
  4312. ;; Headlines
  4313. `(,(if org-fontify-whole-heading-line
  4314. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4315. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4316. (1 (org-get-level-face 1))
  4317. (2 (org-get-level-face 2))
  4318. (3 (org-get-level-face 3)))
  4319. ;; Table lines
  4320. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4321. (1 'org-table t))
  4322. ;; Table internals
  4323. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4324. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4325. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4326. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4327. ;; Drawers
  4328. (list org-drawer-regexp '(0 'org-special-keyword t))
  4329. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4330. ;; Properties
  4331. (list org-property-re
  4332. '(1 'org-special-keyword t)
  4333. '(3 'org-property-value t))
  4334. ;; Links
  4335. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4336. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4337. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4338. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4339. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4340. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4341. (if (memq 'footnote lk) '(org-activate-footnote-links
  4342. (2 'org-footnote t)))
  4343. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4344. '(org-hide-wide-columns (0 nil append))
  4345. ;; TODO lines
  4346. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4347. '(1 (org-get-todo-face 1) t))
  4348. ;; DONE
  4349. (if org-fontify-done-headline
  4350. (list (concat "^[*]+ +\\<\\("
  4351. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4352. "\\)\\(.*\\)")
  4353. '(2 'org-headline-done t))
  4354. nil)
  4355. ;; Priorities
  4356. '(org-font-lock-add-priority-faces)
  4357. ;; Tags
  4358. '(org-font-lock-add-tag-faces)
  4359. ;; Special keywords
  4360. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4361. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4362. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4363. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4364. ;; Emphasis
  4365. (if em
  4366. (if (featurep 'xemacs)
  4367. '(org-do-emphasis-faces (0 nil append))
  4368. '(org-do-emphasis-faces)))
  4369. ;; Checkboxes
  4370. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4371. 2 'org-checkbox prepend)
  4372. (if org-provide-checkbox-statistics
  4373. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4374. (0 (org-get-checkbox-statistics-face) t)))
  4375. ;; Description list items
  4376. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4377. 2 'bold prepend)
  4378. ;; ARCHIVEd headings
  4379. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4380. '(1 'org-archived prepend))
  4381. ;; Specials
  4382. '(org-do-latex-and-special-faces)
  4383. ;; Code
  4384. '(org-activate-code (1 'org-code t))
  4385. ;; COMMENT
  4386. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4387. "\\|" org-quote-string "\\)\\>")
  4388. '(1 'org-special-keyword t))
  4389. '("^#.*" (0 'font-lock-comment-face t))
  4390. ;; Blocks and meta lines
  4391. '(org-fontify-meta-lines-and-blocks)
  4392. )))
  4393. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4394. ;; Now set the full font-lock-keywords
  4395. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4396. (org-set-local 'font-lock-defaults
  4397. '(org-font-lock-keywords t nil nil backward-paragraph))
  4398. (kill-local-variable 'font-lock-keywords) nil))
  4399. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4400. "Fontify string S like in Org-mode"
  4401. (with-temp-buffer
  4402. (insert s)
  4403. (let ((org-odd-levels-only odd-levels))
  4404. (org-mode)
  4405. (font-lock-fontify-buffer)
  4406. (buffer-string))))
  4407. (defvar org-m nil)
  4408. (defvar org-l nil)
  4409. (defvar org-f nil)
  4410. (defun org-get-level-face (n)
  4411. "Get the right face for match N in font-lock matching of headlines."
  4412. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4413. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4414. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4415. (cond
  4416. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4417. ((eq n 2) org-f)
  4418. (t (if org-level-color-stars-only nil org-f))))
  4419. (defun org-get-todo-face (kwd)
  4420. "Get the right face for a TODO keyword KWD.
  4421. If KWD is a number, get the corresponding match group."
  4422. (if (numberp kwd) (setq kwd (match-string kwd)))
  4423. (or (cdr (assoc kwd org-todo-keyword-faces))
  4424. (and (member kwd org-done-keywords) 'org-done)
  4425. 'org-todo))
  4426. (defun org-font-lock-add-tag-faces (limit)
  4427. "Add the special tag faces."
  4428. (when (and org-tag-faces org-tags-special-faces-re)
  4429. (while (re-search-forward org-tags-special-faces-re limit t)
  4430. (add-text-properties (match-beginning 1) (match-end 1)
  4431. (list 'face (org-get-tag-face 1)
  4432. 'font-lock-fontified t))
  4433. (backward-char 1))))
  4434. (defun org-font-lock-add-priority-faces (limit)
  4435. "Add the special priority faces."
  4436. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4437. (add-text-properties
  4438. (match-beginning 0) (match-end 0)
  4439. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4440. org-priority-faces))
  4441. 'org-special-keyword)
  4442. 'font-lock-fontified t))))
  4443. (defun org-get-tag-face (kwd)
  4444. "Get the right face for a TODO keyword KWD.
  4445. If KWD is a number, get the corresponding match group."
  4446. (if (numberp kwd) (setq kwd (match-string kwd)))
  4447. (or (cdr (assoc kwd org-tag-faces))
  4448. 'org-tag))
  4449. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4450. "Remove fontification and activation overlays from links."
  4451. (font-lock-default-unfontify-region beg end)
  4452. (let* ((buffer-undo-list t)
  4453. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4454. (inhibit-modification-hooks t)
  4455. deactivate-mark buffer-file-name buffer-file-truename)
  4456. (remove-text-properties
  4457. beg end
  4458. (if org-indent-mode
  4459. ;; also remove line-prefix and wrap-prefix properties
  4460. '(mouse-face t keymap t org-linked-text t
  4461. invisible t intangible t
  4462. line-prefix t wrap-prefix t
  4463. org-no-flyspell t)
  4464. '(mouse-face t keymap t org-linked-text t
  4465. invisible t intangible t
  4466. org-no-flyspell t)))))
  4467. ;;;; Visibility cycling, including org-goto and indirect buffer
  4468. ;;; Cycling
  4469. (defvar org-cycle-global-status nil)
  4470. (make-variable-buffer-local 'org-cycle-global-status)
  4471. (defvar org-cycle-subtree-status nil)
  4472. (make-variable-buffer-local 'org-cycle-subtree-status)
  4473. ;;;###autoload
  4474. (defvar org-inlinetask-min-level)
  4475. (defun org-cycle (&optional arg)
  4476. "TAB-action and visibility cycling for Org-mode.
  4477. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4478. is outline visibility cycling, but it also invokes other actions
  4479. in special contexts.
  4480. - When this function is called with a prefix argument, rotate the entire
  4481. buffer through 3 states (global cycling)
  4482. 1. OVERVIEW: Show only top-level headlines.
  4483. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4484. 3. SHOW ALL: Show everything.
  4485. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4486. determined by the variable `org-startup-folded', and by any VISIBILITY
  4487. properties in the buffer.
  4488. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4489. including any drawers.
  4490. - When inside a table, re-align the table and move to the next field.
  4491. - When point is at the beginning of a headline, rotate the subtree started
  4492. by this line through 3 different states (local cycling)
  4493. 1. FOLDED: Only the main headline is shown.
  4494. 2. CHILDREN: The main headline and the direct children are shown.
  4495. From this state, you can move to one of the children
  4496. and zoom in further.
  4497. 3. SUBTREE: Show the entire subtree, including body text.
  4498. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4499. - When there is a numeric prefix, go up to a heading with level ARG, do
  4500. a `show-subtree' and return to the previous cursor position. If ARG
  4501. is negative, go up that many levels.
  4502. - When point is not at the beginning of a headline, execute the global
  4503. binding for TAB, which is re-indenting the line. See the option
  4504. `org-cycle-emulate-tab' for details.
  4505. - Special case: if point is at the beginning of the buffer and there is
  4506. no headline in line 1, this function will act as if called with prefix arg.
  4507. But only if also the variable `org-cycle-global-at-bob' is t."
  4508. (interactive "P")
  4509. (org-load-modules-maybe)
  4510. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4511. (and org-cycle-level-after-item/entry-creation
  4512. (or (org-cycle-level)
  4513. (org-cycle-item-indentation))))
  4514. (let* ((limit-level
  4515. (or org-cycle-max-level
  4516. (and (boundp 'org-inlinetask-min-level)
  4517. org-inlinetask-min-level
  4518. (1- org-inlinetask-min-level))))
  4519. (nstars (and limit-level
  4520. (if org-odd-levels-only
  4521. (and limit-level (1- (* limit-level 2)))
  4522. limit-level)))
  4523. (outline-regexp
  4524. (cond
  4525. ((not (org-mode-p)) outline-regexp)
  4526. ((or (eq org-cycle-include-plain-lists 'integrate)
  4527. (and org-cycle-include-plain-lists (org-at-item-p)))
  4528. (concat "\\(?:\\*"
  4529. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4530. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4531. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4532. (bob-special (and org-cycle-global-at-bob (bobp)
  4533. (not (looking-at outline-regexp))))
  4534. (org-cycle-hook
  4535. (if bob-special
  4536. (delq 'org-optimize-window-after-visibility-change
  4537. (copy-sequence org-cycle-hook))
  4538. org-cycle-hook))
  4539. (pos (point)))
  4540. (if (or bob-special (equal arg '(4)))
  4541. ;; special case: use global cycling
  4542. (setq arg t))
  4543. (cond
  4544. ((equal arg '(16))
  4545. (org-set-startup-visibility)
  4546. (message "Startup visibility, plus VISIBILITY properties"))
  4547. ((equal arg '(64))
  4548. (show-all)
  4549. (message "Entire buffer visible, including drawers"))
  4550. ((org-at-table-p 'any)
  4551. ;; Enter the table or move to the next field in the table
  4552. (or (org-table-recognize-table.el)
  4553. (progn
  4554. (if arg (org-table-edit-field t)
  4555. (org-table-justify-field-maybe)
  4556. (call-interactively 'org-table-next-field)))))
  4557. ((run-hook-with-args-until-success
  4558. 'org-tab-after-check-for-table-hook))
  4559. ((eq arg t) ;; Global cycling
  4560. (org-cycle-internal-global))
  4561. ((and org-drawers org-drawer-regexp
  4562. (save-excursion
  4563. (beginning-of-line 1)
  4564. (looking-at org-drawer-regexp)))
  4565. ;; Toggle block visibility
  4566. (org-flag-drawer
  4567. (not (get-char-property (match-end 0) 'invisible))))
  4568. ((integerp arg)
  4569. ;; Show-subtree, ARG levels up from here.
  4570. (save-excursion
  4571. (org-back-to-heading)
  4572. (outline-up-heading (if (< arg 0) (- arg)
  4573. (- (funcall outline-level) arg)))
  4574. (org-show-subtree)))
  4575. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4576. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4577. (org-cycle-internal-local))
  4578. ;; TAB emulation and template completion
  4579. (buffer-read-only (org-back-to-heading))
  4580. ((run-hook-with-args-until-success
  4581. 'org-tab-after-check-for-cycling-hook))
  4582. ((org-try-structure-completion))
  4583. ((org-try-cdlatex-tab))
  4584. ((run-hook-with-args-until-success
  4585. 'org-tab-before-tab-emulation-hook))
  4586. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4587. (or (not (bolp))
  4588. (not (looking-at outline-regexp))))
  4589. (call-interactively (global-key-binding "\t")))
  4590. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4591. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4592. (or (and (eq org-cycle-emulate-tab 'white)
  4593. (= (match-end 0) (point-at-eol)))
  4594. (and (eq org-cycle-emulate-tab 'whitestart)
  4595. (>= (match-end 0) pos))))
  4596. t
  4597. (eq org-cycle-emulate-tab t))
  4598. (call-interactively (global-key-binding "\t")))
  4599. (t (save-excursion
  4600. (org-back-to-heading)
  4601. (org-cycle)))))))
  4602. (defun org-cycle-internal-global ()
  4603. "Do the global cycling action."
  4604. (cond
  4605. ((and (eq last-command this-command)
  4606. (eq org-cycle-global-status 'overview))
  4607. ;; We just created the overview - now do table of contents
  4608. ;; This can be slow in very large buffers, so indicate action
  4609. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4610. (message "CONTENTS...")
  4611. (org-content)
  4612. (message "CONTENTS...done")
  4613. (setq org-cycle-global-status 'contents)
  4614. (run-hook-with-args 'org-cycle-hook 'contents))
  4615. ((and (eq last-command this-command)
  4616. (eq org-cycle-global-status 'contents))
  4617. ;; We just showed the table of contents - now show everything
  4618. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4619. (show-all)
  4620. (message "SHOW ALL")
  4621. (setq org-cycle-global-status 'all)
  4622. (run-hook-with-args 'org-cycle-hook 'all))
  4623. (t
  4624. ;; Default action: go to overview
  4625. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4626. (org-overview)
  4627. (message "OVERVIEW")
  4628. (setq org-cycle-global-status 'overview)
  4629. (run-hook-with-args 'org-cycle-hook 'overview))))
  4630. (defun org-cycle-internal-local ()
  4631. "Do the local cycling action."
  4632. (org-back-to-heading)
  4633. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4634. ;; First, some boundaries
  4635. (save-excursion
  4636. (org-back-to-heading)
  4637. (setq level (funcall outline-level))
  4638. (save-excursion
  4639. (beginning-of-line 2)
  4640. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4641. ; XEmacs does not have `next-single-char-property-change'
  4642. ; I'm not sure about Emacs 21.
  4643. (while (and (not (eobp)) ;; this is like `next-line'
  4644. (get-char-property (1- (point)) 'invisible))
  4645. (beginning-of-line 2))
  4646. (while (and (not (eobp)) ;; this is like `next-line'
  4647. (get-char-property (1- (point)) 'invisible))
  4648. (goto-char (next-single-char-property-change (point) 'invisible))
  4649. ;;;??? (or (bolp) (beginning-of-line 2))))
  4650. (and (eolp) (beginning-of-line 2))))
  4651. (setq eol (point)))
  4652. (outline-end-of-heading) (setq eoh (point))
  4653. (save-excursion
  4654. (outline-next-heading)
  4655. (setq has-children (and (org-at-heading-p t)
  4656. (> (funcall outline-level) level))))
  4657. (org-end-of-subtree t)
  4658. (unless (eobp)
  4659. (skip-chars-forward " \t\n")
  4660. (beginning-of-line 1) ; in case this is an item
  4661. )
  4662. (setq eos (if (eobp) (point) (1- (point)))))
  4663. ;; Find out what to do next and set `this-command'
  4664. (cond
  4665. ((= eos eoh)
  4666. ;; Nothing is hidden behind this heading
  4667. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4668. (message "EMPTY ENTRY")
  4669. (setq org-cycle-subtree-status nil)
  4670. (save-excursion
  4671. (goto-char eos)
  4672. (outline-next-heading)
  4673. (if (org-invisible-p) (org-flag-heading nil))))
  4674. ((and (or (>= eol eos)
  4675. (not (string-match "\\S-" (buffer-substring eol eos))))
  4676. (or has-children
  4677. (not (setq children-skipped
  4678. org-cycle-skip-children-state-if-no-children))))
  4679. ;; Entire subtree is hidden in one line: children view
  4680. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4681. (org-show-entry)
  4682. (show-children)
  4683. (message "CHILDREN")
  4684. (save-excursion
  4685. (goto-char eos)
  4686. (outline-next-heading)
  4687. (if (org-invisible-p) (org-flag-heading nil)))
  4688. (setq org-cycle-subtree-status 'children)
  4689. (run-hook-with-args 'org-cycle-hook 'children))
  4690. ((or children-skipped
  4691. (and (eq last-command this-command)
  4692. (eq org-cycle-subtree-status 'children)))
  4693. ;; We just showed the children, or no children are there,
  4694. ;; now show everything.
  4695. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4696. (org-show-subtree)
  4697. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4698. (setq org-cycle-subtree-status 'subtree)
  4699. (run-hook-with-args 'org-cycle-hook 'subtree))
  4700. (t
  4701. ;; Default action: hide the subtree.
  4702. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4703. (hide-subtree)
  4704. (message "FOLDED")
  4705. (setq org-cycle-subtree-status 'folded)
  4706. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4707. ;;;###autoload
  4708. (defun org-global-cycle (&optional arg)
  4709. "Cycle the global visibility. For details see `org-cycle'.
  4710. With C-u prefix arg, switch to startup visibility.
  4711. With a numeric prefix, show all headlines up to that level."
  4712. (interactive "P")
  4713. (let ((org-cycle-include-plain-lists
  4714. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4715. (cond
  4716. ((integerp arg)
  4717. (show-all)
  4718. (hide-sublevels arg)
  4719. (setq org-cycle-global-status 'contents))
  4720. ((equal arg '(4))
  4721. (org-set-startup-visibility)
  4722. (message "Startup visibility, plus VISIBILITY properties."))
  4723. (t
  4724. (org-cycle '(4))))))
  4725. (defun org-set-startup-visibility ()
  4726. "Set the visibility required by startup options and properties."
  4727. (cond
  4728. ((eq org-startup-folded t)
  4729. (org-cycle '(4)))
  4730. ((eq org-startup-folded 'content)
  4731. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4732. (org-cycle '(4)) (org-cycle '(4)))))
  4733. (unless (eq org-startup-folded 'showeverything)
  4734. (if org-hide-block-startup (org-hide-block-all))
  4735. (org-set-visibility-according-to-property 'no-cleanup)
  4736. (org-cycle-hide-archived-subtrees 'all)
  4737. (org-cycle-hide-drawers 'all)
  4738. (org-cycle-show-empty-lines 'all)))
  4739. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4740. "Switch subtree visibilities according to :VISIBILITY: property."
  4741. (interactive)
  4742. (let (org-show-entry-below state)
  4743. (save-excursion
  4744. (goto-char (point-min))
  4745. (while (re-search-forward
  4746. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4747. nil t)
  4748. (setq state (match-string 1))
  4749. (save-excursion
  4750. (org-back-to-heading t)
  4751. (hide-subtree)
  4752. (org-reveal)
  4753. (cond
  4754. ((equal state '("fold" "folded"))
  4755. (hide-subtree))
  4756. ((equal state "children")
  4757. (org-show-hidden-entry)
  4758. (show-children))
  4759. ((equal state "content")
  4760. (save-excursion
  4761. (save-restriction
  4762. (org-narrow-to-subtree)
  4763. (org-content))))
  4764. ((member state '("all" "showall"))
  4765. (show-subtree)))))
  4766. (unless no-cleanup
  4767. (org-cycle-hide-archived-subtrees 'all)
  4768. (org-cycle-hide-drawers 'all)
  4769. (org-cycle-show-empty-lines 'all)))))
  4770. (defun org-overview ()
  4771. "Switch to overview mode, showing only top-level headlines.
  4772. Really, this shows all headlines with level equal or greater than the level
  4773. of the first headline in the buffer. This is important, because if the
  4774. first headline is not level one, then (hide-sublevels 1) gives confusing
  4775. results."
  4776. (interactive)
  4777. (let ((level (save-excursion
  4778. (goto-char (point-min))
  4779. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4780. (progn
  4781. (goto-char (match-beginning 0))
  4782. (funcall outline-level))))))
  4783. (and level (hide-sublevels level))))
  4784. (defun org-content (&optional arg)
  4785. "Show all headlines in the buffer, like a table of contents.
  4786. With numerical argument N, show content up to level N."
  4787. (interactive "P")
  4788. (save-excursion
  4789. ;; Visit all headings and show their offspring
  4790. (and (integerp arg) (org-overview))
  4791. (goto-char (point-max))
  4792. (catch 'exit
  4793. (while (and (progn (condition-case nil
  4794. (outline-previous-visible-heading 1)
  4795. (error (goto-char (point-min))))
  4796. t)
  4797. (looking-at outline-regexp))
  4798. (if (integerp arg)
  4799. (show-children (1- arg))
  4800. (show-branches))
  4801. (if (bobp) (throw 'exit nil))))))
  4802. (defun org-optimize-window-after-visibility-change (state)
  4803. "Adjust the window after a change in outline visibility.
  4804. This function is the default value of the hook `org-cycle-hook'."
  4805. (when (get-buffer-window (current-buffer))
  4806. (cond
  4807. ((eq state 'content) nil)
  4808. ((eq state 'all) nil)
  4809. ((eq state 'folded) nil)
  4810. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4811. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4812. ;; FIXME: no longer in use
  4813. (defun org-compact-display-after-subtree-move ()
  4814. "Show a compacter version of the tree of the entry's parent."
  4815. (save-excursion
  4816. (if (org-up-heading-safe)
  4817. (progn
  4818. (hide-subtree)
  4819. (show-entry)
  4820. (show-children)
  4821. (org-cycle-show-empty-lines 'children)
  4822. (org-cycle-hide-drawers 'children))
  4823. (org-overview))))
  4824. (defun org-remove-empty-overlays-at (pos)
  4825. "Remove outline overlays that do not contain non-white stuff."
  4826. (mapc
  4827. (lambda (o)
  4828. (and (eq 'outline (org-overlay-get o 'invisible))
  4829. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4830. (org-overlay-end o))))
  4831. (org-delete-overlay o)))
  4832. (org-overlays-at pos)))
  4833. (defun org-clean-visibility-after-subtree-move ()
  4834. "Fix visibility issues after moving a subtree."
  4835. ;; First, find a reasonable region to look at:
  4836. ;; Start two siblings above, end three below
  4837. (let* ((beg (save-excursion
  4838. (and (org-get-last-sibling)
  4839. (org-get-last-sibling))
  4840. (point)))
  4841. (end (save-excursion
  4842. (and (org-get-next-sibling)
  4843. (org-get-next-sibling)
  4844. (org-get-next-sibling))
  4845. (if (org-at-heading-p)
  4846. (point-at-eol)
  4847. (point))))
  4848. (level (looking-at "\\*+"))
  4849. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4850. (save-excursion
  4851. (save-restriction
  4852. (narrow-to-region beg end)
  4853. (when re
  4854. ;; Properly fold already folded siblings
  4855. (goto-char (point-min))
  4856. (while (re-search-forward re nil t)
  4857. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4858. (hide-entry))))
  4859. (org-cycle-show-empty-lines 'overview)
  4860. (org-cycle-hide-drawers 'overview)))))
  4861. (defun org-cycle-show-empty-lines (state)
  4862. "Show empty lines above all visible headlines.
  4863. The region to be covered depends on STATE when called through
  4864. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4865. entire buffer covered. Note that an empty line is only shown if there
  4866. are at least `org-cycle-separator-lines' empty lines before the headline."
  4867. (when (not (= org-cycle-separator-lines 0))
  4868. (save-excursion
  4869. (let* ((n (abs org-cycle-separator-lines))
  4870. (re (cond
  4871. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4872. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4873. (t (let ((ns (number-to-string (- n 2))))
  4874. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4875. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4876. beg end b e)
  4877. (cond
  4878. ((memq state '(overview contents t))
  4879. (setq beg (point-min) end (point-max)))
  4880. ((memq state '(children folded))
  4881. (setq beg (point) end (progn (org-end-of-subtree t t)
  4882. (beginning-of-line 2)
  4883. (point)))))
  4884. (when beg
  4885. (goto-char beg)
  4886. (while (re-search-forward re end t)
  4887. (unless (get-char-property (match-end 1) 'invisible)
  4888. (setq e (match-end 1))
  4889. (if (< org-cycle-separator-lines 0)
  4890. (setq b (save-excursion
  4891. (goto-char (match-beginning 0))
  4892. (org-back-over-empty-lines)
  4893. (if (save-excursion
  4894. (goto-char (max (point-min) (1- (point))))
  4895. (org-on-heading-p))
  4896. (1- (point))
  4897. (point))))
  4898. (setq b (match-beginning 1)))
  4899. (outline-flag-region b e nil)))))))
  4900. ;; Never hide empty lines at the end of the file.
  4901. (save-excursion
  4902. (goto-char (point-max))
  4903. (outline-previous-heading)
  4904. (outline-end-of-heading)
  4905. (if (and (looking-at "[ \t\n]+")
  4906. (= (match-end 0) (point-max)))
  4907. (outline-flag-region (point) (match-end 0) nil))))
  4908. (defun org-show-empty-lines-in-parent ()
  4909. "Move to the parent and re-show empty lines before visible headlines."
  4910. (save-excursion
  4911. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4912. (org-cycle-show-empty-lines context))))
  4913. (defun org-files-list ()
  4914. "Return `org-agenda-files' list, plus all open org-mode files.
  4915. This is useful for operations that need to scan all of a user's
  4916. open and agenda-wise Org files."
  4917. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  4918. (dolist (buf (buffer-list))
  4919. (with-current-buffer buf
  4920. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  4921. (let ((file (expand-file-name (buffer-file-name))))
  4922. (unless (member file files)
  4923. (push file files))))))
  4924. files))
  4925. (defsubst org-entry-beginning-position ()
  4926. "Return the beginning position of the current entry."
  4927. (save-excursion (outline-back-to-heading t) (point)))
  4928. (defsubst org-entry-end-position ()
  4929. "Return the end position of the current entry."
  4930. (save-excursion (outline-next-heading) (point)))
  4931. (defun org-cycle-hide-drawers (state)
  4932. "Re-hide all drawers after a visibility state change."
  4933. (when (and (org-mode-p)
  4934. (not (memq state '(overview folded contents))))
  4935. (save-excursion
  4936. (let* ((globalp (memq state '(contents all)))
  4937. (beg (if globalp (point-min) (point)))
  4938. (end (if globalp (point-max)
  4939. (if (eq state 'children)
  4940. (save-excursion (outline-next-heading) (point))
  4941. (org-end-of-subtree t)))))
  4942. (goto-char beg)
  4943. (while (re-search-forward org-drawer-regexp end t)
  4944. (org-flag-drawer t))))))
  4945. (defun org-flag-drawer (flag)
  4946. (save-excursion
  4947. (beginning-of-line 1)
  4948. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4949. (let ((b (match-end 0))
  4950. (outline-regexp org-outline-regexp))
  4951. (if (re-search-forward
  4952. "^[ \t]*:END:"
  4953. (save-excursion (outline-next-heading) (point)) t)
  4954. (outline-flag-region b (point-at-eol) flag)
  4955. (error ":END: line missing at position %s" b))))))
  4956. (defun org-subtree-end-visible-p ()
  4957. "Is the end of the current subtree visible?"
  4958. (pos-visible-in-window-p
  4959. (save-excursion (org-end-of-subtree t) (point))))
  4960. (defun org-first-headline-recenter (&optional N)
  4961. "Move cursor to the first headline and recenter the headline.
  4962. Optional argument N means, put the headline into the Nth line of the window."
  4963. (goto-char (point-min))
  4964. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4965. (beginning-of-line)
  4966. (recenter (prefix-numeric-value N))))
  4967. ;;; Folding of blocks
  4968. (defconst org-block-regexp
  4969. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4970. "Regular expression for hiding blocks.")
  4971. (defvar org-hide-block-overlays nil
  4972. "Overlays hiding blocks.")
  4973. (make-variable-buffer-local 'org-hide-block-overlays)
  4974. (defun org-block-map (function &optional start end)
  4975. "Call func at the head of all source blocks in the current
  4976. buffer. Optional arguments START and END can be used to limit
  4977. the range."
  4978. (let ((start (or start (point-min)))
  4979. (end (or end (point-max))))
  4980. (save-excursion
  4981. (goto-char start)
  4982. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4983. (save-excursion
  4984. (save-match-data
  4985. (goto-char (match-beginning 0))
  4986. (funcall function)))))))
  4987. (defun org-hide-block-toggle-all ()
  4988. "Toggle the visibility of all blocks in the current buffer."
  4989. (org-block-map #'org-hide-block-toggle))
  4990. (defun org-hide-block-all ()
  4991. "Fold all blocks in the current buffer."
  4992. (interactive)
  4993. (org-show-block-all)
  4994. (org-block-map #'org-hide-block-toggle-maybe))
  4995. (defun org-show-block-all ()
  4996. "Unfold all blocks in the current buffer."
  4997. (mapc 'org-delete-overlay org-hide-block-overlays)
  4998. (setq org-hide-block-overlays nil))
  4999. (defun org-hide-block-toggle-maybe ()
  5000. "Toggle visibility of block at point."
  5001. (interactive)
  5002. (let ((case-fold-search t))
  5003. (if (save-excursion
  5004. (beginning-of-line 1)
  5005. (looking-at org-block-regexp))
  5006. (progn (org-hide-block-toggle)
  5007. t) ;; to signal that we took action
  5008. nil))) ;; to signal that we did not
  5009. (defun org-hide-block-toggle (&optional force)
  5010. "Toggle the visibility of the current block."
  5011. (interactive)
  5012. (save-excursion
  5013. (beginning-of-line)
  5014. (if (re-search-forward org-block-regexp nil t)
  5015. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5016. (end (match-end 0)) ;; end of entire body
  5017. ov)
  5018. (if (memq t (mapcar (lambda (overlay)
  5019. (eq (org-overlay-get overlay 'invisible)
  5020. 'org-hide-block))
  5021. (org-overlays-at start)))
  5022. (if (or (not force) (eq force 'off))
  5023. (mapc (lambda (ov)
  5024. (when (member ov org-hide-block-overlays)
  5025. (setq org-hide-block-overlays
  5026. (delq ov org-hide-block-overlays)))
  5027. (when (eq (org-overlay-get ov 'invisible)
  5028. 'org-hide-block)
  5029. (org-delete-overlay ov)))
  5030. (org-overlays-at start)))
  5031. (setq ov (org-make-overlay start end))
  5032. (org-overlay-put ov 'invisible 'org-hide-block)
  5033. ;; make the block accessible to isearch
  5034. (org-overlay-put
  5035. ov 'isearch-open-invisible
  5036. (lambda (ov)
  5037. (when (member ov org-hide-block-overlays)
  5038. (setq org-hide-block-overlays
  5039. (delq ov org-hide-block-overlays)))
  5040. (when (eq (org-overlay-get ov 'invisible)
  5041. 'org-hide-block)
  5042. (org-delete-overlay ov))))
  5043. (push ov org-hide-block-overlays)))
  5044. (error "Not looking at a source block"))))
  5045. ;; org-tab-after-check-for-cycling-hook
  5046. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5047. ;; Remove overlays when changing major mode
  5048. (add-hook 'org-mode-hook
  5049. (lambda () (org-add-hook 'change-major-mode-hook
  5050. 'org-show-block-all 'append 'local)))
  5051. ;;; Org-goto
  5052. (defvar org-goto-window-configuration nil)
  5053. (defvar org-goto-marker nil)
  5054. (defvar org-goto-map
  5055. (let ((map (make-sparse-keymap)))
  5056. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5057. (while (setq cmd (pop cmds))
  5058. (substitute-key-definition cmd cmd map global-map)))
  5059. (suppress-keymap map)
  5060. (org-defkey map "\C-m" 'org-goto-ret)
  5061. (org-defkey map [(return)] 'org-goto-ret)
  5062. (org-defkey map [(left)] 'org-goto-left)
  5063. (org-defkey map [(right)] 'org-goto-right)
  5064. (org-defkey map [(control ?g)] 'org-goto-quit)
  5065. (org-defkey map "\C-i" 'org-cycle)
  5066. (org-defkey map [(tab)] 'org-cycle)
  5067. (org-defkey map [(down)] 'outline-next-visible-heading)
  5068. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5069. (if org-goto-auto-isearch
  5070. (if (fboundp 'define-key-after)
  5071. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5072. nil)
  5073. (org-defkey map "q" 'org-goto-quit)
  5074. (org-defkey map "n" 'outline-next-visible-heading)
  5075. (org-defkey map "p" 'outline-previous-visible-heading)
  5076. (org-defkey map "f" 'outline-forward-same-level)
  5077. (org-defkey map "b" 'outline-backward-same-level)
  5078. (org-defkey map "u" 'outline-up-heading))
  5079. (org-defkey map "/" 'org-occur)
  5080. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5081. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5082. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5083. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5084. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5085. map))
  5086. (defconst org-goto-help
  5087. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5088. RET=jump to location [Q]uit and return to previous location
  5089. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5090. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5091. ;; FIXME: Docstring does not mention both interfaces
  5092. (defun org-goto (&optional alternative-interface)
  5093. "Look up a different location in the current file, keeping current visibility.
  5094. When you want look-up or go to a different location in a document, the
  5095. fastest way is often to fold the entire buffer and then dive into the tree.
  5096. This method has the disadvantage, that the previous location will be folded,
  5097. which may not be what you want.
  5098. This command works around this by showing a copy of the current buffer
  5099. in an indirect buffer, in overview mode. You can dive into the tree in
  5100. that copy, use org-occur and incremental search to find a location.
  5101. When pressing RET or `Q', the command returns to the original buffer in
  5102. which the visibility is still unchanged. After RET is will also jump to
  5103. the location selected in the indirect buffer and expose the
  5104. the headline hierarchy above."
  5105. (interactive "P")
  5106. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5107. (org-refile-use-outline-path t)
  5108. (org-refile-target-verify-function nil)
  5109. (interface
  5110. (if (not alternative-interface)
  5111. org-goto-interface
  5112. (if (eq org-goto-interface 'outline)
  5113. 'outline-path-completion
  5114. 'outline)))
  5115. (org-goto-start-pos (point))
  5116. (selected-point
  5117. (if (eq interface 'outline)
  5118. (car (org-get-location (current-buffer) org-goto-help))
  5119. (nth 3 (org-refile-get-location "Goto: ")))))
  5120. (if selected-point
  5121. (progn
  5122. (org-mark-ring-push org-goto-start-pos)
  5123. (goto-char selected-point)
  5124. (if (or (org-invisible-p) (org-invisible-p2))
  5125. (org-show-context 'org-goto)))
  5126. (message "Quit"))))
  5127. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5128. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5129. (defvar org-goto-local-auto-isearch-map) ; defined below
  5130. (defun org-get-location (buf help)
  5131. "Let the user select a location in the Org-mode buffer BUF.
  5132. This function uses a recursive edit. It returns the selected position
  5133. or nil."
  5134. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5135. (isearch-hide-immediately nil)
  5136. (isearch-search-fun-function
  5137. (lambda () 'org-goto-local-search-headings))
  5138. (org-goto-selected-point org-goto-exit-command))
  5139. (save-excursion
  5140. (save-window-excursion
  5141. (delete-other-windows)
  5142. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5143. (switch-to-buffer
  5144. (condition-case nil
  5145. (make-indirect-buffer (current-buffer) "*org-goto*")
  5146. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5147. (with-output-to-temp-buffer "*Help*"
  5148. (princ help))
  5149. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5150. (setq buffer-read-only nil)
  5151. (let ((org-startup-truncated t)
  5152. (org-startup-folded nil)
  5153. (org-startup-align-all-tables nil))
  5154. (org-mode)
  5155. (org-overview))
  5156. (setq buffer-read-only t)
  5157. (if (and (boundp 'org-goto-start-pos)
  5158. (integer-or-marker-p org-goto-start-pos))
  5159. (let ((org-show-hierarchy-above t)
  5160. (org-show-siblings t)
  5161. (org-show-following-heading t))
  5162. (goto-char org-goto-start-pos)
  5163. (and (org-invisible-p) (org-show-context)))
  5164. (goto-char (point-min)))
  5165. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5166. (message "Select location and press RET")
  5167. (use-local-map org-goto-map)
  5168. (recursive-edit)
  5169. ))
  5170. (kill-buffer "*org-goto*")
  5171. (cons org-goto-selected-point org-goto-exit-command)))
  5172. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5173. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5174. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5175. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5176. (defun org-goto-local-search-headings (string bound noerror)
  5177. "Search and make sure that any matches are in headlines."
  5178. (catch 'return
  5179. (while (if isearch-forward
  5180. (search-forward string bound noerror)
  5181. (search-backward string bound noerror))
  5182. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5183. (and (member :headline context)
  5184. (not (member :tags context))))
  5185. (throw 'return (point))))))
  5186. (defun org-goto-local-auto-isearch ()
  5187. "Start isearch."
  5188. (interactive)
  5189. (goto-char (point-min))
  5190. (let ((keys (this-command-keys)))
  5191. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5192. (isearch-mode t)
  5193. (isearch-process-search-char (string-to-char keys)))))
  5194. (defun org-goto-ret (&optional arg)
  5195. "Finish `org-goto' by going to the new location."
  5196. (interactive "P")
  5197. (setq org-goto-selected-point (point)
  5198. org-goto-exit-command 'return)
  5199. (throw 'exit nil))
  5200. (defun org-goto-left ()
  5201. "Finish `org-goto' by going to the new location."
  5202. (interactive)
  5203. (if (org-on-heading-p)
  5204. (progn
  5205. (beginning-of-line 1)
  5206. (setq org-goto-selected-point (point)
  5207. org-goto-exit-command 'left)
  5208. (throw 'exit nil))
  5209. (error "Not on a heading")))
  5210. (defun org-goto-right ()
  5211. "Finish `org-goto' by going to the new location."
  5212. (interactive)
  5213. (if (org-on-heading-p)
  5214. (progn
  5215. (setq org-goto-selected-point (point)
  5216. org-goto-exit-command 'right)
  5217. (throw 'exit nil))
  5218. (error "Not on a heading")))
  5219. (defun org-goto-quit ()
  5220. "Finish `org-goto' without cursor motion."
  5221. (interactive)
  5222. (setq org-goto-selected-point nil)
  5223. (setq org-goto-exit-command 'quit)
  5224. (throw 'exit nil))
  5225. ;;; Indirect buffer display of subtrees
  5226. (defvar org-indirect-dedicated-frame nil
  5227. "This is the frame being used for indirect tree display.")
  5228. (defvar org-last-indirect-buffer nil)
  5229. (defun org-tree-to-indirect-buffer (&optional arg)
  5230. "Create indirect buffer and narrow it to current subtree.
  5231. With numerical prefix ARG, go up to this level and then take that tree.
  5232. If ARG is negative, go up that many levels.
  5233. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5234. indirect buffer previously made with this command, to avoid proliferation of
  5235. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5236. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5237. is kept so that you can work with several indirect buffers at the same time.
  5238. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5239. requests that a new frame be made for the new buffer, so that the dedicated
  5240. frame is not changed."
  5241. (interactive "P")
  5242. (let ((cbuf (current-buffer))
  5243. (cwin (selected-window))
  5244. (pos (point))
  5245. beg end level heading ibuf)
  5246. (save-excursion
  5247. (org-back-to-heading t)
  5248. (when (numberp arg)
  5249. (setq level (org-outline-level))
  5250. (if (< arg 0) (setq arg (+ level arg)))
  5251. (while (> (setq level (org-outline-level)) arg)
  5252. (outline-up-heading 1 t)))
  5253. (setq beg (point)
  5254. heading (org-get-heading))
  5255. (org-end-of-subtree t t) (setq end (point)))
  5256. (if (and (buffer-live-p org-last-indirect-buffer)
  5257. (not (eq org-indirect-buffer-display 'new-frame))
  5258. (not arg))
  5259. (kill-buffer org-last-indirect-buffer))
  5260. (setq ibuf (org-get-indirect-buffer cbuf)
  5261. org-last-indirect-buffer ibuf)
  5262. (cond
  5263. ((or (eq org-indirect-buffer-display 'new-frame)
  5264. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5265. (select-frame (make-frame))
  5266. (delete-other-windows)
  5267. (switch-to-buffer ibuf)
  5268. (org-set-frame-title heading))
  5269. ((eq org-indirect-buffer-display 'dedicated-frame)
  5270. (raise-frame
  5271. (select-frame (or (and org-indirect-dedicated-frame
  5272. (frame-live-p org-indirect-dedicated-frame)
  5273. org-indirect-dedicated-frame)
  5274. (setq org-indirect-dedicated-frame (make-frame)))))
  5275. (delete-other-windows)
  5276. (switch-to-buffer ibuf)
  5277. (org-set-frame-title (concat "Indirect: " heading)))
  5278. ((eq org-indirect-buffer-display 'current-window)
  5279. (switch-to-buffer ibuf))
  5280. ((eq org-indirect-buffer-display 'other-window)
  5281. (pop-to-buffer ibuf))
  5282. (t (error "Invalid value")))
  5283. (if (featurep 'xemacs)
  5284. (save-excursion (org-mode) (turn-on-font-lock)))
  5285. (narrow-to-region beg end)
  5286. (show-all)
  5287. (goto-char pos)
  5288. (and (window-live-p cwin) (select-window cwin))))
  5289. (defun org-get-indirect-buffer (&optional buffer)
  5290. (setq buffer (or buffer (current-buffer)))
  5291. (let ((n 1) (base (buffer-name buffer)) bname)
  5292. (while (buffer-live-p
  5293. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5294. (setq n (1+ n)))
  5295. (condition-case nil
  5296. (make-indirect-buffer buffer bname 'clone)
  5297. (error (make-indirect-buffer buffer bname)))))
  5298. (defun org-set-frame-title (title)
  5299. "Set the title of the current frame to the string TITLE."
  5300. ;; FIXME: how to name a single frame in XEmacs???
  5301. (unless (featurep 'xemacs)
  5302. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5303. ;;;; Structure editing
  5304. ;;; Inserting headlines
  5305. (defun org-previous-line-empty-p ()
  5306. (save-excursion
  5307. (and (not (bobp))
  5308. (or (beginning-of-line 0) t)
  5309. (save-match-data
  5310. (looking-at "[ \t]*$")))))
  5311. (defun org-insert-heading (&optional force-heading)
  5312. "Insert a new heading or item with same depth at point.
  5313. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5314. If point is at the beginning of a headline, insert a sibling before the
  5315. current headline. If point is not at the beginning, do not split the line,
  5316. but create the new headline after the current line."
  5317. (interactive "P")
  5318. (if (= (buffer-size) 0)
  5319. (insert "\n* ")
  5320. (when (or force-heading (not (org-insert-item)))
  5321. (let* ((empty-line-p nil)
  5322. (head (save-excursion
  5323. (condition-case nil
  5324. (progn
  5325. (org-back-to-heading)
  5326. (setq empty-line-p (org-previous-line-empty-p))
  5327. (match-string 0))
  5328. (error "*"))))
  5329. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5330. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5331. pos hide-previous previous-pos)
  5332. (cond
  5333. ((and (org-on-heading-p) (bolp)
  5334. (or (bobp)
  5335. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5336. ;; insert before the current line
  5337. (open-line (if blank 2 1)))
  5338. ((and (bolp)
  5339. (not org-insert-heading-respect-content)
  5340. (or (bobp)
  5341. (save-excursion
  5342. (backward-char 1) (not (org-invisible-p)))))
  5343. ;; insert right here
  5344. nil)
  5345. (t
  5346. ;; somewhere in the line
  5347. (save-excursion
  5348. (setq previous-pos (point-at-bol))
  5349. (end-of-line)
  5350. (setq hide-previous (org-invisible-p)))
  5351. (and org-insert-heading-respect-content (org-show-subtree))
  5352. (let ((split
  5353. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5354. (save-excursion
  5355. (let ((p (point)))
  5356. (goto-char (point-at-bol))
  5357. (and (looking-at org-complex-heading-regexp)
  5358. (> p (match-beginning 4)))))))
  5359. tags pos)
  5360. (cond
  5361. (org-insert-heading-respect-content
  5362. (org-end-of-subtree nil t)
  5363. (or (bolp) (newline))
  5364. (or (org-previous-line-empty-p)
  5365. (and blank (newline)))
  5366. (open-line 1))
  5367. ((org-on-heading-p)
  5368. (when hide-previous
  5369. (show-children)
  5370. (org-show-entry))
  5371. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5372. (setq tags (and (match-end 2) (match-string 2)))
  5373. (and (match-end 1)
  5374. (delete-region (match-beginning 1) (match-end 1)))
  5375. (setq pos (point-at-bol))
  5376. (or split (end-of-line 1))
  5377. (delete-horizontal-space)
  5378. (newline (if blank 2 1))
  5379. (when tags
  5380. (save-excursion
  5381. (goto-char pos)
  5382. (end-of-line 1)
  5383. (insert " " tags)
  5384. (org-set-tags nil 'align))))
  5385. (t
  5386. (or split (end-of-line 1))
  5387. (newline (if blank 2 1)))))))
  5388. (insert head) (just-one-space)
  5389. (setq pos (point))
  5390. (end-of-line 1)
  5391. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5392. (when (and org-insert-heading-respect-content hide-previous)
  5393. (save-excursion
  5394. (goto-char previous-pos)
  5395. (hide-subtree)))
  5396. (run-hooks 'org-insert-heading-hook)))))
  5397. (defun org-get-heading (&optional no-tags)
  5398. "Return the heading of the current entry, without the stars."
  5399. (save-excursion
  5400. (org-back-to-heading t)
  5401. (if (looking-at
  5402. (if no-tags
  5403. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5404. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5405. (match-string 1) "")))
  5406. (defun org-heading-components ()
  5407. "Return the components of the current heading.
  5408. This is a list with the following elements:
  5409. - the level as an integer
  5410. - the reduced level, different if `org-odd-levels-only' is set.
  5411. - the TODO keyword, or nil
  5412. - the priority character, like ?A, or nil if no priority is given
  5413. - the headline text itself, or the tags string if no headline text
  5414. - the tags string, or nil."
  5415. (save-excursion
  5416. (org-back-to-heading t)
  5417. (if (looking-at org-complex-heading-regexp)
  5418. (list (length (match-string 1))
  5419. (org-reduced-level (length (match-string 1)))
  5420. (org-match-string-no-properties 2)
  5421. (and (match-end 3) (aref (match-string 3) 2))
  5422. (org-match-string-no-properties 4)
  5423. (org-match-string-no-properties 5)))))
  5424. (defun org-get-entry ()
  5425. "Get the entry text, after heading, entire subtree."
  5426. (save-excursion
  5427. (org-back-to-heading t)
  5428. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5429. (defun org-insert-heading-after-current ()
  5430. "Insert a new heading with same level as current, after current subtree."
  5431. (interactive)
  5432. (org-back-to-heading)
  5433. (org-insert-heading)
  5434. (org-move-subtree-down)
  5435. (end-of-line 1))
  5436. (defun org-insert-heading-respect-content ()
  5437. (interactive)
  5438. (let ((org-insert-heading-respect-content t))
  5439. (org-insert-heading t)))
  5440. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5441. (interactive "P")
  5442. (let ((org-insert-heading-respect-content t))
  5443. (org-insert-todo-heading force-state t)))
  5444. (defun org-insert-todo-heading (arg &optional force-heading)
  5445. "Insert a new heading with the same level and TODO state as current heading.
  5446. If the heading has no TODO state, or if the state is DONE, use the first
  5447. state (TODO by default). Also with prefix arg, force first state."
  5448. (interactive "P")
  5449. (when (or force-heading (not (org-insert-item 'checkbox)))
  5450. (org-insert-heading force-heading)
  5451. (save-excursion
  5452. (org-back-to-heading)
  5453. (outline-previous-heading)
  5454. (looking-at org-todo-line-regexp))
  5455. (let*
  5456. ((new-mark-x
  5457. (if (or arg
  5458. (not (match-beginning 2))
  5459. (member (match-string 2) org-done-keywords))
  5460. (car org-todo-keywords-1)
  5461. (match-string 2)))
  5462. (new-mark
  5463. (or
  5464. (run-hook-with-args-until-success
  5465. 'org-todo-get-default-hook new-mark-x nil)
  5466. new-mark-x)))
  5467. (beginning-of-line 1)
  5468. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5469. (if org-treat-insert-todo-heading-as-state-change
  5470. (org-todo new-mark)
  5471. (insert new-mark " "))))
  5472. (when org-provide-todo-statistics
  5473. (org-update-parent-todo-statistics))))
  5474. (defun org-insert-subheading (arg)
  5475. "Insert a new subheading and demote it.
  5476. Works for outline headings and for plain lists alike."
  5477. (interactive "P")
  5478. (org-insert-heading arg)
  5479. (cond
  5480. ((org-on-heading-p) (org-do-demote))
  5481. ((org-at-item-p) (org-indent-item 1))))
  5482. (defun org-insert-todo-subheading (arg)
  5483. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5484. Works for outline headings and for plain lists alike."
  5485. (interactive "P")
  5486. (org-insert-todo-heading arg)
  5487. (cond
  5488. ((org-on-heading-p) (org-do-demote))
  5489. ((org-at-item-p) (org-indent-item 1))))
  5490. ;;; Promotion and Demotion
  5491. (defvar org-after-demote-entry-hook nil
  5492. "Hook run after an entry has been demoted.
  5493. The cursor will be at the beginning of the entry.
  5494. When a subtree is being demoted, the hook will be called for each node.")
  5495. (defvar org-after-promote-entry-hook nil
  5496. "Hook run after an entry has been promoted.
  5497. The cursor will be at the beginning of the entry.
  5498. When a subtree is being promoted, the hook will be called for each node.")
  5499. (defun org-promote-subtree ()
  5500. "Promote the entire subtree.
  5501. See also `org-promote'."
  5502. (interactive)
  5503. (save-excursion
  5504. (org-map-tree 'org-promote))
  5505. (org-fix-position-after-promote))
  5506. (defun org-demote-subtree ()
  5507. "Demote the entire subtree. See `org-demote'.
  5508. See also `org-promote'."
  5509. (interactive)
  5510. (save-excursion
  5511. (org-map-tree 'org-demote))
  5512. (org-fix-position-after-promote))
  5513. (defun org-do-promote ()
  5514. "Promote the current heading higher up the tree.
  5515. If the region is active in `transient-mark-mode', promote all headings
  5516. in the region."
  5517. (interactive)
  5518. (save-excursion
  5519. (if (org-region-active-p)
  5520. (org-map-region 'org-promote (region-beginning) (region-end))
  5521. (org-promote)))
  5522. (org-fix-position-after-promote))
  5523. (defun org-do-demote ()
  5524. "Demote the current heading lower down the tree.
  5525. If the region is active in `transient-mark-mode', demote all headings
  5526. in the region."
  5527. (interactive)
  5528. (save-excursion
  5529. (if (org-region-active-p)
  5530. (org-map-region 'org-demote (region-beginning) (region-end))
  5531. (org-demote)))
  5532. (org-fix-position-after-promote))
  5533. (defun org-fix-position-after-promote ()
  5534. "Make sure that after pro/demotion cursor position is right."
  5535. (let ((pos (point)))
  5536. (when (save-excursion
  5537. (beginning-of-line 1)
  5538. (looking-at org-todo-line-regexp)
  5539. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5540. (cond ((eobp) (insert " "))
  5541. ((eolp) (insert " "))
  5542. ((equal (char-after) ?\ ) (forward-char 1))))))
  5543. (defun org-current-level ()
  5544. "Return the level of the current entry, or nil if before the first headline.
  5545. The level is the number of stars at the beginning of the headline."
  5546. (save-excursion
  5547. (condition-case nil
  5548. (progn
  5549. (org-back-to-heading t)
  5550. (funcall outline-level))
  5551. (error nil))))
  5552. (defun org-reduced-level (l)
  5553. "Compute the effective level of a heading.
  5554. This takes into account the setting of `org-odd-levels-only'."
  5555. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5556. (defun org-get-valid-level (level &optional change)
  5557. "Rectify a level change under the influence of `org-odd-levels-only'
  5558. LEVEL is a current level, CHANGE is by how much the level should be
  5559. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5560. even level numbers will become the next higher odd number."
  5561. (if org-odd-levels-only
  5562. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5563. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5564. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5565. (max 1 (+ level (or change 0)))))
  5566. (if (boundp 'define-obsolete-function-alias)
  5567. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5568. (define-obsolete-function-alias 'org-get-legal-level
  5569. 'org-get-valid-level)
  5570. (define-obsolete-function-alias 'org-get-legal-level
  5571. 'org-get-valid-level "23.1")))
  5572. (defun org-promote ()
  5573. "Promote the current heading higher up the tree.
  5574. If the region is active in `transient-mark-mode', promote all headings
  5575. in the region."
  5576. (org-back-to-heading t)
  5577. (let* ((level (save-match-data (funcall outline-level)))
  5578. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5579. (diff (abs (- level (length up-head) -1))))
  5580. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5581. (replace-match up-head nil t)
  5582. ;; Fixup tag positioning
  5583. (and org-auto-align-tags (org-set-tags nil t))
  5584. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5585. (run-hooks 'org-after-promote-entry-hook)))
  5586. (defun org-demote ()
  5587. "Demote the current heading lower down the tree.
  5588. If the region is active in `transient-mark-mode', demote all headings
  5589. in the region."
  5590. (org-back-to-heading t)
  5591. (let* ((level (save-match-data (funcall outline-level)))
  5592. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5593. (diff (abs (- level (length down-head) -1))))
  5594. (replace-match down-head nil t)
  5595. ;; Fixup tag positioning
  5596. (and org-auto-align-tags (org-set-tags nil t))
  5597. (if org-adapt-indentation (org-fixup-indentation diff))
  5598. (run-hooks 'org-after-demote-entry-hook)))
  5599. (defvar org-tab-ind-state nil)
  5600. (defun org-cycle-level ()
  5601. (let ((org-adapt-indentation nil))
  5602. (when (and (looking-at "[ \t]*$")
  5603. (looking-back
  5604. (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp "\\)?[ \t]*")))
  5605. (setq this-command 'org-cycle-level)
  5606. (if (eq last-command 'org-cycle-level)
  5607. (condition-case nil
  5608. (progn (org-do-promote)
  5609. (if (equal org-tab-ind-state (org-current-level))
  5610. (org-do-promote)))
  5611. (error
  5612. (progn
  5613. (save-excursion
  5614. (beginning-of-line 1)
  5615. (and (looking-at "\\*+")
  5616. (replace-match
  5617. (make-string org-tab-ind-state ?*))))
  5618. (setq this-command 'org-cycle))))
  5619. (setq org-tab-ind-state (- (match-end 1) (match-beginning 1)))
  5620. (org-do-demote))
  5621. t)))
  5622. (defun org-map-tree (fun)
  5623. "Call FUN for every heading underneath the current one."
  5624. (org-back-to-heading)
  5625. (let ((level (funcall outline-level)))
  5626. (save-excursion
  5627. (funcall fun)
  5628. (while (and (progn
  5629. (outline-next-heading)
  5630. (> (funcall outline-level) level))
  5631. (not (eobp)))
  5632. (funcall fun)))))
  5633. (defun org-map-region (fun beg end)
  5634. "Call FUN for every heading between BEG and END."
  5635. (let ((org-ignore-region t))
  5636. (save-excursion
  5637. (setq end (copy-marker end))
  5638. (goto-char beg)
  5639. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5640. (< (point) end))
  5641. (funcall fun))
  5642. (while (and (progn
  5643. (outline-next-heading)
  5644. (< (point) end))
  5645. (not (eobp)))
  5646. (funcall fun)))))
  5647. (defun org-fixup-indentation (diff)
  5648. "Change the indentation in the current entry by DIFF
  5649. However, if any line in the current entry has no indentation, or if it
  5650. would end up with no indentation after the change, nothing at all is done."
  5651. (save-excursion
  5652. (let ((end (save-excursion (outline-next-heading)
  5653. (point-marker)))
  5654. (prohibit (if (> diff 0)
  5655. "^\\S-"
  5656. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5657. col)
  5658. (unless (save-excursion (end-of-line 1)
  5659. (re-search-forward prohibit end t))
  5660. (while (and (< (point) end)
  5661. (re-search-forward "^[ \t]+" end t))
  5662. (goto-char (match-end 0))
  5663. (setq col (current-column))
  5664. (if (< diff 0) (replace-match ""))
  5665. (org-indent-to-column (+ diff col))))
  5666. (move-marker end nil))))
  5667. (defun org-convert-to-odd-levels ()
  5668. "Convert an org-mode file with all levels allowed to one with odd levels.
  5669. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5670. level 5 etc."
  5671. (interactive)
  5672. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5673. (let ((outline-regexp org-outline-regexp)
  5674. (outline-level 'org-outline-level)
  5675. (org-odd-levels-only nil) n)
  5676. (save-excursion
  5677. (goto-char (point-min))
  5678. (while (re-search-forward "^\\*\\*+ " nil t)
  5679. (setq n (- (length (match-string 0)) 2))
  5680. (while (>= (setq n (1- n)) 0)
  5681. (org-demote))
  5682. (end-of-line 1))))))
  5683. (defun org-convert-to-oddeven-levels ()
  5684. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5685. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5686. section with an even level, conversion would destroy the structure of the file. An error
  5687. is signaled in this case."
  5688. (interactive)
  5689. (goto-char (point-min))
  5690. ;; First check if there are no even levels
  5691. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5692. (org-show-context t)
  5693. (error "Not all levels are odd in this file. Conversion not possible"))
  5694. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5695. (let ((outline-regexp org-outline-regexp)
  5696. (outline-level 'org-outline-level)
  5697. (org-odd-levels-only nil) n)
  5698. (save-excursion
  5699. (goto-char (point-min))
  5700. (while (re-search-forward "^\\*\\*+ " nil t)
  5701. (setq n (/ (1- (length (match-string 0))) 2))
  5702. (while (>= (setq n (1- n)) 0)
  5703. (org-promote))
  5704. (end-of-line 1))))))
  5705. (defun org-tr-level (n)
  5706. "Make N odd if required."
  5707. (if org-odd-levels-only (1+ (/ n 2)) n))
  5708. ;;; Vertical tree motion, cutting and pasting of subtrees
  5709. (defun org-move-subtree-up (&optional arg)
  5710. "Move the current subtree up past ARG headlines of the same level."
  5711. (interactive "p")
  5712. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5713. (defun org-move-subtree-down (&optional arg)
  5714. "Move the current subtree down past ARG headlines of the same level."
  5715. (interactive "p")
  5716. (setq arg (prefix-numeric-value arg))
  5717. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5718. 'org-get-last-sibling))
  5719. (ins-point (make-marker))
  5720. (cnt (abs arg))
  5721. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5722. ;; Select the tree
  5723. (org-back-to-heading)
  5724. (setq beg0 (point))
  5725. (save-excursion
  5726. (setq ne-beg (org-back-over-empty-lines))
  5727. (setq beg (point)))
  5728. (save-match-data
  5729. (save-excursion (outline-end-of-heading)
  5730. (setq folded (org-invisible-p)))
  5731. (outline-end-of-subtree))
  5732. (outline-next-heading)
  5733. (setq ne-end (org-back-over-empty-lines))
  5734. (setq end (point))
  5735. (goto-char beg0)
  5736. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5737. ;; include less whitespace
  5738. (save-excursion
  5739. (goto-char beg)
  5740. (forward-line (- ne-beg ne-end))
  5741. (setq beg (point))))
  5742. ;; Find insertion point, with error handling
  5743. (while (> cnt 0)
  5744. (or (and (funcall movfunc) (looking-at outline-regexp))
  5745. (progn (goto-char beg0)
  5746. (error "Cannot move past superior level or buffer limit")))
  5747. (setq cnt (1- cnt)))
  5748. (if (> arg 0)
  5749. ;; Moving forward - still need to move over subtree
  5750. (progn (org-end-of-subtree t t)
  5751. (save-excursion
  5752. (org-back-over-empty-lines)
  5753. (or (bolp) (newline)))))
  5754. (setq ne-ins (org-back-over-empty-lines))
  5755. (move-marker ins-point (point))
  5756. (setq txt (buffer-substring beg end))
  5757. (org-save-markers-in-region beg end)
  5758. (delete-region beg end)
  5759. (org-remove-empty-overlays-at beg)
  5760. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5761. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5762. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5763. (let ((bbb (point)))
  5764. (insert-before-markers txt)
  5765. (org-reinstall-markers-in-region bbb)
  5766. (move-marker ins-point bbb))
  5767. (or (bolp) (insert "\n"))
  5768. (setq ins-end (point))
  5769. (goto-char ins-point)
  5770. (org-skip-whitespace)
  5771. (when (and (< arg 0)
  5772. (org-first-sibling-p)
  5773. (> ne-ins ne-beg))
  5774. ;; Move whitespace back to beginning
  5775. (save-excursion
  5776. (goto-char ins-end)
  5777. (let ((kill-whole-line t))
  5778. (kill-line (- ne-ins ne-beg)) (point)))
  5779. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5780. (move-marker ins-point nil)
  5781. (if folded
  5782. (hide-subtree)
  5783. (org-show-entry)
  5784. (show-children)
  5785. (org-cycle-hide-drawers 'children))
  5786. (org-clean-visibility-after-subtree-move)))
  5787. (defvar org-subtree-clip ""
  5788. "Clipboard for cut and paste of subtrees.
  5789. This is actually only a copy of the kill, because we use the normal kill
  5790. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5791. (defvar org-subtree-clip-folded nil
  5792. "Was the last copied subtree folded?
  5793. This is used to fold the tree back after pasting.")
  5794. (defun org-cut-subtree (&optional n)
  5795. "Cut the current subtree into the clipboard.
  5796. With prefix arg N, cut this many sequential subtrees.
  5797. This is a short-hand for marking the subtree and then cutting it."
  5798. (interactive "p")
  5799. (org-copy-subtree n 'cut))
  5800. (defun org-copy-subtree (&optional n cut force-store-markers)
  5801. "Cut the current subtree into the clipboard.
  5802. With prefix arg N, cut this many sequential subtrees.
  5803. This is a short-hand for marking the subtree and then copying it.
  5804. If CUT is non-nil, actually cut the subtree.
  5805. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5806. of some markers in the region, even if CUT is non-nil. This is
  5807. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5808. (interactive "p")
  5809. (let (beg end folded (beg0 (point)))
  5810. (if (interactive-p)
  5811. (org-back-to-heading nil) ; take what looks like a subtree
  5812. (org-back-to-heading t)) ; take what is really there
  5813. (org-back-over-empty-lines)
  5814. (setq beg (point))
  5815. (skip-chars-forward " \t\r\n")
  5816. (save-match-data
  5817. (save-excursion (outline-end-of-heading)
  5818. (setq folded (org-invisible-p)))
  5819. (condition-case nil
  5820. (org-forward-same-level (1- n) t)
  5821. (error nil))
  5822. (org-end-of-subtree t t))
  5823. (org-back-over-empty-lines)
  5824. (setq end (point))
  5825. (goto-char beg0)
  5826. (when (> end beg)
  5827. (setq org-subtree-clip-folded folded)
  5828. (when (or cut force-store-markers)
  5829. (org-save-markers-in-region beg end))
  5830. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5831. (setq org-subtree-clip (current-kill 0))
  5832. (message "%s: Subtree(s) with %d characters"
  5833. (if cut "Cut" "Copied")
  5834. (length org-subtree-clip)))))
  5835. (defun org-paste-subtree (&optional level tree for-yank)
  5836. "Paste the clipboard as a subtree, with modification of headline level.
  5837. The entire subtree is promoted or demoted in order to match a new headline
  5838. level.
  5839. If the cursor is at the beginning of a headline, the same level as
  5840. that headline is used to paste the tree
  5841. If not, the new level is derived from the *visible* headings
  5842. before and after the insertion point, and taken to be the inferior headline
  5843. level of the two. So if the previous visible heading is level 3 and the
  5844. next is level 4 (or vice versa), level 4 will be used for insertion.
  5845. This makes sure that the subtree remains an independent subtree and does
  5846. not swallow low level entries.
  5847. You can also force a different level, either by using a numeric prefix
  5848. argument, or by inserting the heading marker by hand. For example, if the
  5849. cursor is after \"*****\", then the tree will be shifted to level 5.
  5850. If optional TREE is given, use this text instead of the kill ring.
  5851. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5852. move back over whitespace before inserting, and move point to the end of
  5853. the inserted text when done."
  5854. (interactive "P")
  5855. (setq tree (or tree (and kill-ring (current-kill 0))))
  5856. (unless (org-kill-is-subtree-p tree)
  5857. (error "%s"
  5858. (substitute-command-keys
  5859. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5860. (let* ((visp (not (org-invisible-p)))
  5861. (txt tree)
  5862. (^re (concat "^\\(" outline-regexp "\\)"))
  5863. (re (concat "\\(" outline-regexp "\\)"))
  5864. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5865. (old-level (if (string-match ^re txt)
  5866. (- (match-end 0) (match-beginning 0) 1)
  5867. -1))
  5868. (force-level (cond (level (prefix-numeric-value level))
  5869. ((and (looking-at "[ \t]*$")
  5870. (string-match
  5871. ^re_ (buffer-substring
  5872. (point-at-bol) (point))))
  5873. (- (match-end 1) (match-beginning 1)))
  5874. ((and (bolp)
  5875. (looking-at org-outline-regexp))
  5876. (- (match-end 0) (point) 1))
  5877. (t nil)))
  5878. (previous-level (save-excursion
  5879. (condition-case nil
  5880. (progn
  5881. (outline-previous-visible-heading 1)
  5882. (if (looking-at re)
  5883. (- (match-end 0) (match-beginning 0) 1)
  5884. 1))
  5885. (error 1))))
  5886. (next-level (save-excursion
  5887. (condition-case nil
  5888. (progn
  5889. (or (looking-at outline-regexp)
  5890. (outline-next-visible-heading 1))
  5891. (if (looking-at re)
  5892. (- (match-end 0) (match-beginning 0) 1)
  5893. 1))
  5894. (error 1))))
  5895. (new-level (or force-level (max previous-level next-level)))
  5896. (shift (if (or (= old-level -1)
  5897. (= new-level -1)
  5898. (= old-level new-level))
  5899. 0
  5900. (- new-level old-level)))
  5901. (delta (if (> shift 0) -1 1))
  5902. (func (if (> shift 0) 'org-demote 'org-promote))
  5903. (org-odd-levels-only nil)
  5904. beg end newend)
  5905. ;; Remove the forced level indicator
  5906. (if force-level
  5907. (delete-region (point-at-bol) (point)))
  5908. ;; Paste
  5909. (beginning-of-line 1)
  5910. (unless for-yank (org-back-over-empty-lines))
  5911. (setq beg (point))
  5912. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5913. (insert-before-markers txt)
  5914. (unless (string-match "\n\\'" txt) (insert "\n"))
  5915. (setq newend (point))
  5916. (org-reinstall-markers-in-region beg)
  5917. (setq end (point))
  5918. (goto-char beg)
  5919. (skip-chars-forward " \t\n\r")
  5920. (setq beg (point))
  5921. (if (and (org-invisible-p) visp)
  5922. (save-excursion (outline-show-heading)))
  5923. ;; Shift if necessary
  5924. (unless (= shift 0)
  5925. (save-restriction
  5926. (narrow-to-region beg end)
  5927. (while (not (= shift 0))
  5928. (org-map-region func (point-min) (point-max))
  5929. (setq shift (+ delta shift)))
  5930. (goto-char (point-min))
  5931. (setq newend (point-max))))
  5932. (when (or (interactive-p) for-yank)
  5933. (message "Clipboard pasted as level %d subtree" new-level))
  5934. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5935. kill-ring
  5936. (eq org-subtree-clip (current-kill 0))
  5937. org-subtree-clip-folded)
  5938. ;; The tree was folded before it was killed/copied
  5939. (hide-subtree))
  5940. (and for-yank (goto-char newend))))
  5941. (defun org-kill-is-subtree-p (&optional txt)
  5942. "Check if the current kill is an outline subtree, or a set of trees.
  5943. Returns nil if kill does not start with a headline, or if the first
  5944. headline level is not the largest headline level in the tree.
  5945. So this will actually accept several entries of equal levels as well,
  5946. which is OK for `org-paste-subtree'.
  5947. If optional TXT is given, check this string instead of the current kill."
  5948. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5949. (start-level (and kill
  5950. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5951. org-outline-regexp "\\)")
  5952. kill)
  5953. (- (match-end 2) (match-beginning 2) 1)))
  5954. (re (concat "^" org-outline-regexp))
  5955. (start (1+ (or (match-beginning 2) -1))))
  5956. (if (not start-level)
  5957. (progn
  5958. nil) ;; does not even start with a heading
  5959. (catch 'exit
  5960. (while (setq start (string-match re kill (1+ start)))
  5961. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5962. (throw 'exit nil)))
  5963. t))))
  5964. (defvar org-markers-to-move nil
  5965. "Markers that should be moved with a cut-and-paste operation.
  5966. Those markers are stored together with their positions relative to
  5967. the start of the region.")
  5968. (defun org-save-markers-in-region (beg end)
  5969. "Check markers in region.
  5970. If these markers are between BEG and END, record their position relative
  5971. to BEG, so that after moving the block of text, we can put the markers back
  5972. into place.
  5973. This function gets called just before an entry or tree gets cut from the
  5974. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5975. called immediately, to move the markers with the entries."
  5976. (setq org-markers-to-move nil)
  5977. (when (featurep 'org-clock)
  5978. (org-clock-save-markers-for-cut-and-paste beg end))
  5979. (when (featurep 'org-agenda)
  5980. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5981. (defun org-check-and-save-marker (marker beg end)
  5982. "Check if MARKER is between BEG and END.
  5983. If yes, remember the marker and the distance to BEG."
  5984. (when (and (marker-buffer marker)
  5985. (equal (marker-buffer marker) (current-buffer)))
  5986. (if (and (>= marker beg) (< marker end))
  5987. (push (cons marker (- marker beg)) org-markers-to-move))))
  5988. (defun org-reinstall-markers-in-region (beg)
  5989. "Move all remembered markers to their position relative to BEG."
  5990. (mapc (lambda (x)
  5991. (move-marker (car x) (+ beg (cdr x))))
  5992. org-markers-to-move)
  5993. (setq org-markers-to-move nil))
  5994. (defun org-narrow-to-subtree ()
  5995. "Narrow buffer to the current subtree."
  5996. (interactive)
  5997. (save-excursion
  5998. (save-match-data
  5999. (narrow-to-region
  6000. (progn (org-back-to-heading t) (point))
  6001. (progn (org-end-of-subtree t t) (point))))))
  6002. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6003. "Clone the task (subtree) at point N times.
  6004. The clones will be inserted as siblings.
  6005. In interactive use, the user will be prompted for the number of clones
  6006. to be produced, and for a time SHIFT, which may be a repeater as used
  6007. in time stamps, for example `+3d'.
  6008. When a valid repeater is given and the entry contains any time stamps,
  6009. the clones will become a sequence in time, with time stamps in the
  6010. subtree shifted for each clone produced. If SHIFT is nil or the
  6011. empty string, time stamps will be left alone.
  6012. If the original subtree did contain time stamps with a repeater,
  6013. the following will happen:
  6014. - the repeater will be removed in each clone
  6015. - an additional clone will be produced, with the current, unshifted
  6016. date(s) in the entry.
  6017. - the original entry will be placed *after* all the clones, with
  6018. repeater intact.
  6019. - the start days in the repeater in the original entry will be shifted
  6020. to past the last clone.
  6021. I this way you can spell out a number of instances of a repeating task,
  6022. and still retain the repeater to cover future instances of the task."
  6023. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6024. (let (beg end template task
  6025. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6026. (if (not (and (integerp n) (> n 0)))
  6027. (error "Invalid number of replications %s" n))
  6028. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6029. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6030. shift)))
  6031. (error "Invalid shift specification %s" shift))
  6032. (when doshift
  6033. (setq shift-n (string-to-number (match-string 1 shift))
  6034. shift-what (cdr (assoc (match-string 2 shift)
  6035. '(("d" . day) ("w" . week)
  6036. ("m" . month) ("y" . year))))))
  6037. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6038. (setq nmin 1 nmax n)
  6039. (org-back-to-heading t)
  6040. (setq beg (point))
  6041. (org-end-of-subtree t t)
  6042. (or (bolp) (insert "\n"))
  6043. (setq end (point))
  6044. (setq template (buffer-substring beg end))
  6045. (when (and doshift
  6046. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6047. (delete-region beg end)
  6048. (setq end beg)
  6049. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6050. (goto-char end)
  6051. (loop for n from nmin to nmax do
  6052. (if (not doshift)
  6053. (setq task template)
  6054. (with-temp-buffer
  6055. (insert template)
  6056. (org-mode)
  6057. (goto-char (point-min))
  6058. (while (re-search-forward org-ts-regexp-both nil t)
  6059. (org-timestamp-change (* n shift-n) shift-what))
  6060. (unless (= n n-no-remove)
  6061. (goto-char (point-min))
  6062. (while (re-search-forward org-ts-regexp nil t)
  6063. (save-excursion
  6064. (goto-char (match-beginning 0))
  6065. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6066. (delete-region (match-beginning 1) (match-end 1))))))
  6067. (setq task (buffer-string))))
  6068. (insert task))
  6069. (goto-char beg)))
  6070. ;;; Outline Sorting
  6071. (defun org-sort (with-case)
  6072. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6073. Optional argument WITH-CASE means sort case-sensitively.
  6074. With a double prefix argument, also remove duplicate entries."
  6075. (interactive "P")
  6076. (if (org-at-table-p)
  6077. (org-call-with-arg 'org-table-sort-lines with-case)
  6078. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6079. (defun org-sort-remove-invisible (s)
  6080. (remove-text-properties 0 (length s) org-rm-props s)
  6081. (while (string-match org-bracket-link-regexp s)
  6082. (setq s (replace-match (if (match-end 2)
  6083. (match-string 3 s)
  6084. (match-string 1 s)) t t s)))
  6085. s)
  6086. (defvar org-priority-regexp) ; defined later in the file
  6087. (defvar org-after-sorting-entries-or-items-hook nil
  6088. "Hook that is run after a bunch of entries or items have been sorted.
  6089. When children are sorted, the cursor is in the parent line when this
  6090. hook gets called. When a region or a plain list is sorted, the cursor
  6091. will be in the first entry of the sorted region/list.")
  6092. (defun org-sort-entries-or-items
  6093. (&optional with-case sorting-type getkey-func compare-func property)
  6094. "Sort entries on a certain level of an outline tree, or plain list items.
  6095. If there is an active region, the entries in the region are sorted.
  6096. Else, if the cursor is before the first entry, sort the top-level items.
  6097. Else, the children of the entry at point are sorted.
  6098. If the cursor is at the first item in a plain list, the list items will be
  6099. sorted.
  6100. Sorting can be alphabetically, numerically, by date/time as given by
  6101. a time stamp, by a property or by priority.
  6102. The command prompts for the sorting type unless it has been given to the
  6103. function through the SORTING-TYPE argument, which needs to a character,
  6104. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6105. precise meaning of each character:
  6106. n Numerically, by converting the beginning of the entry/item to a number.
  6107. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6108. t By date/time, either the first active time stamp in the entry, or, if
  6109. none exist, by the first inactive one.
  6110. In items, only the first line will be checked.
  6111. s By the scheduled date/time.
  6112. d By deadline date/time.
  6113. c By creation time, which is assumed to be the first inactive time stamp
  6114. at the beginning of a line.
  6115. p By priority according to the cookie.
  6116. r By the value of a property.
  6117. Capital letters will reverse the sort order.
  6118. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6119. called with point at the beginning of the record. It must return either
  6120. a string or a number that should serve as the sorting key for that record.
  6121. Comparing entries ignores case by default. However, with an optional argument
  6122. WITH-CASE, the sorting considers case as well."
  6123. (interactive "P")
  6124. (let ((case-func (if with-case 'identity 'downcase))
  6125. start beg end stars re re2
  6126. txt what tmp plain-list-p)
  6127. ;; Find beginning and end of region to sort
  6128. (cond
  6129. ((org-region-active-p)
  6130. ;; we will sort the region
  6131. (setq end (region-end)
  6132. what "region")
  6133. (goto-char (region-beginning))
  6134. (if (not (org-on-heading-p)) (outline-next-heading))
  6135. (setq start (point)))
  6136. ((org-at-item-p)
  6137. ;; we will sort this plain list
  6138. (org-beginning-of-item-list) (setq start (point))
  6139. (org-end-of-item-list) (setq end (point))
  6140. (goto-char start)
  6141. (setq plain-list-p t
  6142. what "plain list"))
  6143. ((or (org-on-heading-p)
  6144. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6145. ;; we will sort the children of the current headline
  6146. (org-back-to-heading)
  6147. (setq start (point)
  6148. end (progn (org-end-of-subtree t t)
  6149. (org-back-over-empty-lines)
  6150. (point))
  6151. what "children")
  6152. (goto-char start)
  6153. (show-subtree)
  6154. (outline-next-heading))
  6155. (t
  6156. ;; we will sort the top-level entries in this file
  6157. (goto-char (point-min))
  6158. (or (org-on-heading-p) (outline-next-heading))
  6159. (setq start (point) end (point-max) what "top-level")
  6160. (goto-char start)
  6161. (show-all)))
  6162. (setq beg (point))
  6163. (if (>= beg end) (error "Nothing to sort"))
  6164. (unless plain-list-p
  6165. (looking-at "\\(\\*+\\)")
  6166. (setq stars (match-string 1)
  6167. re (concat "^" (regexp-quote stars) " +")
  6168. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6169. txt (buffer-substring beg end))
  6170. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6171. (if (and (not (equal stars "*")) (string-match re2 txt))
  6172. (error "Region to sort contains a level above the first entry")))
  6173. (unless sorting-type
  6174. (message
  6175. (if plain-list-p
  6176. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6177. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6178. [t]ime [s]cheduled [d]eadline [c]reated
  6179. A/N/T/S/D/C/P/O/F means reversed:")
  6180. what)
  6181. (setq sorting-type (read-char-exclusive))
  6182. (and (= (downcase sorting-type) ?f)
  6183. (setq getkey-func
  6184. (org-icompleting-read "Sort using function: "
  6185. obarray 'fboundp t nil nil))
  6186. (setq getkey-func (intern getkey-func)))
  6187. (and (= (downcase sorting-type) ?r)
  6188. (setq property
  6189. (org-icompleting-read "Property: "
  6190. (mapcar 'list (org-buffer-property-keys t))
  6191. nil t))))
  6192. (message "Sorting entries...")
  6193. (save-restriction
  6194. (narrow-to-region start end)
  6195. (let ((dcst (downcase sorting-type))
  6196. (case-fold-search nil)
  6197. (now (current-time)))
  6198. (sort-subr
  6199. (/= dcst sorting-type)
  6200. ;; This function moves to the beginning character of the "record" to
  6201. ;; be sorted.
  6202. (if plain-list-p
  6203. (lambda nil
  6204. (if (org-at-item-p) t (goto-char (point-max))))
  6205. (lambda nil
  6206. (if (re-search-forward re nil t)
  6207. (goto-char (match-beginning 0))
  6208. (goto-char (point-max)))))
  6209. ;; This function moves to the last character of the "record" being
  6210. ;; sorted.
  6211. (if plain-list-p
  6212. 'org-end-of-item
  6213. (lambda nil
  6214. (save-match-data
  6215. (condition-case nil
  6216. (outline-forward-same-level 1)
  6217. (error
  6218. (goto-char (point-max)))))))
  6219. ;; This function returns the value that gets sorted against.
  6220. (if plain-list-p
  6221. (lambda nil
  6222. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6223. (cond
  6224. ((= dcst ?n)
  6225. (string-to-number (buffer-substring (match-end 0)
  6226. (point-at-eol))))
  6227. ((= dcst ?a)
  6228. (buffer-substring (match-end 0) (point-at-eol)))
  6229. ((= dcst ?t)
  6230. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6231. (re-search-forward org-ts-regexp-both
  6232. (point-at-eol) t))
  6233. (org-time-string-to-seconds (match-string 0))
  6234. (org-float-time now)))
  6235. ((= dcst ?f)
  6236. (if getkey-func
  6237. (progn
  6238. (setq tmp (funcall getkey-func))
  6239. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6240. tmp)
  6241. (error "Invalid key function `%s'" getkey-func)))
  6242. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6243. (lambda nil
  6244. (cond
  6245. ((= dcst ?n)
  6246. (if (looking-at org-complex-heading-regexp)
  6247. (string-to-number (match-string 4))
  6248. nil))
  6249. ((= dcst ?a)
  6250. (if (looking-at org-complex-heading-regexp)
  6251. (funcall case-func (match-string 4))
  6252. nil))
  6253. ((= dcst ?t)
  6254. (let ((end (save-excursion (outline-next-heading) (point))))
  6255. (if (or (re-search-forward org-ts-regexp end t)
  6256. (re-search-forward org-ts-regexp-both end t))
  6257. (org-time-string-to-seconds (match-string 0))
  6258. (org-float-time now))))
  6259. ((= dcst ?c)
  6260. (let ((end (save-excursion (outline-next-heading) (point))))
  6261. (if (re-search-forward
  6262. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6263. end t)
  6264. (org-time-string-to-seconds (match-string 0))
  6265. (org-float-time now))))
  6266. ((= dcst ?s)
  6267. (let ((end (save-excursion (outline-next-heading) (point))))
  6268. (if (re-search-forward org-scheduled-time-regexp end t)
  6269. (org-time-string-to-seconds (match-string 1))
  6270. (org-float-time now))))
  6271. ((= dcst ?d)
  6272. (let ((end (save-excursion (outline-next-heading) (point))))
  6273. (if (re-search-forward org-deadline-time-regexp end t)
  6274. (org-time-string-to-seconds (match-string 1))
  6275. (org-float-time now))))
  6276. ((= dcst ?p)
  6277. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6278. (string-to-char (match-string 2))
  6279. org-default-priority))
  6280. ((= dcst ?r)
  6281. (or (org-entry-get nil property) ""))
  6282. ((= dcst ?o)
  6283. (if (looking-at org-complex-heading-regexp)
  6284. (- 9999 (length (member (match-string 2)
  6285. org-todo-keywords-1)))))
  6286. ((= dcst ?f)
  6287. (if getkey-func
  6288. (progn
  6289. (setq tmp (funcall getkey-func))
  6290. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6291. tmp)
  6292. (error "Invalid key function `%s'" getkey-func)))
  6293. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6294. nil
  6295. (cond
  6296. ((= dcst ?a) 'string<)
  6297. ((= dcst ?f) compare-func)
  6298. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6299. (t nil)))))
  6300. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6301. (message "Sorting entries...done")))
  6302. (defun org-do-sort (table what &optional with-case sorting-type)
  6303. "Sort TABLE of WHAT according to SORTING-TYPE.
  6304. The user will be prompted for the SORTING-TYPE if the call to this
  6305. function does not specify it. WHAT is only for the prompt, to indicate
  6306. what is being sorted. The sorting key will be extracted from
  6307. the car of the elements of the table.
  6308. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6309. (unless sorting-type
  6310. (message
  6311. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6312. what)
  6313. (setq sorting-type (read-char-exclusive)))
  6314. (let ((dcst (downcase sorting-type))
  6315. extractfun comparefun)
  6316. ;; Define the appropriate functions
  6317. (cond
  6318. ((= dcst ?n)
  6319. (setq extractfun 'string-to-number
  6320. comparefun (if (= dcst sorting-type) '< '>)))
  6321. ((= dcst ?a)
  6322. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6323. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6324. comparefun (if (= dcst sorting-type)
  6325. 'string<
  6326. (lambda (a b) (and (not (string< a b))
  6327. (not (string= a b)))))))
  6328. ((= dcst ?t)
  6329. (setq extractfun
  6330. (lambda (x)
  6331. (if (or (string-match org-ts-regexp x)
  6332. (string-match org-ts-regexp-both x))
  6333. (org-float-time
  6334. (org-time-string-to-time (match-string 0 x)))
  6335. 0))
  6336. comparefun (if (= dcst sorting-type) '< '>)))
  6337. (t (error "Invalid sorting type `%c'" sorting-type)))
  6338. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6339. table)
  6340. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6341. ;;; The orgstruct minor mode
  6342. ;; Define a minor mode which can be used in other modes in order to
  6343. ;; integrate the org-mode structure editing commands.
  6344. ;; This is really a hack, because the org-mode structure commands use
  6345. ;; keys which normally belong to the major mode. Here is how it
  6346. ;; works: The minor mode defines all the keys necessary to operate the
  6347. ;; structure commands, but wraps the commands into a function which
  6348. ;; tests if the cursor is currently at a headline or a plain list
  6349. ;; item. If that is the case, the structure command is used,
  6350. ;; temporarily setting many Org-mode variables like regular
  6351. ;; expressions for filling etc. However, when any of those keys is
  6352. ;; used at a different location, function uses `key-binding' to look
  6353. ;; up if the key has an associated command in another currently active
  6354. ;; keymap (minor modes, major mode, global), and executes that
  6355. ;; command. There might be problems if any of the keys is otherwise
  6356. ;; used as a prefix key.
  6357. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6358. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6359. ;; addresses this by checking explicitly for both bindings.
  6360. (defvar orgstruct-mode-map (make-sparse-keymap)
  6361. "Keymap for the minor `orgstruct-mode'.")
  6362. (defvar org-local-vars nil
  6363. "List of local variables, for use by `orgstruct-mode'")
  6364. ;;;###autoload
  6365. (define-minor-mode orgstruct-mode
  6366. "Toggle the minor more `orgstruct-mode'.
  6367. This mode is for using Org-mode structure commands in other modes.
  6368. The following key behave as if Org-mode was active, if the cursor
  6369. is on a headline, or on a plain list item (both in the definition
  6370. of Org-mode).
  6371. M-up Move entry/item up
  6372. M-down Move entry/item down
  6373. M-left Promote
  6374. M-right Demote
  6375. M-S-up Move entry/item up
  6376. M-S-down Move entry/item down
  6377. M-S-left Promote subtree
  6378. M-S-right Demote subtree
  6379. M-q Fill paragraph and items like in Org-mode
  6380. C-c ^ Sort entries
  6381. C-c - Cycle list bullet
  6382. TAB Cycle item visibility
  6383. M-RET Insert new heading/item
  6384. S-M-RET Insert new TODO heading / Checkbox item
  6385. C-c C-c Set tags / toggle checkbox"
  6386. nil " OrgStruct" nil
  6387. (org-load-modules-maybe)
  6388. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6389. ;;;###autoload
  6390. (defun turn-on-orgstruct ()
  6391. "Unconditionally turn on `orgstruct-mode'."
  6392. (orgstruct-mode 1))
  6393. (defun orgstruct++-mode (&optional arg)
  6394. "Toggle `orgstruct-mode', the enhanced version of it.
  6395. In addition to setting orgstruct-mode, this also exports all indentation
  6396. and autofilling variables from org-mode into the buffer. It will also
  6397. recognize item context in multiline items.
  6398. Note that turning off orgstruct-mode will *not* remove the
  6399. indentation/paragraph settings. This can only be done by refreshing the
  6400. major mode, for example with \\[normal-mode]."
  6401. (interactive "P")
  6402. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6403. (if (< arg 1)
  6404. (orgstruct-mode -1)
  6405. (orgstruct-mode 1)
  6406. (let (var val)
  6407. (mapc
  6408. (lambda (x)
  6409. (when (string-match
  6410. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6411. (symbol-name (car x)))
  6412. (setq var (car x) val (nth 1 x))
  6413. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6414. org-local-vars)
  6415. (org-set-local 'orgstruct-is-++ t))))
  6416. (defvar orgstruct-is-++ nil
  6417. "Is orgstruct-mode in ++ version in the current-buffer?")
  6418. (make-variable-buffer-local 'orgstruct-is-++)
  6419. ;;;###autoload
  6420. (defun turn-on-orgstruct++ ()
  6421. "Unconditionally turn on `orgstruct++-mode'."
  6422. (orgstruct++-mode 1))
  6423. (defun orgstruct-error ()
  6424. "Error when there is no default binding for a structure key."
  6425. (interactive)
  6426. (error "This key has no function outside structure elements"))
  6427. (defun orgstruct-setup ()
  6428. "Setup orgstruct keymaps."
  6429. (let ((nfunc 0)
  6430. (bindings
  6431. (list
  6432. '([(meta up)] org-metaup)
  6433. '([(meta down)] org-metadown)
  6434. '([(meta left)] org-metaleft)
  6435. '([(meta right)] org-metaright)
  6436. '([(meta shift up)] org-shiftmetaup)
  6437. '([(meta shift down)] org-shiftmetadown)
  6438. '([(meta shift left)] org-shiftmetaleft)
  6439. '([(meta shift right)] org-shiftmetaright)
  6440. '([?\e (up)] org-metaup)
  6441. '([?\e (down)] org-metadown)
  6442. '([?\e (left)] org-metaleft)
  6443. '([?\e (right)] org-metaright)
  6444. '([?\e (shift up)] org-shiftmetaup)
  6445. '([?\e (shift down)] org-shiftmetadown)
  6446. '([?\e (shift left)] org-shiftmetaleft)
  6447. '([?\e (shift right)] org-shiftmetaright)
  6448. '([(shift up)] org-shiftup)
  6449. '([(shift down)] org-shiftdown)
  6450. '([(shift left)] org-shiftleft)
  6451. '([(shift right)] org-shiftright)
  6452. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6453. '("\M-q" fill-paragraph)
  6454. '("\C-c^" org-sort)
  6455. '("\C-c-" org-cycle-list-bullet)))
  6456. elt key fun cmd)
  6457. (while (setq elt (pop bindings))
  6458. (setq nfunc (1+ nfunc))
  6459. (setq key (org-key (car elt))
  6460. fun (nth 1 elt)
  6461. cmd (orgstruct-make-binding fun nfunc key))
  6462. (org-defkey orgstruct-mode-map key cmd))
  6463. ;; Special treatment needed for TAB and RET
  6464. (org-defkey orgstruct-mode-map [(tab)]
  6465. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6466. (org-defkey orgstruct-mode-map "\C-i"
  6467. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6468. (org-defkey orgstruct-mode-map "\M-\C-m"
  6469. (orgstruct-make-binding 'org-insert-heading 105
  6470. "\M-\C-m" [(meta return)]))
  6471. (org-defkey orgstruct-mode-map [(meta return)]
  6472. (orgstruct-make-binding 'org-insert-heading 106
  6473. [(meta return)] "\M-\C-m"))
  6474. (org-defkey orgstruct-mode-map [(shift meta return)]
  6475. (orgstruct-make-binding 'org-insert-todo-heading 107
  6476. [(meta return)] "\M-\C-m"))
  6477. (org-defkey orgstruct-mode-map "\e\C-m"
  6478. (orgstruct-make-binding 'org-insert-heading 108
  6479. "\e\C-m" [?\e (return)]))
  6480. (org-defkey orgstruct-mode-map [?\e (return)]
  6481. (orgstruct-make-binding 'org-insert-heading 109
  6482. [?\e (return)] "\e\C-m"))
  6483. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6484. (orgstruct-make-binding 'org-insert-todo-heading 110
  6485. [?\e (return)] "\e\C-m"))
  6486. (unless org-local-vars
  6487. (setq org-local-vars (org-get-local-variables)))
  6488. t))
  6489. (defun orgstruct-make-binding (fun n &rest keys)
  6490. "Create a function for binding in the structure minor mode.
  6491. FUN is the command to call inside a table. N is used to create a unique
  6492. command name. KEYS are keys that should be checked in for a command
  6493. to execute outside of tables."
  6494. (eval
  6495. (list 'defun
  6496. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6497. '(arg)
  6498. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6499. "Outside of structure, run the binding of `"
  6500. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6501. "'.")
  6502. '(interactive "p")
  6503. (list 'if
  6504. `(org-context-p 'headline 'item
  6505. (and orgstruct-is-++
  6506. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6507. 'item-body))
  6508. (list 'org-run-like-in-org-mode (list 'quote fun))
  6509. (list 'let '(orgstruct-mode)
  6510. (list 'call-interactively
  6511. (append '(or)
  6512. (mapcar (lambda (k)
  6513. (list 'key-binding k))
  6514. keys)
  6515. '('orgstruct-error))))))))
  6516. (defun org-context-p (&rest contexts)
  6517. "Check if local context is any of CONTEXTS.
  6518. Possible values in the list of contexts are `table', `headline', and `item'."
  6519. (let ((pos (point)))
  6520. (goto-char (point-at-bol))
  6521. (prog1 (or (and (memq 'table contexts)
  6522. (looking-at "[ \t]*|"))
  6523. (and (memq 'headline contexts)
  6524. ;;????????? (looking-at "\\*+"))
  6525. (looking-at outline-regexp))
  6526. (and (memq 'item contexts)
  6527. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6528. (and (memq 'item-body contexts)
  6529. (org-in-item-p)))
  6530. (goto-char pos))))
  6531. (defun org-get-local-variables ()
  6532. "Return a list of all local variables in an org-mode buffer."
  6533. (let (varlist)
  6534. (with-current-buffer (get-buffer-create "*Org tmp*")
  6535. (erase-buffer)
  6536. (org-mode)
  6537. (setq varlist (buffer-local-variables)))
  6538. (kill-buffer "*Org tmp*")
  6539. (delq nil
  6540. (mapcar
  6541. (lambda (x)
  6542. (setq x
  6543. (if (symbolp x)
  6544. (list x)
  6545. (list (car x) (list 'quote (cdr x)))))
  6546. (if (string-match
  6547. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6548. (symbol-name (car x)))
  6549. x nil))
  6550. varlist))))
  6551. ;;;###autoload
  6552. (defun org-run-like-in-org-mode (cmd)
  6553. "Run a command, pretending that the current buffer is in Org-mode.
  6554. This will temporarily bind local variables that are typically bound in
  6555. Org-mode to the values they have in Org-mode, and then interactively
  6556. call CMD."
  6557. (org-load-modules-maybe)
  6558. (unless org-local-vars
  6559. (setq org-local-vars (org-get-local-variables)))
  6560. (eval (list 'let org-local-vars
  6561. (list 'call-interactively (list 'quote cmd)))))
  6562. ;;;; Archiving
  6563. (defun org-get-category (&optional pos)
  6564. "Get the category applying to position POS."
  6565. (get-text-property (or pos (point)) 'org-category))
  6566. (defun org-refresh-category-properties ()
  6567. "Refresh category text properties in the buffer."
  6568. (let ((def-cat (cond
  6569. ((null org-category)
  6570. (if buffer-file-name
  6571. (file-name-sans-extension
  6572. (file-name-nondirectory buffer-file-name))
  6573. "???"))
  6574. ((symbolp org-category) (symbol-name org-category))
  6575. (t org-category)))
  6576. beg end cat pos optionp)
  6577. (org-unmodified
  6578. (save-excursion
  6579. (save-restriction
  6580. (widen)
  6581. (goto-char (point-min))
  6582. (put-text-property (point) (point-max) 'org-category def-cat)
  6583. (while (re-search-forward
  6584. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6585. (setq pos (match-end 0)
  6586. optionp (equal (char-after (match-beginning 0)) ?#)
  6587. cat (org-trim (match-string 2)))
  6588. (if optionp
  6589. (setq beg (point-at-bol) end (point-max))
  6590. (org-back-to-heading t)
  6591. (setq beg (point) end (org-end-of-subtree t t)))
  6592. (put-text-property beg end 'org-category cat)
  6593. (goto-char pos)))))))
  6594. ;;;; Link Stuff
  6595. ;;; Link abbreviations
  6596. (defun org-link-expand-abbrev (link)
  6597. "Apply replacements as defined in `org-link-abbrev-alist."
  6598. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6599. (let* ((key (match-string 1 link))
  6600. (as (or (assoc key org-link-abbrev-alist-local)
  6601. (assoc key org-link-abbrev-alist)))
  6602. (tag (and (match-end 2) (match-string 3 link)))
  6603. rpl)
  6604. (if (not as)
  6605. link
  6606. (setq rpl (cdr as))
  6607. (cond
  6608. ((symbolp rpl) (funcall rpl tag))
  6609. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6610. ((string-match "%h" rpl)
  6611. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6612. (t (concat rpl tag)))))
  6613. link))
  6614. ;;; Storing and inserting links
  6615. (defvar org-insert-link-history nil
  6616. "Minibuffer history for links inserted with `org-insert-link'.")
  6617. (defvar org-stored-links nil
  6618. "Contains the links stored with `org-store-link'.")
  6619. (defvar org-store-link-plist nil
  6620. "Plist with info about the most recently link created with `org-store-link'.")
  6621. (defvar org-link-protocols nil
  6622. "Link protocols added to Org-mode using `org-add-link-type'.")
  6623. (defvar org-store-link-functions nil
  6624. "List of functions that are called to create and store a link.
  6625. Each function will be called in turn until one returns a non-nil
  6626. value. Each function should check if it is responsible for creating
  6627. this link (for example by looking at the major mode).
  6628. If not, it must exit and return nil.
  6629. If yes, it should return a non-nil value after a calling
  6630. `org-store-link-props' with a list of properties and values.
  6631. Special properties are:
  6632. :type The link prefix. like \"http\". This must be given.
  6633. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6634. This is obligatory as well.
  6635. :description Optional default description for the second pair
  6636. of brackets in an Org-mode link. The user can still change
  6637. this when inserting this link into an Org-mode buffer.
  6638. In addition to these, any additional properties can be specified
  6639. and then used in remember templates.")
  6640. (defun org-add-link-type (type &optional follow export)
  6641. "Add TYPE to the list of `org-link-types'.
  6642. Re-compute all regular expressions depending on `org-link-types'
  6643. FOLLOW and EXPORT are two functions.
  6644. FOLLOW should take the link path as the single argument and do whatever
  6645. is necessary to follow the link, for example find a file or display
  6646. a mail message.
  6647. EXPORT should format the link path for export to one of the export formats.
  6648. It should be a function accepting three arguments:
  6649. path the path of the link, the text after the prefix (like \"http:\")
  6650. desc the description of the link, if any, nil if there was no description
  6651. format the export format, a symbol like `html' or `latex'.
  6652. The function may use the FORMAT information to return different values
  6653. depending on the format. The return value will be put literally into
  6654. the exported file.
  6655. Org-mode has a built-in default for exporting links. If you are happy with
  6656. this default, there is no need to define an export function for the link
  6657. type. For a simple example of an export function, see `org-bbdb.el'."
  6658. (add-to-list 'org-link-types type t)
  6659. (org-make-link-regexps)
  6660. (if (assoc type org-link-protocols)
  6661. (setcdr (assoc type org-link-protocols) (list follow export))
  6662. (push (list type follow export) org-link-protocols)))
  6663. (defvar org-agenda-buffer-name)
  6664. ;;;###autoload
  6665. (defun org-store-link (arg)
  6666. "\\<org-mode-map>Store an org-link to the current location.
  6667. This link is added to `org-stored-links' and can later be inserted
  6668. into an org-buffer with \\[org-insert-link].
  6669. For some link types, a prefix arg is interpreted:
  6670. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6671. For file links, arg negates `org-context-in-file-links'."
  6672. (interactive "P")
  6673. (org-load-modules-maybe)
  6674. (setq org-store-link-plist nil) ; reset
  6675. (let ((outline-regexp (org-get-limited-outline-regexp))
  6676. link cpltxt desc description search txt custom-id)
  6677. (cond
  6678. ((run-hook-with-args-until-success 'org-store-link-functions)
  6679. (setq link (plist-get org-store-link-plist :link)
  6680. desc (or (plist-get org-store-link-plist :description) link)))
  6681. ((equal (buffer-name) "*Org Edit Src Example*")
  6682. (let (label gc)
  6683. (while (or (not label)
  6684. (save-excursion
  6685. (save-restriction
  6686. (widen)
  6687. (goto-char (point-min))
  6688. (re-search-forward
  6689. (regexp-quote (format org-coderef-label-format label))
  6690. nil t))))
  6691. (when label (message "Label exists already") (sit-for 2))
  6692. (setq label (read-string "Code line label: " label)))
  6693. (end-of-line 1)
  6694. (setq link (format org-coderef-label-format label))
  6695. (setq gc (- 79 (length link)))
  6696. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6697. (insert link)
  6698. (setq link (concat "(" label ")") desc nil)))
  6699. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6700. ;; We are in the agenda, link to referenced location
  6701. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6702. (get-text-property (point) 'org-marker))))
  6703. (when m
  6704. (org-with-point-at m
  6705. (call-interactively 'org-store-link)))))
  6706. ((eq major-mode 'calendar-mode)
  6707. (let ((cd (calendar-cursor-to-date)))
  6708. (setq link
  6709. (format-time-string
  6710. (car org-time-stamp-formats)
  6711. (apply 'encode-time
  6712. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6713. nil nil nil))))
  6714. (org-store-link-props :type "calendar" :date cd)))
  6715. ((eq major-mode 'w3-mode)
  6716. (setq cpltxt (if (and (buffer-name)
  6717. (not (string-match "Untitled" (buffer-name))))
  6718. (buffer-name)
  6719. (url-view-url t))
  6720. link (org-make-link (url-view-url t)))
  6721. (org-store-link-props :type "w3" :url (url-view-url t)))
  6722. ((eq major-mode 'w3m-mode)
  6723. (setq cpltxt (or w3m-current-title w3m-current-url)
  6724. link (org-make-link w3m-current-url))
  6725. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6726. ((setq search (run-hook-with-args-until-success
  6727. 'org-create-file-search-functions))
  6728. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6729. "::" search))
  6730. (setq cpltxt (or description link)))
  6731. ((eq major-mode 'image-mode)
  6732. (setq cpltxt (concat "file:"
  6733. (abbreviate-file-name buffer-file-name))
  6734. link (org-make-link cpltxt))
  6735. (org-store-link-props :type "image" :file buffer-file-name))
  6736. ((eq major-mode 'dired-mode)
  6737. ;; link to the file in the current line
  6738. (setq cpltxt (concat "file:"
  6739. (abbreviate-file-name
  6740. (expand-file-name
  6741. (dired-get-filename nil t))))
  6742. link (org-make-link cpltxt)))
  6743. ((and buffer-file-name (org-mode-p))
  6744. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6745. (cond
  6746. ((org-in-regexp "<<\\(.*?\\)>>")
  6747. (setq cpltxt
  6748. (concat "file:"
  6749. (abbreviate-file-name buffer-file-name)
  6750. "::" (match-string 1))
  6751. link (org-make-link cpltxt)))
  6752. ((and (featurep 'org-id)
  6753. (or (eq org-link-to-org-use-id t)
  6754. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6755. (interactive-p))
  6756. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6757. (interactive-p)
  6758. (not custom-id))
  6759. (and org-link-to-org-use-id
  6760. (condition-case nil
  6761. (org-entry-get nil "ID")
  6762. (error nil)))))
  6763. ;; We can make a link using the ID.
  6764. (setq link (condition-case nil
  6765. (prog1 (org-id-store-link)
  6766. (setq desc (plist-get org-store-link-plist
  6767. :description)))
  6768. (error
  6769. ;; probably before first headline, link to file only
  6770. (concat "file:"
  6771. (abbreviate-file-name buffer-file-name))))))
  6772. (t
  6773. ;; Just link to current headline
  6774. (setq cpltxt (concat "file:"
  6775. (abbreviate-file-name buffer-file-name)))
  6776. ;; Add a context search string
  6777. (when (org-xor org-context-in-file-links arg)
  6778. (setq txt (cond
  6779. ((org-on-heading-p) nil)
  6780. ((org-region-active-p)
  6781. (buffer-substring (region-beginning) (region-end)))
  6782. (t nil)))
  6783. (when (or (null txt) (string-match "\\S-" txt))
  6784. (setq cpltxt
  6785. (concat cpltxt "::"
  6786. (condition-case nil
  6787. (org-make-org-heading-search-string txt)
  6788. (error "")))
  6789. desc (or (nth 4 (ignore-errors
  6790. (org-heading-components))) "NONE"))))
  6791. (if (string-match "::\\'" cpltxt)
  6792. (setq cpltxt (substring cpltxt 0 -2)))
  6793. (setq link (org-make-link cpltxt)))))
  6794. ((buffer-file-name (buffer-base-buffer))
  6795. ;; Just link to this file here.
  6796. (setq cpltxt (concat "file:"
  6797. (abbreviate-file-name
  6798. (buffer-file-name (buffer-base-buffer)))))
  6799. ;; Add a context string
  6800. (when (org-xor org-context-in-file-links arg)
  6801. (setq txt (if (org-region-active-p)
  6802. (buffer-substring (region-beginning) (region-end))
  6803. (buffer-substring (point-at-bol) (point-at-eol))))
  6804. ;; Only use search option if there is some text.
  6805. (when (string-match "\\S-" txt)
  6806. (setq cpltxt
  6807. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6808. desc "NONE")))
  6809. (setq link (org-make-link cpltxt)))
  6810. ((interactive-p)
  6811. (error "Cannot link to a buffer which is not visiting a file"))
  6812. (t (setq link nil)))
  6813. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6814. (setq link (or link cpltxt)
  6815. desc (or desc cpltxt))
  6816. (if (equal desc "NONE") (setq desc nil))
  6817. (if (and (or (interactive-p) executing-kbd-macro) link)
  6818. (progn
  6819. (setq org-stored-links
  6820. (cons (list link desc) org-stored-links))
  6821. (message "Stored: %s" (or desc link))
  6822. (when custom-id
  6823. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6824. "::#" custom-id))
  6825. (setq org-stored-links
  6826. (cons (list link desc) org-stored-links))))
  6827. (and link (org-make-link-string link desc)))))
  6828. (defun org-store-link-props (&rest plist)
  6829. "Store link properties, extract names and addresses."
  6830. (let (x adr)
  6831. (when (setq x (plist-get plist :from))
  6832. (setq adr (mail-extract-address-components x))
  6833. (setq plist (plist-put plist :fromname (car adr)))
  6834. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6835. (when (setq x (plist-get plist :to))
  6836. (setq adr (mail-extract-address-components x))
  6837. (setq plist (plist-put plist :toname (car adr)))
  6838. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6839. (let ((from (plist-get plist :from))
  6840. (to (plist-get plist :to)))
  6841. (when (and from to org-from-is-user-regexp)
  6842. (setq plist
  6843. (plist-put plist :fromto
  6844. (if (string-match org-from-is-user-regexp from)
  6845. (concat "to %t")
  6846. (concat "from %f"))))))
  6847. (setq org-store-link-plist plist))
  6848. (defun org-add-link-props (&rest plist)
  6849. "Add these properties to the link property list."
  6850. (let (key value)
  6851. (while plist
  6852. (setq key (pop plist) value (pop plist))
  6853. (setq org-store-link-plist
  6854. (plist-put org-store-link-plist key value)))))
  6855. (defun org-email-link-description (&optional fmt)
  6856. "Return the description part of an email link.
  6857. This takes information from `org-store-link-plist' and formats it
  6858. according to FMT (default from `org-email-link-description-format')."
  6859. (setq fmt (or fmt org-email-link-description-format))
  6860. (let* ((p org-store-link-plist)
  6861. (to (plist-get p :toaddress))
  6862. (from (plist-get p :fromaddress))
  6863. (table
  6864. (list
  6865. (cons "%c" (plist-get p :fromto))
  6866. (cons "%F" (plist-get p :from))
  6867. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6868. (cons "%T" (plist-get p :to))
  6869. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6870. (cons "%s" (plist-get p :subject))
  6871. (cons "%m" (plist-get p :message-id)))))
  6872. (when (string-match "%c" fmt)
  6873. ;; Check if the user wrote this message
  6874. (if (and org-from-is-user-regexp from to
  6875. (save-match-data (string-match org-from-is-user-regexp from)))
  6876. (setq fmt (replace-match "to %t" t t fmt))
  6877. (setq fmt (replace-match "from %f" t t fmt))))
  6878. (org-replace-escapes fmt table)))
  6879. (defun org-make-org-heading-search-string (&optional string heading)
  6880. "Make search string for STRING or current headline."
  6881. (interactive)
  6882. (let ((s (or string (org-get-heading))))
  6883. (unless (and string (not heading))
  6884. ;; We are using a headline, clean up garbage in there.
  6885. (if (string-match org-todo-regexp s)
  6886. (setq s (replace-match "" t t s)))
  6887. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6888. (setq s (replace-match "" t t s)))
  6889. (setq s (org-trim s))
  6890. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6891. org-comment-string "\\)") s)
  6892. (setq s (replace-match "" t t s)))
  6893. (while (string-match org-ts-regexp s)
  6894. (setq s (replace-match "" t t s))))
  6895. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6896. (setq s (replace-match " " t t s)))
  6897. (or string (setq s (concat "*" s))) ; Add * for headlines
  6898. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6899. (defun org-make-link (&rest strings)
  6900. "Concatenate STRINGS."
  6901. (apply 'concat strings))
  6902. (defun org-make-link-string (link &optional description)
  6903. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6904. (unless (string-match "\\S-" link)
  6905. (error "Empty link"))
  6906. (when (stringp description)
  6907. ;; Remove brackets from the description, they are fatal.
  6908. (while (string-match "\\[" description)
  6909. (setq description (replace-match "{" t t description)))
  6910. (while (string-match "\\]" description)
  6911. (setq description (replace-match "}" t t description))))
  6912. (when (equal (org-link-escape link) description)
  6913. ;; No description needed, it is identical
  6914. (setq description nil))
  6915. (when (and (not description)
  6916. (not (equal link (org-link-escape link))))
  6917. (setq description (org-extract-attributes link)))
  6918. (concat "[[" (org-link-escape link) "]"
  6919. (if description (concat "[" description "]") "")
  6920. "]"))
  6921. (defconst org-link-escape-chars
  6922. '((?\ . "%20")
  6923. (?\[ . "%5B")
  6924. (?\] . "%5D")
  6925. (?\340 . "%E0") ; `a
  6926. (?\342 . "%E2") ; ^a
  6927. (?\347 . "%E7") ; ,c
  6928. (?\350 . "%E8") ; `e
  6929. (?\351 . "%E9") ; 'e
  6930. (?\352 . "%EA") ; ^e
  6931. (?\356 . "%EE") ; ^i
  6932. (?\364 . "%F4") ; ^o
  6933. (?\371 . "%F9") ; `u
  6934. (?\373 . "%FB") ; ^u
  6935. (?\; . "%3B")
  6936. (?? . "%3F")
  6937. (?= . "%3D")
  6938. (?+ . "%2B")
  6939. )
  6940. "Association list of escapes for some characters problematic in links.
  6941. This is the list that is used for internal purposes.")
  6942. (defvar org-url-encoding-use-url-hexify nil)
  6943. (defconst org-link-escape-chars-browser
  6944. '((?\ . "%20")) ; 32 for the SPC char
  6945. "Association list of escapes for some characters problematic in links.
  6946. This is the list that is used before handing over to the browser.")
  6947. (defun org-link-escape (text &optional table)
  6948. "Escape characters in TEXT that are problematic for links."
  6949. (if org-url-encoding-use-url-hexify
  6950. (url-hexify-string text)
  6951. (setq table (or table org-link-escape-chars))
  6952. (when text
  6953. (let ((re (mapconcat (lambda (x) (regexp-quote
  6954. (char-to-string (car x))))
  6955. table "\\|")))
  6956. (while (string-match re text)
  6957. (setq text
  6958. (replace-match
  6959. (cdr (assoc (string-to-char (match-string 0 text))
  6960. table))
  6961. t t text)))
  6962. text))))
  6963. (defun org-link-unescape (text &optional table)
  6964. "Reverse the action of `org-link-escape'."
  6965. (if org-url-encoding-use-url-hexify
  6966. (url-unhex-string text)
  6967. (setq table (or table org-link-escape-chars))
  6968. (when text
  6969. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6970. table "\\|")))
  6971. (while (string-match re text)
  6972. (setq text
  6973. (replace-match
  6974. (char-to-string (car (rassoc (match-string 0 text) table)))
  6975. t t text)))
  6976. text))))
  6977. (defun org-xor (a b)
  6978. "Exclusive or."
  6979. (if a (not b) b))
  6980. (defun org-fixup-message-id-for-http (s)
  6981. "Replace special characters in a message id, so it can be used in an http query."
  6982. (while (string-match "<" s)
  6983. (setq s (replace-match "%3C" t t s)))
  6984. (while (string-match ">" s)
  6985. (setq s (replace-match "%3E" t t s)))
  6986. (while (string-match "@" s)
  6987. (setq s (replace-match "%40" t t s)))
  6988. s)
  6989. ;;;###autoload
  6990. (defun org-insert-link-global ()
  6991. "Insert a link like Org-mode does.
  6992. This command can be called in any mode to insert a link in Org-mode syntax."
  6993. (interactive)
  6994. (org-load-modules-maybe)
  6995. (org-run-like-in-org-mode 'org-insert-link))
  6996. (defun org-insert-link (&optional complete-file link-location)
  6997. "Insert a link. At the prompt, enter the link.
  6998. Completion can be used to insert any of the link protocol prefixes like
  6999. http or ftp in use.
  7000. The history can be used to select a link previously stored with
  7001. `org-store-link'. When the empty string is entered (i.e. if you just
  7002. press RET at the prompt), the link defaults to the most recently
  7003. stored link. As SPC triggers completion in the minibuffer, you need to
  7004. use M-SPC or C-q SPC to force the insertion of a space character.
  7005. You will also be prompted for a description, and if one is given, it will
  7006. be displayed in the buffer instead of the link.
  7007. If there is already a link at point, this command will allow you to edit link
  7008. and description parts.
  7009. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7010. be selected using completion. The path to the file will be relative to the
  7011. current directory if the file is in the current directory or a subdirectory.
  7012. Otherwise, the link will be the absolute path as completed in the minibuffer
  7013. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7014. option `org-link-file-path-type'.
  7015. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7016. the current directory or below.
  7017. With three \\[universal-argument] prefixes, negate the meaning of
  7018. `org-keep-stored-link-after-insertion'.
  7019. If `org-make-link-description-function' is non-nil, this function will be
  7020. called with the link target, and the result will be the default
  7021. link description.
  7022. If the LINK-LOCATION parameter is non-nil, this value will be
  7023. used as the link location instead of reading one interactively."
  7024. (interactive "P")
  7025. (let* ((wcf (current-window-configuration))
  7026. (region (if (org-region-active-p)
  7027. (buffer-substring (region-beginning) (region-end))))
  7028. (remove (and region (list (region-beginning) (region-end))))
  7029. (desc region)
  7030. tmphist ; byte-compile incorrectly complains about this
  7031. (link link-location)
  7032. entry file all-prefixes)
  7033. (cond
  7034. (link-location) ; specified by arg, just use it.
  7035. ((org-in-regexp org-bracket-link-regexp 1)
  7036. ;; We do have a link at point, and we are going to edit it.
  7037. (setq remove (list (match-beginning 0) (match-end 0)))
  7038. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7039. (setq link (read-string "Link: "
  7040. (org-link-unescape
  7041. (org-match-string-no-properties 1)))))
  7042. ((or (org-in-regexp org-angle-link-re)
  7043. (org-in-regexp org-plain-link-re))
  7044. ;; Convert to bracket link
  7045. (setq remove (list (match-beginning 0) (match-end 0))
  7046. link (read-string "Link: "
  7047. (org-remove-angle-brackets (match-string 0)))))
  7048. ((member complete-file '((4) (16)))
  7049. ;; Completing read for file names.
  7050. (setq link (org-file-complete-link complete-file)))
  7051. (t
  7052. ;; Read link, with completion for stored links.
  7053. (with-output-to-temp-buffer "*Org Links*"
  7054. (princ "Insert a link.
  7055. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7056. (when org-stored-links
  7057. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7058. (princ (mapconcat
  7059. (lambda (x)
  7060. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7061. (reverse org-stored-links) "\n"))))
  7062. (let ((cw (selected-window)))
  7063. (select-window (get-buffer-window "*Org Links*"))
  7064. (setq truncate-lines t)
  7065. (unless (pos-visible-in-window-p (point-max))
  7066. (org-fit-window-to-buffer))
  7067. (and (window-live-p cw) (select-window cw)))
  7068. ;; Fake a link history, containing the stored links.
  7069. (setq tmphist (append (mapcar 'car org-stored-links)
  7070. org-insert-link-history))
  7071. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7072. (mapcar 'car org-link-abbrev-alist)
  7073. org-link-types))
  7074. (unwind-protect
  7075. (progn
  7076. (setq link
  7077. (let ((org-completion-use-ido nil)
  7078. (org-completion-use-iswitchb nil))
  7079. (org-completing-read
  7080. "Link: "
  7081. (append
  7082. (mapcar (lambda (x) (list (concat x ":")))
  7083. all-prefixes)
  7084. (mapcar 'car org-stored-links))
  7085. nil nil nil
  7086. 'tmphist
  7087. (car (car org-stored-links)))))
  7088. (if (or (member link all-prefixes)
  7089. (and (equal ":" (substring link -1))
  7090. (member (substring link 0 -1) all-prefixes)
  7091. (setq link (substring link 0 -1))))
  7092. (setq link (org-link-try-special-completion link))))
  7093. (set-window-configuration wcf)
  7094. (kill-buffer "*Org Links*"))
  7095. (setq entry (assoc link org-stored-links))
  7096. (or entry (push link org-insert-link-history))
  7097. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7098. (not org-keep-stored-link-after-insertion))
  7099. (setq org-stored-links (delq (assoc link org-stored-links)
  7100. org-stored-links)))
  7101. (setq desc (or desc (nth 1 entry)))))
  7102. (if (string-match org-plain-link-re link)
  7103. ;; URL-like link, normalize the use of angular brackets.
  7104. (setq link (org-make-link (org-remove-angle-brackets link))))
  7105. ;; Check if we are linking to the current file with a search option
  7106. ;; If yes, simplify the link by using only the search option.
  7107. (when (and buffer-file-name
  7108. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7109. (let* ((path (match-string 1 link))
  7110. (case-fold-search nil)
  7111. (search (match-string 2 link)))
  7112. (save-match-data
  7113. (if (equal (file-truename buffer-file-name) (file-truename path))
  7114. ;; We are linking to this same file, with a search option
  7115. (setq link search)))))
  7116. ;; Check if we can/should use a relative path. If yes, simplify the link
  7117. (when (string-match "^file:\\(.*\\)" link)
  7118. (let* ((path (match-string 1 link))
  7119. (origpath path)
  7120. (case-fold-search nil))
  7121. (cond
  7122. ((or (eq org-link-file-path-type 'absolute)
  7123. (equal complete-file '(16)))
  7124. (setq path (abbreviate-file-name (expand-file-name path))))
  7125. ((eq org-link-file-path-type 'noabbrev)
  7126. (setq path (expand-file-name path)))
  7127. ((eq org-link-file-path-type 'relative)
  7128. (setq path (file-relative-name path)))
  7129. (t
  7130. (save-match-data
  7131. (if (string-match (concat "^" (regexp-quote
  7132. (file-name-as-directory
  7133. (expand-file-name "."))))
  7134. (expand-file-name path))
  7135. ;; We are linking a file with relative path name.
  7136. (setq path (substring (expand-file-name path)
  7137. (match-end 0)))
  7138. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7139. (setq link (concat "file:" path))
  7140. (if (equal desc origpath)
  7141. (setq desc path))))
  7142. (if org-make-link-description-function
  7143. (setq desc (funcall org-make-link-description-function link desc)))
  7144. (setq desc (read-string "Description: " desc))
  7145. (unless (string-match "\\S-" desc) (setq desc nil))
  7146. (if remove (apply 'delete-region remove))
  7147. (insert (org-make-link-string link desc))))
  7148. (defun org-link-try-special-completion (type)
  7149. "If there is completion support for link type TYPE, offer it."
  7150. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7151. (if (functionp fun)
  7152. (funcall fun)
  7153. (read-string "Link (no completion support): " (concat type ":")))))
  7154. (defun org-file-complete-link (&optional arg)
  7155. "Create a file link using completion."
  7156. (let (file link)
  7157. (setq file (read-file-name "File: "))
  7158. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7159. (pwd1 (file-name-as-directory (abbreviate-file-name
  7160. (expand-file-name ".")))))
  7161. (cond
  7162. ((equal arg '(16))
  7163. (setq link (org-make-link
  7164. "file:"
  7165. (abbreviate-file-name (expand-file-name file)))))
  7166. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7167. (setq link (org-make-link "file:" (match-string 1 file))))
  7168. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7169. (expand-file-name file))
  7170. (setq link (org-make-link
  7171. "file:" (match-string 1 (expand-file-name file)))))
  7172. (t (setq link (org-make-link "file:" file)))))
  7173. link))
  7174. (defun org-completing-read (&rest args)
  7175. "Completing-read with SPACE being a normal character."
  7176. (let ((minibuffer-local-completion-map
  7177. (copy-keymap minibuffer-local-completion-map)))
  7178. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7179. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7180. (apply 'org-icompleting-read args)))
  7181. (defun org-completing-read-no-i (&rest args)
  7182. (let (org-completion-use-ido org-completion-use-iswitchb)
  7183. (apply 'org-completing-read args)))
  7184. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7185. "Use iswitch as a completing-read replacement to choose from choices.
  7186. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7187. from."
  7188. (let* ((iswitchb-use-virtual-buffers nil)
  7189. (iswitchb-make-buflist-hook
  7190. (lambda ()
  7191. (setq iswitchb-temp-buflist choices))))
  7192. (iswitchb-read-buffer prompt)))
  7193. (defun org-icompleting-read (&rest args)
  7194. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7195. (org-without-partial-completion
  7196. (if (and org-completion-use-ido
  7197. (fboundp 'ido-completing-read)
  7198. (boundp 'ido-mode) ido-mode
  7199. (listp (second args)))
  7200. (let ((ido-enter-matching-directory nil))
  7201. (apply 'ido-completing-read (concat (car args))
  7202. (if (consp (car (nth 1 args)))
  7203. (mapcar (lambda (x) (car x)) (nth 1 args))
  7204. (nth 1 args))
  7205. (cddr args)))
  7206. (if (and org-completion-use-iswitchb
  7207. (boundp 'iswitchb-mode) iswitchb-mode
  7208. (listp (second args)))
  7209. (apply 'org-iswitchb-completing-read (concat (car args))
  7210. (if (consp (car (nth 1 args)))
  7211. (mapcar (lambda (x) (car x)) (nth 1 args))
  7212. (nth 1 args))
  7213. (cddr args))
  7214. (apply 'completing-read args)))))
  7215. (defun org-extract-attributes (s)
  7216. "Extract the attributes cookie from a string and set as text property."
  7217. (let (a attr (start 0) key value)
  7218. (save-match-data
  7219. (when (string-match "{{\\([^}]+\\)}}$" s)
  7220. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7221. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7222. (setq key (match-string 1 a) value (match-string 2 a)
  7223. start (match-end 0)
  7224. attr (plist-put attr (intern key) value))))
  7225. (org-add-props s nil 'org-attr attr))
  7226. s))
  7227. (defun org-extract-attributes-from-string (tag)
  7228. (let (key value attr)
  7229. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7230. (setq key (match-string 1 tag) value (match-string 2 tag)
  7231. tag (replace-match "" t t tag)
  7232. attr (plist-put attr (intern key) value)))
  7233. (cons tag attr)))
  7234. (defun org-attributes-to-string (plist)
  7235. "Format a property list into an HTML attribute list."
  7236. (let ((s "") key value)
  7237. (while plist
  7238. (setq key (pop plist) value (pop plist))
  7239. (and value
  7240. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7241. s))
  7242. ;;; Opening/following a link
  7243. (defvar org-link-search-failed nil)
  7244. (defun org-next-link ()
  7245. "Move forward to the next link.
  7246. If the link is in hidden text, expose it."
  7247. (interactive)
  7248. (when (and org-link-search-failed (eq this-command last-command))
  7249. (goto-char (point-min))
  7250. (message "Link search wrapped back to beginning of buffer"))
  7251. (setq org-link-search-failed nil)
  7252. (let* ((pos (point))
  7253. (ct (org-context))
  7254. (a (assoc :link ct)))
  7255. (if a (goto-char (nth 2 a)))
  7256. (if (re-search-forward org-any-link-re nil t)
  7257. (progn
  7258. (goto-char (match-beginning 0))
  7259. (if (org-invisible-p) (org-show-context)))
  7260. (goto-char pos)
  7261. (setq org-link-search-failed t)
  7262. (error "No further link found"))))
  7263. (defun org-previous-link ()
  7264. "Move backward to the previous link.
  7265. If the link is in hidden text, expose it."
  7266. (interactive)
  7267. (when (and org-link-search-failed (eq this-command last-command))
  7268. (goto-char (point-max))
  7269. (message "Link search wrapped back to end of buffer"))
  7270. (setq org-link-search-failed nil)
  7271. (let* ((pos (point))
  7272. (ct (org-context))
  7273. (a (assoc :link ct)))
  7274. (if a (goto-char (nth 1 a)))
  7275. (if (re-search-backward org-any-link-re nil t)
  7276. (progn
  7277. (goto-char (match-beginning 0))
  7278. (if (org-invisible-p) (org-show-context)))
  7279. (goto-char pos)
  7280. (setq org-link-search-failed t)
  7281. (error "No further link found"))))
  7282. (defun org-translate-link (s)
  7283. "Translate a link string if a translation function has been defined."
  7284. (if (and org-link-translation-function
  7285. (fboundp org-link-translation-function)
  7286. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7287. (progn
  7288. (setq s (funcall org-link-translation-function
  7289. (match-string 1) (match-string 2)))
  7290. (concat (car s) ":" (cdr s)))
  7291. s))
  7292. (defun org-translate-link-from-planner (type path)
  7293. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7294. This is still an experimental function, your mileage may vary."
  7295. (cond
  7296. ((member type '("http" "https" "news" "ftp"))
  7297. ;; standard Internet links are the same.
  7298. nil)
  7299. ((and (equal type "irc") (string-match "^//" path))
  7300. ;; Planner has two / at the beginning of an irc link, we have 1.
  7301. ;; We should have zero, actually....
  7302. (setq path (substring path 1)))
  7303. ((and (equal type "lisp") (string-match "^/" path))
  7304. ;; Planner has a slash, we do not.
  7305. (setq type "elisp" path (substring path 1)))
  7306. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7307. ;; A typical message link. Planner has the id after the final slash,
  7308. ;; we separate it with a hash mark
  7309. (setq path (concat (match-string 1 path) "#"
  7310. (org-remove-angle-brackets (match-string 2 path)))))
  7311. )
  7312. (cons type path))
  7313. (defun org-find-file-at-mouse (ev)
  7314. "Open file link or URL at mouse."
  7315. (interactive "e")
  7316. (mouse-set-point ev)
  7317. (org-open-at-point 'in-emacs))
  7318. (defun org-open-at-mouse (ev)
  7319. "Open file link or URL at mouse."
  7320. (interactive "e")
  7321. (mouse-set-point ev)
  7322. (if (eq major-mode 'org-agenda-mode)
  7323. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7324. (org-open-at-point))
  7325. (defvar org-window-config-before-follow-link nil
  7326. "The window configuration before following a link.
  7327. This is saved in case the need arises to restore it.")
  7328. (defvar org-open-link-marker (make-marker)
  7329. "Marker pointing to the location where `org-open-at-point; was called.")
  7330. ;;;###autoload
  7331. (defun org-open-at-point-global ()
  7332. "Follow a link like Org-mode does.
  7333. This command can be called in any mode to follow a link that has
  7334. Org-mode syntax."
  7335. (interactive)
  7336. (org-run-like-in-org-mode 'org-open-at-point))
  7337. ;;;###autoload
  7338. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7339. "Open a link in the string S, as if it was in Org-mode."
  7340. (interactive "sLink: \nP")
  7341. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7342. (with-temp-buffer
  7343. (let ((org-inhibit-startup t))
  7344. (org-mode)
  7345. (insert s)
  7346. (goto-char (point-min))
  7347. (org-open-at-point arg reference-buffer)))))
  7348. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7349. "Open link at or after point.
  7350. If there is no link at point, this function will search forward up to
  7351. the end of the current line.
  7352. Normally, files will be opened by an appropriate application. If the
  7353. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7354. With a double prefix argument, try to open outside of Emacs, in the
  7355. application the system uses for this file type."
  7356. (interactive "P")
  7357. (org-load-modules-maybe)
  7358. (move-marker org-open-link-marker (point))
  7359. (setq org-window-config-before-follow-link (current-window-configuration))
  7360. (org-remove-occur-highlights nil nil t)
  7361. (cond
  7362. ((and (org-on-heading-p)
  7363. (not (org-in-regexp
  7364. (concat org-plain-link-re "\\|"
  7365. org-bracket-link-regexp "\\|"
  7366. org-angle-link-re "\\|"
  7367. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7368. (or (org-offer-links-in-entry in-emacs)
  7369. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7370. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7371. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7372. (org-footnote-action))
  7373. (t
  7374. (let (type path link line search (pos (point)))
  7375. (catch 'match
  7376. (save-excursion
  7377. (skip-chars-forward "^]\n\r")
  7378. (when (org-in-regexp org-bracket-link-regexp)
  7379. (setq link (org-extract-attributes
  7380. (org-link-unescape (org-match-string-no-properties 1))))
  7381. (while (string-match " *\n *" link)
  7382. (setq link (replace-match " " t t link)))
  7383. (setq link (org-link-expand-abbrev link))
  7384. (cond
  7385. ((or (file-name-absolute-p link)
  7386. (string-match "^\\.\\.?/" link))
  7387. (setq type "file" path link))
  7388. ((string-match org-link-re-with-space3 link)
  7389. (setq type (match-string 1 link) path (match-string 2 link)))
  7390. (t (setq type "thisfile" path link)))
  7391. (throw 'match t)))
  7392. (when (get-text-property (point) 'org-linked-text)
  7393. (setq type "thisfile"
  7394. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7395. (1+ (point)) (point))
  7396. path (buffer-substring
  7397. (previous-single-property-change pos 'org-linked-text)
  7398. (next-single-property-change pos 'org-linked-text)))
  7399. (throw 'match t))
  7400. (save-excursion
  7401. (when (or (org-in-regexp org-angle-link-re)
  7402. (org-in-regexp org-plain-link-re))
  7403. (setq type (match-string 1) path (match-string 2))
  7404. (throw 'match t)))
  7405. (save-excursion
  7406. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7407. (setq type "tags"
  7408. path (match-string 1))
  7409. (while (string-match ":" path)
  7410. (setq path (replace-match "+" t t path)))
  7411. (throw 'match t)))
  7412. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7413. (setq type "tree-match"
  7414. path (match-string 1))
  7415. (throw 'match t)))
  7416. (unless path
  7417. (error "No link found"))
  7418. ;; switch back to reference buffer
  7419. ;; needed when if called in a temporary buffer through
  7420. ;; org-open-link-from-string
  7421. (with-current-buffer (or reference-buffer (current-buffer))
  7422. ;; Remove any trailing spaces in path
  7423. (if (string-match " +\\'" path)
  7424. (setq path (replace-match "" t t path)))
  7425. (if (and org-link-translation-function
  7426. (fboundp org-link-translation-function))
  7427. ;; Check if we need to translate the link
  7428. (let ((tmp (funcall org-link-translation-function type path)))
  7429. (setq type (car tmp) path (cdr tmp))))
  7430. (cond
  7431. ((assoc type org-link-protocols)
  7432. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7433. ((equal type "mailto")
  7434. (let ((cmd (car org-link-mailto-program))
  7435. (args (cdr org-link-mailto-program)) args1
  7436. (address path) (subject "") a)
  7437. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7438. (setq address (match-string 1 path)
  7439. subject (org-link-escape (match-string 2 path))))
  7440. (while args
  7441. (cond
  7442. ((not (stringp (car args))) (push (pop args) args1))
  7443. (t (setq a (pop args))
  7444. (if (string-match "%a" a)
  7445. (setq a (replace-match address t t a)))
  7446. (if (string-match "%s" a)
  7447. (setq a (replace-match subject t t a)))
  7448. (push a args1))))
  7449. (apply cmd (nreverse args1))))
  7450. ((member type '("http" "https" "ftp" "news"))
  7451. (browse-url (concat type ":" (org-link-escape
  7452. path org-link-escape-chars-browser))))
  7453. ((member type '("message"))
  7454. (browse-url (concat type ":" path)))
  7455. ((string= type "tags")
  7456. (org-tags-view in-emacs path))
  7457. ((string= type "thisfile")
  7458. (if in-emacs
  7459. (switch-to-buffer-other-window
  7460. (org-get-buffer-for-internal-link (current-buffer)))
  7461. (org-mark-ring-push))
  7462. (let ((cmd `(org-link-search
  7463. ,path
  7464. ,(cond ((equal in-emacs '(4)) 'occur)
  7465. ((equal in-emacs '(16)) 'org-occur)
  7466. (t nil))
  7467. ,pos)))
  7468. (condition-case nil (eval cmd)
  7469. (error (progn (widen) (eval cmd))))))
  7470. ((string= type "tree-match")
  7471. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7472. ((string= type "file")
  7473. (if (string-match "::\\([0-9]+\\)\\'" path)
  7474. (setq line (string-to-number (match-string 1 path))
  7475. path (substring path 0 (match-beginning 0)))
  7476. (if (string-match "::\\(.+\\)\\'" path)
  7477. (setq search (match-string 1 path)
  7478. path (substring path 0 (match-beginning 0)))))
  7479. (if (string-match "[*?{]" (file-name-nondirectory path))
  7480. (dired path)
  7481. (org-open-file path in-emacs line search)))
  7482. ((string= type "news")
  7483. (require 'org-gnus)
  7484. (org-gnus-follow-link path))
  7485. ((string= type "shell")
  7486. (let ((cmd path))
  7487. (if (or (not org-confirm-shell-link-function)
  7488. (funcall org-confirm-shell-link-function
  7489. (format "Execute \"%s\" in shell? "
  7490. (org-add-props cmd nil
  7491. 'face 'org-warning))))
  7492. (progn
  7493. (message "Executing %s" cmd)
  7494. (shell-command cmd))
  7495. (error "Abort"))))
  7496. ((string= type "elisp")
  7497. (let ((cmd path))
  7498. (if (or (not org-confirm-elisp-link-function)
  7499. (funcall org-confirm-elisp-link-function
  7500. (format "Execute \"%s\" as elisp? "
  7501. (org-add-props cmd nil
  7502. 'face 'org-warning))))
  7503. (message "%s => %s" cmd
  7504. (if (equal (string-to-char cmd) ?\()
  7505. (eval (read cmd))
  7506. (call-interactively (read cmd))))
  7507. (error "Abort"))))
  7508. (t
  7509. (browse-url-at-point)))))))
  7510. (move-marker org-open-link-marker nil)
  7511. (run-hook-with-args 'org-follow-link-hook))
  7512. (defun org-offer-links-in-entry (&optional nth zero)
  7513. "Offer links in the current entry and follow the selected link.
  7514. If there is only one link, follow it immediately as well.
  7515. If NTH is an integer, immediately pick the NTH link found.
  7516. If ZERO is a string, check also this string for a link, and if
  7517. there is one, offer it as link number zero."
  7518. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7519. "\\(" org-angle-link-re "\\)\\|"
  7520. "\\(" org-plain-link-re "\\)"))
  7521. (cnt ?0)
  7522. (in-emacs (if (integerp nth) nil nth))
  7523. have-zero end links link c)
  7524. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7525. (push (match-string 0 zero) links)
  7526. (setq cnt (1- cnt) have-zero t))
  7527. (save-excursion
  7528. (org-back-to-heading t)
  7529. (setq end (save-excursion (outline-next-heading) (point)))
  7530. (while (re-search-forward re end t)
  7531. (push (match-string 0) links))
  7532. (setq links (org-uniquify (reverse links))))
  7533. (cond
  7534. ((null links)
  7535. (message "No links"))
  7536. ((equal (length links) 1)
  7537. (setq link (car links)))
  7538. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7539. (setq link (nth (if have-zero nth (1- nth)) links)))
  7540. (t ; we have to select a link
  7541. (save-excursion
  7542. (save-window-excursion
  7543. (delete-other-windows)
  7544. (with-output-to-temp-buffer "*Select Link*"
  7545. (mapc (lambda (l)
  7546. (if (not (string-match org-bracket-link-regexp l))
  7547. (princ (format "[%c] %s\n" (incf cnt)
  7548. (org-remove-angle-brackets l)))
  7549. (if (match-end 3)
  7550. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7551. (match-string 3 l) (match-string 1 l)))
  7552. (princ (format "[%c] %s\n" (incf cnt)
  7553. (match-string 1 l))))))
  7554. links))
  7555. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7556. (message "Select link to open:")
  7557. (setq c (read-char-exclusive))
  7558. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7559. (when (equal c ?q) (error "Abort"))
  7560. (setq nth (- c ?0))
  7561. (if have-zero (setq nth (1+ nth)))
  7562. (unless (and (integerp nth) (>= (length links) nth))
  7563. (error "Invalid link selection"))
  7564. (setq link (nth (1- nth) links))))
  7565. (if link
  7566. (progn (org-open-link-from-string link in-emacs (current-buffer)) t)
  7567. nil)))
  7568. ;;;; Time estimates
  7569. (defun org-get-effort (&optional pom)
  7570. "Get the effort estimate for the current entry."
  7571. (org-entry-get pom org-effort-property))
  7572. ;;; File search
  7573. (defvar org-create-file-search-functions nil
  7574. "List of functions to construct the right search string for a file link.
  7575. These functions are called in turn with point at the location to
  7576. which the link should point.
  7577. A function in the hook should first test if it would like to
  7578. handle this file type, for example by checking the major-mode or
  7579. the file extension. If it decides not to handle this file, it
  7580. should just return nil to give other functions a chance. If it
  7581. does handle the file, it must return the search string to be used
  7582. when following the link. The search string will be part of the
  7583. file link, given after a double colon, and `org-open-at-point'
  7584. will automatically search for it. If special measures must be
  7585. taken to make the search successful, another function should be
  7586. added to the companion hook `org-execute-file-search-functions',
  7587. which see.
  7588. A function in this hook may also use `setq' to set the variable
  7589. `description' to provide a suggestion for the descriptive text to
  7590. be used for this link when it gets inserted into an Org-mode
  7591. buffer with \\[org-insert-link].")
  7592. (defvar org-execute-file-search-functions nil
  7593. "List of functions to execute a file search triggered by a link.
  7594. Functions added to this hook must accept a single argument, the
  7595. search string that was part of the file link, the part after the
  7596. double colon. The function must first check if it would like to
  7597. handle this search, for example by checking the major-mode or the
  7598. file extension. If it decides not to handle this search, it
  7599. should just return nil to give other functions a chance. If it
  7600. does handle the search, it must return a non-nil value to keep
  7601. other functions from trying.
  7602. Each function can access the current prefix argument through the
  7603. variable `current-prefix-argument'. Note that a single prefix is
  7604. used to force opening a link in Emacs, so it may be good to only
  7605. use a numeric or double prefix to guide the search function.
  7606. In case this is needed, a function in this hook can also restore
  7607. the window configuration before `org-open-at-point' was called using:
  7608. (set-window-configuration org-window-config-before-follow-link)")
  7609. (defun org-link-search (s &optional type avoid-pos)
  7610. "Search for a link search option.
  7611. If S is surrounded by forward slashes, it is interpreted as a
  7612. regular expression. In org-mode files, this will create an `org-occur'
  7613. sparse tree. In ordinary files, `occur' will be used to list matches.
  7614. If the current buffer is in `dired-mode', grep will be used to search
  7615. in all files. If AVOID-POS is given, ignore matches near that position."
  7616. (let ((case-fold-search t)
  7617. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7618. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7619. (append '(("") (" ") ("\t") ("\n"))
  7620. org-emphasis-alist)
  7621. "\\|") "\\)"))
  7622. (pos (point))
  7623. (pre nil) (post nil)
  7624. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7625. (cond
  7626. ;; First check if there are any special
  7627. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7628. ;; Now try the builtin stuff
  7629. ((and (equal (string-to-char s0) ?#)
  7630. (> (length s0) 1)
  7631. (save-excursion
  7632. (goto-char (point-min))
  7633. (and
  7634. (re-search-forward
  7635. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7636. (setq type 'dedicated
  7637. pos (match-beginning 0))))
  7638. ;; There is an exact target for this
  7639. (goto-char pos)
  7640. (org-back-to-heading t)))
  7641. ((save-excursion
  7642. (goto-char (point-min))
  7643. (and
  7644. (re-search-forward
  7645. (concat "<<" (regexp-quote s0) ">>") nil t)
  7646. (setq type 'dedicated
  7647. pos (match-beginning 0))))
  7648. ;; There is an exact target for this
  7649. (goto-char pos))
  7650. ((and (string-match "^(\\(.*\\))$" s0)
  7651. (save-excursion
  7652. (goto-char (point-min))
  7653. (and
  7654. (re-search-forward
  7655. (concat "[^[]" (regexp-quote
  7656. (format org-coderef-label-format
  7657. (match-string 1 s0))))
  7658. nil t)
  7659. (setq type 'dedicated
  7660. pos (1+ (match-beginning 0))))))
  7661. ;; There is a coderef target for this
  7662. (goto-char pos))
  7663. ((string-match "^/\\(.*\\)/$" s)
  7664. ;; A regular expression
  7665. (cond
  7666. ((org-mode-p)
  7667. (org-occur (match-string 1 s)))
  7668. ;;((eq major-mode 'dired-mode)
  7669. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7670. (t (org-do-occur (match-string 1 s)))))
  7671. (t
  7672. ;; A normal search strings
  7673. (when (equal (string-to-char s) ?*)
  7674. ;; Anchor on headlines, post may include tags.
  7675. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7676. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7677. s (substring s 1)))
  7678. (remove-text-properties
  7679. 0 (length s)
  7680. '(face nil mouse-face nil keymap nil fontified nil) s)
  7681. ;; Make a series of regular expressions to find a match
  7682. (setq words (org-split-string s "[ \n\r\t]+")
  7683. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7684. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7685. "\\)" markers)
  7686. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7687. re2a (concat "[ \t\r\n]" re2a_)
  7688. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7689. re4 (concat "[^a-zA-Z_]" re4_)
  7690. re1 (concat pre re2 post)
  7691. re3 (concat pre (if pre re4_ re4) post)
  7692. re5 (concat pre ".*" re4)
  7693. re2 (concat pre re2)
  7694. re2a (concat pre (if pre re2a_ re2a))
  7695. re4 (concat pre (if pre re4_ re4))
  7696. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7697. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7698. re5 "\\)"
  7699. ))
  7700. (cond
  7701. ((eq type 'org-occur) (org-occur reall))
  7702. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7703. (t (goto-char (point-min))
  7704. (setq type 'fuzzy)
  7705. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7706. (org-search-not-self 1 re1 nil t)
  7707. (org-search-not-self 1 re2 nil t)
  7708. (org-search-not-self 1 re2a nil t)
  7709. (org-search-not-self 1 re3 nil t)
  7710. (org-search-not-self 1 re4 nil t)
  7711. (org-search-not-self 1 re5 nil t)
  7712. )
  7713. (goto-char (match-beginning 1))
  7714. (goto-char pos)
  7715. (error "No match")))))
  7716. (t
  7717. ;; Normal string-search
  7718. (goto-char (point-min))
  7719. (if (search-forward s nil t)
  7720. (goto-char (match-beginning 0))
  7721. (error "No match"))))
  7722. (and (org-mode-p) (org-show-context 'link-search))
  7723. type))
  7724. (defun org-search-not-self (group &rest args)
  7725. "Execute `re-search-forward', but only accept matches that do not
  7726. enclose the position of `org-open-link-marker'."
  7727. (let ((m org-open-link-marker))
  7728. (catch 'exit
  7729. (while (apply 're-search-forward args)
  7730. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7731. (goto-char (match-end group))
  7732. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7733. (> (match-beginning 0) (marker-position m))
  7734. (< (match-end 0) (marker-position m)))
  7735. (save-match-data
  7736. (or (not (org-in-regexp
  7737. org-bracket-link-analytic-regexp 1))
  7738. (not (match-end 4)) ; no description
  7739. (and (<= (match-beginning 4) (point))
  7740. (>= (match-end 4) (point))))))
  7741. (throw 'exit (point))))))))
  7742. (defun org-get-buffer-for-internal-link (buffer)
  7743. "Return a buffer to be used for displaying the link target of internal links."
  7744. (cond
  7745. ((not org-display-internal-link-with-indirect-buffer)
  7746. buffer)
  7747. ((string-match "(Clone)$" (buffer-name buffer))
  7748. (message "Buffer is already a clone, not making another one")
  7749. ;; we also do not modify visibility in this case
  7750. buffer)
  7751. (t ; make a new indirect buffer for displaying the link
  7752. (let* ((bn (buffer-name buffer))
  7753. (ibn (concat bn "(Clone)"))
  7754. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7755. (with-current-buffer ib (org-overview))
  7756. ib))))
  7757. (defun org-do-occur (regexp &optional cleanup)
  7758. "Call the Emacs command `occur'.
  7759. If CLEANUP is non-nil, remove the printout of the regular expression
  7760. in the *Occur* buffer. This is useful if the regex is long and not useful
  7761. to read."
  7762. (occur regexp)
  7763. (when cleanup
  7764. (let ((cwin (selected-window)) win beg end)
  7765. (when (setq win (get-buffer-window "*Occur*"))
  7766. (select-window win))
  7767. (goto-char (point-min))
  7768. (when (re-search-forward "match[a-z]+" nil t)
  7769. (setq beg (match-end 0))
  7770. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7771. (setq end (1- (match-beginning 0)))))
  7772. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7773. (goto-char (point-min))
  7774. (select-window cwin))))
  7775. ;;; The mark ring for links jumps
  7776. (defvar org-mark-ring nil
  7777. "Mark ring for positions before jumps in Org-mode.")
  7778. (defvar org-mark-ring-last-goto nil
  7779. "Last position in the mark ring used to go back.")
  7780. ;; Fill and close the ring
  7781. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7782. (loop for i from 1 to org-mark-ring-length do
  7783. (push (make-marker) org-mark-ring))
  7784. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7785. org-mark-ring)
  7786. (defun org-mark-ring-push (&optional pos buffer)
  7787. "Put the current position or POS into the mark ring and rotate it."
  7788. (interactive)
  7789. (setq pos (or pos (point)))
  7790. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7791. (move-marker (car org-mark-ring)
  7792. (or pos (point))
  7793. (or buffer (current-buffer)))
  7794. (message "%s"
  7795. (substitute-command-keys
  7796. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7797. (defun org-mark-ring-goto (&optional n)
  7798. "Jump to the previous position in the mark ring.
  7799. With prefix arg N, jump back that many stored positions. When
  7800. called several times in succession, walk through the entire ring.
  7801. Org-mode commands jumping to a different position in the current file,
  7802. or to another Org-mode file, automatically push the old position
  7803. onto the ring."
  7804. (interactive "p")
  7805. (let (p m)
  7806. (if (eq last-command this-command)
  7807. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7808. (setq p org-mark-ring))
  7809. (setq org-mark-ring-last-goto p)
  7810. (setq m (car p))
  7811. (switch-to-buffer (marker-buffer m))
  7812. (goto-char m)
  7813. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7814. (defun org-remove-angle-brackets (s)
  7815. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7816. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7817. s)
  7818. (defun org-add-angle-brackets (s)
  7819. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7820. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7821. s)
  7822. (defun org-remove-double-quotes (s)
  7823. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7824. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7825. s)
  7826. ;;; Following specific links
  7827. (defun org-follow-timestamp-link ()
  7828. (cond
  7829. ((org-at-date-range-p t)
  7830. (let ((org-agenda-start-on-weekday)
  7831. (t1 (match-string 1))
  7832. (t2 (match-string 2)))
  7833. (setq t1 (time-to-days (org-time-string-to-time t1))
  7834. t2 (time-to-days (org-time-string-to-time t2)))
  7835. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7836. ((org-at-timestamp-p t)
  7837. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7838. (substring (match-string 1) 0 10)))
  7839. 1))
  7840. (t (error "This should not happen"))))
  7841. ;;; Following file links
  7842. (defvar org-wait nil)
  7843. (defun org-open-file (path &optional in-emacs line search)
  7844. "Open the file at PATH.
  7845. First, this expands any special file name abbreviations. Then the
  7846. configuration variable `org-file-apps' is checked if it contains an
  7847. entry for this file type, and if yes, the corresponding command is launched.
  7848. If no application is found, Emacs simply visits the file.
  7849. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7850. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7851. and o use an external application to visit the file.
  7852. Optional LINE specifies a line to go to, optional SEARCH a string to
  7853. search for. If LINE or SEARCH is given, the file will always be
  7854. opened in Emacs.
  7855. If the file does not exist, an error is thrown."
  7856. (setq in-emacs (or in-emacs line search))
  7857. (let* ((file (if (equal path "")
  7858. buffer-file-name
  7859. (substitute-in-file-name (expand-file-name path))))
  7860. (apps (append org-file-apps (org-default-apps)))
  7861. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7862. (dirp (if remp nil (file-directory-p file)))
  7863. (file (if (and dirp org-open-directory-means-index-dot-org)
  7864. (concat (file-name-as-directory file) "index.org")
  7865. file))
  7866. (a-m-a-p (assq 'auto-mode apps))
  7867. (dfile (downcase file))
  7868. (old-buffer (current-buffer))
  7869. (old-pos (point))
  7870. (old-mode major-mode)
  7871. ext cmd)
  7872. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7873. (setq ext (match-string 1 dfile))
  7874. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7875. (setq ext (match-string 1 dfile))))
  7876. (cond
  7877. ((equal in-emacs '(16))
  7878. (setq cmd (cdr (assoc 'system apps))))
  7879. (in-emacs (setq cmd 'emacs))
  7880. (t
  7881. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7882. (and dirp (cdr (assoc 'directory apps)))
  7883. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7884. 'string-match)
  7885. (cdr (assoc ext apps))
  7886. (cdr (assoc t apps))))))
  7887. (when (eq cmd 'system)
  7888. (setq cmd (cdr (assoc 'system apps))))
  7889. (when (eq cmd 'default)
  7890. (setq cmd (cdr (assoc t apps))))
  7891. (when (eq cmd 'mailcap)
  7892. (require 'mailcap)
  7893. (mailcap-parse-mailcaps)
  7894. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7895. (command (mailcap-mime-info mime-type)))
  7896. (if (stringp command)
  7897. (setq cmd command)
  7898. (setq cmd 'emacs))))
  7899. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7900. (not (file-exists-p file))
  7901. (not org-open-non-existing-files))
  7902. (error "No such file: %s" file))
  7903. (cond
  7904. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7905. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7906. (while (string-match "['\"]%s['\"]" cmd)
  7907. (setq cmd (replace-match "%s" t t cmd)))
  7908. (while (string-match "%s" cmd)
  7909. (setq cmd (replace-match
  7910. (save-match-data
  7911. (shell-quote-argument
  7912. (convert-standard-filename file)))
  7913. t t cmd)))
  7914. (save-window-excursion
  7915. (start-process-shell-command cmd nil cmd)
  7916. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7917. ))
  7918. ((or (stringp cmd)
  7919. (eq cmd 'emacs))
  7920. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7921. (widen)
  7922. (if line (org-goto-line line)
  7923. (if search (org-link-search search))))
  7924. ((consp cmd)
  7925. (let ((file (convert-standard-filename file)))
  7926. (eval cmd)))
  7927. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7928. (and (org-mode-p) (eq old-mode 'org-mode)
  7929. (or (not (equal old-buffer (current-buffer)))
  7930. (not (equal old-pos (point))))
  7931. (org-mark-ring-push old-pos old-buffer))))
  7932. (defun org-default-apps ()
  7933. "Return the default applications for this operating system."
  7934. (cond
  7935. ((eq system-type 'darwin)
  7936. org-file-apps-defaults-macosx)
  7937. ((eq system-type 'windows-nt)
  7938. org-file-apps-defaults-windowsnt)
  7939. (t org-file-apps-defaults-gnu)))
  7940. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7941. "Convert extensions to regular expressions in the cars of LIST.
  7942. Also, weed out any non-string entries, because the return value is used
  7943. only for regexp matching.
  7944. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7945. point to the symbol `emacs', indicating that the file should
  7946. be opened in Emacs."
  7947. (append
  7948. (delq nil
  7949. (mapcar (lambda (x)
  7950. (if (not (stringp (car x)))
  7951. nil
  7952. (if (string-match "\\W" (car x))
  7953. x
  7954. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7955. list))
  7956. (if add-auto-mode
  7957. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7958. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7959. (defun org-file-remote-p (file)
  7960. "Test whether FILE specifies a location on a remote system.
  7961. Return non-nil if the location is indeed remote.
  7962. For example, the filename \"/user@host:/foo\" specifies a location
  7963. on the system \"/user@host:\"."
  7964. (cond ((fboundp 'file-remote-p)
  7965. (file-remote-p file))
  7966. ((fboundp 'tramp-handle-file-remote-p)
  7967. (tramp-handle-file-remote-p file))
  7968. ((and (boundp 'ange-ftp-name-format)
  7969. (string-match (car ange-ftp-name-format) file))
  7970. t)
  7971. (t nil)))
  7972. ;;;; Refiling
  7973. (defun org-get-org-file ()
  7974. "Read a filename, with default directory `org-directory'."
  7975. (let ((default (or org-default-notes-file remember-data-file)))
  7976. (read-file-name (format "File name [%s]: " default)
  7977. (file-name-as-directory org-directory)
  7978. default)))
  7979. (defun org-notes-order-reversed-p ()
  7980. "Check if the current file should receive notes in reversed order."
  7981. (cond
  7982. ((not org-reverse-note-order) nil)
  7983. ((eq t org-reverse-note-order) t)
  7984. ((not (listp org-reverse-note-order)) nil)
  7985. (t (catch 'exit
  7986. (let ((all org-reverse-note-order)
  7987. entry)
  7988. (while (setq entry (pop all))
  7989. (if (string-match (car entry) buffer-file-name)
  7990. (throw 'exit (cdr entry))))
  7991. nil)))))
  7992. (defvar org-refile-target-table nil
  7993. "The list of refile targets, created by `org-refile'.")
  7994. (defvar org-agenda-new-buffers nil
  7995. "Buffers created to visit agenda files.")
  7996. (defun org-get-refile-targets (&optional default-buffer)
  7997. "Produce a table with refile targets."
  7998. (let ((case-fold-search nil)
  7999. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8000. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8001. targets txt re files f desc descre fast-path-p level pos0)
  8002. (message "Getting targets...")
  8003. (with-current-buffer (or default-buffer (current-buffer))
  8004. (while (setq entry (pop entries))
  8005. (setq files (car entry) desc (cdr entry))
  8006. (setq fast-path-p nil)
  8007. (cond
  8008. ((null files) (setq files (list (current-buffer))))
  8009. ((eq files 'org-agenda-files)
  8010. (setq files (org-agenda-files 'unrestricted)))
  8011. ((and (symbolp files) (fboundp files))
  8012. (setq files (funcall files)))
  8013. ((and (symbolp files) (boundp files))
  8014. (setq files (symbol-value files))))
  8015. (if (stringp files) (setq files (list files)))
  8016. (cond
  8017. ((eq (car desc) :tag)
  8018. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8019. ((eq (car desc) :todo)
  8020. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8021. ((eq (car desc) :regexp)
  8022. (setq descre (cdr desc)))
  8023. ((eq (car desc) :level)
  8024. (setq descre (concat "^\\*\\{" (number-to-string
  8025. (if org-odd-levels-only
  8026. (1- (* 2 (cdr desc)))
  8027. (cdr desc)))
  8028. "\\}[ \t]")))
  8029. ((eq (car desc) :maxlevel)
  8030. (setq fast-path-p t)
  8031. (setq descre (concat "^\\*\\{1," (number-to-string
  8032. (if org-odd-levels-only
  8033. (1- (* 2 (cdr desc)))
  8034. (cdr desc)))
  8035. "\\}[ \t]")))
  8036. (t (error "Bad refiling target description %s" desc)))
  8037. (while (setq f (pop files))
  8038. (with-current-buffer
  8039. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8040. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8041. (setq f (expand-file-name f))
  8042. (if (eq org-refile-use-outline-path 'file)
  8043. (push (list (file-name-nondirectory f) f nil nil) targets))
  8044. (save-excursion
  8045. (save-restriction
  8046. (widen)
  8047. (goto-char (point-min))
  8048. (while (re-search-forward descre nil t)
  8049. (goto-char (setq pos0 (point-at-bol)))
  8050. (catch 'next
  8051. (when org-refile-target-verify-function
  8052. (save-match-data
  8053. (or (funcall org-refile-target-verify-function)
  8054. (throw 'next t))))
  8055. (when (looking-at org-complex-heading-regexp)
  8056. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8057. txt (org-link-display-format (match-string 4))
  8058. re (concat "^" (regexp-quote
  8059. (buffer-substring (match-beginning 1)
  8060. (match-end 4)))))
  8061. (if (match-end 5) (setq re (concat re "[ \t]+"
  8062. (regexp-quote
  8063. (match-string 5)))))
  8064. (setq re (concat re "[ \t]*$"))
  8065. (when org-refile-use-outline-path
  8066. (setq txt (mapconcat 'org-protect-slash
  8067. (append
  8068. (if (eq org-refile-use-outline-path 'file)
  8069. (list (file-name-nondirectory
  8070. (buffer-file-name (buffer-base-buffer))))
  8071. (if (eq org-refile-use-outline-path 'full-file-path)
  8072. (list (buffer-file-name (buffer-base-buffer)))))
  8073. (org-get-outline-path fast-path-p level txt)
  8074. (list txt))
  8075. "/")))
  8076. (push (list txt f re (point)) targets)))
  8077. (when (= (point) pos0)
  8078. ;; verification function has not moved point
  8079. (goto-char (point-at-eol))))))))))
  8080. (message "Getting targets...done")
  8081. (nreverse targets)))
  8082. (defun org-protect-slash (s)
  8083. (while (string-match "/" s)
  8084. (setq s (replace-match "\\" t t s)))
  8085. s)
  8086. (defvar org-olpa (make-vector 20 nil))
  8087. (defun org-get-outline-path (&optional fastp level heading)
  8088. "Return the outline path to the current entry, as a list.
  8089. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8090. routine which makes outline path derivations for an entire file,
  8091. avoiding backtracing."
  8092. (if fastp
  8093. (progn
  8094. (if (> level 19)
  8095. (error "Outline path failure, more than 19 levels."))
  8096. (loop for i from level upto 19 do
  8097. (aset org-olpa i nil))
  8098. (prog1
  8099. (delq nil (append org-olpa nil))
  8100. (aset org-olpa level heading)))
  8101. (let (rtn)
  8102. (save-excursion
  8103. (while (org-up-heading-safe)
  8104. (when (looking-at org-complex-heading-regexp)
  8105. (push (org-match-string-no-properties 4) rtn)))
  8106. rtn))))
  8107. (defun org-format-outline-path (path &optional width prefix)
  8108. "Format the outlie path PATH for display.
  8109. Width is the maximum number of characters that is available.
  8110. Prefix is a prefix to be included in the returned string,
  8111. such as the file name."
  8112. (setq width (or width 79))
  8113. (if prefix (setq width (- width (length prefix))))
  8114. (if (not path)
  8115. (or prefix "")
  8116. (let* ((nsteps (length path))
  8117. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8118. (maxwidth (if (<= total-width width)
  8119. 10000 ;; everything fits
  8120. ;; we need to shorten the level headings
  8121. (/ (- width nsteps) nsteps)))
  8122. (org-odd-levels-only nil)
  8123. (n 0)
  8124. (total (1+ (length prefix))))
  8125. (setq maxwidth (max maxwidth 10))
  8126. (concat prefix
  8127. (mapconcat
  8128. (lambda (h)
  8129. (setq n (1+ n))
  8130. (if (and (= n nsteps) (< maxwidth 10000))
  8131. (setq maxwidth (- total-width total)))
  8132. (if (< (length h) maxwidth)
  8133. (progn (setq total (+ total (length h) 1)) h)
  8134. (setq h (substring h 0 (- maxwidth 2))
  8135. total (+ total maxwidth 1))
  8136. (if (string-match "[ \t]+\\'" h)
  8137. (setq h (substring h 0 (match-beginning 0))))
  8138. (setq h (concat h "..")))
  8139. (org-add-props h nil 'face
  8140. (nth (% (1- n) org-n-level-faces)
  8141. org-level-faces))
  8142. h)
  8143. path "/")))))
  8144. (defun org-display-outline-path (&optional file current)
  8145. "Display the current outline path in the echo area."
  8146. (interactive "P")
  8147. (let ((bfn (buffer-file-name (buffer-base-buffer)))
  8148. (path (and (org-mode-p) (org-get-outline-path))))
  8149. (if current (setq path (append path
  8150. (save-excursion
  8151. (org-back-to-heading t)
  8152. (if (looking-at org-complex-heading-regexp)
  8153. (list (match-string 4)))))))
  8154. (message (org-format-outline-path
  8155. path
  8156. (1- (frame-width))
  8157. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8158. (defvar org-refile-history nil
  8159. "History for refiling operations.")
  8160. (defvar org-after-refile-insert-hook nil
  8161. "Hook run after `org-refile' has inserted its stuff at the new location.
  8162. Note that this is still *before* the stuff will be removed from
  8163. the *old* location.")
  8164. (defun org-refile (&optional goto default-buffer rfloc)
  8165. "Move the entry at point to another heading.
  8166. The list of target headings is compiled using the information in
  8167. `org-refile-targets', which see. This list is created before each use
  8168. and will therefore always be up-to-date.
  8169. At the target location, the entry is filed as a subitem of the target heading.
  8170. Depending on `org-reverse-note-order', the new subitem will either be the
  8171. first or the last subitem.
  8172. If there is an active region, all entries in that region will be moved.
  8173. However, the region must fulfil the requirement that the first heading
  8174. is the first one sets the top-level of the moved text - at most siblings
  8175. below it are allowed.
  8176. With prefix arg GOTO, the command will only visit the target location,
  8177. not actually move anything.
  8178. With a double prefix `C-u C-u', go to the location where the last refiling
  8179. operation has put the subtree.
  8180. With a prefix argument of `2', refile to the running clock.
  8181. RFLOC can be a refile location obtained in a different way.
  8182. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8183. (interactive "P")
  8184. (let* ((cbuf (current-buffer))
  8185. (regionp (org-region-active-p))
  8186. (region-start (and regionp (region-beginning)))
  8187. (region-end (and regionp (region-end)))
  8188. (region-length (and regionp (- region-end region-start)))
  8189. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8190. pos it nbuf file re level reversed)
  8191. (setq last-command nil)
  8192. (when regionp
  8193. (goto-char region-start)
  8194. (or (bolp) (goto-char (point-at-bol)))
  8195. (setq region-start (point))
  8196. (unless (org-kill-is-subtree-p
  8197. (buffer-substring region-start region-end))
  8198. (error "The region is not a (sequence of) subtree(s)")))
  8199. (if (equal goto '(16))
  8200. (org-refile-goto-last-stored)
  8201. (when (or
  8202. (and (equal goto 2)
  8203. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8204. (prog1
  8205. (setq it (list (or org-clock-heading "running clock")
  8206. (buffer-file-name
  8207. (marker-buffer org-clock-hd-marker))
  8208. ""
  8209. (marker-position org-clock-hd-marker)))
  8210. (setq goto nil)))
  8211. (setq it (or rfloc
  8212. (save-excursion
  8213. (org-refile-get-location
  8214. (if goto "Goto: " "Refile to: ") default-buffer
  8215. org-refile-allow-creating-parent-nodes)))))
  8216. (setq file (nth 1 it)
  8217. re (nth 2 it)
  8218. pos (nth 3 it))
  8219. (if (and (not goto)
  8220. pos
  8221. (equal (buffer-file-name) file)
  8222. (if regionp
  8223. (and (>= pos region-start)
  8224. (<= pos region-end))
  8225. (and (>= pos (point))
  8226. (< pos (save-excursion
  8227. (org-end-of-subtree t t))))))
  8228. (error "Cannot refile to position inside the tree or region"))
  8229. (setq nbuf (or (find-buffer-visiting file)
  8230. (find-file-noselect file)))
  8231. (if goto
  8232. (progn
  8233. (switch-to-buffer nbuf)
  8234. (goto-char pos)
  8235. (org-show-context 'org-goto))
  8236. (if regionp
  8237. (progn
  8238. (org-kill-new (buffer-substring region-start region-end))
  8239. (org-save-markers-in-region region-start region-end))
  8240. (org-copy-subtree 1 nil t))
  8241. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8242. (find-file-noselect file)))
  8243. (setq reversed (org-notes-order-reversed-p))
  8244. (save-excursion
  8245. (save-restriction
  8246. (widen)
  8247. (if pos
  8248. (progn
  8249. (goto-char pos)
  8250. (looking-at outline-regexp)
  8251. (setq level (org-get-valid-level (funcall outline-level) 1))
  8252. (goto-char
  8253. (if reversed
  8254. (or (outline-next-heading) (point-max))
  8255. (or (save-excursion (org-get-next-sibling))
  8256. (org-end-of-subtree t t)
  8257. (point-max)))))
  8258. (setq level 1)
  8259. (if (not reversed)
  8260. (goto-char (point-max))
  8261. (goto-char (point-min))
  8262. (or (outline-next-heading) (goto-char (point-max)))))
  8263. (if (not (bolp)) (newline))
  8264. (bookmark-set "org-refile-last-stored")
  8265. (org-paste-subtree level)
  8266. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8267. (run-hooks 'org-after-refile-insert-hook))))
  8268. (if regionp
  8269. (delete-region (point) (+ (point) region-length))
  8270. (org-cut-subtree))
  8271. (when (featurep 'org-inlinetask)
  8272. (org-inlinetask-remove-END-maybe))
  8273. (setq org-markers-to-move nil)
  8274. (message "Refiled to \"%s\"" (car it))))))
  8275. (org-reveal))
  8276. (defun org-refile-goto-last-stored ()
  8277. "Go to the location where the last refile was stored."
  8278. (interactive)
  8279. (bookmark-jump "org-refile-last-stored")
  8280. (message "This is the location of the last refile"))
  8281. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8282. "Prompt the user for a refile location, using PROMPT."
  8283. (let ((org-refile-targets org-refile-targets)
  8284. (org-refile-use-outline-path org-refile-use-outline-path))
  8285. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8286. (unless org-refile-target-table
  8287. (error "No refile targets"))
  8288. (let* ((cbuf (current-buffer))
  8289. (partial-completion-mode nil)
  8290. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8291. (cfunc (if (and org-refile-use-outline-path
  8292. org-outline-path-complete-in-steps)
  8293. 'org-olpath-completing-read
  8294. 'org-icompleting-read))
  8295. (extra (if org-refile-use-outline-path "/" ""))
  8296. (filename (and cfn (expand-file-name cfn)))
  8297. (tbl (mapcar
  8298. (lambda (x)
  8299. (if (and (not (member org-refile-use-outline-path
  8300. '(file full-file-path)))
  8301. (not (equal filename (nth 1 x))))
  8302. (cons (concat (car x) extra " ("
  8303. (file-name-nondirectory (nth 1 x)) ")")
  8304. (cdr x))
  8305. (cons (concat (car x) extra) (cdr x))))
  8306. org-refile-target-table))
  8307. (completion-ignore-case t)
  8308. pa answ parent-target child parent old-hist)
  8309. (setq old-hist org-refile-history)
  8310. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8311. nil 'org-refile-history))
  8312. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8313. (if pa
  8314. (progn
  8315. (when (or (not org-refile-history)
  8316. (not (eq old-hist org-refile-history))
  8317. (not (equal (car pa) (car org-refile-history))))
  8318. (setq org-refile-history
  8319. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8320. org-refile-history
  8321. (cdr org-refile-history))))
  8322. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8323. (pop org-refile-history)))
  8324. pa)
  8325. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8326. (setq parent (match-string 1 answ)
  8327. child (match-string 2 answ))
  8328. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8329. (when (and parent-target
  8330. (or (eq new-nodes t)
  8331. (and (eq new-nodes 'confirm)
  8332. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8333. (org-refile-new-child parent-target child))))))
  8334. (defun org-refile-new-child (parent-target child)
  8335. "Use refile target PARENT-TARGET to add new CHILD below it."
  8336. (unless parent-target
  8337. (error "Cannot find parent for new node"))
  8338. (let ((file (nth 1 parent-target))
  8339. (pos (nth 3 parent-target))
  8340. level)
  8341. (with-current-buffer (or (find-buffer-visiting file)
  8342. (find-file-noselect file))
  8343. (save-excursion
  8344. (save-restriction
  8345. (widen)
  8346. (if pos
  8347. (goto-char pos)
  8348. (goto-char (point-max))
  8349. (if (not (bolp)) (newline)))
  8350. (when (looking-at outline-regexp)
  8351. (setq level (funcall outline-level))
  8352. (org-end-of-subtree t t))
  8353. (org-back-over-empty-lines)
  8354. (insert "\n" (make-string
  8355. (if pos (org-get-valid-level level 1) 1) ?*)
  8356. " " child "\n")
  8357. (beginning-of-line 0)
  8358. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8359. (defun org-olpath-completing-read (prompt collection &rest args)
  8360. "Read an outline path like a file name."
  8361. (let ((thetable collection)
  8362. (org-completion-use-ido nil) ; does not work with ido.
  8363. (org-completion-use-iswitchb nil)) ; or iswitchb
  8364. (apply
  8365. 'org-icompleting-read prompt
  8366. (lambda (string predicate &optional flag)
  8367. (let (rtn r f (l (length string)))
  8368. (cond
  8369. ((eq flag nil)
  8370. ;; try completion
  8371. (try-completion string thetable))
  8372. ((eq flag t)
  8373. ;; all-completions
  8374. (setq rtn (all-completions string thetable predicate))
  8375. (mapcar
  8376. (lambda (x)
  8377. (setq r (substring x l))
  8378. (if (string-match " ([^)]*)$" x)
  8379. (setq f (match-string 0 x))
  8380. (setq f ""))
  8381. (if (string-match "/" r)
  8382. (concat string (substring r 0 (match-end 0)) f)
  8383. x))
  8384. rtn))
  8385. ((eq flag 'lambda)
  8386. ;; exact match?
  8387. (assoc string thetable)))
  8388. ))
  8389. args)))
  8390. ;;;; Dynamic blocks
  8391. (defun org-find-dblock (name)
  8392. "Find the first dynamic block with name NAME in the buffer.
  8393. If not found, stay at current position and return nil."
  8394. (let (pos)
  8395. (save-excursion
  8396. (goto-char (point-min))
  8397. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8398. nil t)
  8399. (match-beginning 0))))
  8400. (if pos (goto-char pos))
  8401. pos))
  8402. (defconst org-dblock-start-re
  8403. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8404. "Matches the start line of a dynamic block, with parameters.")
  8405. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8406. "Matches the end of a dynamic block.")
  8407. (defun org-create-dblock (plist)
  8408. "Create a dynamic block section, with parameters taken from PLIST.
  8409. PLIST must contain a :name entry which is used as name of the block."
  8410. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8411. (end-of-line 1)
  8412. (newline))
  8413. (let ((col (current-column))
  8414. (name (plist-get plist :name)))
  8415. (insert "#+BEGIN: " name)
  8416. (while plist
  8417. (if (eq (car plist) :name)
  8418. (setq plist (cddr plist))
  8419. (insert " " (prin1-to-string (pop plist)))))
  8420. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8421. (beginning-of-line -2)))
  8422. (defun org-prepare-dblock ()
  8423. "Prepare dynamic block for refresh.
  8424. This empties the block, puts the cursor at the insert position and returns
  8425. the property list including an extra property :name with the block name."
  8426. (unless (looking-at org-dblock-start-re)
  8427. (error "Not at a dynamic block"))
  8428. (let* ((begdel (1+ (match-end 0)))
  8429. (name (org-no-properties (match-string 1)))
  8430. (params (append (list :name name)
  8431. (read (concat "(" (match-string 3) ")")))))
  8432. (save-excursion
  8433. (beginning-of-line 1)
  8434. (skip-chars-forward " \t")
  8435. (setq params (plist-put params :indentation-column (current-column))))
  8436. (unless (re-search-forward org-dblock-end-re nil t)
  8437. (error "Dynamic block not terminated"))
  8438. (setq params
  8439. (append params
  8440. (list :content (buffer-substring
  8441. begdel (match-beginning 0)))))
  8442. (delete-region begdel (match-beginning 0))
  8443. (goto-char begdel)
  8444. (open-line 1)
  8445. params))
  8446. (defun org-map-dblocks (&optional command)
  8447. "Apply COMMAND to all dynamic blocks in the current buffer.
  8448. If COMMAND is not given, use `org-update-dblock'."
  8449. (let ((cmd (or command 'org-update-dblock))
  8450. pos)
  8451. (save-excursion
  8452. (goto-char (point-min))
  8453. (while (re-search-forward org-dblock-start-re nil t)
  8454. (goto-char (setq pos (match-beginning 0)))
  8455. (condition-case nil
  8456. (funcall cmd)
  8457. (error (message "Error during update of dynamic block")))
  8458. (goto-char pos)
  8459. (unless (re-search-forward org-dblock-end-re nil t)
  8460. (error "Dynamic block not terminated"))))))
  8461. (defun org-dblock-update (&optional arg)
  8462. "User command for updating dynamic blocks.
  8463. Update the dynamic block at point. With prefix ARG, update all dynamic
  8464. blocks in the buffer."
  8465. (interactive "P")
  8466. (if arg
  8467. (org-update-all-dblocks)
  8468. (or (looking-at org-dblock-start-re)
  8469. (org-beginning-of-dblock))
  8470. (org-update-dblock)))
  8471. (defun org-update-dblock ()
  8472. "Update the dynamic block at point
  8473. This means to empty the block, parse for parameters and then call
  8474. the correct writing function."
  8475. (save-window-excursion
  8476. (let* ((pos (point))
  8477. (line (org-current-line))
  8478. (params (org-prepare-dblock))
  8479. (name (plist-get params :name))
  8480. (indent (plist-get params :indentation-column))
  8481. (cmd (intern (concat "org-dblock-write:" name))))
  8482. (message "Updating dynamic block `%s' at line %d..." name line)
  8483. (funcall cmd params)
  8484. (message "Updating dynamic block `%s' at line %d...done" name line)
  8485. (goto-char pos)
  8486. (when (and indent (> indent 0))
  8487. (setq indent (make-string indent ?\ ))
  8488. (save-excursion
  8489. (org-beginning-of-dblock)
  8490. (forward-line 1)
  8491. (while (not (looking-at org-dblock-end-re))
  8492. (insert indent)
  8493. (beginning-of-line 2))
  8494. (when (looking-at org-dblock-end-re)
  8495. (and (looking-at "[ \t]+")
  8496. (replace-match ""))
  8497. (insert indent)))))))
  8498. (defun org-beginning-of-dblock ()
  8499. "Find the beginning of the dynamic block at point.
  8500. Error if there is no such block at point."
  8501. (let ((pos (point))
  8502. beg)
  8503. (end-of-line 1)
  8504. (if (and (re-search-backward org-dblock-start-re nil t)
  8505. (setq beg (match-beginning 0))
  8506. (re-search-forward org-dblock-end-re nil t)
  8507. (> (match-end 0) pos))
  8508. (goto-char beg)
  8509. (goto-char pos)
  8510. (error "Not in a dynamic block"))))
  8511. (defun org-update-all-dblocks ()
  8512. "Update all dynamic blocks in the buffer.
  8513. This function can be used in a hook."
  8514. (when (org-mode-p)
  8515. (org-map-dblocks 'org-update-dblock)))
  8516. ;;;; Completion
  8517. (defconst org-additional-option-like-keywords
  8518. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8519. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8520. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
  8521. "BEGIN:" "END:"
  8522. "ORGTBL" "TBLFM:" "TBLNAME:"
  8523. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8524. "BEGIN_QUOTE" "END_QUOTE"
  8525. "BEGIN_VERSE" "END_VERSE"
  8526. "BEGIN_CENTER" "END_CENTER"
  8527. "BEGIN_SRC" "END_SRC"
  8528. "CATEGORY" "COLUMNS"
  8529. "CAPTION" "LABEL"
  8530. "SETUPFILE"
  8531. "BIND"
  8532. "MACRO"))
  8533. (defcustom org-structure-template-alist
  8534. '(
  8535. ("s" "#+begin_src ?\n\n#+end_src"
  8536. "<src lang=\"?\">\n\n</src>")
  8537. ("e" "#+begin_example\n?\n#+end_example"
  8538. "<example>\n?\n</example>")
  8539. ("q" "#+begin_quote\n?\n#+end_quote"
  8540. "<quote>\n?\n</quote>")
  8541. ("v" "#+begin_verse\n?\n#+end_verse"
  8542. "<verse>\n?\n/verse>")
  8543. ("c" "#+begin_center\n?\n#+end_center"
  8544. "<center>\n?\n/center>")
  8545. ("l" "#+begin_latex\n?\n#+end_latex"
  8546. "<literal style=\"latex\">\n?\n</literal>")
  8547. ("L" "#+latex: "
  8548. "<literal style=\"latex\">?</literal>")
  8549. ("h" "#+begin_html\n?\n#+end_html"
  8550. "<literal style=\"html\">\n?\n</literal>")
  8551. ("H" "#+html: "
  8552. "<literal style=\"html\">?</literal>")
  8553. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8554. ("A" "#+ascii: ")
  8555. ("i" "#+include %file ?"
  8556. "<include file=%file markup=\"?\">")
  8557. )
  8558. "Structure completion elements.
  8559. This is a list of abbreviation keys and values. The value gets inserted
  8560. it you type @samp{.} followed by the key and then the completion key,
  8561. usually `M-TAB'. %file will be replaced by a file name after prompting
  8562. for the file using completion.
  8563. There are two templates for each key, the first uses the original Org syntax,
  8564. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8565. the default when the /org-mtags.el/ module has been loaded. See also the
  8566. variable `org-mtags-prefer-muse-templates'.
  8567. This is an experimental feature, it is undecided if it is going to stay in."
  8568. :group 'org-completion
  8569. :type '(repeat
  8570. (string :tag "Key")
  8571. (string :tag "Template")
  8572. (string :tag "Muse Template")))
  8573. (defun org-try-structure-completion ()
  8574. "Try to complete a structure template before point.
  8575. This looks for strings like \"<e\" on an otherwise empty line and
  8576. expands them."
  8577. (let ((l (buffer-substring (point-at-bol) (point)))
  8578. a)
  8579. (when (and (looking-at "[ \t]*$")
  8580. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8581. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8582. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8583. (match-beginning 1)) a)
  8584. t)))
  8585. (defun org-complete-expand-structure-template (start cell)
  8586. "Expand a structure template."
  8587. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8588. (rpl (nth (if musep 2 1) cell))
  8589. (ind ""))
  8590. (delete-region start (point))
  8591. (when (string-match "\\`#\\+" rpl)
  8592. (cond
  8593. ((bolp))
  8594. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8595. (setq ind (buffer-substring (point-at-bol) (point))))
  8596. (t (newline))))
  8597. (setq start (point))
  8598. (if (string-match "%file" rpl)
  8599. (setq rpl (replace-match
  8600. (concat
  8601. "\""
  8602. (save-match-data
  8603. (abbreviate-file-name (read-file-name "Include file: ")))
  8604. "\"")
  8605. t t rpl)))
  8606. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8607. (concat "\n" ind)))
  8608. (insert rpl)
  8609. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8610. (defun org-complete (&optional arg)
  8611. "Perform completion on word at point.
  8612. At the beginning of a headline, this completes TODO keywords as given in
  8613. `org-todo-keywords'.
  8614. If the current word is preceded by a backslash, completes the TeX symbols
  8615. that are supported for HTML support.
  8616. If the current word is preceded by \"#+\", completes special words for
  8617. setting file options.
  8618. In the line after \"#+STARTUP:, complete valid keywords.\"
  8619. At all other locations, this simply calls the value of
  8620. `org-completion-fallback-command'."
  8621. (interactive "P")
  8622. (org-without-partial-completion
  8623. (catch 'exit
  8624. (let* ((a nil)
  8625. (end (point))
  8626. (beg1 (save-excursion
  8627. (skip-chars-backward (org-re "[:alnum:]_@"))
  8628. (point)))
  8629. (beg (save-excursion
  8630. (skip-chars-backward "a-zA-Z0-9_:$")
  8631. (point)))
  8632. (confirm (lambda (x) (stringp (car x))))
  8633. (searchhead (equal (char-before beg) ?*))
  8634. (struct
  8635. (when (and (member (char-before beg1) '(?. ?<))
  8636. (setq a (assoc (buffer-substring beg1 (point))
  8637. org-structure-template-alist)))
  8638. (org-complete-expand-structure-template (1- beg1) a)
  8639. (throw 'exit t)))
  8640. (tag (and (equal (char-before beg1) ?:)
  8641. (equal (char-after (point-at-bol)) ?*)))
  8642. (prop (and (equal (char-before beg1) ?:)
  8643. (not (equal (char-after (point-at-bol)) ?*))))
  8644. (texp (equal (char-before beg) ?\\))
  8645. (link (equal (char-before beg) ?\[))
  8646. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8647. beg)
  8648. "#+"))
  8649. (startup (string-match "^#\\+STARTUP:.*"
  8650. (buffer-substring (point-at-bol) (point))))
  8651. (completion-ignore-case opt)
  8652. (type nil)
  8653. (tbl nil)
  8654. (table (cond
  8655. (opt
  8656. (setq type :opt)
  8657. (require 'org-exp)
  8658. (append
  8659. (delq nil
  8660. (mapcar
  8661. (lambda (x)
  8662. (if (string-match
  8663. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8664. (cons (match-string 2 x)
  8665. (match-string 1 x))))
  8666. (org-split-string (org-get-current-options) "\n")))
  8667. (mapcar 'list org-additional-option-like-keywords)))
  8668. (startup
  8669. (setq type :startup)
  8670. org-startup-options)
  8671. (link (append org-link-abbrev-alist-local
  8672. org-link-abbrev-alist))
  8673. (texp
  8674. (setq type :tex)
  8675. org-html-entities)
  8676. ((string-match "\\`\\*+[ \t]+\\'"
  8677. (buffer-substring (point-at-bol) beg))
  8678. (setq type :todo)
  8679. (mapcar 'list org-todo-keywords-1))
  8680. (searchhead
  8681. (setq type :searchhead)
  8682. (save-excursion
  8683. (goto-char (point-min))
  8684. (while (re-search-forward org-todo-line-regexp nil t)
  8685. (push (list
  8686. (org-make-org-heading-search-string
  8687. (match-string 3) t))
  8688. tbl)))
  8689. tbl)
  8690. (tag (setq type :tag beg beg1)
  8691. (or org-tag-alist (org-get-buffer-tags)))
  8692. (prop (setq type :prop beg beg1)
  8693. (mapcar 'list (org-buffer-property-keys nil t t)))
  8694. (t (progn
  8695. (call-interactively org-completion-fallback-command)
  8696. (throw 'exit nil)))))
  8697. (pattern (buffer-substring-no-properties beg end))
  8698. (completion (try-completion pattern table confirm)))
  8699. (cond ((eq completion t)
  8700. (if (not (assoc (upcase pattern) table))
  8701. (message "Already complete")
  8702. (if (and (equal type :opt)
  8703. (not (member (car (assoc (upcase pattern) table))
  8704. org-additional-option-like-keywords)))
  8705. (insert (substring (cdr (assoc (upcase pattern) table))
  8706. (length pattern)))
  8707. (if (memq type '(:tag :prop)) (insert ":")))))
  8708. ((null completion)
  8709. (message "Can't find completion for \"%s\"" pattern)
  8710. (ding))
  8711. ((not (string= pattern completion))
  8712. (delete-region beg end)
  8713. (if (string-match " +$" completion)
  8714. (setq completion (replace-match "" t t completion)))
  8715. (insert completion)
  8716. (if (get-buffer-window "*Completions*")
  8717. (delete-window (get-buffer-window "*Completions*")))
  8718. (if (assoc completion table)
  8719. (if (eq type :todo) (insert " ")
  8720. (if (memq type '(:tag :prop)) (insert ":"))))
  8721. (if (and (equal type :opt) (assoc completion table))
  8722. (message "%s" (substitute-command-keys
  8723. "Press \\[org-complete] again to insert example settings"))))
  8724. (t
  8725. (message "Making completion list...")
  8726. (let ((list (sort (all-completions pattern table confirm)
  8727. 'string<)))
  8728. (with-output-to-temp-buffer "*Completions*"
  8729. (condition-case nil
  8730. ;; Protection needed for XEmacs and emacs 21
  8731. (display-completion-list list pattern)
  8732. (error (display-completion-list list)))))
  8733. (message "Making completion list...%s" "done")))))))
  8734. ;;;; TODO, DEADLINE, Comments
  8735. (defun org-toggle-comment ()
  8736. "Change the COMMENT state of an entry."
  8737. (interactive)
  8738. (save-excursion
  8739. (org-back-to-heading)
  8740. (let (case-fold-search)
  8741. (if (looking-at (concat outline-regexp
  8742. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8743. (replace-match "" t t nil 1)
  8744. (if (looking-at outline-regexp)
  8745. (progn
  8746. (goto-char (match-end 0))
  8747. (insert org-comment-string " ")))))))
  8748. (defvar org-last-todo-state-is-todo nil
  8749. "This is non-nil when the last TODO state change led to a TODO state.
  8750. If the last change removed the TODO tag or switched to DONE, then
  8751. this is nil.")
  8752. (defvar org-setting-tags nil) ; dynamically skipped
  8753. (defun org-parse-local-options (string var)
  8754. "Parse STRING for startup setting relevant for variable VAR."
  8755. (let ((rtn (symbol-value var))
  8756. e opts)
  8757. (save-match-data
  8758. (if (or (not string) (not (string-match "\\S-" string)))
  8759. rtn
  8760. (setq opts (delq nil (mapcar (lambda (x)
  8761. (setq e (assoc x org-startup-options))
  8762. (if (eq (nth 1 e) var) e nil))
  8763. (org-split-string string "[ \t]+"))))
  8764. (if (not opts)
  8765. rtn
  8766. (setq rtn nil)
  8767. (while (setq e (pop opts))
  8768. (if (not (nth 3 e))
  8769. (setq rtn (nth 2 e))
  8770. (if (not (listp rtn)) (setq rtn nil))
  8771. (push (nth 2 e) rtn)))
  8772. rtn)))))
  8773. (defvar org-todo-setup-filter-hook nil
  8774. "Hook for functions that pre-filter todo specs.
  8775. Each function takes a todo spec and returns either `nil' or the spec
  8776. transformed into canonical form." )
  8777. (defvar org-todo-get-default-hook nil
  8778. "Hook for functions that get a default item for todo.
  8779. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8780. `nil' or a string to be used for the todo mark." )
  8781. (defvar org-agenda-headline-snapshot-before-repeat)
  8782. (defun org-todo (&optional arg)
  8783. "Change the TODO state of an item.
  8784. The state of an item is given by a keyword at the start of the heading,
  8785. like
  8786. *** TODO Write paper
  8787. *** DONE Call mom
  8788. The different keywords are specified in the variable `org-todo-keywords'.
  8789. By default the available states are \"TODO\" and \"DONE\".
  8790. So for this example: when the item starts with TODO, it is changed to DONE.
  8791. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8792. DONE are present, add TODO at the beginning of the heading.
  8793. With C-u prefix arg, use completion to determine the new state.
  8794. With numeric prefix arg, switch to that state.
  8795. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8796. With a triple C-u prefix, circumvent any state blocking.
  8797. For calling through lisp, arg is also interpreted in the following way:
  8798. 'none -> empty state
  8799. \"\"(empty string) -> switch to empty state
  8800. 'done -> switch to DONE
  8801. 'nextset -> switch to the next set of keywords
  8802. 'previousset -> switch to the previous set of keywords
  8803. \"WAITING\" -> switch to the specified keyword, but only if it
  8804. really is a member of `org-todo-keywords'."
  8805. (interactive "P")
  8806. (if (equal arg '(16)) (setq arg 'nextset))
  8807. (let ((org-blocker-hook org-blocker-hook)
  8808. (case-fold-search nil))
  8809. (when (equal arg '(64))
  8810. (setq arg nil org-blocker-hook nil))
  8811. (when (and org-blocker-hook
  8812. (or org-inhibit-blocking
  8813. (org-entry-get nil "NOBLOCKING")))
  8814. (setq org-blocker-hook nil))
  8815. (save-excursion
  8816. (catch 'exit
  8817. (org-back-to-heading t)
  8818. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8819. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8820. (looking-at " *"))
  8821. (let* ((match-data (match-data))
  8822. (startpos (point-at-bol))
  8823. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8824. (org-log-done org-log-done)
  8825. (org-log-repeat org-log-repeat)
  8826. (org-todo-log-states org-todo-log-states)
  8827. (this (match-string 1))
  8828. (hl-pos (match-beginning 0))
  8829. (head (org-get-todo-sequence-head this))
  8830. (ass (assoc head org-todo-kwd-alist))
  8831. (interpret (nth 1 ass))
  8832. (done-word (nth 3 ass))
  8833. (final-done-word (nth 4 ass))
  8834. (last-state (or this ""))
  8835. (completion-ignore-case t)
  8836. (member (member this org-todo-keywords-1))
  8837. (tail (cdr member))
  8838. (state (cond
  8839. ((and org-todo-key-trigger
  8840. (or (and (equal arg '(4))
  8841. (eq org-use-fast-todo-selection 'prefix))
  8842. (and (not arg) org-use-fast-todo-selection
  8843. (not (eq org-use-fast-todo-selection
  8844. 'prefix)))))
  8845. ;; Use fast selection
  8846. (org-fast-todo-selection))
  8847. ((and (equal arg '(4))
  8848. (or (not org-use-fast-todo-selection)
  8849. (not org-todo-key-trigger)))
  8850. ;; Read a state with completion
  8851. (org-icompleting-read
  8852. "State: " (mapcar (lambda(x) (list x))
  8853. org-todo-keywords-1)
  8854. nil t))
  8855. ((eq arg 'right)
  8856. (if this
  8857. (if tail (car tail) nil)
  8858. (car org-todo-keywords-1)))
  8859. ((eq arg 'left)
  8860. (if (equal member org-todo-keywords-1)
  8861. nil
  8862. (if this
  8863. (nth (- (length org-todo-keywords-1)
  8864. (length tail) 2)
  8865. org-todo-keywords-1)
  8866. (org-last org-todo-keywords-1))))
  8867. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8868. (setq arg nil))) ; hack to fall back to cycling
  8869. (arg
  8870. ;; user or caller requests a specific state
  8871. (cond
  8872. ((equal arg "") nil)
  8873. ((eq arg 'none) nil)
  8874. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8875. ((eq arg 'nextset)
  8876. (or (car (cdr (member head org-todo-heads)))
  8877. (car org-todo-heads)))
  8878. ((eq arg 'previousset)
  8879. (let ((org-todo-heads (reverse org-todo-heads)))
  8880. (or (car (cdr (member head org-todo-heads)))
  8881. (car org-todo-heads))))
  8882. ((car (member arg org-todo-keywords-1)))
  8883. ((stringp arg)
  8884. (error "State `%s' not valid in this file" arg))
  8885. ((nth (1- (prefix-numeric-value arg))
  8886. org-todo-keywords-1))))
  8887. ((null member) (or head (car org-todo-keywords-1)))
  8888. ((equal this final-done-word) nil) ;; -> make empty
  8889. ((null tail) nil) ;; -> first entry
  8890. ((memq interpret '(type priority))
  8891. (if (eq this-command last-command)
  8892. (car tail)
  8893. (if (> (length tail) 0)
  8894. (or done-word (car org-done-keywords))
  8895. nil)))
  8896. (t
  8897. (car tail))))
  8898. (state (or
  8899. (run-hook-with-args-until-success
  8900. 'org-todo-get-default-hook state last-state)
  8901. state))
  8902. (next (if state (concat " " state " ") " "))
  8903. (change-plist (list :type 'todo-state-change :from this :to state
  8904. :position startpos))
  8905. dolog now-done-p)
  8906. (when org-blocker-hook
  8907. (setq org-last-todo-state-is-todo
  8908. (not (member this org-done-keywords)))
  8909. (unless (save-excursion
  8910. (save-match-data
  8911. (run-hook-with-args-until-failure
  8912. 'org-blocker-hook change-plist)))
  8913. (if (interactive-p)
  8914. (error "TODO state change from %s to %s blocked" this state)
  8915. ;; fail silently
  8916. (message "TODO state change from %s to %s blocked" this state)
  8917. (throw 'exit nil))))
  8918. (store-match-data match-data)
  8919. (replace-match next t t)
  8920. (unless (pos-visible-in-window-p hl-pos)
  8921. (message "TODO state changed to %s" (org-trim next)))
  8922. (unless head
  8923. (setq head (org-get-todo-sequence-head state)
  8924. ass (assoc head org-todo-kwd-alist)
  8925. interpret (nth 1 ass)
  8926. done-word (nth 3 ass)
  8927. final-done-word (nth 4 ass)))
  8928. (when (memq arg '(nextset previousset))
  8929. (message "Keyword-Set %d/%d: %s"
  8930. (- (length org-todo-sets) -1
  8931. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8932. (length org-todo-sets)
  8933. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8934. (setq org-last-todo-state-is-todo
  8935. (not (member state org-done-keywords)))
  8936. (setq now-done-p (and (member state org-done-keywords)
  8937. (not (member this org-done-keywords))))
  8938. (and logging (org-local-logging logging))
  8939. (when (and (or org-todo-log-states org-log-done)
  8940. (not (eq org-inhibit-logging t))
  8941. (not (memq arg '(nextset previousset))))
  8942. ;; we need to look at recording a time and note
  8943. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8944. (nth 2 (assoc this org-todo-log-states))))
  8945. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8946. (setq dolog 'time))
  8947. (when (and state
  8948. (member state org-not-done-keywords)
  8949. (not (member this org-not-done-keywords)))
  8950. ;; This is now a todo state and was not one before
  8951. ;; If there was a CLOSED time stamp, get rid of it.
  8952. (org-add-planning-info nil nil 'closed))
  8953. (when (and now-done-p org-log-done)
  8954. ;; It is now done, and it was not done before
  8955. (org-add-planning-info 'closed (org-current-time))
  8956. (if (and (not dolog) (eq 'note org-log-done))
  8957. (org-add-log-setup 'done state this 'findpos 'note)))
  8958. (when (and state dolog)
  8959. ;; This is a non-nil state, and we need to log it
  8960. (org-add-log-setup 'state state this 'findpos dolog)))
  8961. ;; Fixup tag positioning
  8962. (org-todo-trigger-tag-changes state)
  8963. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8964. (when org-provide-todo-statistics
  8965. (org-update-parent-todo-statistics))
  8966. (run-hooks 'org-after-todo-state-change-hook)
  8967. (if (and arg (not (member state org-done-keywords)))
  8968. (setq head (org-get-todo-sequence-head state)))
  8969. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8970. ;; Do we need to trigger a repeat?
  8971. (when now-done-p
  8972. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8973. ;; This is for the agenda, take a snapshot of the headline.
  8974. (save-match-data
  8975. (setq org-agenda-headline-snapshot-before-repeat
  8976. (org-get-heading))))
  8977. (org-auto-repeat-maybe state))
  8978. ;; Fixup cursor location if close to the keyword
  8979. (if (and (outline-on-heading-p)
  8980. (not (bolp))
  8981. (save-excursion (beginning-of-line 1)
  8982. (looking-at org-todo-line-regexp))
  8983. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8984. (progn
  8985. (goto-char (or (match-end 2) (match-end 1)))
  8986. (and (looking-at " ") (just-one-space))))
  8987. (when org-trigger-hook
  8988. (save-excursion
  8989. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8990. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8991. "Block turning an entry into a TODO, using the hierarchy.
  8992. This checks whether the current task should be blocked from state
  8993. changes. Such blocking occurs when:
  8994. 1. The task has children which are not all in a completed state.
  8995. 2. A task has a parent with the property :ORDERED:, and there
  8996. are siblings prior to the current task with incomplete
  8997. status.
  8998. 3. The parent of the task is blocked because it has siblings that should
  8999. be done first, or is child of a block grandparent TODO entry."
  9000. (catch 'dont-block
  9001. ;; If this is not a todo state change, or if this entry is already DONE,
  9002. ;; do not block
  9003. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9004. (member (plist-get change-plist :from)
  9005. (cons 'done org-done-keywords))
  9006. (member (plist-get change-plist :to)
  9007. (cons 'todo org-not-done-keywords))
  9008. (not (plist-get change-plist :to)))
  9009. (throw 'dont-block t))
  9010. ;; If this task has children, and any are undone, it's blocked
  9011. (save-excursion
  9012. (org-back-to-heading t)
  9013. (let ((this-level (funcall outline-level)))
  9014. (outline-next-heading)
  9015. (let ((child-level (funcall outline-level)))
  9016. (while (and (not (eobp))
  9017. (> child-level this-level))
  9018. ;; this todo has children, check whether they are all
  9019. ;; completed
  9020. (if (and (not (org-entry-is-done-p))
  9021. (org-entry-is-todo-p))
  9022. (throw 'dont-block nil))
  9023. (outline-next-heading)
  9024. (setq child-level (funcall outline-level))))))
  9025. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9026. ;; any previous siblings are undone, it's blocked
  9027. (save-excursion
  9028. (org-back-to-heading t)
  9029. (let* ((pos (point))
  9030. (parent-pos (and (org-up-heading-safe) (point))))
  9031. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9032. (when (and (org-entry-get (point) "ORDERED")
  9033. (forward-line 1)
  9034. (re-search-forward org-not-done-heading-regexp pos t))
  9035. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9036. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9037. (while t
  9038. (goto-char parent-pos)
  9039. (if (not (looking-at org-not-done-heading-regexp))
  9040. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9041. (setq pos (point))
  9042. (setq parent-pos (and (org-up-heading-safe) (point)))
  9043. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9044. (when (and (org-entry-get (point) "ORDERED")
  9045. (forward-line 1)
  9046. (re-search-forward org-not-done-heading-regexp pos t))
  9047. (throw 'dont-block nil))))))) ; block, older sibling not done.
  9048. (defcustom org-track-ordered-property-with-tag nil
  9049. "Should the ORDERED property also be shown as a tag?
  9050. The ORDERED property decides if an entry should require subtasks to be
  9051. completed in sequence. Since a property is not very visible, setting
  9052. this option means that toggling the ORDERED property with the command
  9053. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9054. not relevant for the behavior, but it makes things more visible.
  9055. Note that toggling the tag with tags commands will not change the property
  9056. and therefore not influence behavior!
  9057. This can be t, meaning the tag ORDERED should be used, It can also be a
  9058. string to select a different tag for this task."
  9059. :group 'org-todo
  9060. :type '(choice
  9061. (const :tag "No tracking" nil)
  9062. (const :tag "Track with ORDERED tag" t)
  9063. (string :tag "Use other tag")))
  9064. (defun org-toggle-ordered-property ()
  9065. "Toggle the ORDERED property of the current entry.
  9066. For better visibility, you can track the value of this property with a tag.
  9067. See variable `org-track-ordered-property-with-tag'."
  9068. (interactive)
  9069. (let* ((t1 org-track-ordered-property-with-tag)
  9070. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9071. (save-excursion
  9072. (org-back-to-heading)
  9073. (if (org-entry-get nil "ORDERED")
  9074. (progn
  9075. (org-delete-property "ORDERED")
  9076. (and tag (org-toggle-tag tag 'off))
  9077. (message "Subtasks can be completed in arbitrary order"))
  9078. (org-entry-put nil "ORDERED" "t")
  9079. (and tag (org-toggle-tag tag 'on))
  9080. (message "Subtasks must be completed in sequence")))))
  9081. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9082. (defun org-block-todo-from-checkboxes (change-plist)
  9083. "Block turning an entry into a TODO, using checkboxes.
  9084. This checks whether the current task should be blocked from state
  9085. changes because there are unchecked boxes in this entry."
  9086. (catch 'dont-block
  9087. ;; If this is not a todo state change, or if this entry is already DONE,
  9088. ;; do not block
  9089. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9090. (member (plist-get change-plist :from)
  9091. (cons 'done org-done-keywords))
  9092. (member (plist-get change-plist :to)
  9093. (cons 'todo org-not-done-keywords))
  9094. (not (plist-get change-plist :to)))
  9095. (throw 'dont-block t))
  9096. ;; If this task has checkboxes that are not checked, it's blocked
  9097. (save-excursion
  9098. (org-back-to-heading t)
  9099. (let ((beg (point)) end)
  9100. (outline-next-heading)
  9101. (setq end (point))
  9102. (goto-char beg)
  9103. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9104. end t)
  9105. (progn
  9106. (if (boundp 'org-blocked-by-checkboxes)
  9107. (setq org-blocked-by-checkboxes t))
  9108. (throw 'dont-block nil)))))
  9109. t)) ; do not block
  9110. (defun org-update-statistics-cookies (all)
  9111. "Update the statistics cookie, either from TODO or from checkboxes.
  9112. This should be called with the cursor in a line with a statistics cookie."
  9113. (interactive "P")
  9114. (if all
  9115. (progn
  9116. (org-update-checkbox-count 'all)
  9117. (org-map-entries 'org-update-parent-todo-statistics))
  9118. (if (not (org-on-heading-p))
  9119. (org-update-checkbox-count)
  9120. (let ((pos (move-marker (make-marker) (point)))
  9121. end l1 l2)
  9122. (ignore-errors (org-back-to-heading t))
  9123. (if (not (org-on-heading-p))
  9124. (org-update-checkbox-count)
  9125. (setq l1 (org-outline-level))
  9126. (setq end (save-excursion
  9127. (outline-next-heading)
  9128. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9129. (point)))
  9130. (if (and (save-excursion (re-search-forward
  9131. "^[ \t]*[-+*] \\[[- X]\\]" end t))
  9132. (not (save-excursion (re-search-forward
  9133. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9134. (org-update-checkbox-count)
  9135. (if (and l2 (> l2 l1))
  9136. (progn
  9137. (goto-char end)
  9138. (org-update-parent-todo-statistics))
  9139. (error "No data for statistics cookie"))))
  9140. (goto-char pos)
  9141. (move-marker pos nil)))))
  9142. (defvar org-entry-property-inherited-from) ;; defined below
  9143. (defun org-update-parent-todo-statistics ()
  9144. "Update any statistics cookie in the parent of the current headline.
  9145. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9146. the entire subtree and this will travel up the hierarchy and update
  9147. statistics everywhere."
  9148. (interactive)
  9149. (let* ((lim 0) prop
  9150. (recursive (or (not org-hierarchical-todo-statistics)
  9151. (string-match
  9152. "\\<recursive\\>"
  9153. (or (setq prop (org-entry-get
  9154. nil "COOKIE_DATA" 'inherit)) ""))))
  9155. (lim (or (and prop (marker-position
  9156. org-entry-property-inherited-from))
  9157. lim))
  9158. (first t)
  9159. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9160. level ltoggle l1 new ndel
  9161. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9162. (catch 'exit
  9163. (save-excursion
  9164. (beginning-of-line 1)
  9165. (if (org-at-heading-p)
  9166. (setq ltoggle (funcall outline-level))
  9167. (error "This should not happen"))
  9168. (while (and (setq level (org-up-heading-safe))
  9169. (or recursive first)
  9170. (>= (point) lim))
  9171. (setq first nil cookie-present nil)
  9172. (unless (and level
  9173. (not (string-match
  9174. "\\<checkbox\\>"
  9175. (downcase
  9176. (or (org-entry-get
  9177. nil "COOKIE_DATA")
  9178. "")))))
  9179. (throw 'exit nil))
  9180. (while (re-search-forward box-re (point-at-eol) t)
  9181. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9182. (setq is-percent (match-end 2))
  9183. (save-match-data
  9184. (unless (outline-next-heading) (throw 'exit nil))
  9185. (while (and (looking-at org-complex-heading-regexp)
  9186. (> (setq l1 (length (match-string 1))) level))
  9187. (setq kwd (and (or recursive (= l1 ltoggle))
  9188. (match-string 2)))
  9189. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9190. (and (listp org-provide-todo-statistics)
  9191. (or (member kwd org-provide-todo-statistics)
  9192. (member kwd org-done-keywords))))
  9193. (setq cnt-all (1+ cnt-all))
  9194. (if (eq org-provide-todo-statistics t)
  9195. (and kwd (setq cnt-all (1+ cnt-all)))))
  9196. (and (member kwd org-done-keywords)
  9197. (setq cnt-done (1+ cnt-done)))
  9198. (outline-next-heading)))
  9199. (setq new
  9200. (if is-percent
  9201. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9202. (format "[%d/%d]" cnt-done cnt-all))
  9203. ndel (- (match-end 0) (match-beginning 0)))
  9204. (goto-char (match-beginning 0))
  9205. (insert new)
  9206. (delete-region (point) (+ (point) ndel)))
  9207. (when cookie-present
  9208. (run-hook-with-args 'org-after-todo-statistics-hook
  9209. cnt-done (- cnt-all cnt-done))))))
  9210. (run-hooks 'org-todo-statistics-hook)))
  9211. (defvar org-after-todo-statistics-hook nil
  9212. "Hook that is called after a TODO statistics cookie has been updated.
  9213. Each function is called with two arguments: the number of not-done entries
  9214. and the number of done entries.
  9215. For example, the following function, when added to this hook, will switch
  9216. an entry to DONE when all children are done, and back to TODO when new
  9217. entries are set to a TODO status. Note that this hook is only called
  9218. when there is a statistics cookie in the headline!
  9219. (defun org-summary-todo (n-done n-not-done)
  9220. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9221. (let (org-log-done org-log-states) ; turn off logging
  9222. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9223. ")
  9224. (defvar org-todo-statistics-hook nil
  9225. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9226. This hook runs even if there is no statistics cookie present, in which case
  9227. `org-after-todo-statistics-hook' would not run.")
  9228. (defun org-todo-trigger-tag-changes (state)
  9229. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9230. (let ((l org-todo-state-tags-triggers)
  9231. changes)
  9232. (when (or (not state) (equal state ""))
  9233. (setq changes (append changes (cdr (assoc "" l)))))
  9234. (when (and (stringp state) (> (length state) 0))
  9235. (setq changes (append changes (cdr (assoc state l)))))
  9236. (when (member state org-not-done-keywords)
  9237. (setq changes (append changes (cdr (assoc 'todo l)))))
  9238. (when (member state org-done-keywords)
  9239. (setq changes (append changes (cdr (assoc 'done l)))))
  9240. (dolist (c changes)
  9241. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9242. (defun org-local-logging (value)
  9243. "Get logging settings from a property VALUE."
  9244. (let* (words w a)
  9245. ;; directly set the variables, they are already local.
  9246. (setq org-log-done nil
  9247. org-log-repeat nil
  9248. org-todo-log-states nil)
  9249. (setq words (org-split-string value))
  9250. (while (setq w (pop words))
  9251. (cond
  9252. ((setq a (assoc w org-startup-options))
  9253. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9254. (set (nth 1 a) (nth 2 a))))
  9255. ((setq a (org-extract-log-state-settings w))
  9256. (and (member (car a) org-todo-keywords-1)
  9257. (push a org-todo-log-states)))))))
  9258. (defun org-get-todo-sequence-head (kwd)
  9259. "Return the head of the TODO sequence to which KWD belongs.
  9260. If KWD is not set, check if there is a text property remembering the
  9261. right sequence."
  9262. (let (p)
  9263. (cond
  9264. ((not kwd)
  9265. (or (get-text-property (point-at-bol) 'org-todo-head)
  9266. (progn
  9267. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9268. nil (point-at-eol)))
  9269. (get-text-property p 'org-todo-head))))
  9270. ((not (member kwd org-todo-keywords-1))
  9271. (car org-todo-keywords-1))
  9272. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9273. (defun org-fast-todo-selection ()
  9274. "Fast TODO keyword selection with single keys.
  9275. Returns the new TODO keyword, or nil if no state change should occur."
  9276. (let* ((fulltable org-todo-key-alist)
  9277. (done-keywords org-done-keywords) ;; needed for the faces.
  9278. (maxlen (apply 'max (mapcar
  9279. (lambda (x)
  9280. (if (stringp (car x)) (string-width (car x)) 0))
  9281. fulltable)))
  9282. (expert nil)
  9283. (fwidth (+ maxlen 3 1 3))
  9284. (ncol (/ (- (window-width) 4) fwidth))
  9285. tg cnt e c tbl
  9286. groups ingroup)
  9287. (save-excursion
  9288. (save-window-excursion
  9289. (if expert
  9290. (set-buffer (get-buffer-create " *Org todo*"))
  9291. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9292. (erase-buffer)
  9293. (org-set-local 'org-done-keywords done-keywords)
  9294. (setq tbl fulltable cnt 0)
  9295. (while (setq e (pop tbl))
  9296. (cond
  9297. ((equal e '(:startgroup))
  9298. (push '() groups) (setq ingroup t)
  9299. (when (not (= cnt 0))
  9300. (setq cnt 0)
  9301. (insert "\n"))
  9302. (insert "{ "))
  9303. ((equal e '(:endgroup))
  9304. (setq ingroup nil cnt 0)
  9305. (insert "}\n"))
  9306. ((equal e '(:newline))
  9307. (when (not (= cnt 0))
  9308. (setq cnt 0)
  9309. (insert "\n")
  9310. (setq e (car tbl))
  9311. (while (equal (car tbl) '(:newline))
  9312. (insert "\n")
  9313. (setq tbl (cdr tbl)))))
  9314. (t
  9315. (setq tg (car e) c (cdr e))
  9316. (if ingroup (push tg (car groups)))
  9317. (setq tg (org-add-props tg nil 'face
  9318. (org-get-todo-face tg)))
  9319. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9320. (insert "[" c "] " tg (make-string
  9321. (- fwidth 4 (length tg)) ?\ ))
  9322. (when (= (setq cnt (1+ cnt)) ncol)
  9323. (insert "\n")
  9324. (if ingroup (insert " "))
  9325. (setq cnt 0)))))
  9326. (insert "\n")
  9327. (goto-char (point-min))
  9328. (if (not expert) (org-fit-window-to-buffer))
  9329. (message "[a-z..]:Set [SPC]:clear")
  9330. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9331. (cond
  9332. ((or (= c ?\C-g)
  9333. (and (= c ?q) (not (rassoc c fulltable))))
  9334. (setq quit-flag t))
  9335. ((= c ?\ ) nil)
  9336. ((setq e (rassoc c fulltable) tg (car e))
  9337. tg)
  9338. (t (setq quit-flag t)))))))
  9339. (defun org-entry-is-todo-p ()
  9340. (member (org-get-todo-state) org-not-done-keywords))
  9341. (defun org-entry-is-done-p ()
  9342. (member (org-get-todo-state) org-done-keywords))
  9343. (defun org-get-todo-state ()
  9344. (save-excursion
  9345. (org-back-to-heading t)
  9346. (and (looking-at org-todo-line-regexp)
  9347. (match-end 2)
  9348. (match-string 2))))
  9349. (defun org-at-date-range-p (&optional inactive-ok)
  9350. "Is the cursor inside a date range?"
  9351. (interactive)
  9352. (save-excursion
  9353. (catch 'exit
  9354. (let ((pos (point)))
  9355. (skip-chars-backward "^[<\r\n")
  9356. (skip-chars-backward "<[")
  9357. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9358. (>= (match-end 0) pos)
  9359. (throw 'exit t))
  9360. (skip-chars-backward "^<[\r\n")
  9361. (skip-chars-backward "<[")
  9362. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9363. (>= (match-end 0) pos)
  9364. (throw 'exit t)))
  9365. nil)))
  9366. (defun org-get-repeat (&optional tagline)
  9367. "Check if there is a deadline/schedule with repeater in this entry."
  9368. (save-match-data
  9369. (save-excursion
  9370. (org-back-to-heading t)
  9371. (and (re-search-forward (if tagline
  9372. (concat tagline "\\s-*" org-repeat-re)
  9373. org-repeat-re)
  9374. (org-entry-end-position) t)
  9375. (match-string-no-properties 1)))))
  9376. (defvar org-last-changed-timestamp)
  9377. (defvar org-last-inserted-timestamp)
  9378. (defvar org-log-post-message)
  9379. (defvar org-log-note-purpose)
  9380. (defvar org-log-note-how)
  9381. (defvar org-log-note-extra)
  9382. (defun org-auto-repeat-maybe (done-word)
  9383. "Check if the current headline contains a repeated deadline/schedule.
  9384. If yes, set TODO state back to what it was and change the base date
  9385. of repeating deadline/scheduled time stamps to new date.
  9386. This function is run automatically after each state change to a DONE state."
  9387. ;; last-state is dynamically scoped into this function
  9388. (let* ((repeat (org-get-repeat))
  9389. (aa (assoc last-state org-todo-kwd-alist))
  9390. (interpret (nth 1 aa))
  9391. (head (nth 2 aa))
  9392. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9393. (msg "Entry repeats: ")
  9394. (org-log-done nil)
  9395. (org-todo-log-states nil)
  9396. (nshiftmax 10) (nshift 0)
  9397. re type n what ts time)
  9398. (when repeat
  9399. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9400. (org-todo (if (eq interpret 'type) last-state head))
  9401. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9402. (org-time-stamp-format t t)))
  9403. (when org-log-repeat
  9404. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9405. (memq 'org-add-log-note post-command-hook))
  9406. ;; OK, we are already setup for some record
  9407. (if (eq org-log-repeat 'note)
  9408. ;; make sure we take a note, not only a time stamp
  9409. (setq org-log-note-how 'note))
  9410. ;; Set up for taking a record
  9411. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9412. last-state
  9413. 'findpos org-log-repeat)))
  9414. (org-back-to-heading t)
  9415. (org-add-planning-info nil nil 'closed)
  9416. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9417. org-deadline-time-regexp "\\)\\|\\("
  9418. org-ts-regexp "\\)"))
  9419. (while (re-search-forward
  9420. re (save-excursion (outline-next-heading) (point)) t)
  9421. (setq type (if (match-end 1) org-scheduled-string
  9422. (if (match-end 3) org-deadline-string "Plain:"))
  9423. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9424. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9425. (setq n (string-to-number (match-string 2 ts))
  9426. what (match-string 3 ts))
  9427. (if (equal what "w") (setq n (* n 7) what "d"))
  9428. ;; Preparation, see if we need to modify the start date for the change
  9429. (when (match-end 1)
  9430. (setq time (save-match-data (org-time-string-to-time ts)))
  9431. (cond
  9432. ((equal (match-string 1 ts) ".")
  9433. ;; Shift starting date to today
  9434. (org-timestamp-change
  9435. (- (time-to-days (current-time)) (time-to-days time))
  9436. 'day))
  9437. ((equal (match-string 1 ts) "+")
  9438. (while (or (= nshift 0)
  9439. (<= (time-to-days time) (time-to-days (current-time))))
  9440. (when (= (incf nshift) nshiftmax)
  9441. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9442. (error "Abort")))
  9443. (org-timestamp-change n (cdr (assoc what whata)))
  9444. (org-at-timestamp-p t)
  9445. (setq ts (match-string 1))
  9446. (setq time (save-match-data (org-time-string-to-time ts))))
  9447. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9448. ;; rematch, so that we have everything in place for the real shift
  9449. (org-at-timestamp-p t)
  9450. (setq ts (match-string 1))
  9451. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9452. (org-timestamp-change n (cdr (assoc what whata)))
  9453. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9454. (setq org-log-post-message msg)
  9455. (message "%s" msg))))
  9456. (defun org-show-todo-tree (arg)
  9457. "Make a compact tree which shows all headlines marked with TODO.
  9458. The tree will show the lines where the regexp matches, and all higher
  9459. headlines above the match.
  9460. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9461. With a numeric prefix N, construct a sparse tree for the Nth element
  9462. of `org-todo-keywords-1'."
  9463. (interactive "P")
  9464. (let ((case-fold-search nil)
  9465. (kwd-re
  9466. (cond ((null arg) org-not-done-regexp)
  9467. ((equal arg '(4))
  9468. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9469. (mapcar 'list org-todo-keywords-1))))
  9470. (concat "\\("
  9471. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9472. "\\)\\>")))
  9473. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9474. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9475. org-todo-keywords-1)))
  9476. (t (error "Invalid prefix argument: %s" arg)))))
  9477. (message "%d TODO entries found"
  9478. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9479. (defun org-deadline (&optional remove time)
  9480. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9481. With argument REMOVE, remove any deadline from the item.
  9482. When TIME is set, it should be an internal time specification, and the
  9483. scheduling will use the corresponding date."
  9484. (interactive "P")
  9485. (let ((old-date (org-entry-get nil "DEADLINE")))
  9486. (if remove
  9487. (progn
  9488. (org-remove-timestamp-with-keyword org-deadline-string)
  9489. (message "Item no longer has a deadline."))
  9490. (if (org-get-repeat)
  9491. (error "Cannot change deadline on task with repeater, please do that by hand")
  9492. (org-add-planning-info 'deadline time 'closed)
  9493. (when (and old-date org-log-redeadline
  9494. (not (equal old-date
  9495. (substring org-last-inserted-timestamp 1 -1))))
  9496. (org-add-log-setup 'redeadline nil old-date 'findpos
  9497. org-log-redeadline))
  9498. (message "Deadline on %s" org-last-inserted-timestamp)))))
  9499. (defun org-schedule (&optional remove time)
  9500. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9501. With argument REMOVE, remove any scheduling date from the item.
  9502. When TIME is set, it should be an internal time specification, and the
  9503. scheduling will use the corresponding date."
  9504. (interactive "P")
  9505. (let ((old-date (org-entry-get nil "SCHEDULED")))
  9506. (if remove
  9507. (progn
  9508. (org-remove-timestamp-with-keyword org-scheduled-string)
  9509. (message "Item is no longer scheduled."))
  9510. (if (org-get-repeat)
  9511. (error "Cannot reschedule task with repeater, please do that by hand")
  9512. (org-add-planning-info 'scheduled time 'closed)
  9513. (when (and old-date org-log-reschedule
  9514. (not (equal old-date
  9515. (substring org-last-inserted-timestamp 1 -1))))
  9516. (org-add-log-setup 'reschedule nil old-date 'findpos
  9517. org-log-reschedule))
  9518. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  9519. (defun org-get-scheduled-time (pom &optional inherit)
  9520. "Get the scheduled time as a time tuple, of a format suitable
  9521. for calling org-schedule with, or if there is no scheduling,
  9522. returns nil."
  9523. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9524. (when time
  9525. (apply 'encode-time (org-parse-time-string time)))))
  9526. (defun org-get-deadline-time (pom &optional inherit)
  9527. "Get the deadine as a time tuple, of a format suitable for
  9528. calling org-deadline with, or if there is no scheduling, returns
  9529. nil."
  9530. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9531. (when time
  9532. (apply 'encode-time (org-parse-time-string time)))))
  9533. (defun org-remove-timestamp-with-keyword (keyword)
  9534. "Remove all time stamps with KEYWORD in the current entry."
  9535. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9536. beg)
  9537. (save-excursion
  9538. (org-back-to-heading t)
  9539. (setq beg (point))
  9540. (outline-next-heading)
  9541. (while (re-search-backward re beg t)
  9542. (replace-match "")
  9543. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9544. (equal (char-before) ?\ ))
  9545. (backward-delete-char 1)
  9546. (if (string-match "^[ \t]*$" (buffer-substring
  9547. (point-at-bol) (point-at-eol)))
  9548. (delete-region (point-at-bol)
  9549. (min (point-max) (1+ (point-at-eol))))))))))
  9550. (defun org-add-planning-info (what &optional time &rest remove)
  9551. "Insert new timestamp with keyword in the line directly after the headline.
  9552. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9553. If non is given, the user is prompted for a date.
  9554. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9555. be removed."
  9556. (interactive)
  9557. (let (org-time-was-given org-end-time-was-given ts
  9558. end default-time default-input)
  9559. (catch 'exit
  9560. (when (and (not time) (memq what '(scheduled deadline)))
  9561. ;; Try to get a default date/time from existing timestamp
  9562. (save-excursion
  9563. (org-back-to-heading t)
  9564. (setq end (save-excursion (outline-next-heading) (point)))
  9565. (when (re-search-forward (if (eq what 'scheduled)
  9566. org-scheduled-time-regexp
  9567. org-deadline-time-regexp)
  9568. end t)
  9569. (setq ts (match-string 1)
  9570. default-time
  9571. (apply 'encode-time (org-parse-time-string ts))
  9572. default-input (and ts (org-get-compact-tod ts))))))
  9573. (when what
  9574. ;; If necessary, get the time from the user
  9575. (setq time (or time (org-read-date nil 'to-time nil nil
  9576. default-time default-input))))
  9577. (when (and org-insert-labeled-timestamps-at-point
  9578. (member what '(scheduled deadline)))
  9579. (insert
  9580. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9581. (org-insert-time-stamp time org-time-was-given
  9582. nil nil nil (list org-end-time-was-given))
  9583. (setq what nil))
  9584. (save-excursion
  9585. (save-restriction
  9586. (let (col list elt ts buffer-invisibility-spec)
  9587. (org-back-to-heading t)
  9588. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9589. (goto-char (match-end 1))
  9590. (setq col (current-column))
  9591. (goto-char (match-end 0))
  9592. (if (eobp) (insert "\n") (forward-char 1))
  9593. (when (and (not what)
  9594. (not (looking-at
  9595. (concat "[ \t]*"
  9596. org-keyword-time-not-clock-regexp))))
  9597. ;; Nothing to add, nothing to remove...... :-)
  9598. (throw 'exit nil))
  9599. (if (and (not (looking-at outline-regexp))
  9600. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9601. "[^\r\n]*"))
  9602. (not (equal (match-string 1) org-clock-string)))
  9603. (narrow-to-region (match-beginning 0) (match-end 0))
  9604. (insert-before-markers "\n")
  9605. (backward-char 1)
  9606. (narrow-to-region (point) (point))
  9607. (and org-adapt-indentation (org-indent-to-column col)))
  9608. ;; Check if we have to remove something.
  9609. (setq list (cons what remove))
  9610. (while list
  9611. (setq elt (pop list))
  9612. (goto-char (point-min))
  9613. (when (or (and (eq elt 'scheduled)
  9614. (re-search-forward org-scheduled-time-regexp nil t))
  9615. (and (eq elt 'deadline)
  9616. (re-search-forward org-deadline-time-regexp nil t))
  9617. (and (eq elt 'closed)
  9618. (re-search-forward org-closed-time-regexp nil t)))
  9619. (replace-match "")
  9620. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9621. (skip-chars-backward " ")
  9622. (if (looking-at " +") (replace-match ""))))
  9623. (goto-char (point-max))
  9624. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  9625. (when what
  9626. (insert
  9627. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9628. (cond ((eq what 'scheduled) org-scheduled-string)
  9629. ((eq what 'deadline) org-deadline-string)
  9630. ((eq what 'closed) org-closed-string))
  9631. " ")
  9632. (setq ts (org-insert-time-stamp
  9633. time
  9634. (or org-time-was-given
  9635. (and (eq what 'closed) org-log-done-with-time))
  9636. (eq what 'closed)
  9637. nil nil (list org-end-time-was-given)))
  9638. (end-of-line 1))
  9639. (goto-char (point-min))
  9640. (widen)
  9641. (if (and (looking-at "[ \t]+\n")
  9642. (equal (char-before) ?\n))
  9643. (delete-region (1- (point)) (point-at-eol)))
  9644. ts))))))
  9645. (defvar org-log-note-marker (make-marker))
  9646. (defvar org-log-note-purpose nil)
  9647. (defvar org-log-note-state nil)
  9648. (defvar org-log-note-previous-state nil)
  9649. (defvar org-log-note-how nil)
  9650. (defvar org-log-note-extra nil)
  9651. (defvar org-log-note-window-configuration nil)
  9652. (defvar org-log-note-return-to (make-marker))
  9653. (defvar org-log-post-message nil
  9654. "Message to be displayed after a log note has been stored.
  9655. The auto-repeater uses this.")
  9656. (defun org-add-note ()
  9657. "Add a note to the current entry.
  9658. This is done in the same way as adding a state change note."
  9659. (interactive)
  9660. (org-add-log-setup 'note nil nil 'findpos nil))
  9661. (defvar org-property-end-re)
  9662. (defun org-add-log-setup (&optional purpose state prev-state
  9663. findpos how &optional extra)
  9664. "Set up the post command hook to take a note.
  9665. If this is about to TODO state change, the new state is expected in STATE.
  9666. When FINDPOS is non-nil, find the correct position for the note in
  9667. the current entry. If not, assume that it can be inserted at point.
  9668. HOW is an indicator what kind of note should be created.
  9669. EXTRA is additional text that will be inserted into the notes buffer."
  9670. (let* ((org-log-into-drawer (org-log-into-drawer))
  9671. (drawer (cond ((stringp org-log-into-drawer)
  9672. org-log-into-drawer)
  9673. (org-log-into-drawer "LOGBOOK")
  9674. (t nil))))
  9675. (save-restriction
  9676. (save-excursion
  9677. (when findpos
  9678. (org-back-to-heading t)
  9679. (narrow-to-region (point) (save-excursion
  9680. (outline-next-heading) (point)))
  9681. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9682. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9683. "[^\r\n]*\\)?"))
  9684. (goto-char (match-end 0))
  9685. (cond
  9686. (drawer
  9687. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9688. nil t)
  9689. (progn
  9690. (goto-char (match-end 0))
  9691. (or org-log-states-order-reversed
  9692. (and (re-search-forward org-property-end-re nil t)
  9693. (goto-char (1- (match-beginning 0))))))
  9694. (insert "\n:" drawer ":\n:END:")
  9695. (beginning-of-line 0)
  9696. (org-indent-line-function)
  9697. (beginning-of-line 2)
  9698. (org-indent-line-function)
  9699. (end-of-line 0)))
  9700. ((and org-log-state-notes-insert-after-drawers
  9701. (save-excursion
  9702. (forward-line) (looking-at org-drawer-regexp)))
  9703. (forward-line)
  9704. (while (looking-at org-drawer-regexp)
  9705. (goto-char (match-end 0))
  9706. (re-search-forward org-property-end-re (point-max) t)
  9707. (forward-line))
  9708. (forward-line -1)))
  9709. (unless org-log-states-order-reversed
  9710. (and (= (char-after) ?\n) (forward-char 1))
  9711. (org-skip-over-state-notes)
  9712. (skip-chars-backward " \t\n\r")))
  9713. (move-marker org-log-note-marker (point))
  9714. (setq org-log-note-purpose purpose
  9715. org-log-note-state state
  9716. org-log-note-previous-state prev-state
  9717. org-log-note-how how
  9718. org-log-note-extra extra)
  9719. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9720. (defun org-skip-over-state-notes ()
  9721. "Skip past the list of State notes in an entry."
  9722. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9723. (while (looking-at "[ \t]*- State")
  9724. (condition-case nil
  9725. (org-next-item)
  9726. (error (org-end-of-item)))))
  9727. (defun org-add-log-note (&optional purpose)
  9728. "Pop up a window for taking a note, and add this note later at point."
  9729. (remove-hook 'post-command-hook 'org-add-log-note)
  9730. (setq org-log-note-window-configuration (current-window-configuration))
  9731. (delete-other-windows)
  9732. (move-marker org-log-note-return-to (point))
  9733. (switch-to-buffer (marker-buffer org-log-note-marker))
  9734. (goto-char org-log-note-marker)
  9735. (org-switch-to-buffer-other-window "*Org Note*")
  9736. (erase-buffer)
  9737. (if (memq org-log-note-how '(time state))
  9738. (let (current-prefix-arg) (org-store-log-note))
  9739. (let ((org-inhibit-startup t)) (org-mode))
  9740. (insert (format "# Insert note for %s.
  9741. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9742. (cond
  9743. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9744. ((eq org-log-note-purpose 'done) "closed todo item")
  9745. ((eq org-log-note-purpose 'state)
  9746. (format "state change from \"%s\" to \"%s\""
  9747. (or org-log-note-previous-state "")
  9748. (or org-log-note-state "")))
  9749. ((eq org-log-note-purpose 'reschedule)
  9750. "rescheduling")
  9751. ((eq org-log-note-purpose 'redeadline)
  9752. "changing deadline")
  9753. ((eq org-log-note-purpose 'note)
  9754. "this entry")
  9755. (t (error "This should not happen")))))
  9756. (if org-log-note-extra (insert org-log-note-extra))
  9757. (org-set-local 'org-finish-function 'org-store-log-note)))
  9758. (defvar org-note-abort nil) ; dynamically scoped
  9759. (defun org-store-log-note ()
  9760. "Finish taking a log note, and insert it to where it belongs."
  9761. (let ((txt (buffer-string))
  9762. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9763. lines ind)
  9764. (kill-buffer (current-buffer))
  9765. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9766. (setq txt (replace-match "" t t txt)))
  9767. (if (string-match "\\s-+\\'" txt)
  9768. (setq txt (replace-match "" t t txt)))
  9769. (setq lines (org-split-string txt "\n"))
  9770. (when (and note (string-match "\\S-" note))
  9771. (setq note
  9772. (org-replace-escapes
  9773. note
  9774. (list (cons "%u" (user-login-name))
  9775. (cons "%U" user-full-name)
  9776. (cons "%t" (format-time-string
  9777. (org-time-stamp-format 'long 'inactive)
  9778. (current-time)))
  9779. (cons "%s" (if org-log-note-state
  9780. (concat "\"" org-log-note-state "\"")
  9781. ""))
  9782. (cons "%S" (if org-log-note-previous-state
  9783. (concat "\"" org-log-note-previous-state "\"")
  9784. "\"\"")))))
  9785. (if lines (setq note (concat note " \\\\")))
  9786. (push note lines))
  9787. (when (or current-prefix-arg org-note-abort)
  9788. (when org-log-into-drawer
  9789. (org-remove-empty-drawer-at
  9790. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9791. org-log-note-marker))
  9792. (setq lines nil))
  9793. (when lines
  9794. (with-current-buffer (marker-buffer org-log-note-marker)
  9795. (save-excursion
  9796. (goto-char org-log-note-marker)
  9797. (move-marker org-log-note-marker nil)
  9798. (end-of-line 1)
  9799. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9800. (insert "- " (pop lines))
  9801. (org-indent-line-function)
  9802. (beginning-of-line 1)
  9803. (looking-at "[ \t]*")
  9804. (setq ind (concat (match-string 0) " "))
  9805. (end-of-line 1)
  9806. (while lines (insert "\n" ind (pop lines)))
  9807. (message "Note stored")
  9808. (org-back-to-heading t)
  9809. (org-cycle-hide-drawers 'children)))))
  9810. (set-window-configuration org-log-note-window-configuration)
  9811. (with-current-buffer (marker-buffer org-log-note-return-to)
  9812. (goto-char org-log-note-return-to))
  9813. (move-marker org-log-note-return-to nil)
  9814. (and org-log-post-message (message "%s" org-log-post-message)))
  9815. (defun org-remove-empty-drawer-at (drawer pos)
  9816. "Remove an empty drawer DRAWER at position POS.
  9817. POS may also be a marker."
  9818. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9819. (save-excursion
  9820. (save-restriction
  9821. (widen)
  9822. (goto-char pos)
  9823. (if (org-in-regexp
  9824. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9825. (replace-match ""))))))
  9826. (defun org-sparse-tree (&optional arg)
  9827. "Create a sparse tree, prompt for the details.
  9828. This command can create sparse trees. You first need to select the type
  9829. of match used to create the tree:
  9830. t Show entries with a specific TODO keyword.
  9831. m Show entries selected by a tags/property match.
  9832. p Enter a property name and its value (both with completion on existing
  9833. names/values) and show entries with that property.
  9834. / Show entries matching a regular expression (`r' can be used as well)
  9835. d Show deadlines due within `org-deadline-warning-days'.
  9836. b Show deadlines and scheduled items before a date.
  9837. a Show deadlines and scheduled items after a date."
  9838. (interactive "P")
  9839. (let (ans kwd value)
  9840. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9841. (setq ans (read-char-exclusive))
  9842. (cond
  9843. ((equal ans ?d)
  9844. (call-interactively 'org-check-deadlines))
  9845. ((equal ans ?b)
  9846. (call-interactively 'org-check-before-date))
  9847. ((equal ans ?a)
  9848. (call-interactively 'org-check-after-date))
  9849. ((equal ans ?t)
  9850. (org-show-todo-tree '(4)))
  9851. ((member ans '(?T ?m))
  9852. (call-interactively 'org-match-sparse-tree))
  9853. ((member ans '(?p ?P))
  9854. (setq kwd (org-icompleting-read "Property: "
  9855. (mapcar 'list (org-buffer-property-keys))))
  9856. (setq value (org-icompleting-read "Value: "
  9857. (mapcar 'list (org-property-values kwd))))
  9858. (unless (string-match "\\`{.*}\\'" value)
  9859. (setq value (concat "\"" value "\"")))
  9860. (org-match-sparse-tree arg (concat kwd "=" value)))
  9861. ((member ans '(?r ?R ?/))
  9862. (call-interactively 'org-occur))
  9863. (t (error "No such sparse tree command \"%c\"" ans)))))
  9864. (defvar org-occur-highlights nil
  9865. "List of overlays used for occur matches.")
  9866. (make-variable-buffer-local 'org-occur-highlights)
  9867. (defvar org-occur-parameters nil
  9868. "Parameters of the active org-occur calls.
  9869. This is a list, each call to org-occur pushes as cons cell,
  9870. containing the regular expression and the callback, onto the list.
  9871. The list can contain several entries if `org-occur' has been called
  9872. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9873. will only contain one set of parameters. When the highlights are
  9874. removed (for example with `C-c C-c', or with the next edit (depending
  9875. on `org-remove-highlights-with-change'), this variable is emptied
  9876. as well.")
  9877. (make-variable-buffer-local 'org-occur-parameters)
  9878. (defun org-occur (regexp &optional keep-previous callback)
  9879. "Make a compact tree which shows all matches of REGEXP.
  9880. The tree will show the lines where the regexp matches, and all higher
  9881. headlines above the match. It will also show the heading after the match,
  9882. to make sure editing the matching entry is easy.
  9883. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9884. call to `org-occur' will be kept, to allow stacking of calls to this
  9885. command.
  9886. If CALLBACK is non-nil, it is a function which is called to confirm
  9887. that the match should indeed be shown."
  9888. (interactive "sRegexp: \nP")
  9889. (when (equal regexp "")
  9890. (error "Regexp cannot be empty"))
  9891. (unless keep-previous
  9892. (org-remove-occur-highlights nil nil t))
  9893. (push (cons regexp callback) org-occur-parameters)
  9894. (let ((cnt 0))
  9895. (save-excursion
  9896. (goto-char (point-min))
  9897. (if (or (not keep-previous) ; do not want to keep
  9898. (not org-occur-highlights)) ; no previous matches
  9899. ;; hide everything
  9900. (org-overview))
  9901. (while (re-search-forward regexp nil t)
  9902. (when (or (not callback)
  9903. (save-match-data (funcall callback)))
  9904. (setq cnt (1+ cnt))
  9905. (when org-highlight-sparse-tree-matches
  9906. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9907. (org-show-context 'occur-tree))))
  9908. (when org-remove-highlights-with-change
  9909. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9910. nil 'local))
  9911. (unless org-sparse-tree-open-archived-trees
  9912. (org-hide-archived-subtrees (point-min) (point-max)))
  9913. (run-hooks 'org-occur-hook)
  9914. (if (interactive-p)
  9915. (message "%d match(es) for regexp %s" cnt regexp))
  9916. cnt))
  9917. (defun org-show-context (&optional key)
  9918. "Make sure point and context and visible.
  9919. How much context is shown depends upon the variables
  9920. `org-show-hierarchy-above', `org-show-following-heading'. and
  9921. `org-show-siblings'."
  9922. (let ((heading-p (org-on-heading-p t))
  9923. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9924. (following-p (org-get-alist-option org-show-following-heading key))
  9925. (entry-p (org-get-alist-option org-show-entry-below key))
  9926. (siblings-p (org-get-alist-option org-show-siblings key)))
  9927. (catch 'exit
  9928. ;; Show heading or entry text
  9929. (if (and heading-p (not entry-p))
  9930. (org-flag-heading nil) ; only show the heading
  9931. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9932. (org-show-hidden-entry))) ; show entire entry
  9933. (when following-p
  9934. ;; Show next sibling, or heading below text
  9935. (save-excursion
  9936. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9937. (org-flag-heading nil))))
  9938. (when siblings-p (org-show-siblings))
  9939. (when hierarchy-p
  9940. ;; show all higher headings, possibly with siblings
  9941. (save-excursion
  9942. (while (and (condition-case nil
  9943. (progn (org-up-heading-all 1) t)
  9944. (error nil))
  9945. (not (bobp)))
  9946. (org-flag-heading nil)
  9947. (when siblings-p (org-show-siblings))))))))
  9948. (defun org-reveal (&optional siblings)
  9949. "Show current entry, hierarchy above it, and the following headline.
  9950. This can be used to show a consistent set of context around locations
  9951. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9952. not t for the search context.
  9953. With optional argument SIBLINGS, on each level of the hierarchy all
  9954. siblings are shown. This repairs the tree structure to what it would
  9955. look like when opened with hierarchical calls to `org-cycle'."
  9956. (interactive "P")
  9957. (let ((org-show-hierarchy-above t)
  9958. (org-show-following-heading t)
  9959. (org-show-siblings (if siblings t org-show-siblings)))
  9960. (org-show-context nil)))
  9961. (defun org-highlight-new-match (beg end)
  9962. "Highlight from BEG to END and mark the highlight is an occur headline."
  9963. (let ((ov (org-make-overlay beg end)))
  9964. (org-overlay-put ov 'face 'secondary-selection)
  9965. (push ov org-occur-highlights)))
  9966. (defun org-remove-occur-highlights (&optional beg end noremove)
  9967. "Remove the occur highlights from the buffer.
  9968. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9969. from the `before-change-functions' in the current buffer."
  9970. (interactive)
  9971. (unless org-inhibit-highlight-removal
  9972. (mapc 'org-delete-overlay org-occur-highlights)
  9973. (setq org-occur-highlights nil)
  9974. (setq org-occur-parameters nil)
  9975. (unless noremove
  9976. (remove-hook 'before-change-functions
  9977. 'org-remove-occur-highlights 'local))))
  9978. ;;;; Priorities
  9979. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9980. "Regular expression matching the priority indicator.")
  9981. (defvar org-remove-priority-next-time nil)
  9982. (defun org-priority-up ()
  9983. "Increase the priority of the current item."
  9984. (interactive)
  9985. (org-priority 'up))
  9986. (defun org-priority-down ()
  9987. "Decrease the priority of the current item."
  9988. (interactive)
  9989. (org-priority 'down))
  9990. (defun org-priority (&optional action)
  9991. "Change the priority of an item by ARG.
  9992. ACTION can be `set', `up', `down', or a character."
  9993. (interactive)
  9994. (unless org-enable-priority-commands
  9995. (error "Priority commands are disabled"))
  9996. (setq action (or action 'set))
  9997. (let (current new news have remove)
  9998. (save-excursion
  9999. (org-back-to-heading t)
  10000. (if (looking-at org-priority-regexp)
  10001. (setq current (string-to-char (match-string 2))
  10002. have t)
  10003. (setq current org-default-priority))
  10004. (cond
  10005. ((eq action 'remove)
  10006. (setq remove t new ?\ ))
  10007. ((or (eq action 'set)
  10008. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10009. (if (not (eq action 'set))
  10010. (setq new action)
  10011. (message "Priority %c-%c, SPC to remove: "
  10012. org-highest-priority org-lowest-priority)
  10013. (setq new (read-char-exclusive)))
  10014. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10015. (= (upcase org-lowest-priority) org-lowest-priority))
  10016. (setq new (upcase new)))
  10017. (cond ((equal new ?\ ) (setq remove t))
  10018. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10019. (error "Priority must be between `%c' and `%c'"
  10020. org-highest-priority org-lowest-priority))))
  10021. ((eq action 'up)
  10022. (if (and (not have) (eq last-command this-command))
  10023. (setq new org-lowest-priority)
  10024. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10025. org-default-priority (1- current)))))
  10026. ((eq action 'down)
  10027. (if (and (not have) (eq last-command this-command))
  10028. (setq new org-highest-priority)
  10029. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10030. org-default-priority (1+ current)))))
  10031. (t (error "Invalid action")))
  10032. (if (or (< (upcase new) org-highest-priority)
  10033. (> (upcase new) org-lowest-priority))
  10034. (setq remove t))
  10035. (setq news (format "%c" new))
  10036. (if have
  10037. (if remove
  10038. (replace-match "" t t nil 1)
  10039. (replace-match news t t nil 2))
  10040. (if remove
  10041. (error "No priority cookie found in line")
  10042. (let ((case-fold-search nil))
  10043. (looking-at org-todo-line-regexp))
  10044. (if (match-end 2)
  10045. (progn
  10046. (goto-char (match-end 2))
  10047. (insert " [#" news "]"))
  10048. (goto-char (match-beginning 3))
  10049. (insert "[#" news "] "))))
  10050. (org-preserve-lc (org-set-tags nil 'align)))
  10051. (if remove
  10052. (message "Priority removed")
  10053. (message "Priority of current item set to %s" news))))
  10054. (defun org-get-priority (s)
  10055. "Find priority cookie and return priority."
  10056. (save-match-data
  10057. (if (not (string-match org-priority-regexp s))
  10058. (* 1000 (- org-lowest-priority org-default-priority))
  10059. (* 1000 (- org-lowest-priority
  10060. (string-to-char (match-string 2 s)))))))
  10061. ;;;; Tags
  10062. (defvar org-agenda-archives-mode)
  10063. (defvar org-map-continue-from nil
  10064. "Position from where mapping should continue.
  10065. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10066. (defvar org-scanner-tags nil
  10067. "The current tag list while the tags scanner is running.")
  10068. (defvar org-trust-scanner-tags nil
  10069. "Should `org-get-tags-at' use the tags fro the scanner.
  10070. This is for internal dynamical scoping only.
  10071. When this is non-nil, the function `org-get-tags-at' will return the value
  10072. of `org-scanner-tags' instead of building the list by itself. This
  10073. can lead to large speed-ups when the tags scanner is used in a file with
  10074. many entries, and when the list of tags is retrieved, for example to
  10075. obtain a list of properties. Building the tags list for each entry in such
  10076. a file becomes an N^2 operation - but with this variable set, it scales
  10077. as N.")
  10078. (defun org-scan-tags (action matcher &optional todo-only)
  10079. "Scan headline tags with inheritance and produce output ACTION.
  10080. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10081. or `agenda' to produce an entry list for an agenda view. It can also be
  10082. a Lisp form or a function that should be called at each matched headline, in
  10083. this case the return value is a list of all return values from these calls.
  10084. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10085. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10086. only lines with a TODO keyword are included in the output."
  10087. (require 'org-agenda)
  10088. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10089. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10090. (org-re
  10091. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10092. (props (list 'face 'default
  10093. 'done-face 'org-agenda-done
  10094. 'undone-face 'default
  10095. 'mouse-face 'highlight
  10096. 'org-not-done-regexp org-not-done-regexp
  10097. 'org-todo-regexp org-todo-regexp
  10098. 'help-echo
  10099. (format "mouse-2 or RET jump to org file %s"
  10100. (abbreviate-file-name
  10101. (or (buffer-file-name (buffer-base-buffer))
  10102. (buffer-name (buffer-base-buffer)))))))
  10103. (case-fold-search nil)
  10104. (org-map-continue-from nil)
  10105. lspos tags tags-list
  10106. (tags-alist (list (cons 0 org-file-tags)))
  10107. (llast 0) rtn rtn1 level category i txt
  10108. todo marker entry priority)
  10109. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10110. (setq action (list 'lambda nil action)))
  10111. (save-excursion
  10112. (goto-char (point-min))
  10113. (when (eq action 'sparse-tree)
  10114. (org-overview)
  10115. (org-remove-occur-highlights))
  10116. (while (re-search-forward re nil t)
  10117. (catch :skip
  10118. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10119. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10120. (goto-char (setq lspos (match-beginning 0)))
  10121. (setq level (org-reduced-level (funcall outline-level))
  10122. category (org-get-category))
  10123. (setq i llast llast level)
  10124. ;; remove tag lists from same and sublevels
  10125. (while (>= i level)
  10126. (when (setq entry (assoc i tags-alist))
  10127. (setq tags-alist (delete entry tags-alist)))
  10128. (setq i (1- i)))
  10129. ;; add the next tags
  10130. (when tags
  10131. (setq tags (org-split-string tags ":")
  10132. tags-alist
  10133. (cons (cons level tags) tags-alist)))
  10134. ;; compile tags for current headline
  10135. (setq tags-list
  10136. (if org-use-tag-inheritance
  10137. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10138. tags)
  10139. org-scanner-tags tags-list)
  10140. (when org-use-tag-inheritance
  10141. (setcdr (car tags-alist)
  10142. (mapcar (lambda (x)
  10143. (setq x (copy-sequence x))
  10144. (org-add-prop-inherited x))
  10145. (cdar tags-alist))))
  10146. (when (and tags org-use-tag-inheritance
  10147. (or (not (eq t org-use-tag-inheritance))
  10148. org-tags-exclude-from-inheritance))
  10149. ;; selective inheritance, remove uninherited ones
  10150. (setcdr (car tags-alist)
  10151. (org-remove-uniherited-tags (cdar tags-alist))))
  10152. (when (and (or (not todo-only)
  10153. (and (member todo org-not-done-keywords)
  10154. (or (not org-agenda-tags-todo-honor-ignore-options)
  10155. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10156. (let ((case-fold-search t)) (eval matcher))
  10157. (or
  10158. (not (member org-archive-tag tags-list))
  10159. ;; we have an archive tag, should we use this anyway?
  10160. (or (not org-agenda-skip-archived-trees)
  10161. (and (eq action 'agenda) org-agenda-archives-mode))))
  10162. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10163. ;; select this headline
  10164. (cond
  10165. ((eq action 'sparse-tree)
  10166. (and org-highlight-sparse-tree-matches
  10167. (org-get-heading) (match-end 0)
  10168. (org-highlight-new-match
  10169. (match-beginning 0) (match-beginning 1)))
  10170. (org-show-context 'tags-tree))
  10171. ((eq action 'agenda)
  10172. (setq txt (org-format-agenda-item
  10173. ""
  10174. (concat
  10175. (if (eq org-tags-match-list-sublevels 'indented)
  10176. (make-string (1- level) ?.) "")
  10177. (org-get-heading))
  10178. category
  10179. tags-list
  10180. )
  10181. priority (org-get-priority txt))
  10182. (goto-char lspos)
  10183. (setq marker (org-agenda-new-marker))
  10184. (org-add-props txt props
  10185. 'org-marker marker 'org-hd-marker marker 'org-category category
  10186. 'todo-state todo
  10187. 'priority priority 'type "tagsmatch")
  10188. (push txt rtn))
  10189. ((functionp action)
  10190. (setq org-map-continue-from nil)
  10191. (save-excursion
  10192. (setq rtn1 (funcall action))
  10193. (push rtn1 rtn)))
  10194. (t (error "Invalid action")))
  10195. ;; if we are to skip sublevels, jump to end of subtree
  10196. (unless org-tags-match-list-sublevels
  10197. (org-end-of-subtree t)
  10198. (backward-char 1))))
  10199. ;; Get the correct position from where to continue
  10200. (if org-map-continue-from
  10201. (goto-char org-map-continue-from)
  10202. (and (= (point) lspos) (end-of-line 1)))))
  10203. (when (and (eq action 'sparse-tree)
  10204. (not org-sparse-tree-open-archived-trees))
  10205. (org-hide-archived-subtrees (point-min) (point-max)))
  10206. (nreverse rtn)))
  10207. (defun org-remove-uniherited-tags (tags)
  10208. "Remove all tags that are not inherited from the list TAGS."
  10209. (cond
  10210. ((eq org-use-tag-inheritance t)
  10211. (if org-tags-exclude-from-inheritance
  10212. (org-delete-all org-tags-exclude-from-inheritance tags)
  10213. tags))
  10214. ((not org-use-tag-inheritance) nil)
  10215. ((stringp org-use-tag-inheritance)
  10216. (delq nil (mapcar
  10217. (lambda (x)
  10218. (if (and (string-match org-use-tag-inheritance x)
  10219. (not (member x org-tags-exclude-from-inheritance)))
  10220. x nil))
  10221. tags)))
  10222. ((listp org-use-tag-inheritance)
  10223. (delq nil (mapcar
  10224. (lambda (x)
  10225. (if (member x org-use-tag-inheritance) x nil))
  10226. tags)))))
  10227. (defvar todo-only) ;; dynamically scoped
  10228. (defun org-match-sparse-tree (&optional todo-only match)
  10229. "Create a sparse tree according to tags string MATCH.
  10230. MATCH can contain positive and negative selection of tags, like
  10231. \"+WORK+URGENT-WITHBOSS\".
  10232. If optional argument TODO-ONLY is non-nil, only select lines that are
  10233. also TODO lines."
  10234. (interactive "P")
  10235. (org-prepare-agenda-buffers (list (current-buffer)))
  10236. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10237. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10238. (defvar org-cached-props nil)
  10239. (defun org-cached-entry-get (pom property)
  10240. (if (or (eq t org-use-property-inheritance)
  10241. (and (stringp org-use-property-inheritance)
  10242. (string-match org-use-property-inheritance property))
  10243. (and (listp org-use-property-inheritance)
  10244. (member property org-use-property-inheritance)))
  10245. ;; Caching is not possible, check it directly
  10246. (org-entry-get pom property 'inherit)
  10247. ;; Get all properties, so that we can do complicated checks easily
  10248. (cdr (assoc property (or org-cached-props
  10249. (setq org-cached-props
  10250. (org-entry-properties pom)))))))
  10251. (defun org-global-tags-completion-table (&optional files)
  10252. "Return the list of all tags in all agenda buffer/files."
  10253. (save-excursion
  10254. (org-uniquify
  10255. (delq nil
  10256. (apply 'append
  10257. (mapcar
  10258. (lambda (file)
  10259. (set-buffer (find-file-noselect file))
  10260. (append (org-get-buffer-tags)
  10261. (mapcar (lambda (x) (if (stringp (car-safe x))
  10262. (list (car-safe x)) nil))
  10263. org-tag-alist)))
  10264. (if (and files (car files))
  10265. files
  10266. (org-agenda-files))))))))
  10267. (defun org-make-tags-matcher (match)
  10268. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10269. ;; todo-only is scoped dynamically into this function, and the function
  10270. ;; may change it if the matcher asks for it.
  10271. (unless match
  10272. ;; Get a new match request, with completion
  10273. (let ((org-last-tags-completion-table
  10274. (org-global-tags-completion-table)))
  10275. (setq match (org-completing-read-no-i
  10276. "Match: " 'org-tags-completion-function nil nil nil
  10277. 'org-tags-history))))
  10278. ;; Parse the string and create a lisp form
  10279. (let ((match0 match)
  10280. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10281. minus tag mm
  10282. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10283. orterms term orlist re-p str-p level-p level-op time-p
  10284. prop-p pn pv po cat-p gv rest)
  10285. (if (string-match "/+" match)
  10286. ;; match contains also a todo-matching request
  10287. (progn
  10288. (setq tagsmatch (substring match 0 (match-beginning 0))
  10289. todomatch (substring match (match-end 0)))
  10290. (if (string-match "^!" todomatch)
  10291. (setq todo-only t todomatch (substring todomatch 1)))
  10292. (if (string-match "^\\s-*$" todomatch)
  10293. (setq todomatch nil)))
  10294. ;; only matching tags
  10295. (setq tagsmatch match todomatch nil))
  10296. ;; Make the tags matcher
  10297. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10298. (setq tagsmatcher t)
  10299. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10300. (while (setq term (pop orterms))
  10301. (while (and (equal (substring term -1) "\\") orterms)
  10302. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10303. (while (string-match re term)
  10304. (setq rest (substring term (match-end 0))
  10305. minus (and (match-end 1)
  10306. (equal (match-string 1 term) "-"))
  10307. tag (match-string 2 term)
  10308. re-p (equal (string-to-char tag) ?{)
  10309. level-p (match-end 4)
  10310. prop-p (match-end 5)
  10311. mm (cond
  10312. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10313. (level-p
  10314. (setq level-op (org-op-to-function (match-string 3 term)))
  10315. `(,level-op level ,(string-to-number
  10316. (match-string 4 term))))
  10317. (prop-p
  10318. (setq pn (match-string 5 term)
  10319. po (match-string 6 term)
  10320. pv (match-string 7 term)
  10321. cat-p (equal pn "CATEGORY")
  10322. re-p (equal (string-to-char pv) ?{)
  10323. str-p (equal (string-to-char pv) ?\")
  10324. time-p (save-match-data
  10325. (string-match "^\"[[<].*[]>]\"$" pv))
  10326. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10327. (if time-p (setq pv (org-matcher-time pv)))
  10328. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10329. (cond
  10330. ((equal pn "CATEGORY")
  10331. (setq gv '(get-text-property (point) 'org-category)))
  10332. ((equal pn "TODO")
  10333. (setq gv 'todo))
  10334. (t
  10335. (setq gv `(org-cached-entry-get nil ,pn))))
  10336. (if re-p
  10337. (if (eq po 'org<>)
  10338. `(not (string-match ,pv (or ,gv "")))
  10339. `(string-match ,pv (or ,gv "")))
  10340. (if str-p
  10341. `(,po (or ,gv "") ,pv)
  10342. `(,po (string-to-number (or ,gv ""))
  10343. ,(string-to-number pv) ))))
  10344. (t `(member ,tag tags-list)))
  10345. mm (if minus (list 'not mm) mm)
  10346. term rest)
  10347. (push mm tagsmatcher))
  10348. (push (if (> (length tagsmatcher) 1)
  10349. (cons 'and tagsmatcher)
  10350. (car tagsmatcher))
  10351. orlist)
  10352. (setq tagsmatcher nil))
  10353. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10354. (setq tagsmatcher
  10355. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10356. ;; Make the todo matcher
  10357. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10358. (setq todomatcher t)
  10359. (setq orterms (org-split-string todomatch "|") orlist nil)
  10360. (while (setq term (pop orterms))
  10361. (while (string-match re term)
  10362. (setq minus (and (match-end 1)
  10363. (equal (match-string 1 term) "-"))
  10364. kwd (match-string 2 term)
  10365. re-p (equal (string-to-char kwd) ?{)
  10366. term (substring term (match-end 0))
  10367. mm (if re-p
  10368. `(string-match ,(substring kwd 1 -1) todo)
  10369. (list 'equal 'todo kwd))
  10370. mm (if minus (list 'not mm) mm))
  10371. (push mm todomatcher))
  10372. (push (if (> (length todomatcher) 1)
  10373. (cons 'and todomatcher)
  10374. (car todomatcher))
  10375. orlist)
  10376. (setq todomatcher nil))
  10377. (setq todomatcher (if (> (length orlist) 1)
  10378. (cons 'or orlist) (car orlist))))
  10379. ;; Return the string and lisp forms of the matcher
  10380. (setq matcher (if todomatcher
  10381. (list 'and tagsmatcher todomatcher)
  10382. tagsmatcher))
  10383. (cons match0 matcher)))
  10384. (defun org-op-to-function (op &optional stringp)
  10385. "Turn an operator into the appropriate function."
  10386. (setq op
  10387. (cond
  10388. ((equal op "<" ) '(< string< org-time<))
  10389. ((equal op ">" ) '(> org-string> org-time>))
  10390. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10391. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10392. ((member op '("=" "==")) '(= string= org-time=))
  10393. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10394. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10395. (defun org<> (a b) (not (= a b)))
  10396. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10397. (defun org-string>= (a b) (not (string< a b)))
  10398. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10399. (defun org-string<> (a b) (not (string= a b)))
  10400. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10401. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10402. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10403. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10404. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10405. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10406. (defun org-2ft (s)
  10407. "Convert S to a floating point time.
  10408. If S is already a number, just return it. If it is a string, parse
  10409. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10410. (cond
  10411. ((numberp s) s)
  10412. ((stringp s)
  10413. (condition-case nil
  10414. (float-time (apply 'encode-time (org-parse-time-string s)))
  10415. (error 0.)))
  10416. (t 0.)))
  10417. (defun org-time-today ()
  10418. "Time in seconds today at 0:00.
  10419. Returns the float number of seconds since the beginning of the
  10420. epoch to the beginning of today (00:00)."
  10421. (float-time (apply 'encode-time
  10422. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10423. (defun org-matcher-time (s)
  10424. "Interpret a time comparison value."
  10425. (save-match-data
  10426. (cond
  10427. ((string= s "<now>") (float-time))
  10428. ((string= s "<today>") (org-time-today))
  10429. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10430. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10431. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10432. (+ (org-time-today)
  10433. (* (string-to-number (match-string 1 s))
  10434. (cdr (assoc (match-string 2 s)
  10435. '(("d" . 86400.0) ("w" . 604800.0)
  10436. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10437. (t (org-2ft s)))))
  10438. (defun org-match-any-p (re list)
  10439. "Does re match any element of list?"
  10440. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10441. (delq nil list))
  10442. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10443. (defvar org-tags-overlay (org-make-overlay 1 1))
  10444. (org-detach-overlay org-tags-overlay)
  10445. (defun org-get-local-tags-at (&optional pos)
  10446. "Get a list of tags defined in the current headline."
  10447. (org-get-tags-at pos 'local))
  10448. (defun org-get-local-tags ()
  10449. "Get a list of tags defined in the current headline."
  10450. (org-get-tags-at nil 'local))
  10451. (defun org-get-tags-at (&optional pos local)
  10452. "Get a list of all headline tags applicable at POS.
  10453. POS defaults to point. If tags are inherited, the list contains
  10454. the targets in the same sequence as the headlines appear, i.e.
  10455. the tags of the current headline come last.
  10456. When LOCAL is non-nil, only return tags from the current headline,
  10457. ignore inherited ones."
  10458. (interactive)
  10459. (if (and org-trust-scanner-tags
  10460. (or (not pos) (equal pos (point)))
  10461. (not local))
  10462. org-scanner-tags
  10463. (let (tags ltags lastpos parent)
  10464. (save-excursion
  10465. (save-restriction
  10466. (widen)
  10467. (goto-char (or pos (point)))
  10468. (save-match-data
  10469. (catch 'done
  10470. (condition-case nil
  10471. (progn
  10472. (org-back-to-heading t)
  10473. (while (not (equal lastpos (point)))
  10474. (setq lastpos (point))
  10475. (when (looking-at
  10476. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10477. (setq ltags (org-split-string
  10478. (org-match-string-no-properties 1) ":"))
  10479. (when parent
  10480. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10481. (setq tags (append
  10482. (if parent
  10483. (org-remove-uniherited-tags ltags)
  10484. ltags)
  10485. tags)))
  10486. (or org-use-tag-inheritance (throw 'done t))
  10487. (if local (throw 'done t))
  10488. (or (org-up-heading-safe) (error nil))
  10489. (setq parent t)))
  10490. (error nil)))))
  10491. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10492. (defun org-add-prop-inherited (s)
  10493. (add-text-properties 0 (length s) '(inherited t) s)
  10494. s)
  10495. (defun org-toggle-tag (tag &optional onoff)
  10496. "Toggle the tag TAG for the current line.
  10497. If ONOFF is `on' or `off', don't toggle but set to this state."
  10498. (let (res current)
  10499. (save-excursion
  10500. (org-back-to-heading t)
  10501. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10502. (point-at-eol) t)
  10503. (progn
  10504. (setq current (match-string 1))
  10505. (replace-match ""))
  10506. (setq current ""))
  10507. (setq current (nreverse (org-split-string current ":")))
  10508. (cond
  10509. ((eq onoff 'on)
  10510. (setq res t)
  10511. (or (member tag current) (push tag current)))
  10512. ((eq onoff 'off)
  10513. (or (not (member tag current)) (setq current (delete tag current))))
  10514. (t (if (member tag current)
  10515. (setq current (delete tag current))
  10516. (setq res t)
  10517. (push tag current))))
  10518. (end-of-line 1)
  10519. (if current
  10520. (progn
  10521. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10522. (org-set-tags nil t))
  10523. (delete-horizontal-space))
  10524. (run-hooks 'org-after-tags-change-hook))
  10525. res))
  10526. (defun org-align-tags-here (to-col)
  10527. ;; Assumes that this is a headline
  10528. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10529. (beginning-of-line 1)
  10530. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10531. (< pos (match-beginning 2)))
  10532. (progn
  10533. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10534. (goto-char (match-beginning 1))
  10535. (insert " ")
  10536. (delete-region (point) (1+ (match-beginning 2)))
  10537. (setq ncol (max (1+ (current-column))
  10538. (1+ col)
  10539. (if (> to-col 0)
  10540. to-col
  10541. (- (abs to-col) tags-l))))
  10542. (setq p (point))
  10543. (insert (make-string (- ncol (current-column)) ?\ ))
  10544. (setq ncol (current-column))
  10545. (when indent-tabs-mode (tabify p (point-at-eol)))
  10546. (org-move-to-column (min ncol col) t))
  10547. (goto-char pos))))
  10548. (defun org-set-tags-command (&optional arg just-align)
  10549. "Call the set-tags command for the current entry."
  10550. (interactive "P")
  10551. (if (org-on-heading-p)
  10552. (org-set-tags arg just-align)
  10553. (save-excursion
  10554. (org-back-to-heading t)
  10555. (org-set-tags arg just-align))))
  10556. (defun org-set-tags-to (data)
  10557. "Set the tags of the current entry to DATA, replacing the current tags.
  10558. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10559. If DATA is nil or the empty string, any tags will be removed."
  10560. (interactive "sTags: ")
  10561. (setq data
  10562. (cond
  10563. ((eq data nil) "")
  10564. ((equal data "") "")
  10565. ((stringp data)
  10566. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10567. ":"))
  10568. ((listp data)
  10569. (concat ":" (mapconcat 'identity data ":") ":"))
  10570. (t nil)))
  10571. (when data
  10572. (save-excursion
  10573. (org-back-to-heading t)
  10574. (when (looking-at org-complex-heading-regexp)
  10575. (if (match-end 5)
  10576. (progn
  10577. (goto-char (match-beginning 5))
  10578. (insert data)
  10579. (delete-region (point) (point-at-eol))
  10580. (org-set-tags nil 'align))
  10581. (goto-char (point-at-eol))
  10582. (insert " " data)
  10583. (org-set-tags nil 'align)))
  10584. (beginning-of-line 1)
  10585. (if (looking-at ".*?\\([ \t]+\\)$")
  10586. (delete-region (match-beginning 1) (match-end 1))))))
  10587. (defun org-set-tags (&optional arg just-align)
  10588. "Set the tags for the current headline.
  10589. With prefix ARG, realign all tags in headings in the current buffer."
  10590. (interactive "P")
  10591. (let* ((re (concat "^" outline-regexp))
  10592. (current (org-get-tags-string))
  10593. (col (current-column))
  10594. (org-setting-tags t)
  10595. table current-tags inherited-tags ; computed below when needed
  10596. tags p0 c0 c1 rpl)
  10597. (if arg
  10598. (save-excursion
  10599. (goto-char (point-min))
  10600. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10601. (while (re-search-forward re nil t)
  10602. (org-set-tags nil t)
  10603. (end-of-line 1)))
  10604. (message "All tags realigned to column %d" org-tags-column))
  10605. (if just-align
  10606. (setq tags current)
  10607. ;; Get a new set of tags from the user
  10608. (save-excursion
  10609. (setq table (append org-tag-persistent-alist
  10610. (or org-tag-alist (org-get-buffer-tags)))
  10611. org-last-tags-completion-table table
  10612. current-tags (org-split-string current ":")
  10613. inherited-tags (nreverse
  10614. (nthcdr (length current-tags)
  10615. (nreverse (org-get-tags-at))))
  10616. tags
  10617. (if (or (eq t org-use-fast-tag-selection)
  10618. (and org-use-fast-tag-selection
  10619. (delq nil (mapcar 'cdr table))))
  10620. (org-fast-tag-selection
  10621. current-tags inherited-tags table
  10622. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10623. (let ((org-add-colon-after-tag-completion t))
  10624. (org-trim
  10625. (org-without-partial-completion
  10626. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10627. nil nil current 'org-tags-history)))))))
  10628. (while (string-match "[-+&]+" tags)
  10629. ;; No boolean logic, just a list
  10630. (setq tags (replace-match ":" t t tags))))
  10631. (if org-tags-sort-function
  10632. (setq tags (mapconcat 'identity
  10633. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10634. org-tags-sort-function) ":")))
  10635. (if (string-match "\\`[\t ]*\\'" tags)
  10636. (setq tags "")
  10637. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10638. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10639. ;; Insert new tags at the correct column
  10640. (beginning-of-line 1)
  10641. (cond
  10642. ((and (equal current "") (equal tags "")))
  10643. ((re-search-forward
  10644. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10645. (point-at-eol) t)
  10646. (if (equal tags "")
  10647. (setq rpl "")
  10648. (goto-char (match-beginning 0))
  10649. (setq c0 (current-column) p0 (point)
  10650. c1 (max (1+ c0) (if (> org-tags-column 0)
  10651. org-tags-column
  10652. (- (- org-tags-column) (length tags))))
  10653. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10654. (replace-match rpl t t)
  10655. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10656. tags)
  10657. (t (error "Tags alignment failed")))
  10658. (org-move-to-column col)
  10659. (unless just-align
  10660. (run-hooks 'org-after-tags-change-hook)))))
  10661. (defun org-change-tag-in-region (beg end tag off)
  10662. "Add or remove TAG for each entry in the region.
  10663. This works in the agenda, and also in an org-mode buffer."
  10664. (interactive
  10665. (list (region-beginning) (region-end)
  10666. (let ((org-last-tags-completion-table
  10667. (if (org-mode-p)
  10668. (org-get-buffer-tags)
  10669. (org-global-tags-completion-table))))
  10670. (org-icompleting-read
  10671. "Tag: " 'org-tags-completion-function nil nil nil
  10672. 'org-tags-history))
  10673. (progn
  10674. (message "[s]et or [r]emove? ")
  10675. (equal (read-char-exclusive) ?r))))
  10676. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10677. (let ((agendap (equal major-mode 'org-agenda-mode))
  10678. l1 l2 m buf pos newhead (cnt 0))
  10679. (goto-char end)
  10680. (setq l2 (1- (org-current-line)))
  10681. (goto-char beg)
  10682. (setq l1 (org-current-line))
  10683. (loop for l from l1 to l2 do
  10684. (org-goto-line l)
  10685. (setq m (get-text-property (point) 'org-hd-marker))
  10686. (when (or (and (org-mode-p) (org-on-heading-p))
  10687. (and agendap m))
  10688. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10689. pos (if agendap m (point)))
  10690. (with-current-buffer buf
  10691. (save-excursion
  10692. (save-restriction
  10693. (goto-char pos)
  10694. (setq cnt (1+ cnt))
  10695. (org-toggle-tag tag (if off 'off 'on))
  10696. (setq newhead (org-get-heading)))))
  10697. (and agendap (org-agenda-change-all-lines newhead m))))
  10698. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10699. (defun org-tags-completion-function (string predicate &optional flag)
  10700. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10701. (confirm (lambda (x) (stringp (car x)))))
  10702. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10703. (setq s1 (match-string 1 string)
  10704. s2 (match-string 2 string))
  10705. (setq s1 "" s2 string))
  10706. (cond
  10707. ((eq flag nil)
  10708. ;; try completion
  10709. (setq rtn (try-completion s2 ctable confirm))
  10710. (if (stringp rtn)
  10711. (setq rtn
  10712. (concat s1 s2 (substring rtn (length s2))
  10713. (if (and org-add-colon-after-tag-completion
  10714. (assoc rtn ctable))
  10715. ":" ""))))
  10716. rtn)
  10717. ((eq flag t)
  10718. ;; all-completions
  10719. (all-completions s2 ctable confirm)
  10720. )
  10721. ((eq flag 'lambda)
  10722. ;; exact match?
  10723. (assoc s2 ctable)))
  10724. ))
  10725. (defun org-fast-tag-insert (kwd tags face &optional end)
  10726. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10727. (insert (format "%-12s" (concat kwd ":"))
  10728. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10729. (or end "")))
  10730. (defun org-fast-tag-show-exit (flag)
  10731. (save-excursion
  10732. (org-goto-line 3)
  10733. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10734. (replace-match ""))
  10735. (when flag
  10736. (end-of-line 1)
  10737. (org-move-to-column (- (window-width) 19) t)
  10738. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10739. (defun org-set-current-tags-overlay (current prefix)
  10740. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10741. (if (featurep 'xemacs)
  10742. (org-overlay-display org-tags-overlay (concat prefix s)
  10743. 'secondary-selection)
  10744. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10745. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10746. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10747. "Fast tag selection with single keys.
  10748. CURRENT is the current list of tags in the headline, INHERITED is the
  10749. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10750. possibly with grouping information. TODO-TABLE is a similar table with
  10751. TODO keywords, should these have keys assigned to them.
  10752. If the keys are nil, a-z are automatically assigned.
  10753. Returns the new tags string, or nil to not change the current settings."
  10754. (let* ((fulltable (append table todo-table))
  10755. (maxlen (apply 'max (mapcar
  10756. (lambda (x)
  10757. (if (stringp (car x)) (string-width (car x)) 0))
  10758. fulltable)))
  10759. (buf (current-buffer))
  10760. (expert (eq org-fast-tag-selection-single-key 'expert))
  10761. (buffer-tags nil)
  10762. (fwidth (+ maxlen 3 1 3))
  10763. (ncol (/ (- (window-width) 4) fwidth))
  10764. (i-face 'org-done)
  10765. (c-face 'org-todo)
  10766. tg cnt e c char c1 c2 ntable tbl rtn
  10767. ov-start ov-end ov-prefix
  10768. (exit-after-next org-fast-tag-selection-single-key)
  10769. (done-keywords org-done-keywords)
  10770. groups ingroup)
  10771. (save-excursion
  10772. (beginning-of-line 1)
  10773. (if (looking-at
  10774. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10775. (setq ov-start (match-beginning 1)
  10776. ov-end (match-end 1)
  10777. ov-prefix "")
  10778. (setq ov-start (1- (point-at-eol))
  10779. ov-end (1+ ov-start))
  10780. (skip-chars-forward "^\n\r")
  10781. (setq ov-prefix
  10782. (concat
  10783. (buffer-substring (1- (point)) (point))
  10784. (if (> (current-column) org-tags-column)
  10785. " "
  10786. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10787. (org-move-overlay org-tags-overlay ov-start ov-end)
  10788. (save-window-excursion
  10789. (if expert
  10790. (set-buffer (get-buffer-create " *Org tags*"))
  10791. (delete-other-windows)
  10792. (split-window-vertically)
  10793. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10794. (erase-buffer)
  10795. (org-set-local 'org-done-keywords done-keywords)
  10796. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10797. (org-fast-tag-insert "Current" current c-face "\n\n")
  10798. (org-fast-tag-show-exit exit-after-next)
  10799. (org-set-current-tags-overlay current ov-prefix)
  10800. (setq tbl fulltable char ?a cnt 0)
  10801. (while (setq e (pop tbl))
  10802. (cond
  10803. ((equal (car e) :startgroup)
  10804. (push '() groups) (setq ingroup t)
  10805. (when (not (= cnt 0))
  10806. (setq cnt 0)
  10807. (insert "\n"))
  10808. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  10809. ((equal (car e) :endgroup)
  10810. (setq ingroup nil cnt 0)
  10811. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  10812. ((equal e '(:newline))
  10813. (when (not (= cnt 0))
  10814. (setq cnt 0)
  10815. (insert "\n")
  10816. (setq e (car tbl))
  10817. (while (equal (car tbl) '(:newline))
  10818. (insert "\n")
  10819. (setq tbl (cdr tbl)))))
  10820. (t
  10821. (setq tg (copy-sequence (car e)) c2 nil)
  10822. (if (cdr e)
  10823. (setq c (cdr e))
  10824. ;; automatically assign a character.
  10825. (setq c1 (string-to-char
  10826. (downcase (substring
  10827. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10828. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10829. (while (or (rassoc char ntable) (rassoc char table))
  10830. (setq char (1+ char)))
  10831. (setq c2 c1))
  10832. (setq c (or c2 char)))
  10833. (if ingroup (push tg (car groups)))
  10834. (setq tg (org-add-props tg nil 'face
  10835. (cond
  10836. ((not (assoc tg table))
  10837. (org-get-todo-face tg))
  10838. ((member tg current) c-face)
  10839. ((member tg inherited) i-face)
  10840. (t nil))))
  10841. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10842. (insert "[" c "] " tg (make-string
  10843. (- fwidth 4 (length tg)) ?\ ))
  10844. (push (cons tg c) ntable)
  10845. (when (= (setq cnt (1+ cnt)) ncol)
  10846. (insert "\n")
  10847. (if ingroup (insert " "))
  10848. (setq cnt 0)))))
  10849. (setq ntable (nreverse ntable))
  10850. (insert "\n")
  10851. (goto-char (point-min))
  10852. (if (not expert) (org-fit-window-to-buffer))
  10853. (setq rtn
  10854. (catch 'exit
  10855. (while t
  10856. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  10857. (if (not groups) "no " "")
  10858. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10859. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10860. (cond
  10861. ((= c ?\r) (throw 'exit t))
  10862. ((= c ?!)
  10863. (setq groups (not groups))
  10864. (goto-char (point-min))
  10865. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10866. ((= c ?\C-c)
  10867. (if (not expert)
  10868. (org-fast-tag-show-exit
  10869. (setq exit-after-next (not exit-after-next)))
  10870. (setq expert nil)
  10871. (delete-other-windows)
  10872. (split-window-vertically)
  10873. (org-switch-to-buffer-other-window " *Org tags*")
  10874. (org-fit-window-to-buffer)))
  10875. ((or (= c ?\C-g)
  10876. (and (= c ?q) (not (rassoc c ntable))))
  10877. (org-detach-overlay org-tags-overlay)
  10878. (setq quit-flag t))
  10879. ((= c ?\ )
  10880. (setq current nil)
  10881. (if exit-after-next (setq exit-after-next 'now)))
  10882. ((= c ?\t)
  10883. (condition-case nil
  10884. (setq tg (org-icompleting-read
  10885. "Tag: "
  10886. (or buffer-tags
  10887. (with-current-buffer buf
  10888. (org-get-buffer-tags)))))
  10889. (quit (setq tg "")))
  10890. (when (string-match "\\S-" tg)
  10891. (add-to-list 'buffer-tags (list tg))
  10892. (if (member tg current)
  10893. (setq current (delete tg current))
  10894. (push tg current)))
  10895. (if exit-after-next (setq exit-after-next 'now)))
  10896. ((setq e (rassoc c todo-table) tg (car e))
  10897. (with-current-buffer buf
  10898. (save-excursion (org-todo tg)))
  10899. (if exit-after-next (setq exit-after-next 'now)))
  10900. ((setq e (rassoc c ntable) tg (car e))
  10901. (if (member tg current)
  10902. (setq current (delete tg current))
  10903. (loop for g in groups do
  10904. (if (member tg g)
  10905. (mapc (lambda (x)
  10906. (setq current (delete x current)))
  10907. g)))
  10908. (push tg current))
  10909. (if exit-after-next (setq exit-after-next 'now))))
  10910. ;; Create a sorted list
  10911. (setq current
  10912. (sort current
  10913. (lambda (a b)
  10914. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10915. (if (eq exit-after-next 'now) (throw 'exit t))
  10916. (goto-char (point-min))
  10917. (beginning-of-line 2)
  10918. (delete-region (point) (point-at-eol))
  10919. (org-fast-tag-insert "Current" current c-face)
  10920. (org-set-current-tags-overlay current ov-prefix)
  10921. (while (re-search-forward
  10922. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10923. (setq tg (match-string 1))
  10924. (add-text-properties
  10925. (match-beginning 1) (match-end 1)
  10926. (list 'face
  10927. (cond
  10928. ((member tg current) c-face)
  10929. ((member tg inherited) i-face)
  10930. (t (get-text-property (match-beginning 1) 'face))))))
  10931. (goto-char (point-min)))))
  10932. (org-detach-overlay org-tags-overlay)
  10933. (if rtn
  10934. (mapconcat 'identity current ":")
  10935. nil))))
  10936. (defun org-get-tags-string ()
  10937. "Get the TAGS string in the current headline."
  10938. (unless (org-on-heading-p t)
  10939. (error "Not on a heading"))
  10940. (save-excursion
  10941. (beginning-of-line 1)
  10942. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10943. (org-match-string-no-properties 1)
  10944. "")))
  10945. (defun org-get-tags ()
  10946. "Get the list of tags specified in the current headline."
  10947. (org-split-string (org-get-tags-string) ":"))
  10948. (defun org-get-buffer-tags ()
  10949. "Get a table of all tags used in the buffer, for completion."
  10950. (let (tags)
  10951. (save-excursion
  10952. (goto-char (point-min))
  10953. (while (re-search-forward
  10954. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10955. (when (equal (char-after (point-at-bol 0)) ?*)
  10956. (mapc (lambda (x) (add-to-list 'tags x))
  10957. (org-split-string (org-match-string-no-properties 1) ":")))))
  10958. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  10959. (mapcar 'list tags)))
  10960. ;;;; The mapping API
  10961. ;;;###autoload
  10962. (defun org-map-entries (func &optional match scope &rest skip)
  10963. "Call FUNC at each headline selected by MATCH in SCOPE.
  10964. FUNC is a function or a lisp form. The function will be called without
  10965. arguments, with the cursor positioned at the beginning of the headline.
  10966. The return values of all calls to the function will be collected and
  10967. returned as a list.
  10968. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10969. does not need to preserve point. After evaluation, the cursor will be
  10970. moved to the end of the line (presumably of the headline of the
  10971. processed entry) and search continues from there. Under some
  10972. circumstances, this may not produce the wanted results. For example,
  10973. if you have removed (e.g. archived) the current (sub)tree it could
  10974. mean that the next entry will be skipped entirely. In such cases, you
  10975. can specify the position from where search should continue by making
  10976. FUNC set the variable `org-map-continue-from' to the desired buffer
  10977. position.
  10978. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10979. Only headlines that are matched by this query will be considered during
  10980. the iteration. When MATCH is nil or t, all headlines will be
  10981. visited by the iteration.
  10982. SCOPE determines the scope of this command. It can be any of:
  10983. nil The current buffer, respecting the restriction if any
  10984. tree The subtree started with the entry at point
  10985. file The current buffer, without restriction
  10986. file-with-archives
  10987. The current buffer, and any archives associated with it
  10988. agenda All agenda files
  10989. agenda-with-archives
  10990. All agenda files with any archive files associated with them
  10991. \(file1 file2 ...)
  10992. If this is a list, all files in the list will be scanned
  10993. The remaining args are treated as settings for the skipping facilities of
  10994. the scanner. The following items can be given here:
  10995. archive skip trees with the archive tag.
  10996. comment skip trees with the COMMENT keyword
  10997. function or Emacs Lisp form:
  10998. will be used as value for `org-agenda-skip-function', so whenever
  10999. the function returns t, FUNC will not be called for that
  11000. entry and search will continue from the point where the
  11001. function leaves it.
  11002. If your function needs to retrieve the tags including inherited tags
  11003. at the *current* entry, you can use the value of the variable
  11004. `org-scanner-tags' which will be much faster than getting the value
  11005. with `org-get-tags-at'. If your function gets properties with
  11006. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11007. to t around the call to `org-entry-properties' to get the same speedup.
  11008. Note that if your function moves around to retrieve tags and properties at
  11009. a *different* entry, you cannot use these techniques."
  11010. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11011. (org-agenda-skip-archived-trees (memq 'archive skip))
  11012. (org-agenda-skip-comment-trees (memq 'comment skip))
  11013. (org-agenda-skip-function
  11014. (car (org-delete-all '(comment archive) skip)))
  11015. (org-tags-match-list-sublevels t)
  11016. matcher file res
  11017. org-todo-keywords-for-agenda
  11018. org-done-keywords-for-agenda
  11019. org-todo-keyword-alist-for-agenda
  11020. org-drawers-for-agenda
  11021. org-tag-alist-for-agenda)
  11022. (cond
  11023. ((eq match t) (setq matcher t))
  11024. ((eq match nil) (setq matcher t))
  11025. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11026. (save-excursion
  11027. (save-restriction
  11028. (when (eq scope 'tree)
  11029. (org-back-to-heading t)
  11030. (org-narrow-to-subtree)
  11031. (setq scope nil))
  11032. (if (not scope)
  11033. (progn
  11034. (org-prepare-agenda-buffers
  11035. (list (buffer-file-name (current-buffer))))
  11036. (setq res (org-scan-tags func matcher)))
  11037. ;; Get the right scope
  11038. (cond
  11039. ((and scope (listp scope) (symbolp (car scope)))
  11040. (setq scope (eval scope)))
  11041. ((eq scope 'agenda)
  11042. (setq scope (org-agenda-files t)))
  11043. ((eq scope 'agenda-with-archives)
  11044. (setq scope (org-agenda-files t))
  11045. (setq scope (org-add-archive-files scope)))
  11046. ((eq scope 'file)
  11047. (setq scope (list (buffer-file-name))))
  11048. ((eq scope 'file-with-archives)
  11049. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11050. (org-prepare-agenda-buffers scope)
  11051. (while (setq file (pop scope))
  11052. (with-current-buffer (org-find-base-buffer-visiting file)
  11053. (save-excursion
  11054. (save-restriction
  11055. (widen)
  11056. (goto-char (point-min))
  11057. (setq res (append res (org-scan-tags func matcher))))))))))
  11058. res))
  11059. ;;;; Properties
  11060. ;;; Setting and retrieving properties
  11061. (defconst org-special-properties
  11062. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11063. "TIMESTAMP" "TIMESTAMP_IA")
  11064. "The special properties valid in Org-mode.
  11065. These are properties that are not defined in the property drawer,
  11066. but in some other way.")
  11067. (defconst org-default-properties
  11068. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11069. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11070. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11071. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11072. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  11073. "CLOCK_MODELINE_TOTAL" "STYLE")
  11074. "Some properties that are used by Org-mode for various purposes.
  11075. Being in this list makes sure that they are offered for completion.")
  11076. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11077. "Regular expression matching the first line of a property drawer.")
  11078. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11079. "Regular expression matching the first line of a property drawer.")
  11080. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11081. "Regular expression matching the first line of a property drawer.")
  11082. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11083. "Regular expression matching the first line of a property drawer.")
  11084. (defconst org-property-drawer-re
  11085. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11086. org-property-end-re "\\)\n?")
  11087. "Matches an entire property drawer.")
  11088. (defconst org-clock-drawer-re
  11089. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11090. org-property-end-re "\\)\n?")
  11091. "Matches an entire clock drawer.")
  11092. (defun org-property-action ()
  11093. "Do an action on properties."
  11094. (interactive)
  11095. (let (c)
  11096. (org-at-property-p)
  11097. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11098. (setq c (read-char-exclusive))
  11099. (cond
  11100. ((equal c ?s)
  11101. (call-interactively 'org-set-property))
  11102. ((equal c ?d)
  11103. (call-interactively 'org-delete-property))
  11104. ((equal c ?D)
  11105. (call-interactively 'org-delete-property-globally))
  11106. ((equal c ?c)
  11107. (call-interactively 'org-compute-property-at-point))
  11108. (t (error "No such property action %c" c)))))
  11109. (defun org-set-effort (&optional value)
  11110. "Set the effort property of the current entry.
  11111. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11112. allowed value."
  11113. (interactive "P")
  11114. (if (equal value 0) (setq value 10))
  11115. (let* ((completion-ignore-case t)
  11116. (prop org-effort-property)
  11117. (cur (org-entry-get nil prop))
  11118. (allowed (org-property-get-allowed-values nil prop 'table))
  11119. (existing (mapcar 'list (org-property-values prop)))
  11120. rpl
  11121. (val (cond
  11122. ((stringp value) value)
  11123. ((and allowed (integerp value))
  11124. (or (car (nth (1- value) allowed))
  11125. (car (org-last allowed))))
  11126. (allowed
  11127. (message "Select 1-9,0, [RET%s]: %s"
  11128. (if cur (concat "=" cur) "")
  11129. (mapconcat 'car allowed " "))
  11130. (setq rpl (read-char-exclusive))
  11131. (if (equal rpl ?\r)
  11132. cur
  11133. (setq rpl (- rpl ?0))
  11134. (if (equal rpl 0) (setq rpl 10))
  11135. (if (and (> rpl 0) (<= rpl (length allowed)))
  11136. (car (nth (1- rpl) allowed))
  11137. (org-completing-read "Value: " allowed nil))))
  11138. (t
  11139. (let (org-completion-use-ido org-completion-use-iswitchb)
  11140. (org-completing-read
  11141. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11142. (concat "[" cur "]") "")
  11143. ": ")
  11144. existing nil nil "" nil cur))))))
  11145. (unless (equal (org-entry-get nil prop) val)
  11146. (org-entry-put nil prop val))
  11147. (message "%s is now %s" prop val)))
  11148. (defun org-at-property-p ()
  11149. "Is the cursor in a property line?"
  11150. ;; FIXME: Does not check if we are actually in the drawer.
  11151. ;; FIXME: also returns true on any drawers.....
  11152. ;; This is used by C-c C-c for property action.
  11153. (save-excursion
  11154. (beginning-of-line 1)
  11155. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  11156. (defun org-get-property-block (&optional beg end force)
  11157. "Return the (beg . end) range of the body of the property drawer.
  11158. BEG and END can be beginning and end of subtree, if not given
  11159. they will be found.
  11160. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11161. (catch 'exit
  11162. (save-excursion
  11163. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11164. (end (or end (progn (outline-next-heading) (point)))))
  11165. (goto-char beg)
  11166. (if (re-search-forward org-property-start-re end t)
  11167. (setq beg (1+ (match-end 0)))
  11168. (if force
  11169. (save-excursion
  11170. (org-insert-property-drawer)
  11171. (setq end (progn (outline-next-heading) (point))))
  11172. (throw 'exit nil))
  11173. (goto-char beg)
  11174. (if (re-search-forward org-property-start-re end t)
  11175. (setq beg (1+ (match-end 0)))))
  11176. (if (re-search-forward org-property-end-re end t)
  11177. (setq end (match-beginning 0))
  11178. (or force (throw 'exit nil))
  11179. (goto-char beg)
  11180. (setq end beg)
  11181. (org-indent-line-function)
  11182. (insert ":END:\n"))
  11183. (cons beg end)))))
  11184. (defun org-entry-properties (&optional pom which)
  11185. "Get all properties of the entry at point-or-marker POM.
  11186. This includes the TODO keyword, the tags, time strings for deadline,
  11187. scheduled, and clocking, and any additional properties defined in the
  11188. entry. The return value is an alist, keys may occur multiple times
  11189. if the property key was used several times.
  11190. POM may also be nil, in which case the current entry is used.
  11191. If WHICH is nil or `all', get all properties. If WHICH is
  11192. `special' or `standard', only get that subclass."
  11193. (setq which (or which 'all))
  11194. (org-with-point-at pom
  11195. (let ((clockstr (substring org-clock-string 0 -1))
  11196. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  11197. beg end range props sum-props key value string clocksum)
  11198. (save-excursion
  11199. (when (condition-case nil
  11200. (and (org-mode-p) (org-back-to-heading t))
  11201. (error nil))
  11202. (setq beg (point))
  11203. (setq sum-props (get-text-property (point) 'org-summaries))
  11204. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11205. (outline-next-heading)
  11206. (setq end (point))
  11207. (when (memq which '(all special))
  11208. ;; Get the special properties, like TODO and tags
  11209. (goto-char beg)
  11210. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  11211. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11212. (when (looking-at org-priority-regexp)
  11213. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11214. (when (and (setq value (org-get-tags-string))
  11215. (string-match "\\S-" value))
  11216. (push (cons "TAGS" value) props))
  11217. (when (setq value (org-get-tags-at))
  11218. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  11219. props))
  11220. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11221. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11222. string (if (equal key clockstr)
  11223. (org-no-properties
  11224. (org-trim
  11225. (buffer-substring
  11226. (match-beginning 3) (goto-char (point-at-eol)))))
  11227. (substring (org-match-string-no-properties 3) 1 -1)))
  11228. (unless key
  11229. (if (= (char-after (match-beginning 3)) ?\[)
  11230. (setq key "TIMESTAMP_IA")
  11231. (setq key "TIMESTAMP")))
  11232. (when (or (equal key clockstr) (not (assoc key props)))
  11233. (push (cons key string) props)))
  11234. )
  11235. (when (memq which '(all standard))
  11236. ;; Get the standard properties, like :PROP: ...
  11237. (setq range (org-get-property-block beg end))
  11238. (when range
  11239. (goto-char (car range))
  11240. (while (re-search-forward
  11241. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11242. (cdr range) t)
  11243. (setq key (org-match-string-no-properties 1)
  11244. value (org-trim (or (org-match-string-no-properties 2) "")))
  11245. (unless (member key excluded)
  11246. (push (cons key (or value "")) props)))))
  11247. (if clocksum
  11248. (push (cons "CLOCKSUM"
  11249. (org-columns-number-to-string (/ (float clocksum) 60.)
  11250. 'add_times))
  11251. props))
  11252. (unless (assoc "CATEGORY" props)
  11253. (setq value (or (org-get-category)
  11254. (progn (org-refresh-category-properties)
  11255. (org-get-category))))
  11256. (push (cons "CATEGORY" value) props))
  11257. (append sum-props (nreverse props)))))))
  11258. (defun org-entry-get (pom property &optional inherit)
  11259. "Get value of PROPERTY for entry at point-or-marker POM.
  11260. If INHERIT is non-nil and the entry does not have the property,
  11261. then also check higher levels of the hierarchy.
  11262. If INHERIT is the symbol `selective', use inheritance only if the setting
  11263. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11264. If the property is present but empty, the return value is the empty string.
  11265. If the property is not present at all, nil is returned."
  11266. (org-with-point-at pom
  11267. (if (and inherit (if (eq inherit 'selective)
  11268. (org-property-inherit-p property)
  11269. t))
  11270. (org-entry-get-with-inheritance property)
  11271. (if (member property org-special-properties)
  11272. ;; We need a special property. Use brute force, get all properties.
  11273. (cdr (assoc property (org-entry-properties nil 'special)))
  11274. (let ((range (org-get-property-block)))
  11275. (if (and range
  11276. (goto-char (car range))
  11277. (re-search-forward
  11278. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11279. (cdr range) t))
  11280. ;; Found the property, return it.
  11281. (if (match-end 1)
  11282. (org-match-string-no-properties 1)
  11283. "")))))))
  11284. (defun org-property-or-variable-value (var &optional inherit)
  11285. "Check if there is a property fixing the value of VAR.
  11286. If yes, return this value. If not, return the current value of the variable."
  11287. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11288. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11289. (read prop)
  11290. (symbol-value var))))
  11291. (defun org-entry-delete (pom property)
  11292. "Delete the property PROPERTY from entry at point-or-marker POM."
  11293. (org-with-point-at pom
  11294. (if (member property org-special-properties)
  11295. nil ; cannot delete these properties.
  11296. (let ((range (org-get-property-block)))
  11297. (if (and range
  11298. (goto-char (car range))
  11299. (re-search-forward
  11300. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11301. (cdr range) t))
  11302. (progn
  11303. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11304. t)
  11305. nil)))))
  11306. ;; Multi-values properties are properties that contain multiple values
  11307. ;; These values are assumed to be single words, separated by whitespace.
  11308. (defun org-entry-add-to-multivalued-property (pom property value)
  11309. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11310. (let* ((old (org-entry-get pom property))
  11311. (values (and old (org-split-string old "[ \t]"))))
  11312. (setq value (org-entry-protect-space value))
  11313. (unless (member value values)
  11314. (setq values (cons value values))
  11315. (org-entry-put pom property
  11316. (mapconcat 'identity values " ")))))
  11317. (defun org-entry-remove-from-multivalued-property (pom property value)
  11318. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11319. (let* ((old (org-entry-get pom property))
  11320. (values (and old (org-split-string old "[ \t]"))))
  11321. (setq value (org-entry-protect-space value))
  11322. (when (member value values)
  11323. (setq values (delete value values))
  11324. (org-entry-put pom property
  11325. (mapconcat 'identity values " ")))))
  11326. (defun org-entry-member-in-multivalued-property (pom property value)
  11327. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11328. (let* ((old (org-entry-get pom property))
  11329. (values (and old (org-split-string old "[ \t]"))))
  11330. (setq value (org-entry-protect-space value))
  11331. (member value values)))
  11332. (defun org-entry-get-multivalued-property (pom property)
  11333. "Return a list of values in a multivalued property."
  11334. (let* ((value (org-entry-get pom property))
  11335. (values (and value (org-split-string value "[ \t]"))))
  11336. (mapcar 'org-entry-restore-space values)))
  11337. (defun org-entry-put-multivalued-property (pom property &rest values)
  11338. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11339. VALUES should be a list of strings. Spaces will be protected."
  11340. (org-entry-put pom property
  11341. (mapconcat 'org-entry-protect-space values " "))
  11342. (let* ((value (org-entry-get pom property))
  11343. (values (and value (org-split-string value "[ \t]"))))
  11344. (mapcar 'org-entry-restore-space values)))
  11345. (defun org-entry-protect-space (s)
  11346. "Protect spaces and newline in string S."
  11347. (while (string-match " " s)
  11348. (setq s (replace-match "%20" t t s)))
  11349. (while (string-match "\n" s)
  11350. (setq s (replace-match "%0A" t t s)))
  11351. s)
  11352. (defun org-entry-restore-space (s)
  11353. "Restore spaces and newline in string S."
  11354. (while (string-match "%20" s)
  11355. (setq s (replace-match " " t t s)))
  11356. (while (string-match "%0A" s)
  11357. (setq s (replace-match "\n" t t s)))
  11358. s)
  11359. (defvar org-entry-property-inherited-from (make-marker)
  11360. "Marker pointing to the entry from where a property was inherited.
  11361. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11362. location of the entry where the inheritance search matched. If there was
  11363. no match, the marker will point nowhere.
  11364. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11365. is set.")
  11366. (defun org-entry-get-with-inheritance (property)
  11367. "Get entry property, and search higher levels if not present."
  11368. (move-marker org-entry-property-inherited-from nil)
  11369. (let (tmp)
  11370. (save-excursion
  11371. (save-restriction
  11372. (widen)
  11373. (catch 'ex
  11374. (while t
  11375. (when (setq tmp (org-entry-get nil property))
  11376. (org-back-to-heading t)
  11377. (move-marker org-entry-property-inherited-from (point))
  11378. (throw 'ex tmp))
  11379. (or (org-up-heading-safe) (throw 'ex nil)))))
  11380. (or tmp
  11381. (cdr (assoc property org-file-properties))
  11382. (cdr (assoc property org-global-properties))
  11383. (cdr (assoc property org-global-properties-fixed))))))
  11384. (defun org-entry-put (pom property value)
  11385. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11386. (org-with-point-at pom
  11387. (org-back-to-heading t)
  11388. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11389. range)
  11390. (cond
  11391. ((equal property "TODO")
  11392. (when (and (stringp value) (string-match "\\S-" value)
  11393. (not (member value org-todo-keywords-1)))
  11394. (error "\"%s\" is not a valid TODO state" value))
  11395. (if (or (not value)
  11396. (not (string-match "\\S-" value)))
  11397. (setq value 'none))
  11398. (org-todo value)
  11399. (org-set-tags nil 'align))
  11400. ((equal property "PRIORITY")
  11401. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11402. (string-to-char value) ?\ ))
  11403. (org-set-tags nil 'align))
  11404. ((equal property "SCHEDULED")
  11405. (if (re-search-forward org-scheduled-time-regexp end t)
  11406. (cond
  11407. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11408. ((eq value 'later) (org-timestamp-change 1 'day))
  11409. (t (call-interactively 'org-schedule)))
  11410. (call-interactively 'org-schedule)))
  11411. ((equal property "DEADLINE")
  11412. (if (re-search-forward org-deadline-time-regexp end t)
  11413. (cond
  11414. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11415. ((eq value 'later) (org-timestamp-change 1 'day))
  11416. (t (call-interactively 'org-deadline)))
  11417. (call-interactively 'org-deadline)))
  11418. ((member property org-special-properties)
  11419. (error "The %s property can not yet be set with `org-entry-put'"
  11420. property))
  11421. (t ; a non-special property
  11422. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11423. (setq range (org-get-property-block beg end 'force))
  11424. (goto-char (car range))
  11425. (if (re-search-forward
  11426. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11427. (progn
  11428. (delete-region (match-beginning 1) (match-end 1))
  11429. (goto-char (match-beginning 1)))
  11430. (goto-char (cdr range))
  11431. (insert "\n")
  11432. (backward-char 1)
  11433. (org-indent-line-function)
  11434. (insert ":" property ":"))
  11435. (and value (insert " " value))
  11436. (org-indent-line-function)))))))
  11437. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11438. "Get all property keys in the current buffer.
  11439. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11440. like tags and TODO state.
  11441. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11442. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11443. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11444. formats in the current buffer."
  11445. (let (rtn range cfmt s p)
  11446. (save-excursion
  11447. (save-restriction
  11448. (widen)
  11449. (goto-char (point-min))
  11450. (while (re-search-forward org-property-start-re nil t)
  11451. (setq range (org-get-property-block))
  11452. (goto-char (car range))
  11453. (while (re-search-forward
  11454. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11455. (cdr range) t)
  11456. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11457. (outline-next-heading))))
  11458. (when include-specials
  11459. (setq rtn (append org-special-properties rtn)))
  11460. (when include-defaults
  11461. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11462. (add-to-list 'rtn org-effort-property))
  11463. (when include-columns
  11464. (save-excursion
  11465. (save-restriction
  11466. (widen)
  11467. (goto-char (point-min))
  11468. (while (re-search-forward
  11469. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11470. nil t)
  11471. (setq cfmt (match-string 2) s 0)
  11472. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11473. cfmt s)
  11474. (setq s (match-end 0)
  11475. p (match-string 1 cfmt))
  11476. (unless (or (equal p "ITEM")
  11477. (member p org-special-properties))
  11478. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11479. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11480. (defun org-property-values (key)
  11481. "Return a list of all values of property KEY."
  11482. (save-excursion
  11483. (save-restriction
  11484. (widen)
  11485. (goto-char (point-min))
  11486. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11487. values)
  11488. (while (re-search-forward re nil t)
  11489. (add-to-list 'values (org-trim (match-string 1))))
  11490. (delete "" values)))))
  11491. (defun org-insert-property-drawer ()
  11492. "Insert a property drawer into the current entry."
  11493. (interactive)
  11494. (org-back-to-heading t)
  11495. (looking-at outline-regexp)
  11496. (let ((indent (if org-adapt-indentation
  11497. (- (match-end 0)(match-beginning 0))
  11498. 0))
  11499. (beg (point))
  11500. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11501. end hiddenp)
  11502. (outline-next-heading)
  11503. (setq end (point))
  11504. (goto-char beg)
  11505. (while (re-search-forward re end t))
  11506. (setq hiddenp (org-invisible-p))
  11507. (end-of-line 1)
  11508. (and (equal (char-after) ?\n) (forward-char 1))
  11509. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11510. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11511. ;; just skip this line
  11512. (beginning-of-line 2)
  11513. ;; Drawer start, find the end
  11514. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11515. (beginning-of-line 1)))
  11516. (org-skip-over-state-notes)
  11517. (skip-chars-backward " \t\n\r")
  11518. (if (eq (char-before) ?*) (forward-char 1))
  11519. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11520. (beginning-of-line 0)
  11521. (org-indent-to-column indent)
  11522. (beginning-of-line 2)
  11523. (org-indent-to-column indent)
  11524. (beginning-of-line 0)
  11525. (if hiddenp
  11526. (save-excursion
  11527. (org-back-to-heading t)
  11528. (hide-entry))
  11529. (org-flag-drawer t))))
  11530. (defun org-set-property (property value)
  11531. "In the current entry, set PROPERTY to VALUE.
  11532. When called interactively, this will prompt for a property name, offering
  11533. completion on existing and default properties. And then it will prompt
  11534. for a value, offering completion either on allowed values (via an inherited
  11535. xxx_ALL property) or on existing values in other instances of this property
  11536. in the current file."
  11537. (interactive
  11538. (let* ((completion-ignore-case t)
  11539. (keys (org-buffer-property-keys nil t t))
  11540. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11541. (prop (if (member prop0 keys)
  11542. prop0
  11543. (or (cdr (assoc (downcase prop0)
  11544. (mapcar (lambda (x) (cons (downcase x) x))
  11545. keys)))
  11546. prop0)))
  11547. (cur (org-entry-get nil prop))
  11548. (allowed (org-property-get-allowed-values nil prop 'table))
  11549. (existing (mapcar 'list (org-property-values prop)))
  11550. (val (if allowed
  11551. (org-completing-read "Value: " allowed nil 'req-match)
  11552. (let (org-completion-use-ido org-completion-use-iswitchb)
  11553. (org-completing-read
  11554. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11555. (concat "[" cur "]") "")
  11556. ": ")
  11557. existing nil nil "" nil cur)))))
  11558. (list prop (if (equal val "") cur val))))
  11559. (unless (equal (org-entry-get nil property) value)
  11560. (org-entry-put nil property value)))
  11561. (defun org-delete-property (property)
  11562. "In the current entry, delete PROPERTY."
  11563. (interactive
  11564. (let* ((completion-ignore-case t)
  11565. (prop (org-icompleting-read
  11566. "Property: " (org-entry-properties nil 'standard))))
  11567. (list prop)))
  11568. (message "Property %s %s" property
  11569. (if (org-entry-delete nil property)
  11570. "deleted"
  11571. "was not present in the entry")))
  11572. (defun org-delete-property-globally (property)
  11573. "Remove PROPERTY globally, from all entries."
  11574. (interactive
  11575. (let* ((completion-ignore-case t)
  11576. (prop (org-icompleting-read
  11577. "Globally remove property: "
  11578. (mapcar 'list (org-buffer-property-keys)))))
  11579. (list prop)))
  11580. (save-excursion
  11581. (save-restriction
  11582. (widen)
  11583. (goto-char (point-min))
  11584. (let ((cnt 0))
  11585. (while (re-search-forward
  11586. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11587. nil t)
  11588. (setq cnt (1+ cnt))
  11589. (replace-match ""))
  11590. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11591. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11592. (defun org-compute-property-at-point ()
  11593. "Compute the property at point.
  11594. This looks for an enclosing column format, extracts the operator and
  11595. then applies it to the property in the column format's scope."
  11596. (interactive)
  11597. (unless (org-at-property-p)
  11598. (error "Not at a property"))
  11599. (let ((prop (org-match-string-no-properties 2)))
  11600. (org-columns-get-format-and-top-level)
  11601. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11602. (error "No operator defined for property %s" prop))
  11603. (org-columns-compute prop)))
  11604. (defun org-property-get-allowed-values (pom property &optional table)
  11605. "Get allowed values for the property PROPERTY.
  11606. When TABLE is non-nil, return an alist that can directly be used for
  11607. completion."
  11608. (let (vals)
  11609. (cond
  11610. ((equal property "TODO")
  11611. (setq vals (org-with-point-at pom
  11612. (append org-todo-keywords-1 '("")))))
  11613. ((equal property "PRIORITY")
  11614. (let ((n org-lowest-priority))
  11615. (while (>= n org-highest-priority)
  11616. (push (char-to-string n) vals)
  11617. (setq n (1- n)))))
  11618. ((member property org-special-properties))
  11619. (t
  11620. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11621. (when (and vals (string-match "\\S-" vals))
  11622. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11623. (setq vals (mapcar (lambda (x)
  11624. (cond ((stringp x) x)
  11625. ((numberp x) (number-to-string x))
  11626. ((symbolp x) (symbol-name x))
  11627. (t "???")))
  11628. vals)))))
  11629. (if table (mapcar 'list vals) vals)))
  11630. (defun org-property-previous-allowed-value (&optional previous)
  11631. "Switch to the next allowed value for this property."
  11632. (interactive)
  11633. (org-property-next-allowed-value t))
  11634. (defun org-property-next-allowed-value (&optional previous)
  11635. "Switch to the next allowed value for this property."
  11636. (interactive)
  11637. (unless (org-at-property-p)
  11638. (error "Not at a property"))
  11639. (let* ((key (match-string 2))
  11640. (value (match-string 3))
  11641. (allowed (or (org-property-get-allowed-values (point) key)
  11642. (and (member value '("[ ]" "[-]" "[X]"))
  11643. '("[ ]" "[X]"))))
  11644. nval)
  11645. (unless allowed
  11646. (error "Allowed values for this property have not been defined"))
  11647. (if previous (setq allowed (reverse allowed)))
  11648. (if (member value allowed)
  11649. (setq nval (car (cdr (member value allowed)))))
  11650. (setq nval (or nval (car allowed)))
  11651. (if (equal nval value)
  11652. (error "Only one allowed value for this property"))
  11653. (org-at-property-p)
  11654. (replace-match (concat " :" key ": " nval) t t)
  11655. (org-indent-line-function)
  11656. (beginning-of-line 1)
  11657. (skip-chars-forward " \t")))
  11658. (defun org-find-entry-with-id (ident)
  11659. "Locate the entry that contains the ID property with exact value IDENT.
  11660. IDENT can be a string, a symbol or a number, this function will search for
  11661. the string representation of it.
  11662. Return the position where this entry starts, or nil if there is no such entry."
  11663. (interactive "sID: ")
  11664. (let ((id (cond
  11665. ((stringp ident) ident)
  11666. ((symbol-name ident) (symbol-name ident))
  11667. ((numberp ident) (number-to-string ident))
  11668. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11669. (case-fold-search nil))
  11670. (save-excursion
  11671. (save-restriction
  11672. (widen)
  11673. (goto-char (point-min))
  11674. (when (re-search-forward
  11675. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11676. nil t)
  11677. (org-back-to-heading t)
  11678. (point))))))
  11679. ;;;; Timestamps
  11680. (defvar org-last-changed-timestamp nil)
  11681. (defvar org-last-inserted-timestamp nil
  11682. "The last time stamp inserted with `org-insert-time-stamp'.")
  11683. (defvar org-time-was-given) ; dynamically scoped parameter
  11684. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11685. (defvar org-ts-what) ; dynamically scoped parameter
  11686. (defun org-time-stamp (arg &optional inactive)
  11687. "Prompt for a date/time and insert a time stamp.
  11688. If the user specifies a time like HH:MM, or if this command is called
  11689. with a prefix argument, the time stamp will contain date and time.
  11690. Otherwise, only the date will be included. All parts of a date not
  11691. specified by the user will be filled in from the current date/time.
  11692. So if you press just return without typing anything, the time stamp
  11693. will represent the current date/time. If there is already a timestamp
  11694. at the cursor, it will be modified."
  11695. (interactive "P")
  11696. (let* ((ts nil)
  11697. (default-time
  11698. ;; Default time is either today, or, when entering a range,
  11699. ;; the range start.
  11700. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11701. (save-excursion
  11702. (re-search-backward
  11703. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11704. (- (point) 20) t)))
  11705. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11706. (current-time)))
  11707. (default-input (and ts (org-get-compact-tod ts)))
  11708. org-time-was-given org-end-time-was-given time)
  11709. (cond
  11710. ((and (org-at-timestamp-p t)
  11711. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11712. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11713. (insert "--")
  11714. (setq time (let ((this-command this-command))
  11715. (org-read-date arg 'totime nil nil
  11716. default-time default-input)))
  11717. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11718. ((org-at-timestamp-p t)
  11719. (setq time (let ((this-command this-command))
  11720. (org-read-date arg 'totime nil nil default-time default-input)))
  11721. (when (org-at-timestamp-p t) ; just to get the match data
  11722. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11723. (replace-match "")
  11724. (setq org-last-changed-timestamp
  11725. (org-insert-time-stamp
  11726. time (or org-time-was-given arg)
  11727. inactive nil nil (list org-end-time-was-given))))
  11728. (message "Timestamp updated"))
  11729. (t
  11730. (setq time (let ((this-command this-command))
  11731. (org-read-date arg 'totime nil nil default-time default-input)))
  11732. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11733. nil nil (list org-end-time-was-given))))))
  11734. ;; FIXME: can we use this for something else, like computing time differences?
  11735. (defun org-get-compact-tod (s)
  11736. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11737. (let* ((t1 (match-string 1 s))
  11738. (h1 (string-to-number (match-string 2 s)))
  11739. (m1 (string-to-number (match-string 3 s)))
  11740. (t2 (and (match-end 4) (match-string 5 s)))
  11741. (h2 (and t2 (string-to-number (match-string 6 s))))
  11742. (m2 (and t2 (string-to-number (match-string 7 s))))
  11743. dh dm)
  11744. (if (not t2)
  11745. t1
  11746. (setq dh (- h2 h1) dm (- m2 m1))
  11747. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11748. (concat t1 "+" (number-to-string dh)
  11749. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11750. (defun org-time-stamp-inactive (&optional arg)
  11751. "Insert an inactive time stamp.
  11752. An inactive time stamp is enclosed in square brackets instead of angle
  11753. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11754. does not link to the calendar and cannot be changed with the S-cursor keys.
  11755. So these are more for recording a certain time/date."
  11756. (interactive "P")
  11757. (org-time-stamp arg 'inactive))
  11758. (defvar org-date-ovl (org-make-overlay 1 1))
  11759. (org-overlay-put org-date-ovl 'face 'org-warning)
  11760. (org-detach-overlay org-date-ovl)
  11761. (defvar org-ans1) ; dynamically scoped parameter
  11762. (defvar org-ans2) ; dynamically scoped parameter
  11763. (defvar org-plain-time-of-day-regexp) ; defined below
  11764. (defvar org-overriding-default-time nil) ; dynamically scoped
  11765. (defvar org-read-date-overlay nil)
  11766. (defvar org-dcst nil) ; dynamically scoped
  11767. (defvar org-read-date-history nil)
  11768. (defvar org-read-date-final-answer nil)
  11769. (defun org-read-date (&optional with-time to-time from-string prompt
  11770. default-time default-input)
  11771. "Read a date, possibly a time, and make things smooth for the user.
  11772. The prompt will suggest to enter an ISO date, but you can also enter anything
  11773. which will at least partially be understood by `parse-time-string'.
  11774. Unrecognized parts of the date will default to the current day, month, year,
  11775. hour and minute. If this command is called to replace a timestamp at point,
  11776. of to enter the second timestamp of a range, the default time is taken from the
  11777. existing stamp. For example,
  11778. 3-2-5 --> 2003-02-05
  11779. feb 15 --> currentyear-02-15
  11780. sep 12 9 --> 2009-09-12
  11781. 12:45 --> today 12:45
  11782. 22 sept 0:34 --> currentyear-09-22 0:34
  11783. 12 --> currentyear-currentmonth-12
  11784. Fri --> nearest Friday (today or later)
  11785. etc.
  11786. Furthermore you can specify a relative date by giving, as the *first* thing
  11787. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11788. change in days weeks, months, years.
  11789. With a single plus or minus, the date is relative to today. With a double
  11790. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11791. +4d --> four days from today
  11792. +4 --> same as above
  11793. +2w --> two weeks from today
  11794. ++5 --> five days from default date
  11795. The function understands only English month and weekday abbreviations,
  11796. but this can be configured with the variables `parse-time-months' and
  11797. `parse-time-weekdays'.
  11798. While prompting, a calendar is popped up - you can also select the
  11799. date with the mouse (button 1). The calendar shows a period of three
  11800. months. To scroll it to other months, use the keys `>' and `<'.
  11801. If you don't like the calendar, turn it off with
  11802. \(setq org-read-date-popup-calendar nil)
  11803. With optional argument TO-TIME, the date will immediately be converted
  11804. to an internal time.
  11805. With an optional argument WITH-TIME, the prompt will suggest to also
  11806. insert a time. Note that when WITH-TIME is not set, you can still
  11807. enter a time, and this function will inform the calling routine about
  11808. this change. The calling routine may then choose to change the format
  11809. used to insert the time stamp into the buffer to include the time.
  11810. With optional argument FROM-STRING, read from this string instead from
  11811. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11812. the time/date that is used for everything that is not specified by the
  11813. user."
  11814. (require 'parse-time)
  11815. (let* ((org-time-stamp-rounding-minutes
  11816. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11817. (org-dcst org-display-custom-times)
  11818. (ct (org-current-time))
  11819. (def (or org-overriding-default-time default-time ct))
  11820. (defdecode (decode-time def))
  11821. (dummy (progn
  11822. (when (< (nth 2 defdecode) org-extend-today-until)
  11823. (setcar (nthcdr 2 defdecode) -1)
  11824. (setcar (nthcdr 1 defdecode) 59)
  11825. (setq def (apply 'encode-time defdecode)
  11826. defdecode (decode-time def)))))
  11827. (calendar-frame-setup nil)
  11828. (calendar-move-hook nil)
  11829. (calendar-view-diary-initially-flag nil)
  11830. (view-diary-entries-initially nil)
  11831. (calendar-view-holidays-initially-flag nil)
  11832. (view-calendar-holidays-initially nil)
  11833. (timestr (format-time-string
  11834. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11835. (prompt (concat (if prompt (concat prompt " ") "")
  11836. (format "Date+time [%s]: " timestr)))
  11837. ans (org-ans0 "") org-ans1 org-ans2 final)
  11838. (cond
  11839. (from-string (setq ans from-string))
  11840. (org-read-date-popup-calendar
  11841. (save-excursion
  11842. (save-window-excursion
  11843. (calendar)
  11844. (calendar-forward-day (- (time-to-days def)
  11845. (calendar-absolute-from-gregorian
  11846. (calendar-current-date))))
  11847. (org-eval-in-calendar nil t)
  11848. (let* ((old-map (current-local-map))
  11849. (map (copy-keymap calendar-mode-map))
  11850. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11851. (org-defkey map (kbd "RET") 'org-calendar-select)
  11852. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11853. 'org-calendar-select-mouse)
  11854. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11855. 'org-calendar-select-mouse)
  11856. (org-defkey minibuffer-local-map [(meta shift left)]
  11857. (lambda () (interactive)
  11858. (org-eval-in-calendar '(calendar-backward-month 1))))
  11859. (org-defkey minibuffer-local-map [(meta shift right)]
  11860. (lambda () (interactive)
  11861. (org-eval-in-calendar '(calendar-forward-month 1))))
  11862. (org-defkey minibuffer-local-map [(meta shift up)]
  11863. (lambda () (interactive)
  11864. (org-eval-in-calendar '(calendar-backward-year 1))))
  11865. (org-defkey minibuffer-local-map [(meta shift down)]
  11866. (lambda () (interactive)
  11867. (org-eval-in-calendar '(calendar-forward-year 1))))
  11868. (org-defkey minibuffer-local-map [?\e (shift left)]
  11869. (lambda () (interactive)
  11870. (org-eval-in-calendar '(calendar-backward-month 1))))
  11871. (org-defkey minibuffer-local-map [?\e (shift right)]
  11872. (lambda () (interactive)
  11873. (org-eval-in-calendar '(calendar-forward-month 1))))
  11874. (org-defkey minibuffer-local-map [?\e (shift up)]
  11875. (lambda () (interactive)
  11876. (org-eval-in-calendar '(calendar-backward-year 1))))
  11877. (org-defkey minibuffer-local-map [?\e (shift down)]
  11878. (lambda () (interactive)
  11879. (org-eval-in-calendar '(calendar-forward-year 1))))
  11880. (org-defkey minibuffer-local-map [(shift up)]
  11881. (lambda () (interactive)
  11882. (org-eval-in-calendar '(calendar-backward-week 1))))
  11883. (org-defkey minibuffer-local-map [(shift down)]
  11884. (lambda () (interactive)
  11885. (org-eval-in-calendar '(calendar-forward-week 1))))
  11886. (org-defkey minibuffer-local-map [(shift left)]
  11887. (lambda () (interactive)
  11888. (org-eval-in-calendar '(calendar-backward-day 1))))
  11889. (org-defkey minibuffer-local-map [(shift right)]
  11890. (lambda () (interactive)
  11891. (org-eval-in-calendar '(calendar-forward-day 1))))
  11892. (org-defkey minibuffer-local-map ">"
  11893. (lambda () (interactive)
  11894. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11895. (org-defkey minibuffer-local-map "<"
  11896. (lambda () (interactive)
  11897. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11898. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11899. (unwind-protect
  11900. (progn
  11901. (use-local-map map)
  11902. (add-hook 'post-command-hook 'org-read-date-display)
  11903. (setq org-ans0 (read-string prompt default-input
  11904. 'org-read-date-history nil))
  11905. ;; org-ans0: from prompt
  11906. ;; org-ans1: from mouse click
  11907. ;; org-ans2: from calendar motion
  11908. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11909. (remove-hook 'post-command-hook 'org-read-date-display)
  11910. (use-local-map old-map)
  11911. (when org-read-date-overlay
  11912. (org-delete-overlay org-read-date-overlay)
  11913. (setq org-read-date-overlay nil)))))))
  11914. (t ; Naked prompt only
  11915. (unwind-protect
  11916. (setq ans (read-string prompt default-input
  11917. 'org-read-date-history timestr))
  11918. (when org-read-date-overlay
  11919. (org-delete-overlay org-read-date-overlay)
  11920. (setq org-read-date-overlay nil)))))
  11921. (setq final (org-read-date-analyze ans def defdecode))
  11922. (setq org-read-date-final-answer ans)
  11923. (if to-time
  11924. (apply 'encode-time final)
  11925. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11926. (format "%04d-%02d-%02d %02d:%02d"
  11927. (nth 5 final) (nth 4 final) (nth 3 final)
  11928. (nth 2 final) (nth 1 final))
  11929. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11930. (defvar def)
  11931. (defvar defdecode)
  11932. (defvar with-time)
  11933. (defvar org-read-date-analyze-futurep nil)
  11934. (defun org-read-date-display ()
  11935. "Display the current date prompt interpretation in the minibuffer."
  11936. (when org-read-date-display-live
  11937. (when org-read-date-overlay
  11938. (org-delete-overlay org-read-date-overlay))
  11939. (let ((p (point)))
  11940. (end-of-line 1)
  11941. (while (not (equal (buffer-substring
  11942. (max (point-min) (- (point) 4)) (point))
  11943. " "))
  11944. (insert " "))
  11945. (goto-char p))
  11946. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11947. " " (or org-ans1 org-ans2)))
  11948. (org-end-time-was-given nil)
  11949. (f (org-read-date-analyze ans def defdecode))
  11950. (fmts (if org-dcst
  11951. org-time-stamp-custom-formats
  11952. org-time-stamp-formats))
  11953. (fmt (if (or with-time
  11954. (and (boundp 'org-time-was-given) org-time-was-given))
  11955. (cdr fmts)
  11956. (car fmts)))
  11957. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11958. (when (and org-end-time-was-given
  11959. (string-match org-plain-time-of-day-regexp txt))
  11960. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11961. org-end-time-was-given
  11962. (substring txt (match-end 0)))))
  11963. (when org-read-date-analyze-futurep
  11964. (setq txt (concat txt " (=>F)")))
  11965. (setq org-read-date-overlay
  11966. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11967. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11968. (defun org-read-date-analyze (ans def defdecode)
  11969. "Analyse the combined answer of the date prompt."
  11970. ;; FIXME: cleanup and comment
  11971. (let (delta deltan deltaw deltadef year month day
  11972. hour minute second wday pm h2 m2 tl wday1
  11973. iso-year iso-weekday iso-week iso-year iso-date futurep)
  11974. (setq org-read-date-analyze-futurep nil)
  11975. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11976. (setq ans "+0"))
  11977. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11978. (setq ans (replace-match "" t t ans)
  11979. deltan (car delta)
  11980. deltaw (nth 1 delta)
  11981. deltadef (nth 2 delta)))
  11982. ;; Check if there is an iso week date in there
  11983. ;; If yes, sore the info and postpone interpreting it until the rest
  11984. ;; of the parsing is done
  11985. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11986. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11987. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11988. iso-week (string-to-number (match-string 2 ans)))
  11989. (setq ans (replace-match "" t t ans)))
  11990. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11991. (when (string-match
  11992. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11993. (setq year (if (match-end 2)
  11994. (string-to-number (match-string 2 ans))
  11995. (string-to-number (format-time-string "%Y")))
  11996. month (string-to-number (match-string 3 ans))
  11997. day (string-to-number (match-string 4 ans)))
  11998. (if (< year 100) (setq year (+ 2000 year)))
  11999. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12000. t nil ans)))
  12001. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12002. ;; If there is a time with am/pm, and *no* time without it, we convert
  12003. ;; so that matching will be successful.
  12004. (loop for i from 1 to 2 do ; twice, for end time as well
  12005. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12006. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12007. (setq hour (string-to-number (match-string 1 ans))
  12008. minute (if (match-end 3)
  12009. (string-to-number (match-string 3 ans))
  12010. 0)
  12011. pm (equal ?p
  12012. (string-to-char (downcase (match-string 4 ans)))))
  12013. (if (and (= hour 12) (not pm))
  12014. (setq hour 0)
  12015. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12016. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12017. t t ans))))
  12018. ;; Check if a time range is given as a duration
  12019. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12020. (setq hour (string-to-number (match-string 1 ans))
  12021. h2 (+ hour (string-to-number (match-string 3 ans)))
  12022. minute (string-to-number (match-string 2 ans))
  12023. m2 (+ minute (if (match-end 5) (string-to-number
  12024. (match-string 5 ans))0)))
  12025. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12026. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12027. t t ans)))
  12028. ;; Check if there is a time range
  12029. (when (boundp 'org-end-time-was-given)
  12030. (setq org-time-was-given nil)
  12031. (when (and (string-match org-plain-time-of-day-regexp ans)
  12032. (match-end 8))
  12033. (setq org-end-time-was-given (match-string 8 ans))
  12034. (setq ans (concat (substring ans 0 (match-beginning 7))
  12035. (substring ans (match-end 7))))))
  12036. (setq tl (parse-time-string ans)
  12037. day (or (nth 3 tl) (nth 3 defdecode))
  12038. month (or (nth 4 tl)
  12039. (if (and org-read-date-prefer-future
  12040. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  12041. (prog1 (1+ (nth 4 defdecode)) (setq futurep t))
  12042. (nth 4 defdecode)))
  12043. year (or (nth 5 tl)
  12044. (if (and org-read-date-prefer-future
  12045. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  12046. (prog1 (1+ (nth 5 defdecode)) (setq futurep t))
  12047. (nth 5 defdecode)))
  12048. hour (or (nth 2 tl) (nth 2 defdecode))
  12049. minute (or (nth 1 tl) (nth 1 defdecode))
  12050. second (or (nth 0 tl) 0)
  12051. wday (nth 6 tl))
  12052. (when (and (eq org-read-date-prefer-future 'time)
  12053. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12054. (equal day (nth 3 defdecode))
  12055. (equal month (nth 4 defdecode))
  12056. (equal year (nth 5 defdecode))
  12057. (nth 2 tl)
  12058. (or (< (nth 2 tl) (nth 2 defdecode))
  12059. (and (= (nth 2 tl) (nth 2 defdecode))
  12060. (nth 1 tl)
  12061. (< (nth 1 tl) (nth 1 defdecode)))))
  12062. (setq day (1+ day)
  12063. futurep t))
  12064. ;; Special date definitions below
  12065. (cond
  12066. (iso-week
  12067. ;; There was an iso week
  12068. (setq futurep nil)
  12069. (setq year (or iso-year year)
  12070. day (or iso-weekday wday 1)
  12071. wday nil ; to make sure that the trigger below does not match
  12072. iso-date (calendar-gregorian-from-absolute
  12073. (calendar-absolute-from-iso
  12074. (list iso-week day year))))
  12075. ; FIXME: Should we also push ISO weeks into the future?
  12076. ; (when (and org-read-date-prefer-future
  12077. ; (not iso-year)
  12078. ; (< (calendar-absolute-from-gregorian iso-date)
  12079. ; (time-to-days (current-time))))
  12080. ; (setq year (1+ year)
  12081. ; iso-date (calendar-gregorian-from-absolute
  12082. ; (calendar-absolute-from-iso
  12083. ; (list iso-week day year)))))
  12084. (setq month (car iso-date)
  12085. year (nth 2 iso-date)
  12086. day (nth 1 iso-date)))
  12087. (deltan
  12088. (setq futurep nil)
  12089. (unless deltadef
  12090. (let ((now (decode-time (current-time))))
  12091. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12092. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12093. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12094. ((equal deltaw "m") (setq month (+ month deltan)))
  12095. ((equal deltaw "y") (setq year (+ year deltan)))))
  12096. ((and wday (not (nth 3 tl)))
  12097. (setq futurep nil)
  12098. ;; Weekday was given, but no day, so pick that day in the week
  12099. ;; on or after the derived date.
  12100. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12101. (unless (equal wday wday1)
  12102. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12103. (if (and (boundp 'org-time-was-given)
  12104. (nth 2 tl))
  12105. (setq org-time-was-given t))
  12106. (if (< year 100) (setq year (+ 2000 year)))
  12107. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12108. (setq org-read-date-analyze-futurep futurep)
  12109. (list second minute hour day month year)))
  12110. (defvar parse-time-weekdays)
  12111. (defun org-read-date-get-relative (s today default)
  12112. "Check string S for special relative date string.
  12113. TODAY and DEFAULT are internal times, for today and for a default.
  12114. Return shift list (N what def-flag)
  12115. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12116. N is the number of WHATs to shift.
  12117. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12118. the DEFAULT date rather than TODAY."
  12119. (when (and
  12120. (string-match
  12121. (concat
  12122. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12123. "\\([0-9]+\\)?"
  12124. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12125. "\\([ \t]\\|$\\)") s)
  12126. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12127. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12128. (string-to-char (substring (match-string 1 s) -1))
  12129. ?+))
  12130. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12131. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12132. (what (if (match-end 3) (match-string 3 s) "d"))
  12133. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12134. (date (if rel default today))
  12135. (wday (nth 6 (decode-time date)))
  12136. delta)
  12137. (if wday1
  12138. (progn
  12139. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12140. (if (= dir ?-) (setq delta (- delta 7)))
  12141. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12142. (list delta "d" rel))
  12143. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12144. (defun org-eval-in-calendar (form &optional keepdate)
  12145. "Eval FORM in the calendar window and return to current window.
  12146. Also, store the cursor date in variable org-ans2."
  12147. (let ((sf (selected-frame))
  12148. (sw (selected-window)))
  12149. (select-window (get-buffer-window "*Calendar*" t))
  12150. (eval form)
  12151. (when (and (not keepdate) (calendar-cursor-to-date))
  12152. (let* ((date (calendar-cursor-to-date))
  12153. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12154. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12155. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12156. (select-window sw)
  12157. (org-select-frame-set-input-focus sf)))
  12158. (defun org-calendar-select ()
  12159. "Return to `org-read-date' with the date currently selected.
  12160. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12161. (interactive)
  12162. (when (calendar-cursor-to-date)
  12163. (let* ((date (calendar-cursor-to-date))
  12164. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12165. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12166. (if (active-minibuffer-window) (exit-minibuffer))))
  12167. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12168. "Insert a date stamp for the date given by the internal TIME.
  12169. WITH-HM means, use the stamp format that includes the time of the day.
  12170. INACTIVE means use square brackets instead of angular ones, so that the
  12171. stamp will not contribute to the agenda.
  12172. PRE and POST are optional strings to be inserted before and after the
  12173. stamp.
  12174. The command returns the inserted time stamp."
  12175. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12176. stamp)
  12177. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12178. (insert-before-markers (or pre ""))
  12179. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12180. (when (listp extra)
  12181. (setq extra (car extra))
  12182. (if (and (stringp extra)
  12183. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12184. (setq extra (format "-%02d:%02d"
  12185. (string-to-number (match-string 1 extra))
  12186. (string-to-number (match-string 2 extra))))
  12187. (setq extra nil)))
  12188. (when extra
  12189. (backward-char 1)
  12190. (insert-before-markers extra)
  12191. (forward-char 1))
  12192. (insert-before-markers (or post ""))
  12193. (setq org-last-inserted-timestamp stamp)))
  12194. (defun org-toggle-time-stamp-overlays ()
  12195. "Toggle the use of custom time stamp formats."
  12196. (interactive)
  12197. (setq org-display-custom-times (not org-display-custom-times))
  12198. (unless org-display-custom-times
  12199. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12200. (while (setq p (next-single-property-change p 'display))
  12201. (if (and (get-text-property p 'display)
  12202. (eq (get-text-property p 'face) 'org-date))
  12203. (remove-text-properties
  12204. p (setq p (next-single-property-change p 'display))
  12205. '(display t))))
  12206. (set-buffer-modified-p bmp)))
  12207. (if (featurep 'xemacs)
  12208. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12209. (org-restart-font-lock)
  12210. (setq org-table-may-need-update t)
  12211. (if org-display-custom-times
  12212. (message "Time stamps are overlayed with custom format")
  12213. (message "Time stamp overlays removed")))
  12214. (defun org-display-custom-time (beg end)
  12215. "Overlay modified time stamp format over timestamp between BEG and END."
  12216. (let* ((ts (buffer-substring beg end))
  12217. t1 w1 with-hm tf time str w2 (off 0))
  12218. (save-match-data
  12219. (setq t1 (org-parse-time-string ts t))
  12220. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12221. (setq off (- (match-end 0) (match-beginning 0)))))
  12222. (setq end (- end off))
  12223. (setq w1 (- end beg)
  12224. with-hm (and (nth 1 t1) (nth 2 t1))
  12225. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12226. time (org-fix-decoded-time t1)
  12227. str (org-add-props
  12228. (format-time-string
  12229. (substring tf 1 -1) (apply 'encode-time time))
  12230. nil 'mouse-face 'highlight)
  12231. w2 (length str))
  12232. (if (not (= w2 w1))
  12233. (add-text-properties (1+ beg) (+ 2 beg)
  12234. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12235. (if (featurep 'xemacs)
  12236. (progn
  12237. (put-text-property beg end 'invisible t)
  12238. (put-text-property beg end 'end-glyph (make-glyph str)))
  12239. (put-text-property beg end 'display str))))
  12240. (defun org-translate-time (string)
  12241. "Translate all timestamps in STRING to custom format.
  12242. But do this only if the variable `org-display-custom-times' is set."
  12243. (when org-display-custom-times
  12244. (save-match-data
  12245. (let* ((start 0)
  12246. (re org-ts-regexp-both)
  12247. t1 with-hm inactive tf time str beg end)
  12248. (while (setq start (string-match re string start))
  12249. (setq beg (match-beginning 0)
  12250. end (match-end 0)
  12251. t1 (save-match-data
  12252. (org-parse-time-string (substring string beg end) t))
  12253. with-hm (and (nth 1 t1) (nth 2 t1))
  12254. inactive (equal (substring string beg (1+ beg)) "[")
  12255. tf (funcall (if with-hm 'cdr 'car)
  12256. org-time-stamp-custom-formats)
  12257. time (org-fix-decoded-time t1)
  12258. str (format-time-string
  12259. (concat
  12260. (if inactive "[" "<") (substring tf 1 -1)
  12261. (if inactive "]" ">"))
  12262. (apply 'encode-time time))
  12263. string (replace-match str t t string)
  12264. start (+ start (length str)))))))
  12265. string)
  12266. (defun org-fix-decoded-time (time)
  12267. "Set 0 instead of nil for the first 6 elements of time.
  12268. Don't touch the rest."
  12269. (let ((n 0))
  12270. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12271. (defun org-days-to-time (timestamp-string)
  12272. "Difference between TIMESTAMP-STRING and now in days."
  12273. (- (time-to-days (org-time-string-to-time timestamp-string))
  12274. (time-to-days (current-time))))
  12275. (defun org-deadline-close (timestamp-string &optional ndays)
  12276. "Is the time in TIMESTAMP-STRING close to the current date?"
  12277. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12278. (and (< (org-days-to-time timestamp-string) ndays)
  12279. (not (org-entry-is-done-p))))
  12280. (defun org-get-wdays (ts)
  12281. "Get the deadline lead time appropriate for timestring TS."
  12282. (cond
  12283. ((<= org-deadline-warning-days 0)
  12284. ;; 0 or negative, enforce this value no matter what
  12285. (- org-deadline-warning-days))
  12286. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12287. ;; lead time is specified.
  12288. (floor (* (string-to-number (match-string 1 ts))
  12289. (cdr (assoc (match-string 2 ts)
  12290. '(("d" . 1) ("w" . 7)
  12291. ("m" . 30.4) ("y" . 365.25)))))))
  12292. ;; go for the default.
  12293. (t org-deadline-warning-days)))
  12294. (defun org-calendar-select-mouse (ev)
  12295. "Return to `org-read-date' with the date currently selected.
  12296. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12297. (interactive "e")
  12298. (mouse-set-point ev)
  12299. (when (calendar-cursor-to-date)
  12300. (let* ((date (calendar-cursor-to-date))
  12301. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12302. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12303. (if (active-minibuffer-window) (exit-minibuffer))))
  12304. (defun org-check-deadlines (ndays)
  12305. "Check if there are any deadlines due or past due.
  12306. A deadline is considered due if it happens within `org-deadline-warning-days'
  12307. days from today's date. If the deadline appears in an entry marked DONE,
  12308. it is not shown. The prefix arg NDAYS can be used to test that many
  12309. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12310. (interactive "P")
  12311. (let* ((org-warn-days
  12312. (cond
  12313. ((equal ndays '(4)) 100000)
  12314. (ndays (prefix-numeric-value ndays))
  12315. (t (abs org-deadline-warning-days))))
  12316. (case-fold-search nil)
  12317. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12318. (callback
  12319. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12320. (message "%d deadlines past-due or due within %d days"
  12321. (org-occur regexp nil callback)
  12322. org-warn-days)))
  12323. (defun org-check-before-date (date)
  12324. "Check if there are deadlines or scheduled entries before DATE."
  12325. (interactive (list (org-read-date)))
  12326. (let ((case-fold-search nil)
  12327. (regexp (concat "\\<\\(" org-deadline-string
  12328. "\\|" org-scheduled-string
  12329. "\\) *<\\([^>]+\\)>"))
  12330. (callback
  12331. (lambda () (time-less-p
  12332. (org-time-string-to-time (match-string 2))
  12333. (org-time-string-to-time date)))))
  12334. (message "%d entries before %s"
  12335. (org-occur regexp nil callback) date)))
  12336. (defun org-check-after-date (date)
  12337. "Check if there are deadlines or scheduled entries after DATE."
  12338. (interactive (list (org-read-date)))
  12339. (let ((case-fold-search nil)
  12340. (regexp (concat "\\<\\(" org-deadline-string
  12341. "\\|" org-scheduled-string
  12342. "\\) *<\\([^>]+\\)>"))
  12343. (callback
  12344. (lambda () (not
  12345. (time-less-p
  12346. (org-time-string-to-time (match-string 2))
  12347. (org-time-string-to-time date))))))
  12348. (message "%d entries after %s"
  12349. (org-occur regexp nil callback) date)))
  12350. (defun org-evaluate-time-range (&optional to-buffer)
  12351. "Evaluate a time range by computing the difference between start and end.
  12352. Normally the result is just printed in the echo area, but with prefix arg
  12353. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12354. If the time range is actually in a table, the result is inserted into the
  12355. next column.
  12356. For time difference computation, a year is assumed to be exactly 365
  12357. days in order to avoid rounding problems."
  12358. (interactive "P")
  12359. (or
  12360. (org-clock-update-time-maybe)
  12361. (save-excursion
  12362. (unless (org-at-date-range-p t)
  12363. (goto-char (point-at-bol))
  12364. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12365. (if (not (org-at-date-range-p t))
  12366. (error "Not at a time-stamp range, and none found in current line")))
  12367. (let* ((ts1 (match-string 1))
  12368. (ts2 (match-string 2))
  12369. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12370. (match-end (match-end 0))
  12371. (time1 (org-time-string-to-time ts1))
  12372. (time2 (org-time-string-to-time ts2))
  12373. (t1 (org-float-time time1))
  12374. (t2 (org-float-time time2))
  12375. (diff (abs (- t2 t1)))
  12376. (negative (< (- t2 t1) 0))
  12377. ;; (ys (floor (* 365 24 60 60)))
  12378. (ds (* 24 60 60))
  12379. (hs (* 60 60))
  12380. (fy "%dy %dd %02d:%02d")
  12381. (fy1 "%dy %dd")
  12382. (fd "%dd %02d:%02d")
  12383. (fd1 "%dd")
  12384. (fh "%02d:%02d")
  12385. y d h m align)
  12386. (if havetime
  12387. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12388. y 0
  12389. d (floor (/ diff ds)) diff (mod diff ds)
  12390. h (floor (/ diff hs)) diff (mod diff hs)
  12391. m (floor (/ diff 60)))
  12392. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12393. y 0
  12394. d (floor (+ (/ diff ds) 0.5))
  12395. h 0 m 0))
  12396. (if (not to-buffer)
  12397. (message "%s" (org-make-tdiff-string y d h m))
  12398. (if (org-at-table-p)
  12399. (progn
  12400. (goto-char match-end)
  12401. (setq align t)
  12402. (and (looking-at " *|") (goto-char (match-end 0))))
  12403. (goto-char match-end))
  12404. (if (looking-at
  12405. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12406. (replace-match ""))
  12407. (if negative (insert " -"))
  12408. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12409. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12410. (insert " " (format fh h m))))
  12411. (if align (org-table-align))
  12412. (message "Time difference inserted")))))
  12413. (defun org-make-tdiff-string (y d h m)
  12414. (let ((fmt "")
  12415. (l nil))
  12416. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12417. l (push y l)))
  12418. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12419. l (push d l)))
  12420. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12421. l (push h l)))
  12422. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12423. l (push m l)))
  12424. (apply 'format fmt (nreverse l))))
  12425. (defun org-time-string-to-time (s)
  12426. (apply 'encode-time (org-parse-time-string s)))
  12427. (defun org-time-string-to-seconds (s)
  12428. (org-float-time (org-time-string-to-time s)))
  12429. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12430. "Convert a time stamp to an absolute day number.
  12431. If there is a specifyer for a cyclic time stamp, get the closest date to
  12432. DAYNR.
  12433. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12434. the variable date is bound by the calendar when this is called."
  12435. (cond
  12436. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12437. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12438. daynr
  12439. (+ daynr 1000)))
  12440. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12441. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12442. (time-to-days (current-time))) (match-string 0 s)
  12443. prefer show-all))
  12444. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12445. (defun org-days-to-iso-week (days)
  12446. "Return the iso week number."
  12447. (require 'cal-iso)
  12448. (car (calendar-iso-from-absolute days)))
  12449. (defun org-small-year-to-year (year)
  12450. "Convert 2-digit years into 4-digit years.
  12451. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12452. The year 2000 cannot be abbreviated. Any year larger than 99
  12453. is returned unchanged."
  12454. (if (< year 38)
  12455. (setq year (+ 2000 year))
  12456. (if (< year 100)
  12457. (setq year (+ 1900 year))))
  12458. year)
  12459. (defun org-time-from-absolute (d)
  12460. "Return the time corresponding to date D.
  12461. D may be an absolute day number, or a calendar-type list (month day year)."
  12462. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12463. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12464. (defun org-calendar-holiday ()
  12465. "List of holidays, for Diary display in Org-mode."
  12466. (require 'holidays)
  12467. (let ((hl (funcall
  12468. (if (fboundp 'calendar-check-holidays)
  12469. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12470. (if hl (mapconcat 'identity hl "; "))))
  12471. (defun org-diary-sexp-entry (sexp entry date)
  12472. "Process a SEXP diary ENTRY for DATE."
  12473. (require 'diary-lib)
  12474. (let ((result (if calendar-debug-sexp
  12475. (let ((stack-trace-on-error t))
  12476. (eval (car (read-from-string sexp))))
  12477. (condition-case nil
  12478. (eval (car (read-from-string sexp)))
  12479. (error
  12480. (beep)
  12481. (message "Bad sexp at line %d in %s: %s"
  12482. (org-current-line)
  12483. (buffer-file-name) sexp)
  12484. (sleep-for 2))))))
  12485. (cond ((stringp result) result)
  12486. ((and (consp result)
  12487. (stringp (cdr result))) (cdr result))
  12488. (result entry)
  12489. (t nil))))
  12490. (defun org-diary-to-ical-string (frombuf)
  12491. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12492. This uses the icalendar.el library."
  12493. (let* ((tmpdir (if (featurep 'xemacs)
  12494. (temp-directory)
  12495. temporary-file-directory))
  12496. (tmpfile (make-temp-name
  12497. (expand-file-name "orgics" tmpdir)))
  12498. buf rtn b e)
  12499. (with-current-buffer frombuf
  12500. (icalendar-export-region (point-min) (point-max) tmpfile)
  12501. (setq buf (find-buffer-visiting tmpfile))
  12502. (set-buffer buf)
  12503. (goto-char (point-min))
  12504. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12505. (setq b (match-beginning 0)))
  12506. (goto-char (point-max))
  12507. (if (re-search-backward "^END:VEVENT" nil t)
  12508. (setq e (match-end 0)))
  12509. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12510. (kill-buffer buf)
  12511. (delete-file tmpfile)
  12512. rtn))
  12513. (defun org-closest-date (start current change prefer show-all)
  12514. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12515. When PREFER is `past' return a date that is either CURRENT or past.
  12516. When PREFER is `future', return a date that is either CURRENT or future.
  12517. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12518. ;; Make the proper lists from the dates
  12519. (catch 'exit
  12520. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12521. dn dw sday cday n1 n2 n0
  12522. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12523. (setq start (org-date-to-gregorian start)
  12524. current (org-date-to-gregorian
  12525. (if show-all
  12526. current
  12527. (time-to-days (current-time))))
  12528. sday (calendar-absolute-from-gregorian start)
  12529. cday (calendar-absolute-from-gregorian current))
  12530. (if (<= cday sday) (throw 'exit sday))
  12531. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12532. (setq dn (string-to-number (match-string 1 change))
  12533. dw (cdr (assoc (match-string 2 change) a1)))
  12534. (error "Invalid change specifyer: %s" change))
  12535. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12536. (cond
  12537. ((eq dw 'day)
  12538. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12539. n2 (+ n1 dn)))
  12540. ((eq dw 'year)
  12541. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12542. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12543. (setq date1 (list m d y1)
  12544. n1 (calendar-absolute-from-gregorian date1)
  12545. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12546. n2 (calendar-absolute-from-gregorian date2)))
  12547. ((eq dw 'month)
  12548. ;; approx number of month between the two dates
  12549. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12550. ;; How often does dn fit in there?
  12551. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12552. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12553. m (+ m nm)
  12554. ny (floor (/ m 12))
  12555. y (+ y ny)
  12556. m (- m (* ny 12)))
  12557. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12558. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12559. (setq m2 (+ m dn) y2 y)
  12560. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12561. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12562. (while (<= n2 cday)
  12563. (setq n1 n2 m m2 y y2)
  12564. (setq m2 (+ m dn) y2 y)
  12565. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12566. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12567. ;; Make sure n1 is the earlier date
  12568. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12569. (if show-all
  12570. (cond
  12571. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12572. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12573. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12574. (cond
  12575. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12576. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12577. (t (if (= cday n1) n1 n2)))))))
  12578. (defun org-date-to-gregorian (date)
  12579. "Turn any specification of DATE into a gregorian date for the calendar."
  12580. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12581. ((and (listp date) (= (length date) 3)) date)
  12582. ((stringp date)
  12583. (setq date (org-parse-time-string date))
  12584. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12585. ((listp date)
  12586. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12587. (defun org-parse-time-string (s &optional nodefault)
  12588. "Parse the standard Org-mode time string.
  12589. This should be a lot faster than the normal `parse-time-string'.
  12590. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12591. hour and minute fields will be nil if not given."
  12592. (if (string-match org-ts-regexp0 s)
  12593. (list 0
  12594. (if (or (match-beginning 8) (not nodefault))
  12595. (string-to-number (or (match-string 8 s) "0")))
  12596. (if (or (match-beginning 7) (not nodefault))
  12597. (string-to-number (or (match-string 7 s) "0")))
  12598. (string-to-number (match-string 4 s))
  12599. (string-to-number (match-string 3 s))
  12600. (string-to-number (match-string 2 s))
  12601. nil nil nil)
  12602. (error "Not a standard Org-mode time string: %s" s)))
  12603. (defun org-timestamp-up (&optional arg)
  12604. "Increase the date item at the cursor by one.
  12605. If the cursor is on the year, change the year. If it is on the month or
  12606. the day, change that.
  12607. With prefix ARG, change by that many units."
  12608. (interactive "p")
  12609. (org-timestamp-change (prefix-numeric-value arg)))
  12610. (defun org-timestamp-down (&optional arg)
  12611. "Decrease the date item at the cursor by one.
  12612. If the cursor is on the year, change the year. If it is on the month or
  12613. the day, change that.
  12614. With prefix ARG, change by that many units."
  12615. (interactive "p")
  12616. (org-timestamp-change (- (prefix-numeric-value arg))))
  12617. (defun org-timestamp-up-day (&optional arg)
  12618. "Increase the date in the time stamp by one day.
  12619. With prefix ARG, change that many days."
  12620. (interactive "p")
  12621. (if (and (not (org-at-timestamp-p t))
  12622. (org-on-heading-p))
  12623. (org-todo 'up)
  12624. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12625. (defun org-timestamp-down-day (&optional arg)
  12626. "Decrease the date in the time stamp by one day.
  12627. With prefix ARG, change that many days."
  12628. (interactive "p")
  12629. (if (and (not (org-at-timestamp-p t))
  12630. (org-on-heading-p))
  12631. (org-todo 'down)
  12632. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12633. (defun org-at-timestamp-p (&optional inactive-ok)
  12634. "Determine if the cursor is in or at a timestamp."
  12635. (interactive)
  12636. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12637. (pos (point))
  12638. (ans (or (looking-at tsr)
  12639. (save-excursion
  12640. (skip-chars-backward "^[<\n\r\t")
  12641. (if (> (point) (point-min)) (backward-char 1))
  12642. (and (looking-at tsr)
  12643. (> (- (match-end 0) pos) -1))))))
  12644. (and ans
  12645. (boundp 'org-ts-what)
  12646. (setq org-ts-what
  12647. (cond
  12648. ((= pos (match-beginning 0)) 'bracket)
  12649. ((= pos (1- (match-end 0))) 'bracket)
  12650. ((org-pos-in-match-range pos 2) 'year)
  12651. ((org-pos-in-match-range pos 3) 'month)
  12652. ((org-pos-in-match-range pos 7) 'hour)
  12653. ((org-pos-in-match-range pos 8) 'minute)
  12654. ((or (org-pos-in-match-range pos 4)
  12655. (org-pos-in-match-range pos 5)) 'day)
  12656. ((and (> pos (or (match-end 8) (match-end 5)))
  12657. (< pos (match-end 0)))
  12658. (- pos (or (match-end 8) (match-end 5))))
  12659. (t 'day))))
  12660. ans))
  12661. (defun org-toggle-timestamp-type ()
  12662. "Toggle the type (<active> or [inactive]) of a time stamp."
  12663. (interactive)
  12664. (when (org-at-timestamp-p t)
  12665. (let ((beg (match-beginning 0)) (end (match-end 0))
  12666. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12667. (save-excursion
  12668. (goto-char beg)
  12669. (while (re-search-forward "[][<>]" end t)
  12670. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12671. t t)))
  12672. (message "Timestamp is now %sactive"
  12673. (if (equal (char-after beg) ?<) "" "in")))))
  12674. (defun org-timestamp-change (n &optional what)
  12675. "Change the date in the time stamp at point.
  12676. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12677. `year', `minute', `second'. If WHAT is not given, the cursor position
  12678. in the timestamp determines what will be changed."
  12679. (let ((pos (point))
  12680. with-hm inactive
  12681. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12682. org-ts-what
  12683. extra rem
  12684. ts time time0)
  12685. (if (not (org-at-timestamp-p t))
  12686. (error "Not at a timestamp"))
  12687. (if (and (not what) (eq org-ts-what 'bracket))
  12688. (org-toggle-timestamp-type)
  12689. (if (and (not what) (not (eq org-ts-what 'day))
  12690. org-display-custom-times
  12691. (get-text-property (point) 'display)
  12692. (not (get-text-property (1- (point)) 'display)))
  12693. (setq org-ts-what 'day))
  12694. (setq org-ts-what (or what org-ts-what)
  12695. inactive (= (char-after (match-beginning 0)) ?\[)
  12696. ts (match-string 0))
  12697. (replace-match "")
  12698. (if (string-match
  12699. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  12700. ts)
  12701. (setq extra (match-string 1 ts)))
  12702. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12703. (setq with-hm t))
  12704. (setq time0 (org-parse-time-string ts))
  12705. (when (and (eq org-ts-what 'minute)
  12706. (eq current-prefix-arg nil))
  12707. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12708. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12709. (setcar (cdr time0) (+ (nth 1 time0)
  12710. (if (> n 0) (- rem) (- dm rem))))))
  12711. (setq time
  12712. (encode-time (or (car time0) 0)
  12713. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12714. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12715. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12716. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12717. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12718. (nthcdr 6 time0)))
  12719. (when (and (member org-ts-what '(hour minute))
  12720. extra
  12721. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12722. (setq extra (org-modify-ts-extra
  12723. extra
  12724. (if (eq org-ts-what 'hour) 2 5)
  12725. n dm)))
  12726. (when (integerp org-ts-what)
  12727. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12728. (if (eq what 'calendar)
  12729. (let ((cal-date (org-get-date-from-calendar)))
  12730. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12731. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12732. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12733. (setcar time0 (or (car time0) 0))
  12734. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12735. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12736. (setq time (apply 'encode-time time0))))
  12737. (setq org-last-changed-timestamp
  12738. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12739. (org-clock-update-time-maybe)
  12740. (goto-char pos)
  12741. ;; Try to recenter the calendar window, if any
  12742. (if (and org-calendar-follow-timestamp-change
  12743. (get-buffer-window "*Calendar*" t)
  12744. (memq org-ts-what '(day month year)))
  12745. (org-recenter-calendar (time-to-days time))))))
  12746. (defun org-modify-ts-extra (s pos n dm)
  12747. "Change the different parts of the lead-time and repeat fields in timestamp."
  12748. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12749. ng h m new rem)
  12750. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12751. (cond
  12752. ((or (org-pos-in-match-range pos 2)
  12753. (org-pos-in-match-range pos 3))
  12754. (setq m (string-to-number (match-string 3 s))
  12755. h (string-to-number (match-string 2 s)))
  12756. (if (org-pos-in-match-range pos 2)
  12757. (setq h (+ h n))
  12758. (setq n (* dm (org-no-warnings (signum n))))
  12759. (when (not (= 0 (setq rem (% m dm))))
  12760. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12761. (setq m (+ m n)))
  12762. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12763. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12764. (setq h (min 24 (max 0 h)))
  12765. (setq ng 1 new (format "-%02d:%02d" h m)))
  12766. ((org-pos-in-match-range pos 6)
  12767. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12768. ((org-pos-in-match-range pos 5)
  12769. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12770. ((org-pos-in-match-range pos 9)
  12771. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12772. ((org-pos-in-match-range pos 8)
  12773. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12774. (when ng
  12775. (setq s (concat
  12776. (substring s 0 (match-beginning ng))
  12777. new
  12778. (substring s (match-end ng))))))
  12779. s))
  12780. (defun org-recenter-calendar (date)
  12781. "If the calendar is visible, recenter it to DATE."
  12782. (let* ((win (selected-window))
  12783. (cwin (get-buffer-window "*Calendar*" t))
  12784. (calendar-move-hook nil))
  12785. (when cwin
  12786. (select-window cwin)
  12787. (calendar-goto-date (if (listp date) date
  12788. (calendar-gregorian-from-absolute date)))
  12789. (select-window win))))
  12790. (defun org-goto-calendar (&optional arg)
  12791. "Go to the Emacs calendar at the current date.
  12792. If there is a time stamp in the current line, go to that date.
  12793. A prefix ARG can be used to force the current date."
  12794. (interactive "P")
  12795. (let ((tsr org-ts-regexp) diff
  12796. (calendar-move-hook nil)
  12797. (calendar-view-holidays-initially-flag nil)
  12798. (view-calendar-holidays-initially nil)
  12799. (calendar-view-diary-initially-flag nil)
  12800. (view-diary-entries-initially nil))
  12801. (if (or (org-at-timestamp-p)
  12802. (save-excursion
  12803. (beginning-of-line 1)
  12804. (looking-at (concat ".*" tsr))))
  12805. (let ((d1 (time-to-days (current-time)))
  12806. (d2 (time-to-days
  12807. (org-time-string-to-time (match-string 1)))))
  12808. (setq diff (- d2 d1))))
  12809. (calendar)
  12810. (calendar-goto-today)
  12811. (if (and diff (not arg)) (calendar-forward-day diff))))
  12812. (defun org-get-date-from-calendar ()
  12813. "Return a list (month day year) of date at point in calendar."
  12814. (with-current-buffer "*Calendar*"
  12815. (save-match-data
  12816. (calendar-cursor-to-date))))
  12817. (defun org-date-from-calendar ()
  12818. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12819. If there is already a time stamp at the cursor position, update it."
  12820. (interactive)
  12821. (if (org-at-timestamp-p t)
  12822. (org-timestamp-change 0 'calendar)
  12823. (let ((cal-date (org-get-date-from-calendar)))
  12824. (org-insert-time-stamp
  12825. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12826. (defun org-minutes-to-hh:mm-string (m)
  12827. "Compute H:MM from a number of minutes."
  12828. (let ((h (/ m 60)))
  12829. (setq m (- m (* 60 h)))
  12830. (format org-time-clocksum-format h m)))
  12831. (defun org-hh:mm-string-to-minutes (s)
  12832. "Convert a string H:MM to a number of minutes.
  12833. If the string is just a number, interpret it as minutes.
  12834. In fact, the first hh:mm or number in the string will be taken,
  12835. there can be extra stuff in the string.
  12836. If no number is found, the return value is 0."
  12837. (cond
  12838. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12839. (+ (* (string-to-number (match-string 1 s)) 60)
  12840. (string-to-number (match-string 2 s))))
  12841. ((string-match "\\([0-9]+\\)" s)
  12842. (string-to-number (match-string 1 s)))
  12843. (t 0)))
  12844. ;;;; Files
  12845. (defun org-save-all-org-buffers ()
  12846. "Save all Org-mode buffers without user confirmation."
  12847. (interactive)
  12848. (message "Saving all Org-mode buffers...")
  12849. (save-some-buffers t 'org-mode-p)
  12850. (when (featurep 'org-id) (org-id-locations-save))
  12851. (message "Saving all Org-mode buffers... done"))
  12852. (defun org-revert-all-org-buffers ()
  12853. "Revert all Org-mode buffers.
  12854. Prompt for confirmation when there are unsaved changes.
  12855. Be sure you know what you are doing before letting this function
  12856. overwrite your changes.
  12857. This function is useful in a setup where one tracks org files
  12858. with a version control system, to revert on one machine after pulling
  12859. changes from another. I believe the procedure must be like this:
  12860. 1. M-x org-save-all-org-buffers
  12861. 2. Pull changes from the other machine, resolve conflicts
  12862. 3. M-x org-revert-all-org-buffers"
  12863. (interactive)
  12864. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12865. (error "Abort"))
  12866. (save-excursion
  12867. (save-window-excursion
  12868. (mapc
  12869. (lambda (b)
  12870. (when (and (with-current-buffer b (org-mode-p))
  12871. (with-current-buffer b buffer-file-name))
  12872. (switch-to-buffer b)
  12873. (revert-buffer t 'no-confirm)))
  12874. (buffer-list))
  12875. (when (and (featurep 'org-id) org-id-track-globally)
  12876. (org-id-locations-load)))))
  12877. ;;;; Agenda files
  12878. ;;;###autoload
  12879. (defun org-iswitchb (&optional arg)
  12880. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12881. With a prefix argument, restrict available to files.
  12882. With two prefix arguments, restrict available buffers to agenda files."
  12883. (interactive "P")
  12884. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12885. ((equal arg '(16)) (org-buffer-list 'agenda))
  12886. (t (org-buffer-list)))))
  12887. (switch-to-buffer
  12888. (org-icompleting-read "Org buffer: "
  12889. (mapcar 'list (mapcar 'buffer-name blist))
  12890. nil t))))
  12891. ;;;###autoload
  12892. (defalias 'org-ido-switchb 'org-iswitchb)
  12893. (defun org-buffer-list (&optional predicate exclude-tmp)
  12894. "Return a list of Org buffers.
  12895. PREDICATE can be `export', `files' or `agenda'.
  12896. export restrict the list to Export buffers.
  12897. files restrict the list to buffers visiting Org files.
  12898. agenda restrict the list to buffers visiting agenda files.
  12899. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12900. (let* ((bfn nil)
  12901. (agenda-files (and (eq predicate 'agenda)
  12902. (mapcar 'file-truename (org-agenda-files t))))
  12903. (filter
  12904. (cond
  12905. ((eq predicate 'files)
  12906. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12907. ((eq predicate 'export)
  12908. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12909. ((eq predicate 'agenda)
  12910. (lambda (b)
  12911. (with-current-buffer b
  12912. (and (eq major-mode 'org-mode)
  12913. (setq bfn (buffer-file-name b))
  12914. (member (file-truename bfn) agenda-files)))))
  12915. (t (lambda (b) (with-current-buffer b
  12916. (or (eq major-mode 'org-mode)
  12917. (string-match "\*Org .*Export"
  12918. (buffer-name b)))))))))
  12919. (delq nil
  12920. (mapcar
  12921. (lambda(b)
  12922. (if (and (funcall filter b)
  12923. (or (not exclude-tmp)
  12924. (not (string-match "tmp" (buffer-name b)))))
  12925. b
  12926. nil))
  12927. (buffer-list)))))
  12928. (defun org-agenda-files (&optional unrestricted archives)
  12929. "Get the list of agenda files.
  12930. Optional UNRESTRICTED means return the full list even if a restriction
  12931. is currently in place.
  12932. When ARCHIVES is t, include all archive files hat are really being
  12933. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12934. `org-agenda-archives-mode' is t."
  12935. (let ((files
  12936. (cond
  12937. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12938. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12939. ((listp org-agenda-files) org-agenda-files)
  12940. (t (error "Invalid value of `org-agenda-files'")))))
  12941. (setq files (apply 'append
  12942. (mapcar (lambda (f)
  12943. (if (file-directory-p f)
  12944. (directory-files
  12945. f t org-agenda-file-regexp)
  12946. (list f)))
  12947. files)))
  12948. (when org-agenda-skip-unavailable-files
  12949. (setq files (delq nil
  12950. (mapcar (function
  12951. (lambda (file)
  12952. (and (file-readable-p file) file)))
  12953. files))))
  12954. (when (or (eq archives t)
  12955. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12956. (setq files (org-add-archive-files files)))
  12957. files))
  12958. (defun org-edit-agenda-file-list ()
  12959. "Edit the list of agenda files.
  12960. Depending on setup, this either uses customize to edit the variable
  12961. `org-agenda-files', or it visits the file that is holding the list. In the
  12962. latter case, the buffer is set up in a way that saving it automatically kills
  12963. the buffer and restores the previous window configuration."
  12964. (interactive)
  12965. (if (stringp org-agenda-files)
  12966. (let ((cw (current-window-configuration)))
  12967. (find-file org-agenda-files)
  12968. (org-set-local 'org-window-configuration cw)
  12969. (org-add-hook 'after-save-hook
  12970. (lambda ()
  12971. (set-window-configuration
  12972. (prog1 org-window-configuration
  12973. (kill-buffer (current-buffer))))
  12974. (org-install-agenda-files-menu)
  12975. (message "New agenda file list installed"))
  12976. nil 'local)
  12977. (message "%s" (substitute-command-keys
  12978. "Edit list and finish with \\[save-buffer]")))
  12979. (customize-variable 'org-agenda-files)))
  12980. (defun org-store-new-agenda-file-list (list)
  12981. "Set new value for the agenda file list and save it correctly."
  12982. (if (stringp org-agenda-files)
  12983. (let ((f org-agenda-files) b)
  12984. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12985. (with-temp-file f
  12986. (insert (mapconcat 'identity list "\n") "\n")))
  12987. (let ((org-mode-hook nil) (org-inhibit-startup t)
  12988. (org-insert-mode-line-in-empty-file nil))
  12989. (setq org-agenda-files list)
  12990. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12991. (defun org-read-agenda-file-list ()
  12992. "Read the list of agenda files from a file."
  12993. (when (file-directory-p org-agenda-files)
  12994. (error "`org-agenda-files' cannot be a single directory"))
  12995. (when (stringp org-agenda-files)
  12996. (with-temp-buffer
  12997. (insert-file-contents org-agenda-files)
  12998. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12999. ;;;###autoload
  13000. (defun org-cycle-agenda-files ()
  13001. "Cycle through the files in `org-agenda-files'.
  13002. If the current buffer visits an agenda file, find the next one in the list.
  13003. If the current buffer does not, find the first agenda file."
  13004. (interactive)
  13005. (let* ((fs (org-agenda-files t))
  13006. (files (append fs (list (car fs))))
  13007. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13008. file)
  13009. (unless files (error "No agenda files"))
  13010. (catch 'exit
  13011. (while (setq file (pop files))
  13012. (if (equal (file-truename file) tcf)
  13013. (when (car files)
  13014. (find-file (car files))
  13015. (throw 'exit t))))
  13016. (find-file (car fs)))
  13017. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13018. (defun org-agenda-file-to-front (&optional to-end)
  13019. "Move/add the current file to the top of the agenda file list.
  13020. If the file is not present in the list, it is added to the front. If it is
  13021. present, it is moved there. With optional argument TO-END, add/move to the
  13022. end of the list."
  13023. (interactive "P")
  13024. (let ((org-agenda-skip-unavailable-files nil)
  13025. (file-alist (mapcar (lambda (x)
  13026. (cons (file-truename x) x))
  13027. (org-agenda-files t)))
  13028. (ctf (file-truename buffer-file-name))
  13029. x had)
  13030. (setq x (assoc ctf file-alist) had x)
  13031. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13032. (if to-end
  13033. (setq file-alist (append (delq x file-alist) (list x)))
  13034. (setq file-alist (cons x (delq x file-alist))))
  13035. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13036. (org-install-agenda-files-menu)
  13037. (message "File %s to %s of agenda file list"
  13038. (if had "moved" "added") (if to-end "end" "front"))))
  13039. (defun org-remove-file (&optional file)
  13040. "Remove current file from the list of files in variable `org-agenda-files'.
  13041. These are the files which are being checked for agenda entries.
  13042. Optional argument FILE means, use this file instead of the current."
  13043. (interactive)
  13044. (let* ((org-agenda-skip-unavailable-files nil)
  13045. (file (or file buffer-file-name))
  13046. (true-file (file-truename file))
  13047. (afile (abbreviate-file-name file))
  13048. (files (delq nil (mapcar
  13049. (lambda (x)
  13050. (if (equal true-file
  13051. (file-truename x))
  13052. nil x))
  13053. (org-agenda-files t)))))
  13054. (if (not (= (length files) (length (org-agenda-files t))))
  13055. (progn
  13056. (org-store-new-agenda-file-list files)
  13057. (org-install-agenda-files-menu)
  13058. (message "Removed file: %s" afile))
  13059. (message "File was not in list: %s (not removed)" afile))))
  13060. (defun org-file-menu-entry (file)
  13061. (vector file (list 'find-file file) t))
  13062. (defun org-check-agenda-file (file)
  13063. "Make sure FILE exists. If not, ask user what to do."
  13064. (when (not (file-exists-p file))
  13065. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13066. (abbreviate-file-name file))
  13067. (let ((r (downcase (read-char-exclusive))))
  13068. (cond
  13069. ((equal r ?r)
  13070. (org-remove-file file)
  13071. (throw 'nextfile t))
  13072. (t (error "Abort"))))))
  13073. (defun org-get-agenda-file-buffer (file)
  13074. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13075. it to the list of buffers which might be released later."
  13076. (let ((buf (org-find-base-buffer-visiting file)))
  13077. (if buf
  13078. buf ; just return it
  13079. ;; Make a new buffer and remember it
  13080. (setq buf (find-file-noselect file))
  13081. (if buf (push buf org-agenda-new-buffers))
  13082. buf)))
  13083. (defun org-release-buffers (blist)
  13084. "Release all buffers in list, asking the user for confirmation when needed.
  13085. When a buffer is unmodified, it is just killed. When modified, it is saved
  13086. \(if the user agrees) and then killed."
  13087. (let (buf file)
  13088. (while (setq buf (pop blist))
  13089. (setq file (buffer-file-name buf))
  13090. (when (and (buffer-modified-p buf)
  13091. file
  13092. (y-or-n-p (format "Save file %s? " file)))
  13093. (with-current-buffer buf (save-buffer)))
  13094. (kill-buffer buf))))
  13095. (defun org-prepare-agenda-buffers (files)
  13096. "Create buffers for all agenda files, protect archived trees and comments."
  13097. (interactive)
  13098. (let ((pa '(:org-archived t))
  13099. (pc '(:org-comment t))
  13100. (pall '(:org-archived t :org-comment t))
  13101. (inhibit-read-only t)
  13102. (rea (concat ":" org-archive-tag ":"))
  13103. bmp file re)
  13104. (save-excursion
  13105. (save-restriction
  13106. (while (setq file (pop files))
  13107. (catch 'nextfile
  13108. (if (bufferp file)
  13109. (set-buffer file)
  13110. (org-check-agenda-file file)
  13111. (set-buffer (org-get-agenda-file-buffer file)))
  13112. (widen)
  13113. (setq bmp (buffer-modified-p))
  13114. (org-refresh-category-properties)
  13115. (setq org-todo-keywords-for-agenda
  13116. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13117. (setq org-done-keywords-for-agenda
  13118. (append org-done-keywords-for-agenda org-done-keywords))
  13119. (setq org-todo-keyword-alist-for-agenda
  13120. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13121. (setq org-drawers-for-agenda
  13122. (append org-drawers-for-agenda org-drawers))
  13123. (setq org-tag-alist-for-agenda
  13124. (append org-tag-alist-for-agenda org-tag-alist))
  13125. (save-excursion
  13126. (remove-text-properties (point-min) (point-max) pall)
  13127. (when org-agenda-skip-archived-trees
  13128. (goto-char (point-min))
  13129. (while (re-search-forward rea nil t)
  13130. (if (org-on-heading-p t)
  13131. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13132. (goto-char (point-min))
  13133. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13134. (while (re-search-forward re nil t)
  13135. (add-text-properties
  13136. (match-beginning 0) (org-end-of-subtree t) pc)))
  13137. (set-buffer-modified-p bmp)))))
  13138. (setq org-todo-keyword-alist-for-agenda
  13139. (org-uniquify org-todo-keyword-alist-for-agenda)
  13140. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13141. ;;;; Embedded LaTeX
  13142. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13143. "Keymap for the minor `org-cdlatex-mode'.")
  13144. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13145. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13146. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13147. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13148. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13149. (defvar org-cdlatex-texmathp-advice-is-done nil
  13150. "Flag remembering if we have applied the advice to texmathp already.")
  13151. (define-minor-mode org-cdlatex-mode
  13152. "Toggle the minor `org-cdlatex-mode'.
  13153. This mode supports entering LaTeX environment and math in LaTeX fragments
  13154. in Org-mode.
  13155. \\{org-cdlatex-mode-map}"
  13156. nil " OCDL" nil
  13157. (when org-cdlatex-mode (require 'cdlatex))
  13158. (unless org-cdlatex-texmathp-advice-is-done
  13159. (setq org-cdlatex-texmathp-advice-is-done t)
  13160. (defadvice texmathp (around org-math-always-on activate)
  13161. "Always return t in org-mode buffers.
  13162. This is because we want to insert math symbols without dollars even outside
  13163. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13164. an embedded LaTeX fragment, let texmathp do its job.
  13165. \\[org-cdlatex-mode-map]"
  13166. (interactive)
  13167. (let (p)
  13168. (cond
  13169. ((not (org-mode-p)) ad-do-it)
  13170. ((eq this-command 'cdlatex-math-symbol)
  13171. (setq ad-return-value t
  13172. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13173. (t
  13174. (let ((p (org-inside-LaTeX-fragment-p)))
  13175. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13176. (setq ad-return-value t
  13177. texmathp-why '("Org-mode embedded math" . 0))
  13178. (if p ad-do-it)))))))))
  13179. (defun turn-on-org-cdlatex ()
  13180. "Unconditionally turn on `org-cdlatex-mode'."
  13181. (org-cdlatex-mode 1))
  13182. (defun org-inside-LaTeX-fragment-p ()
  13183. "Test if point is inside a LaTeX fragment.
  13184. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13185. sequence appearing also before point.
  13186. Even though the matchers for math are configurable, this function assumes
  13187. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13188. delimiters are skipped when they have been removed by customization.
  13189. The return value is nil, or a cons cell with the delimiter and
  13190. and the position of this delimiter.
  13191. This function does a reasonably good job, but can locally be fooled by
  13192. for example currency specifications. For example it will assume being in
  13193. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13194. fragments that are properly closed, but during editing, we have to live
  13195. with the uncertainty caused by missing closing delimiters. This function
  13196. looks only before point, not after."
  13197. (catch 'exit
  13198. (let ((pos (point))
  13199. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13200. (lim (progn
  13201. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13202. (point)))
  13203. dd-on str (start 0) m re)
  13204. (goto-char pos)
  13205. (when dodollar
  13206. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13207. re (nth 1 (assoc "$" org-latex-regexps)))
  13208. (while (string-match re str start)
  13209. (cond
  13210. ((= (match-end 0) (length str))
  13211. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13212. ((= (match-end 0) (- (length str) 5))
  13213. (throw 'exit nil))
  13214. (t (setq start (match-end 0))))))
  13215. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13216. (goto-char pos)
  13217. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13218. (and (match-beginning 2) (throw 'exit nil))
  13219. ;; count $$
  13220. (while (re-search-backward "\\$\\$" lim t)
  13221. (setq dd-on (not dd-on)))
  13222. (goto-char pos)
  13223. (if dd-on (cons "$$" m))))))
  13224. (defun org-try-cdlatex-tab ()
  13225. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13226. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13227. - inside a LaTeX fragment, or
  13228. - after the first word in a line, where an abbreviation expansion could
  13229. insert a LaTeX environment."
  13230. (when org-cdlatex-mode
  13231. (cond
  13232. ((save-excursion
  13233. (skip-chars-backward "a-zA-Z0-9*")
  13234. (skip-chars-backward " \t")
  13235. (bolp))
  13236. (cdlatex-tab) t)
  13237. ((org-inside-LaTeX-fragment-p)
  13238. (cdlatex-tab) t)
  13239. (t nil))))
  13240. (defun org-cdlatex-underscore-caret (&optional arg)
  13241. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13242. Revert to the normal definition outside of these fragments."
  13243. (interactive "P")
  13244. (if (org-inside-LaTeX-fragment-p)
  13245. (call-interactively 'cdlatex-sub-superscript)
  13246. (let (org-cdlatex-mode)
  13247. (call-interactively (key-binding (vector last-input-event))))))
  13248. (defun org-cdlatex-math-modify (&optional arg)
  13249. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13250. Revert to the normal definition outside of these fragments."
  13251. (interactive "P")
  13252. (if (org-inside-LaTeX-fragment-p)
  13253. (call-interactively 'cdlatex-math-modify)
  13254. (let (org-cdlatex-mode)
  13255. (call-interactively (key-binding (vector last-input-event))))))
  13256. (defvar org-latex-fragment-image-overlays nil
  13257. "List of overlays carrying the images of latex fragments.")
  13258. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13259. (defun org-remove-latex-fragment-image-overlays ()
  13260. "Remove all overlays with LaTeX fragment images in current buffer."
  13261. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13262. (setq org-latex-fragment-image-overlays nil))
  13263. (defun org-preview-latex-fragment (&optional subtree)
  13264. "Preview the LaTeX fragment at point, or all locally or globally.
  13265. If the cursor is in a LaTeX fragment, create the image and overlay
  13266. it over the source code. If there is no fragment at point, display
  13267. all fragments in the current text, from one headline to the next. With
  13268. prefix SUBTREE, display all fragments in the current subtree. With a
  13269. double prefix `C-u C-u', or when the cursor is before the first headline,
  13270. display all fragments in the buffer.
  13271. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13272. (interactive "P")
  13273. (org-remove-latex-fragment-image-overlays)
  13274. (save-excursion
  13275. (save-restriction
  13276. (let (beg end at msg)
  13277. (cond
  13278. ((or (equal subtree '(16))
  13279. (not (save-excursion
  13280. (re-search-backward (concat "^" outline-regexp) nil t))))
  13281. (setq beg (point-min) end (point-max)
  13282. msg "Creating images for buffer...%s"))
  13283. ((equal subtree '(4))
  13284. (org-back-to-heading)
  13285. (setq beg (point) end (org-end-of-subtree t)
  13286. msg "Creating images for subtree...%s"))
  13287. (t
  13288. (if (setq at (org-inside-LaTeX-fragment-p))
  13289. (goto-char (max (point-min) (- (cdr at) 2)))
  13290. (org-back-to-heading))
  13291. (setq beg (point) end (progn (outline-next-heading) (point))
  13292. msg (if at "Creating image...%s"
  13293. "Creating images for entry...%s"))))
  13294. (message msg "")
  13295. (narrow-to-region beg end)
  13296. (goto-char beg)
  13297. (org-format-latex
  13298. (concat "ltxpng/" (file-name-sans-extension
  13299. (file-name-nondirectory
  13300. buffer-file-name)))
  13301. default-directory 'overlays msg at 'forbuffer)
  13302. (message msg "done. Use `C-c C-c' to remove images.")))))
  13303. (defvar org-latex-regexps
  13304. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13305. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13306. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13307. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13308. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13309. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13310. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13311. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13312. "Regular expressions for matching embedded LaTeX.")
  13313. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13314. "Replace LaTeX fragments with links to an image, and produce images.
  13315. Some of the options can be changed using the variable
  13316. `org-format-latex-options'."
  13317. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13318. (let* ((prefixnodir (file-name-nondirectory prefix))
  13319. (absprefix (expand-file-name prefix dir))
  13320. (todir (file-name-directory absprefix))
  13321. (opt org-format-latex-options)
  13322. (matchers (plist-get opt :matchers))
  13323. (re-list org-latex-regexps)
  13324. (cnt 0) txt link beg end re e checkdir
  13325. executables-checked
  13326. m n block linkfile movefile ov)
  13327. ;; Check if there are old images files with this prefix, and remove them
  13328. (when (file-directory-p todir)
  13329. (mapc 'delete-file
  13330. (directory-files
  13331. todir 'full
  13332. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  13333. ;; Check the different regular expressions
  13334. (while (setq e (pop re-list))
  13335. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13336. block (if (nth 3 e) "\n\n" ""))
  13337. (when (member m matchers)
  13338. (goto-char (point-min))
  13339. (while (re-search-forward re nil t)
  13340. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13341. (not (get-text-property (match-beginning n)
  13342. 'org-protected))
  13343. (or (not overlays)
  13344. (not (eq (get-char-property (match-beginning n)
  13345. 'org-overlay-type)
  13346. 'org-latex-overlay))))
  13347. (setq txt (match-string n)
  13348. beg (match-beginning n) end (match-end n)
  13349. cnt (1+ cnt)
  13350. linkfile (format "%s_%04d.png" prefix cnt)
  13351. movefile (format "%s_%04d.png" absprefix cnt)
  13352. link (concat block "[[file:" linkfile "]]" block))
  13353. (if msg (message msg cnt))
  13354. (goto-char beg)
  13355. (unless checkdir ; make sure the directory exists
  13356. (setq checkdir t)
  13357. (or (file-directory-p todir) (make-directory todir)))
  13358. (unless executables-checked
  13359. (org-check-external-command
  13360. "latex" "needed to convert LaTeX fragments to images")
  13361. (org-check-external-command
  13362. "dvipng" "needed to convert LaTeX fragments to images")
  13363. (setq executables-checked t))
  13364. (org-create-formula-image
  13365. txt movefile opt forbuffer)
  13366. (if overlays
  13367. (progn
  13368. (mapc (lambda (o)
  13369. (if (eq (org-overlay-get o 'org-overlay-type)
  13370. 'org-latex-overlay)
  13371. (org-delete-overlay o)))
  13372. (org-overlays-in beg end))
  13373. (setq ov (org-make-overlay beg end))
  13374. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13375. (if (featurep 'xemacs)
  13376. (progn
  13377. (org-overlay-put ov 'invisible t)
  13378. (org-overlay-put
  13379. ov 'end-glyph
  13380. (make-glyph (vector 'png :file movefile))))
  13381. (org-overlay-put
  13382. ov 'display
  13383. (list 'image :type 'png :file movefile :ascent 'center)))
  13384. (push ov org-latex-fragment-image-overlays)
  13385. (goto-char end))
  13386. (delete-region beg end)
  13387. (insert link))))))))
  13388. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  13389. ;; This function borrows from Ganesh Swami's latex2png.el
  13390. (defun org-create-formula-image (string tofile options buffer)
  13391. "This calls dvipng."
  13392. (require 'org-latex)
  13393. (let* ((tmpdir (if (featurep 'xemacs)
  13394. (temp-directory)
  13395. temporary-file-directory))
  13396. (texfilebase (make-temp-name
  13397. (expand-file-name "orgtex" tmpdir)))
  13398. (texfile (concat texfilebase ".tex"))
  13399. (dvifile (concat texfilebase ".dvi"))
  13400. (pngfile (concat texfilebase ".png"))
  13401. (fnh (if (featurep 'xemacs)
  13402. (font-height (get-face-font 'default))
  13403. (face-attribute 'default :height nil)))
  13404. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13405. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13406. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13407. "Black"))
  13408. (bg (or (plist-get options (if buffer :background :html-background))
  13409. "Transparent")))
  13410. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13411. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13412. (with-temp-file texfile
  13413. (insert org-format-latex-header
  13414. (if org-export-latex-packages-alist
  13415. (concat "\n"
  13416. (mapconcat (lambda(p)
  13417. (if (equal "" (car p))
  13418. (format "\\usepackage{%s}" (cadr p))
  13419. (format "\\usepackage[%s]{%s}"
  13420. (car p) (cadr p))))
  13421. org-export-latex-packages-alist "\n"))
  13422. "")
  13423. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13424. (let ((dir default-directory))
  13425. (condition-case nil
  13426. (progn
  13427. (cd tmpdir)
  13428. (call-process "latex" nil nil nil texfile))
  13429. (error nil))
  13430. (cd dir))
  13431. (if (not (file-exists-p dvifile))
  13432. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13433. (condition-case nil
  13434. (call-process "dvipng" nil nil nil
  13435. "-fg" fg "-bg" bg
  13436. "-D" dpi
  13437. ;;"-x" scale "-y" scale
  13438. "-T" "tight"
  13439. "-o" pngfile
  13440. dvifile)
  13441. (error nil))
  13442. (if (not (file-exists-p pngfile))
  13443. (progn (message "Failed to create png file from %s" texfile) nil)
  13444. ;; Use the requested file name and clean up
  13445. (copy-file pngfile tofile 'replace)
  13446. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13447. (delete-file (concat texfilebase e)))
  13448. pngfile))))
  13449. (defun org-dvipng-color (attr)
  13450. "Return an rgb color specification for dvipng."
  13451. (apply 'format "rgb %s %s %s"
  13452. (mapcar 'org-normalize-color
  13453. (color-values (face-attribute 'default attr nil)))))
  13454. (defun org-normalize-color (value)
  13455. "Return string to be used as color value for an RGB component."
  13456. (format "%g" (/ value 65535.0)))
  13457. ;;;; Key bindings
  13458. ;; Make `C-c C-x' a prefix key
  13459. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13460. ;; TAB key with modifiers
  13461. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13462. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13463. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13464. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13465. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13466. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13467. ;; The following line is necessary under Suse GNU/Linux
  13468. (unless (featurep 'xemacs)
  13469. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13470. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13471. (define-key org-mode-map [backtab] 'org-shifttab)
  13472. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13473. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13474. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13475. ;; Cursor keys with modifiers
  13476. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13477. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13478. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13479. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13480. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13481. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13482. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13483. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13484. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13485. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13486. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13487. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13488. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13489. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13490. ;;; Extra keys for tty access.
  13491. ;; We only set them when really needed because otherwise the
  13492. ;; menus don't show the simple keys
  13493. (when (or org-use-extra-keys
  13494. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13495. (not window-system))
  13496. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13497. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13498. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13499. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13500. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13501. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13502. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13503. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13504. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13505. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13506. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13507. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13508. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13509. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13510. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13511. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13512. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13513. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13514. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13515. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13516. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13517. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13518. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13519. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13520. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13521. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13522. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13523. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13524. ;; All the other keys
  13525. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13526. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13527. (if (boundp 'narrow-map)
  13528. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13529. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13530. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13531. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13532. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13533. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13534. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  13535. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13536. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13537. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13538. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13539. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13540. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13541. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13542. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13543. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13544. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13545. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13546. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13547. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13548. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13549. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13550. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13551. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13552. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13553. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13554. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13555. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13556. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13557. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13558. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13559. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13560. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13561. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13562. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13563. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13564. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13565. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13566. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13567. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13568. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13569. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13570. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13571. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13572. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13573. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13574. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13575. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13576. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13577. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13578. (org-defkey org-mode-map "\C-m" 'org-return)
  13579. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13580. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13581. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13582. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13583. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13584. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13585. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13586. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13587. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13588. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13589. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13590. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13591. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13592. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13593. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13594. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13595. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13596. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  13597. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  13598. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13599. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13600. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13601. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13602. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13603. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13604. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13605. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13606. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13607. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13608. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13609. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13610. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13611. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13612. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13613. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13614. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13615. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13616. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13617. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13618. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13619. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13620. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13621. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13622. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13623. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13624. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13625. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13626. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13627. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13628. (when (featurep 'xemacs)
  13629. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13630. (defconst org-speed-commands-default
  13631. '(
  13632. ("n" . org-speed-move-safe)
  13633. ("p" . org-speed-move-safe)
  13634. ("f" . org-speed-move-safe)
  13635. ("b" . org-speed-move-safe)
  13636. ("u" . org-speed-move-safe)
  13637. (" " . org-display-outline-path)
  13638. ("c" . org-cycle)
  13639. ("C" . org-shifttab)
  13640. ("U" . org-shiftmetaup)
  13641. ("D" . org-shiftmetadown)
  13642. ("r" . org-metaright)
  13643. ("l" . org-metaleft)
  13644. ("R" . org-shiftmetaright)
  13645. ("L" . org-shiftmetaleft)
  13646. ("i" . (progn (forward-char 1) (call-interactively
  13647. 'org-insert-heading-respect-content)))
  13648. ("v" . org-agenda)
  13649. ("/" . org-sparse-tree)
  13650. (";" . org-set-tags-command)
  13651. ("I" . org-clock-in)
  13652. ("O" . org-clock-out)
  13653. ("o" . org-open-at-point)
  13654. ("t" . org-todo)
  13655. ("j" . org-goto)
  13656. ("g" . (org-refile t))
  13657. ("e" . org-set-effort)
  13658. ("0" . (org-priority ?\ ))
  13659. ("1" . (org-priority ?A))
  13660. ("2" . (org-priority ?B))
  13661. ("3" . (org-priority ?C))
  13662. ("." . outline-mark-subtree)
  13663. ("^" . org-sort)
  13664. ("w" . org-refile)
  13665. ("a" . org-archive-subtree-default-with-confirmation)
  13666. ("/" . org-sparse-tree)
  13667. ("?" . org-speed-command-help)
  13668. )
  13669. "The default speed commands.")
  13670. (defun org-print-speed-command (e)
  13671. (princ (car e))
  13672. (princ " ")
  13673. (if (symbolp (cdr e))
  13674. (princ (symbol-name (cdr e)))
  13675. (prin1 (cdr e)))
  13676. (princ "\n"))
  13677. (defun org-speed-command-help ()
  13678. "Show the available speed commands."
  13679. (interactive)
  13680. (if (not org-use-speed-commands)
  13681. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  13682. (with-output-to-temp-buffer "*Help*"
  13683. (princ "Speed commands\n==============\n")
  13684. (mapc 'org-print-speed-command org-speed-commands-user)
  13685. (princ "\n")
  13686. (mapc 'org-print-speed-command org-speed-commands-default))))
  13687. (defun org-speed-move-safe ()
  13688. (interactive)
  13689. (let ((pos (point))
  13690. (tbl '(("n" . outline-next-visible-heading)
  13691. ("p" . outline-previous-visible-heading)
  13692. ("f" . org-forward-same-level)
  13693. ("b" . org-backward-same-level)
  13694. ("u" . outline-up-heading))))
  13695. (call-interactively (cdr (assoc (this-command-keys) tbl)))
  13696. (unless (and (bolp) (org-on-heading-p))
  13697. (goto-char pos)
  13698. (error "Boundary reached"))))
  13699. (defvar org-self-insert-command-undo-counter 0)
  13700. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13701. (defvar org-speed-command nil)
  13702. (defun org-self-insert-command (N)
  13703. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13704. If the cursor is in a table looking at whitespace, the whitespace is
  13705. overwritten, and the table is not marked as requiring realignment."
  13706. (interactive "p")
  13707. (cond
  13708. ((and org-use-speed-commands
  13709. (or (and (bolp) (looking-at outline-regexp))
  13710. (and (functionp org-use-speed-commands)
  13711. (funcall org-use-speed-commands)))
  13712. (setq
  13713. org-speed-command
  13714. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  13715. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  13716. (cond
  13717. ((commandp org-speed-command)
  13718. (setq this-command org-speed-command)
  13719. (call-interactively org-speed-command))
  13720. ((functionp org-speed-command)
  13721. (funcall org-speed-command))
  13722. ((and org-speed-command (listp org-speed-command))
  13723. (eval org-speed-command))
  13724. (t (let (org-use-speed-commands)
  13725. (call-interactively 'org-self-insert-command)))))
  13726. ((and
  13727. (org-table-p)
  13728. (progn
  13729. ;; check if we blank the field, and if that triggers align
  13730. (and (featurep 'org-table) org-table-auto-blank-field
  13731. (member last-command
  13732. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13733. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13734. ;; got extra space, this field does not determine column width
  13735. (let (org-table-may-need-update) (org-table-blank-field))
  13736. ;; no extra space, this field may determine column width
  13737. (org-table-blank-field)))
  13738. t)
  13739. (eq N 1)
  13740. (looking-at "[^|\n]* |"))
  13741. (let (org-table-may-need-update)
  13742. (goto-char (1- (match-end 0)))
  13743. (delete-backward-char 1)
  13744. (goto-char (match-beginning 0))
  13745. (self-insert-command N)))
  13746. (t
  13747. (setq org-table-may-need-update t)
  13748. (self-insert-command N)
  13749. (org-fix-tags-on-the-fly)
  13750. (if org-self-insert-cluster-for-undo
  13751. (if (not (eq last-command 'org-self-insert-command))
  13752. (setq org-self-insert-command-undo-counter 1)
  13753. (if (>= org-self-insert-command-undo-counter 20)
  13754. (setq org-self-insert-command-undo-counter 1)
  13755. (and (> org-self-insert-command-undo-counter 0)
  13756. buffer-undo-list
  13757. (not (cadr buffer-undo-list)) ; remove nil entry
  13758. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13759. (setq org-self-insert-command-undo-counter
  13760. (1+ org-self-insert-command-undo-counter))))))))
  13761. (defun org-fix-tags-on-the-fly ()
  13762. (when (and (equal (char-after (point-at-bol)) ?*)
  13763. (org-on-heading-p))
  13764. (org-align-tags-here org-tags-column)))
  13765. (defun org-delete-backward-char (N)
  13766. "Like `delete-backward-char', insert whitespace at field end in tables.
  13767. When deleting backwards, in tables this function will insert whitespace in
  13768. front of the next \"|\" separator, to keep the table aligned. The table will
  13769. still be marked for re-alignment if the field did fill the entire column,
  13770. because, in this case the deletion might narrow the column."
  13771. (interactive "p")
  13772. (if (and (org-table-p)
  13773. (eq N 1)
  13774. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13775. (looking-at ".*?|"))
  13776. (let ((pos (point))
  13777. (noalign (looking-at "[^|\n\r]* |"))
  13778. (c org-table-may-need-update))
  13779. (backward-delete-char N)
  13780. (skip-chars-forward "^|")
  13781. (insert " ")
  13782. (goto-char (1- pos))
  13783. ;; noalign: if there were two spaces at the end, this field
  13784. ;; does not determine the width of the column.
  13785. (if noalign (setq org-table-may-need-update c)))
  13786. (backward-delete-char N)
  13787. (org-fix-tags-on-the-fly)))
  13788. (defun org-delete-char (N)
  13789. "Like `delete-char', but insert whitespace at field end in tables.
  13790. When deleting characters, in tables this function will insert whitespace in
  13791. front of the next \"|\" separator, to keep the table aligned. The table will
  13792. still be marked for re-alignment if the field did fill the entire column,
  13793. because, in this case the deletion might narrow the column."
  13794. (interactive "p")
  13795. (if (and (org-table-p)
  13796. (not (bolp))
  13797. (not (= (char-after) ?|))
  13798. (eq N 1))
  13799. (if (looking-at ".*?|")
  13800. (let ((pos (point))
  13801. (noalign (looking-at "[^|\n\r]* |"))
  13802. (c org-table-may-need-update))
  13803. (replace-match (concat
  13804. (substring (match-string 0) 1 -1)
  13805. " |"))
  13806. (goto-char pos)
  13807. ;; noalign: if there were two spaces at the end, this field
  13808. ;; does not determine the width of the column.
  13809. (if noalign (setq org-table-may-need-update c)))
  13810. (delete-char N))
  13811. (delete-char N)
  13812. (org-fix-tags-on-the-fly)))
  13813. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13814. (put 'org-self-insert-command 'delete-selection t)
  13815. (put 'orgtbl-self-insert-command 'delete-selection t)
  13816. (put 'org-delete-char 'delete-selection 'supersede)
  13817. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13818. (put 'org-yank 'delete-selection 'yank)
  13819. ;; Make `flyspell-mode' delay after some commands
  13820. (put 'org-self-insert-command 'flyspell-delayed t)
  13821. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13822. (put 'org-delete-char 'flyspell-delayed t)
  13823. (put 'org-delete-backward-char 'flyspell-delayed t)
  13824. ;; Make pabbrev-mode expand after org-mode commands
  13825. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13826. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13827. ;; How to do this: Measure non-white length of current string
  13828. ;; If equal to column width, we should realign.
  13829. (defun org-remap (map &rest commands)
  13830. "In MAP, remap the functions given in COMMANDS.
  13831. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13832. (let (new old)
  13833. (while commands
  13834. (setq old (pop commands) new (pop commands))
  13835. (if (fboundp 'command-remapping)
  13836. (org-defkey map (vector 'remap old) new)
  13837. (substitute-key-definition old new map global-map)))))
  13838. (when (eq org-enable-table-editor 'optimized)
  13839. ;; If the user wants maximum table support, we need to hijack
  13840. ;; some standard editing functions
  13841. (org-remap org-mode-map
  13842. 'self-insert-command 'org-self-insert-command
  13843. 'delete-char 'org-delete-char
  13844. 'delete-backward-char 'org-delete-backward-char)
  13845. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13846. (defvar org-ctrl-c-ctrl-c-hook nil
  13847. "Hook for functions attaching themselves to `C-c C-c'.
  13848. This can be used to add additional functionality to the C-c C-c key which
  13849. executes context-dependent commands.
  13850. Each function will be called with no arguments. The function must check
  13851. if the context is appropriate for it to act. If yes, it should do its
  13852. thing and then return a non-nil value. If the context is wrong,
  13853. just do nothing and return nil.")
  13854. (defvar org-tab-first-hook nil
  13855. "Hook for functions to attach themselves to TAB.
  13856. See `org-ctrl-c-ctrl-c-hook' for more information.
  13857. This hook runs as the first action when TAB is pressed, even before
  13858. `org-cycle' messes around with the `outline-regexp' to cater for
  13859. inline tasks and plain list item folding.
  13860. If any function in this hook returns t, not other actions like table
  13861. field motion visibility cycling will be done.")
  13862. (defvar org-tab-after-check-for-table-hook nil
  13863. "Hook for functions to attach themselves to TAB.
  13864. See `org-ctrl-c-ctrl-c-hook' for more information.
  13865. This hook runs after it has been established that the cursor is not in a
  13866. table, but before checking if the cursor is in a headline or if global cycling
  13867. should be done.
  13868. If any function in this hook returns t, not other actions like visibility
  13869. cycling will be done.")
  13870. (defvar org-tab-after-check-for-cycling-hook nil
  13871. "Hook for functions to attach themselves to TAB.
  13872. See `org-ctrl-c-ctrl-c-hook' for more information.
  13873. This hook runs after it has been established that not table field motion and
  13874. not visibility should be done because of current context. This is probably
  13875. the place where a package like yasnippets can hook in.")
  13876. (defvar org-tab-before-tab-emulation-hook nil
  13877. "Hook for functions to attach themselves to TAB.
  13878. See `org-ctrl-c-ctrl-c-hook' for more information.
  13879. This hook runs after every other options for TAB have been exhausted, but
  13880. before indentation and \t insertion takes place.")
  13881. (defvar org-metaleft-hook nil
  13882. "Hook for functions attaching themselves to `M-left'.
  13883. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13884. (defvar org-metaright-hook nil
  13885. "Hook for functions attaching themselves to `M-right'.
  13886. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13887. (defvar org-metaup-hook nil
  13888. "Hook for functions attaching themselves to `M-up'.
  13889. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13890. (defvar org-metadown-hook nil
  13891. "Hook for functions attaching themselves to `M-down'.
  13892. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13893. (defvar org-shiftmetaleft-hook nil
  13894. "Hook for functions attaching themselves to `M-S-left'.
  13895. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13896. (defvar org-shiftmetaright-hook nil
  13897. "Hook for functions attaching themselves to `M-S-right'.
  13898. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13899. (defvar org-shiftmetaup-hook nil
  13900. "Hook for functions attaching themselves to `M-S-up'.
  13901. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13902. (defvar org-shiftmetadown-hook nil
  13903. "Hook for functions attaching themselves to `M-S-down'.
  13904. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13905. (defvar org-metareturn-hook nil
  13906. "Hook for functions attaching themselves to `M-RET'.
  13907. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13908. (defun org-modifier-cursor-error ()
  13909. "Throw an error, a modified cursor command was applied in wrong context."
  13910. (error "This command is active in special context like tables, headlines or items"))
  13911. (defun org-shiftselect-error ()
  13912. "Throw an error because Shift-Cursor command was applied in wrong context."
  13913. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13914. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  13915. (error "This command works only in special context like headlines or timestamps")))
  13916. (defun org-call-for-shift-select (cmd)
  13917. (let ((this-command-keys-shift-translated t))
  13918. (call-interactively cmd)))
  13919. (defun org-shifttab (&optional arg)
  13920. "Global visibility cycling or move to previous table field.
  13921. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13922. on context.
  13923. See the individual commands for more information."
  13924. (interactive "P")
  13925. (cond
  13926. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13927. ((integerp arg)
  13928. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  13929. (message "Content view to level: %d" arg)
  13930. (org-content (prefix-numeric-value arg2))
  13931. (setq org-cycle-global-status 'overview)))
  13932. (t (call-interactively 'org-global-cycle))))
  13933. (defun org-shiftmetaleft ()
  13934. "Promote subtree or delete table column.
  13935. Calls `org-promote-subtree', `org-outdent-item',
  13936. or `org-table-delete-column', depending on context.
  13937. See the individual commands for more information."
  13938. (interactive)
  13939. (cond
  13940. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13941. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13942. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13943. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13944. (t (org-modifier-cursor-error))))
  13945. (defun org-shiftmetaright ()
  13946. "Demote subtree or insert table column.
  13947. Calls `org-demote-subtree', `org-indent-item',
  13948. or `org-table-insert-column', depending on context.
  13949. See the individual commands for more information."
  13950. (interactive)
  13951. (cond
  13952. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13953. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13954. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13955. ((org-at-item-p) (call-interactively 'org-indent-item))
  13956. (t (org-modifier-cursor-error))))
  13957. (defun org-shiftmetaup (&optional arg)
  13958. "Move subtree up or kill table row.
  13959. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13960. `org-move-item-up' depending on context. See the individual commands
  13961. for more information."
  13962. (interactive "P")
  13963. (cond
  13964. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13965. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13966. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13967. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13968. (t (org-modifier-cursor-error))))
  13969. (defun org-shiftmetadown (&optional arg)
  13970. "Move subtree down or insert table row.
  13971. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13972. `org-move-item-down', depending on context. See the individual
  13973. commands for more information."
  13974. (interactive "P")
  13975. (cond
  13976. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13977. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13978. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13979. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13980. (t (org-modifier-cursor-error))))
  13981. (defun org-metaleft (&optional arg)
  13982. "Promote heading or move table column to left.
  13983. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13984. With no specific context, calls the Emacs default `backward-word'.
  13985. See the individual commands for more information."
  13986. (interactive "P")
  13987. (cond
  13988. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13989. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13990. ((or (org-on-heading-p)
  13991. (and (org-region-active-p)
  13992. (save-excursion
  13993. (goto-char (region-beginning))
  13994. (org-on-heading-p))))
  13995. (call-interactively 'org-do-promote))
  13996. ((or (org-at-item-p)
  13997. (and (org-region-active-p)
  13998. (save-excursion
  13999. (goto-char (region-beginning))
  14000. (org-at-item-p))))
  14001. (call-interactively 'org-outdent-item))
  14002. (t (call-interactively 'backward-word))))
  14003. (defun org-metaright (&optional arg)
  14004. "Demote subtree or move table column to right.
  14005. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14006. With no specific context, calls the Emacs default `forward-word'.
  14007. See the individual commands for more information."
  14008. (interactive "P")
  14009. (cond
  14010. ((run-hook-with-args-until-success 'org-metaright-hook))
  14011. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14012. ((or (org-on-heading-p)
  14013. (and (org-region-active-p)
  14014. (save-excursion
  14015. (goto-char (region-beginning))
  14016. (org-on-heading-p))))
  14017. (call-interactively 'org-do-demote))
  14018. ((or (org-at-item-p)
  14019. (and (org-region-active-p)
  14020. (save-excursion
  14021. (goto-char (region-beginning))
  14022. (org-at-item-p))))
  14023. (call-interactively 'org-indent-item))
  14024. (t (call-interactively 'forward-word))))
  14025. (defun org-metaup (&optional arg)
  14026. "Move subtree up or move table row up.
  14027. Calls `org-move-subtree-up' or `org-table-move-row' or
  14028. `org-move-item-up', depending on context. See the individual commands
  14029. for more information."
  14030. (interactive "P")
  14031. (cond
  14032. ((run-hook-with-args-until-success 'org-metaup-hook))
  14033. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14034. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14035. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14036. (t (transpose-lines 1) (beginning-of-line -1))))
  14037. (defun org-metadown (&optional arg)
  14038. "Move subtree down or move table row down.
  14039. Calls `org-move-subtree-down' or `org-table-move-row' or
  14040. `org-move-item-down', depending on context. See the individual
  14041. commands for more information."
  14042. (interactive "P")
  14043. (cond
  14044. ((run-hook-with-args-until-success 'org-metadown-hook))
  14045. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14046. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14047. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14048. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14049. (defun org-shiftup (&optional arg)
  14050. "Increase item in timestamp or increase priority of current headline.
  14051. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14052. depending on context. See the individual commands for more information."
  14053. (interactive "P")
  14054. (cond
  14055. ((and org-support-shift-select (org-region-active-p))
  14056. (org-call-for-shift-select 'previous-line))
  14057. ((org-at-timestamp-p t)
  14058. (call-interactively (if org-edit-timestamp-down-means-later
  14059. 'org-timestamp-down 'org-timestamp-up)))
  14060. ((and (not (eq org-support-shift-select 'always))
  14061. org-enable-priority-commands
  14062. (org-on-heading-p))
  14063. (call-interactively 'org-priority-up))
  14064. ((and (not org-support-shift-select) (org-at-item-p))
  14065. (call-interactively 'org-previous-item))
  14066. ((org-clocktable-try-shift 'up arg))
  14067. (org-support-shift-select
  14068. (org-call-for-shift-select 'previous-line))
  14069. (t (org-shiftselect-error))))
  14070. (defun org-shiftdown (&optional arg)
  14071. "Decrease item in timestamp or decrease priority of current headline.
  14072. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14073. depending on context. See the individual commands for more information."
  14074. (interactive "P")
  14075. (cond
  14076. ((and org-support-shift-select (org-region-active-p))
  14077. (org-call-for-shift-select 'next-line))
  14078. ((org-at-timestamp-p t)
  14079. (call-interactively (if org-edit-timestamp-down-means-later
  14080. 'org-timestamp-up 'org-timestamp-down)))
  14081. ((and (not (eq org-support-shift-select 'always))
  14082. org-enable-priority-commands
  14083. (org-on-heading-p))
  14084. (call-interactively 'org-priority-down))
  14085. ((and (not org-support-shift-select) (org-at-item-p))
  14086. (call-interactively 'org-next-item))
  14087. ((org-clocktable-try-shift 'down arg))
  14088. (org-support-shift-select
  14089. (org-call-for-shift-select 'next-line))
  14090. (t (org-shiftselect-error))))
  14091. (defun org-shiftright (&optional arg)
  14092. "Cycle the thing at point or in the current line, depending on context.
  14093. Depending on context, this does one of the following:
  14094. - switch a timestamp at point one day into the future
  14095. - on a headline, switch to the next TODO keyword.
  14096. - on an item, switch entire list to the next bullet type
  14097. - on a property line, switch to the next allowed value
  14098. - on a clocktable definition line, move time block into the future"
  14099. (interactive "P")
  14100. (cond
  14101. ((and org-support-shift-select (org-region-active-p))
  14102. (org-call-for-shift-select 'forward-char))
  14103. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14104. ((and (not (eq org-support-shift-select 'always))
  14105. (org-on-heading-p))
  14106. (let ((org-inhibit-logging
  14107. (not org-treat-S-cursor-todo-selection-as-state-change))
  14108. (org-inhibit-blocking
  14109. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14110. (org-call-with-arg 'org-todo 'right)))
  14111. ((or (and org-support-shift-select
  14112. (not (eq org-support-shift-select 'always))
  14113. (org-at-item-bullet-p))
  14114. (and (not org-support-shift-select) (org-at-item-p)))
  14115. (org-call-with-arg 'org-cycle-list-bullet nil))
  14116. ((and (not (eq org-support-shift-select 'always))
  14117. (org-at-property-p))
  14118. (call-interactively 'org-property-next-allowed-value))
  14119. ((org-clocktable-try-shift 'right arg))
  14120. (org-support-shift-select
  14121. (org-call-for-shift-select 'forward-char))
  14122. (t (org-shiftselect-error))))
  14123. (defun org-shiftleft (&optional arg)
  14124. "Cycle the thing at point or in the current line, depending on context.
  14125. Depending on context, this does one of the following:
  14126. - switch a timestamp at point one day into the past
  14127. - on a headline, switch to the previous TODO keyword.
  14128. - on an item, switch entire list to the previous bullet type
  14129. - on a property line, switch to the previous allowed value
  14130. - on a clocktable definition line, move time block into the past"
  14131. (interactive "P")
  14132. (cond
  14133. ((and org-support-shift-select (org-region-active-p))
  14134. (org-call-for-shift-select 'backward-char))
  14135. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14136. ((and (not (eq org-support-shift-select 'always))
  14137. (org-on-heading-p))
  14138. (let ((org-inhibit-logging
  14139. (not org-treat-S-cursor-todo-selection-as-state-change))
  14140. (org-inhibit-blocking
  14141. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14142. (org-call-with-arg 'org-todo 'left)))
  14143. ((or (and org-support-shift-select
  14144. (not (eq org-support-shift-select 'always))
  14145. (org-at-item-bullet-p))
  14146. (and (not org-support-shift-select) (org-at-item-p)))
  14147. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14148. ((and (not (eq org-support-shift-select 'always))
  14149. (org-at-property-p))
  14150. (call-interactively 'org-property-previous-allowed-value))
  14151. ((org-clocktable-try-shift 'left arg))
  14152. (org-support-shift-select
  14153. (org-call-for-shift-select 'backward-char))
  14154. (t (org-shiftselect-error))))
  14155. (defun org-shiftcontrolright ()
  14156. "Switch to next TODO set."
  14157. (interactive)
  14158. (cond
  14159. ((and org-support-shift-select (org-region-active-p))
  14160. (org-call-for-shift-select 'forward-word))
  14161. ((and (not (eq org-support-shift-select 'always))
  14162. (org-on-heading-p))
  14163. (org-call-with-arg 'org-todo 'nextset))
  14164. (org-support-shift-select
  14165. (org-call-for-shift-select 'forward-word))
  14166. (t (org-shiftselect-error))))
  14167. (defun org-shiftcontrolleft ()
  14168. "Switch to previous TODO set."
  14169. (interactive)
  14170. (cond
  14171. ((and org-support-shift-select (org-region-active-p))
  14172. (org-call-for-shift-select 'backward-word))
  14173. ((and (not (eq org-support-shift-select 'always))
  14174. (org-on-heading-p))
  14175. (org-call-with-arg 'org-todo 'previousset))
  14176. (org-support-shift-select
  14177. (org-call-for-shift-select 'backward-word))
  14178. (t (org-shiftselect-error))))
  14179. (defun org-ctrl-c-ret ()
  14180. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14181. (interactive)
  14182. (cond
  14183. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14184. (t (call-interactively 'org-insert-heading))))
  14185. (defun org-copy-special ()
  14186. "Copy region in table or copy current subtree.
  14187. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14188. See the individual commands for more information."
  14189. (interactive)
  14190. (call-interactively
  14191. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14192. (defun org-cut-special ()
  14193. "Cut region in table or cut current subtree.
  14194. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14195. See the individual commands for more information."
  14196. (interactive)
  14197. (call-interactively
  14198. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14199. (defun org-paste-special (arg)
  14200. "Paste rectangular region into table, or past subtree relative to level.
  14201. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14202. See the individual commands for more information."
  14203. (interactive "P")
  14204. (if (org-at-table-p)
  14205. (org-table-paste-rectangle)
  14206. (org-paste-subtree arg)))
  14207. (defun org-edit-special ()
  14208. "Call a special editor for the stuff at point.
  14209. When at a table, call the formula editor with `org-table-edit-formulas'.
  14210. When at the first line of an src example, call `org-edit-src-code'.
  14211. When in an #+include line, visit the include file. Otherwise call
  14212. `ffap' to visit the file at point."
  14213. (interactive)
  14214. (cond
  14215. ((org-at-table-p)
  14216. (call-interactively 'org-table-edit-formulas))
  14217. ((save-excursion
  14218. (beginning-of-line 1)
  14219. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14220. (find-file (org-trim (match-string 1))))
  14221. ((org-edit-src-code))
  14222. ((org-edit-fixed-width-region))
  14223. (t (call-interactively 'ffap))))
  14224. (defun org-ctrl-c-ctrl-c (&optional arg)
  14225. "Set tags in headline, or update according to changed information at point.
  14226. This command does many different things, depending on context:
  14227. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14228. this is what we do.
  14229. - If the cursor is on a statistics cookie, update it.
  14230. - If the cursor is in a headline, prompt for tags and insert them
  14231. into the current line, aligned to `org-tags-column'. When called
  14232. with prefix arg, realign all tags in the current buffer.
  14233. - If the cursor is in one of the special #+KEYWORD lines, this
  14234. triggers scanning the buffer for these lines and updating the
  14235. information.
  14236. - If the cursor is inside a table, realign the table. This command
  14237. works even if the automatic table editor has been turned off.
  14238. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14239. the entire table.
  14240. - If the cursor is at a footnote reference or definition, jump to
  14241. the corresponding definition or references, respectively.
  14242. - If the cursor is a the beginning of a dynamic block, update it.
  14243. - If the cursor is inside a table created by the table.el package,
  14244. activate that table.
  14245. - If the current buffer is a remember buffer, close note and file
  14246. it. A prefix argument of 1 files to the default location
  14247. without further interaction. A prefix argument of 2 files to
  14248. the currently clocking task.
  14249. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14250. links in this buffer.
  14251. - If the cursor is on a numbered item in a plain list, renumber the
  14252. ordered list.
  14253. - If the cursor is on a checkbox, toggle it."
  14254. (interactive "P")
  14255. (let ((org-enable-table-editor t))
  14256. (cond
  14257. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14258. org-occur-highlights
  14259. org-latex-fragment-image-overlays)
  14260. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14261. (org-remove-occur-highlights)
  14262. (org-remove-latex-fragment-image-overlays)
  14263. (message "Temporary highlights/overlays removed from current buffer"))
  14264. ((and (local-variable-p 'org-finish-function (current-buffer))
  14265. (fboundp org-finish-function))
  14266. (funcall org-finish-function))
  14267. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14268. ((org-at-property-p)
  14269. (call-interactively 'org-property-action))
  14270. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14271. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14272. (or (org-on-heading-p) (org-at-item-p)))
  14273. (call-interactively 'org-update-statistics-cookies))
  14274. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14275. ((org-at-table.el-p)
  14276. (require 'table)
  14277. (beginning-of-line 1)
  14278. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14279. (call-interactively 'table-recognize-table))
  14280. ((org-at-table-p)
  14281. (org-table-maybe-eval-formula)
  14282. (if arg
  14283. (call-interactively 'org-table-recalculate)
  14284. (org-table-maybe-recalculate-line))
  14285. (call-interactively 'org-table-align))
  14286. ((or (org-footnote-at-reference-p)
  14287. (org-footnote-at-definition-p))
  14288. (call-interactively 'org-footnote-action))
  14289. ((org-at-item-checkbox-p)
  14290. (call-interactively 'org-toggle-checkbox))
  14291. ((org-at-item-p)
  14292. (if arg
  14293. (call-interactively 'org-toggle-checkbox)
  14294. (call-interactively 'org-maybe-renumber-ordered-list)))
  14295. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14296. ;; Dynamic block
  14297. (beginning-of-line 1)
  14298. (save-excursion (org-update-dblock)))
  14299. ((save-excursion
  14300. (beginning-of-line 1)
  14301. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14302. (cond
  14303. ((equal (match-string 1) "TBLFM")
  14304. ;; Recalculate the table before this line
  14305. (save-excursion
  14306. (beginning-of-line 1)
  14307. (skip-chars-backward " \r\n\t")
  14308. (if (org-at-table-p)
  14309. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14310. (t
  14311. ; (org-set-regexps-and-options)
  14312. ; (org-restart-font-lock)
  14313. (let ((org-inhibit-startup t)) (org-mode-restart))
  14314. (message "Local setup has been refreshed"))))
  14315. ((org-clock-update-time-maybe))
  14316. (t (error "C-c C-c can do nothing useful at this location")))))
  14317. (defun org-mode-restart ()
  14318. "Restart Org-mode, to scan again for special lines.
  14319. Also updates the keyword regular expressions."
  14320. (interactive)
  14321. (org-mode)
  14322. (message "Org-mode restarted"))
  14323. (defun org-kill-note-or-show-branches ()
  14324. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14325. (interactive)
  14326. (if (not org-finish-function)
  14327. (call-interactively 'show-branches)
  14328. (let ((org-note-abort t))
  14329. (funcall org-finish-function))))
  14330. (defun org-return (&optional indent)
  14331. "Goto next table row or insert a newline.
  14332. Calls `org-table-next-row' or `newline', depending on context.
  14333. See the individual commands for more information."
  14334. (interactive)
  14335. (cond
  14336. ((bobp) (if indent (newline-and-indent) (newline)))
  14337. ((org-at-table-p)
  14338. (org-table-justify-field-maybe)
  14339. (call-interactively 'org-table-next-row))
  14340. ((and org-return-follows-link
  14341. (eq (get-text-property (point) 'face) 'org-link))
  14342. (call-interactively 'org-open-at-point))
  14343. ((and (org-at-heading-p)
  14344. (looking-at
  14345. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14346. (org-show-entry)
  14347. (end-of-line 1)
  14348. (newline))
  14349. (t (if indent (newline-and-indent) (newline)))))
  14350. (defun org-return-indent ()
  14351. "Goto next table row or insert a newline and indent.
  14352. Calls `org-table-next-row' or `newline-and-indent', depending on
  14353. context. See the individual commands for more information."
  14354. (interactive)
  14355. (org-return t))
  14356. (defun org-ctrl-c-star ()
  14357. "Compute table, or change heading status of lines.
  14358. Calls `org-table-recalculate' or `org-toggle-heading',
  14359. depending on context."
  14360. (interactive)
  14361. (cond
  14362. ((org-at-table-p)
  14363. (call-interactively 'org-table-recalculate))
  14364. (t
  14365. ;; Convert all lines in region to list items
  14366. (call-interactively 'org-toggle-heading))))
  14367. (defun org-ctrl-c-minus ()
  14368. "Insert separator line in table or modify bullet status of line.
  14369. Also turns a plain line or a region of lines into list items.
  14370. Calls `org-table-insert-hline', `org-toggle-item', or
  14371. `org-cycle-list-bullet', depending on context."
  14372. (interactive)
  14373. (cond
  14374. ((org-at-table-p)
  14375. (call-interactively 'org-table-insert-hline))
  14376. ((org-region-active-p)
  14377. (call-interactively 'org-toggle-item))
  14378. ((org-in-item-p)
  14379. (call-interactively 'org-cycle-list-bullet))
  14380. (t
  14381. (call-interactively 'org-toggle-item))))
  14382. (defun org-toggle-item ()
  14383. "Convert headings or normal lines to items, items to normal lines.
  14384. If there is no active region, only the current line is considered.
  14385. If the first line in the region is a headline, convert all headlines to items.
  14386. If the first line in the region is an item, convert all items to normal lines.
  14387. If the first line is normal text, add an item bullet to each line."
  14388. (interactive)
  14389. (let (l2 l beg end)
  14390. (if (org-region-active-p)
  14391. (setq beg (region-beginning) end (region-end))
  14392. (setq beg (point-at-bol)
  14393. end (min (1+ (point-at-eol)) (point-max))))
  14394. (save-excursion
  14395. (goto-char end)
  14396. (setq l2 (org-current-line))
  14397. (goto-char beg)
  14398. (beginning-of-line 1)
  14399. (setq l (1- (org-current-line)))
  14400. (if (org-at-item-p)
  14401. ;; We already have items, de-itemize
  14402. (while (< (setq l (1+ l)) l2)
  14403. (when (org-at-item-p)
  14404. (goto-char (match-beginning 2))
  14405. (delete-region (match-beginning 2) (match-end 2))
  14406. (and (looking-at "[ \t]+") (replace-match "")))
  14407. (beginning-of-line 2))
  14408. (if (org-on-heading-p)
  14409. ;; Headings, convert to items
  14410. (while (< (setq l (1+ l)) l2)
  14411. (if (looking-at org-outline-regexp)
  14412. (replace-match "- " t t))
  14413. (beginning-of-line 2))
  14414. ;; normal lines, turn them into items
  14415. (while (< (setq l (1+ l)) l2)
  14416. (unless (org-at-item-p)
  14417. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14418. (replace-match "\\1- \\2")))
  14419. (beginning-of-line 2)))))))
  14420. (defun org-toggle-heading (&optional nstars)
  14421. "Convert headings to normal text, or items or text to headings.
  14422. If there is no active region, only the current line is considered.
  14423. If the first line is a heading, remove the stars from all headlines
  14424. in the region.
  14425. If the first line is a plain list item, turn all plain list items
  14426. into headings.
  14427. If the first line is a normal line, turn each and every line in the
  14428. region into a heading.
  14429. When converting a line into a heading, the number of stars is chosen
  14430. such that the lines become children of the current entry. However,
  14431. when a prefix argument is given, its value determines the number of
  14432. stars to add."
  14433. (interactive "P")
  14434. (let (l2 l itemp beg end)
  14435. (if (org-region-active-p)
  14436. (setq beg (region-beginning) end (region-end))
  14437. (setq beg (point-at-bol)
  14438. end (min (1+ (point-at-eol)) (point-max))))
  14439. (save-excursion
  14440. (goto-char end)
  14441. (setq l2 (org-current-line))
  14442. (goto-char beg)
  14443. (beginning-of-line 1)
  14444. (setq l (1- (org-current-line)))
  14445. (if (org-on-heading-p)
  14446. ;; We already have headlines, de-star them
  14447. (while (< (setq l (1+ l)) l2)
  14448. (when (org-on-heading-p t)
  14449. (and (looking-at outline-regexp) (replace-match "")))
  14450. (beginning-of-line 2))
  14451. (setq itemp (org-at-item-p))
  14452. (let* ((stars
  14453. (if nstars
  14454. (make-string (prefix-numeric-value current-prefix-arg)
  14455. ?*)
  14456. (save-excursion
  14457. (if (re-search-backward org-complex-heading-regexp nil t)
  14458. (match-string 1) ""))))
  14459. (add-stars (cond (nstars "")
  14460. ((equal stars "") "*")
  14461. (org-odd-levels-only "**")
  14462. (t "*")))
  14463. (rpl (concat stars add-stars " ")))
  14464. (while (< (setq l (1+ l)) l2)
  14465. (if itemp
  14466. (and (org-at-item-p) (replace-match rpl t t))
  14467. (unless (org-on-heading-p)
  14468. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14469. (replace-match (concat rpl (match-string 2))))))
  14470. (beginning-of-line 2)))))))
  14471. (defun org-meta-return (&optional arg)
  14472. "Insert a new heading or wrap a region in a table.
  14473. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14474. See the individual commands for more information."
  14475. (interactive "P")
  14476. (cond
  14477. ((run-hook-with-args-until-success 'org-metareturn-hook))
  14478. ((org-at-table-p)
  14479. (call-interactively 'org-table-wrap-region))
  14480. (t (call-interactively 'org-insert-heading))))
  14481. ;;; Menu entries
  14482. ;; Define the Org-mode menus
  14483. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14484. '("Tbl"
  14485. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  14486. ["Next Field" org-cycle (org-at-table-p)]
  14487. ["Previous Field" org-shifttab (org-at-table-p)]
  14488. ["Next Row" org-return (org-at-table-p)]
  14489. "--"
  14490. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14491. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14492. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14493. "--"
  14494. ("Column"
  14495. ["Move Column Left" org-metaleft (org-at-table-p)]
  14496. ["Move Column Right" org-metaright (org-at-table-p)]
  14497. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14498. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  14499. ("Row"
  14500. ["Move Row Up" org-metaup (org-at-table-p)]
  14501. ["Move Row Down" org-metadown (org-at-table-p)]
  14502. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14503. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14504. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14505. "--"
  14506. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  14507. ("Rectangle"
  14508. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14509. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14510. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14511. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14512. "--"
  14513. ("Calculate"
  14514. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14515. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14516. ["Edit Formulas" org-edit-special (org-at-table-p)]
  14517. "--"
  14518. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14519. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14520. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  14521. "--"
  14522. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14523. "--"
  14524. ["Sum Column/Rectangle" org-table-sum
  14525. (or (org-at-table-p) (org-region-active-p))]
  14526. ["Which Column?" org-table-current-column (org-at-table-p)])
  14527. ["Debug Formulas"
  14528. org-table-toggle-formula-debugger
  14529. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  14530. ["Show Col/Row Numbers"
  14531. org-table-toggle-coordinate-overlays
  14532. :style toggle
  14533. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14534. "--"
  14535. ["Create" org-table-create (and (not (org-at-table-p))
  14536. org-enable-table-editor)]
  14537. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14538. ["Import from File" org-table-import (not (org-at-table-p))]
  14539. ["Export to File" org-table-export (org-at-table-p)]
  14540. "--"
  14541. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14542. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14543. '("Org"
  14544. ("Show/Hide"
  14545. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14546. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14547. ["Sparse Tree..." org-sparse-tree t]
  14548. ["Reveal Context" org-reveal t]
  14549. ["Show All" show-all t]
  14550. "--"
  14551. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14552. "--"
  14553. ["New Heading" org-insert-heading t]
  14554. ("Navigate Headings"
  14555. ["Up" outline-up-heading t]
  14556. ["Next" outline-next-visible-heading t]
  14557. ["Previous" outline-previous-visible-heading t]
  14558. ["Next Same Level" outline-forward-same-level t]
  14559. ["Previous Same Level" outline-backward-same-level t]
  14560. "--"
  14561. ["Jump" org-goto t])
  14562. ("Edit Structure"
  14563. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14564. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14565. "--"
  14566. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14567. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14568. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14569. "--"
  14570. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14571. "--"
  14572. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14573. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14574. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14575. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14576. "--"
  14577. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14578. "--"
  14579. ["Convert to odd levels" org-convert-to-odd-levels t]
  14580. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14581. ("Editing"
  14582. ["Emphasis..." org-emphasize t]
  14583. ["Edit Source Example" org-edit-special t]
  14584. "--"
  14585. ["Footnote new/jump" org-footnote-action t]
  14586. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14587. ("Archive"
  14588. ["Archive (default method)" org-archive-subtree-default t]
  14589. "--"
  14590. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  14591. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14592. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  14593. )
  14594. "--"
  14595. ("Hyperlinks"
  14596. ["Store Link (Global)" org-store-link t]
  14597. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14598. ["Insert Link" org-insert-link t]
  14599. ["Follow Link" org-open-at-point t]
  14600. "--"
  14601. ["Next link" org-next-link t]
  14602. ["Previous link" org-previous-link t]
  14603. "--"
  14604. ["Descriptive Links"
  14605. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14606. :style radio
  14607. :selected (member '(org-link) buffer-invisibility-spec)]
  14608. ["Literal Links"
  14609. (progn
  14610. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14611. :style radio
  14612. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14613. "--"
  14614. ("TODO Lists"
  14615. ["TODO/DONE/-" org-todo t]
  14616. ("Select keyword"
  14617. ["Next keyword" org-shiftright (org-on-heading-p)]
  14618. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14619. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14620. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14621. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14622. ["Show TODO Tree" org-show-todo-tree t]
  14623. ["Global TODO list" org-todo-list t]
  14624. "--"
  14625. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14626. :selected org-enforce-todo-dependencies :style toggle :active t]
  14627. "Settings for tree at point"
  14628. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14629. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14630. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14631. ["Do Children parallel" org-toggle-ordered-property :style radio
  14632. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14633. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14634. "--"
  14635. ["Set Priority" org-priority t]
  14636. ["Priority Up" org-shiftup t]
  14637. ["Priority Down" org-shiftdown t]
  14638. "--"
  14639. ["Get news from all feeds" org-feed-update-all t]
  14640. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14641. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14642. ("TAGS and Properties"
  14643. ["Set Tags" org-set-tags-command t]
  14644. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14645. "--"
  14646. ["Set property" org-set-property t]
  14647. ["Column view of properties" org-columns t]
  14648. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14649. ("Dates and Scheduling"
  14650. ["Timestamp" org-time-stamp t]
  14651. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14652. ("Change Date"
  14653. ["1 Day Later" org-shiftright t]
  14654. ["1 Day Earlier" org-shiftleft t]
  14655. ["1 ... Later" org-shiftup t]
  14656. ["1 ... Earlier" org-shiftdown t])
  14657. ["Compute Time Range" org-evaluate-time-range t]
  14658. ["Schedule Item" org-schedule t]
  14659. ["Deadline" org-deadline t]
  14660. "--"
  14661. ["Custom time format" org-toggle-time-stamp-overlays
  14662. :style radio :selected org-display-custom-times]
  14663. "--"
  14664. ["Goto Calendar" org-goto-calendar t]
  14665. ["Date from Calendar" org-date-from-calendar t]
  14666. "--"
  14667. ["Start/Restart Timer" org-timer-start t]
  14668. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14669. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14670. ["Insert Timer String" org-timer t]
  14671. ["Insert Timer Item" org-timer-item t])
  14672. ("Logging work"
  14673. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14674. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14675. ["Clock out" org-clock-out t]
  14676. ["Clock cancel" org-clock-cancel t]
  14677. "--"
  14678. ["Mark as default task" org-clock-mark-default-task t]
  14679. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14680. ["Goto running clock" org-clock-goto t]
  14681. "--"
  14682. ["Display times" org-clock-display t]
  14683. ["Create clock table" org-clock-report t]
  14684. "--"
  14685. ["Record DONE time"
  14686. (progn (setq org-log-done (not org-log-done))
  14687. (message "Switching to %s will %s record a timestamp"
  14688. (car org-done-keywords)
  14689. (if org-log-done "automatically" "not")))
  14690. :style toggle :selected org-log-done])
  14691. "--"
  14692. ["Agenda Command..." org-agenda t]
  14693. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14694. ("File List for Agenda")
  14695. ("Special views current file"
  14696. ["TODO Tree" org-show-todo-tree t]
  14697. ["Check Deadlines" org-check-deadlines t]
  14698. ["Timeline" org-timeline t]
  14699. ["Tags/Property tree" org-match-sparse-tree t])
  14700. "--"
  14701. ["Export/Publish..." org-export t]
  14702. ("LaTeX"
  14703. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14704. :selected org-cdlatex-mode]
  14705. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14706. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14707. ["Modify math symbol" org-cdlatex-math-modify
  14708. (org-inside-LaTeX-fragment-p)]
  14709. ["Insert citation" org-reftex-citation t]
  14710. "--"
  14711. ["Export LaTeX fragments as images"
  14712. (if (featurep 'org-exp)
  14713. (setq org-export-with-LaTeX-fragments
  14714. (not org-export-with-LaTeX-fragments))
  14715. (require 'org-exp))
  14716. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14717. org-export-with-LaTeX-fragments)])
  14718. "--"
  14719. ("MobileOrg"
  14720. ["Push Files and Views" org-mobile-push t]
  14721. ["Get Captured and Flagged" org-mobile-pull t]
  14722. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  14723. "--"
  14724. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  14725. "--"
  14726. ("Documentation"
  14727. ["Show Version" org-version t]
  14728. ["Info Documentation" org-info t])
  14729. ("Customize"
  14730. ["Browse Org Group" org-customize t]
  14731. "--"
  14732. ["Expand This Menu" org-create-customize-menu
  14733. (fboundp 'customize-menu-create)])
  14734. ["Send bug report" org-submit-bug-report t]
  14735. "--"
  14736. ("Refresh/Reload"
  14737. ["Refresh setup current buffer" org-mode-restart t]
  14738. ["Reload Org (after update)" org-reload t]
  14739. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14740. ))
  14741. (defun org-info (&optional node)
  14742. "Read documentation for Org-mode in the info system.
  14743. With optional NODE, go directly to that node."
  14744. (interactive)
  14745. (info (format "(org)%s" (or node ""))))
  14746. ;;;###autoload
  14747. (defun org-submit-bug-report ()
  14748. "Submit a bug report on Org-mode via mail.
  14749. Don't hesitate to report any problems or inaccurate documentation.
  14750. If you don't have setup sending mail from (X)Emacs, please copy the
  14751. output buffer into your mail program, as it gives us important
  14752. information about your Org-mode version and configuration."
  14753. (interactive)
  14754. (require 'reporter)
  14755. (org-load-modules-maybe)
  14756. (org-require-autoloaded-modules)
  14757. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14758. (reporter-submit-bug-report
  14759. "emacs-orgmode@gnu.org"
  14760. (org-version)
  14761. (let (list)
  14762. (save-window-excursion
  14763. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14764. (delete-other-windows)
  14765. (erase-buffer)
  14766. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14767. We would like to add your full Org-mode and Outline configuration to the
  14768. bug report. This greatly simplifies the work of the maintainer and
  14769. other experts on the mailing list.
  14770. HOWEVER, some variables you have customized may contain private
  14771. information. The names of customers, colleagues, or friends, might
  14772. appear in the form of file names, tags, todo states, or search strings.
  14773. If you answer yes to the prompt, you might want to check and remove
  14774. such private information before sending the email.")
  14775. (add-text-properties (point-min) (point-max) '(face org-warning))
  14776. (when (yes-or-no-p "Include your Org-mode configuration ")
  14777. (mapatoms
  14778. (lambda (v)
  14779. (and (boundp v)
  14780. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14781. (or (and (symbol-value v)
  14782. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14783. (and
  14784. (get v 'custom-type) (get v 'standard-value)
  14785. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14786. (push v list)))))
  14787. (kill-buffer (get-buffer "*Warn about privacy*"))
  14788. list))
  14789. nil nil
  14790. "Remember to cover the basics, that is, what you expected to happen and
  14791. what in fact did happen. You don't know how to make a good report? See
  14792. http://orgmode.org/manual/Feedback.html#Feedback
  14793. Your bug report will be posted to the Org-mode mailing list.
  14794. ------------------------------------------------------------------------")
  14795. (save-excursion
  14796. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  14797. (replace-match "\\1Bug: \\3 [\\2]")))))
  14798. (defun org-install-agenda-files-menu ()
  14799. (let ((bl (buffer-list)))
  14800. (save-excursion
  14801. (while bl
  14802. (set-buffer (pop bl))
  14803. (if (org-mode-p) (setq bl nil)))
  14804. (when (org-mode-p)
  14805. (easy-menu-change
  14806. '("Org") "File List for Agenda"
  14807. (append
  14808. (list
  14809. ["Edit File List" (org-edit-agenda-file-list) t]
  14810. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14811. ["Remove Current File from List" org-remove-file t]
  14812. ["Cycle through agenda files" org-cycle-agenda-files t]
  14813. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14814. "--")
  14815. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14816. ;;;; Documentation
  14817. ;;;###autoload
  14818. (defun org-require-autoloaded-modules ()
  14819. (interactive)
  14820. (mapc 'require
  14821. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14822. org-docbook org-exp org-html org-icalendar
  14823. org-id org-latex
  14824. org-publish org-remember org-table
  14825. org-timer org-xoxo)))
  14826. ;;;###autoload
  14827. (defun org-reload (&optional uncompiled)
  14828. "Reload all org lisp files.
  14829. With prefix arg UNCOMPILED, load the uncompiled versions."
  14830. (interactive "P")
  14831. (require 'find-func)
  14832. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14833. (dir-org (file-name-directory (org-find-library-name "org")))
  14834. (dir-org-contrib (ignore-errors
  14835. (file-name-directory
  14836. (org-find-library-name "org-contribdir"))))
  14837. (files
  14838. (append (directory-files dir-org t file-re)
  14839. (and dir-org-contrib
  14840. (directory-files dir-org-contrib t file-re))))
  14841. (remove-re (concat (if (featurep 'xemacs)
  14842. "org-colview" "org-colview-xemacs")
  14843. "\\'")))
  14844. (setq files (mapcar 'file-name-sans-extension files))
  14845. (setq files (mapcar
  14846. (lambda (x) (if (string-match remove-re x) nil x))
  14847. files))
  14848. (setq files (delq nil files))
  14849. (mapc
  14850. (lambda (f)
  14851. (when (featurep (intern (file-name-nondirectory f)))
  14852. (if (and (not uncompiled)
  14853. (file-exists-p (concat f ".elc")))
  14854. (load (concat f ".elc") nil nil t)
  14855. (load (concat f ".el") nil nil t))))
  14856. files))
  14857. (org-version))
  14858. ;;;###autoload
  14859. (defun org-customize ()
  14860. "Call the customize function with org as argument."
  14861. (interactive)
  14862. (org-load-modules-maybe)
  14863. (org-require-autoloaded-modules)
  14864. (customize-browse 'org))
  14865. (defun org-create-customize-menu ()
  14866. "Create a full customization menu for Org-mode, insert it into the menu."
  14867. (interactive)
  14868. (org-load-modules-maybe)
  14869. (org-require-autoloaded-modules)
  14870. (if (fboundp 'customize-menu-create)
  14871. (progn
  14872. (easy-menu-change
  14873. '("Org") "Customize"
  14874. `(["Browse Org group" org-customize t]
  14875. "--"
  14876. ,(customize-menu-create 'org)
  14877. ["Set" Custom-set t]
  14878. ["Save" Custom-save t]
  14879. ["Reset to Current" Custom-reset-current t]
  14880. ["Reset to Saved" Custom-reset-saved t]
  14881. ["Reset to Standard Settings" Custom-reset-standard t]))
  14882. (message "\"Org\"-menu now contains full customization menu"))
  14883. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14884. ;;;; Miscellaneous stuff
  14885. ;;; Generally useful functions
  14886. (defun org-get-at-bol (property)
  14887. "Get text property PROPERTY at beginning of line."
  14888. (get-text-property (point-at-bol) property))
  14889. (defun org-find-text-property-in-string (prop s)
  14890. "Return the first non-nil value of property PROP in string S."
  14891. (or (get-text-property 0 prop s)
  14892. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14893. prop s)))
  14894. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14895. "Display the given MESSAGE as a warning."
  14896. (if (fboundp 'display-warning)
  14897. (display-warning 'org message
  14898. (if (featurep 'xemacs)
  14899. 'warning
  14900. :warning))
  14901. (let ((buf (get-buffer-create "*Org warnings*")))
  14902. (with-current-buffer buf
  14903. (goto-char (point-max))
  14904. (insert "Warning (Org): " message)
  14905. (unless (bolp)
  14906. (newline)))
  14907. (display-buffer buf)
  14908. (sit-for 0))))
  14909. (defun org-in-commented-line ()
  14910. "Is point in a line starting with `#'?"
  14911. (equal (char-after (point-at-bol)) ?#))
  14912. (defun org-in-verbatim-emphasis ()
  14913. (save-match-data
  14914. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  14915. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14916. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14917. (if (and marker (marker-buffer marker)
  14918. (buffer-live-p (marker-buffer marker)))
  14919. (progn
  14920. (switch-to-buffer (marker-buffer marker))
  14921. (if (or (> marker (point-max)) (< marker (point-min)))
  14922. (widen))
  14923. (goto-char marker)
  14924. (org-show-context 'org-goto))
  14925. (if bookmark
  14926. (bookmark-jump bookmark)
  14927. (error "Cannot find location"))))
  14928. (defun org-quote-csv-field (s)
  14929. "Quote field for inclusion in CSV material."
  14930. (if (string-match "[\",]" s)
  14931. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14932. s))
  14933. (defun org-plist-delete (plist property)
  14934. "Delete PROPERTY from PLIST.
  14935. This is in contrast to merely setting it to 0."
  14936. (let (p)
  14937. (while plist
  14938. (if (not (eq property (car plist)))
  14939. (setq p (plist-put p (car plist) (nth 1 plist))))
  14940. (setq plist (cddr plist)))
  14941. p))
  14942. (defun org-force-self-insert (N)
  14943. "Needed to enforce self-insert under remapping."
  14944. (interactive "p")
  14945. (self-insert-command N))
  14946. (defun org-string-width (s)
  14947. "Compute width of string, ignoring invisible characters.
  14948. This ignores character with invisibility property `org-link', and also
  14949. characters with property `org-cwidth', because these will become invisible
  14950. upon the next fontification round."
  14951. (let (b l)
  14952. (when (or (eq t buffer-invisibility-spec)
  14953. (assq 'org-link buffer-invisibility-spec))
  14954. (while (setq b (text-property-any 0 (length s)
  14955. 'invisible 'org-link s))
  14956. (setq s (concat (substring s 0 b)
  14957. (substring s (or (next-single-property-change
  14958. b 'invisible s) (length s)))))))
  14959. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14960. (setq s (concat (substring s 0 b)
  14961. (substring s (or (next-single-property-change
  14962. b 'org-cwidth s) (length s))))))
  14963. (setq l (string-width s) b -1)
  14964. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14965. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14966. l))
  14967. (defun org-get-indentation (&optional line)
  14968. "Get the indentation of the current line, interpreting tabs.
  14969. When LINE is given, assume it represents a line and compute its indentation."
  14970. (if line
  14971. (if (string-match "^ *" (org-remove-tabs line))
  14972. (match-end 0))
  14973. (save-excursion
  14974. (beginning-of-line 1)
  14975. (skip-chars-forward " \t")
  14976. (current-column))))
  14977. (defun org-remove-tabs (s &optional width)
  14978. "Replace tabulators in S with spaces.
  14979. Assumes that s is a single line, starting in column 0."
  14980. (setq width (or width tab-width))
  14981. (while (string-match "\t" s)
  14982. (setq s (replace-match
  14983. (make-string
  14984. (- (* width (/ (+ (match-beginning 0) width) width))
  14985. (match-beginning 0)) ?\ )
  14986. t t s)))
  14987. s)
  14988. (defun org-fix-indentation (line ind)
  14989. "Fix indentation in LINE.
  14990. IND is a cons cell with target and minimum indentation.
  14991. If the current indentation in LINE is smaller than the minimum,
  14992. leave it alone. If it is larger than ind, set it to the target."
  14993. (let* ((l (org-remove-tabs line))
  14994. (i (org-get-indentation l))
  14995. (i1 (car ind)) (i2 (cdr ind)))
  14996. (if (>= i i2) (setq l (substring line i2)))
  14997. (if (> i1 0)
  14998. (concat (make-string i1 ?\ ) l)
  14999. l)))
  15000. (defun org-remove-indentation (code &optional n)
  15001. "Remove the maximum common indentation from the lines in CODE.
  15002. N may optionally be the number of spaces to remove."
  15003. (with-temp-buffer
  15004. (insert code)
  15005. (org-do-remove-indentation n)
  15006. (buffer-string)))
  15007. (defun org-do-remove-indentation (&optional n)
  15008. "Remove the maximum common indentation from the buffer."
  15009. (untabify (point-min) (point-max))
  15010. (let ((min 10000) re)
  15011. (if n
  15012. (setq min n)
  15013. (goto-char (point-min))
  15014. (while (re-search-forward "^ *[^ \n]" nil t)
  15015. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15016. (unless (or (= min 0) (= min 10000))
  15017. (setq re (format "^ \\{%d\\}" min))
  15018. (goto-char (point-min))
  15019. (while (re-search-forward re nil t)
  15020. (replace-match "")
  15021. (end-of-line 1))
  15022. min)))
  15023. (defun org-fill-template (template alist)
  15024. "Find each %key of ALIST in TEMPLATE and replace it."
  15025. (let (entry key value)
  15026. (setq alist (sort (copy-sequence alist)
  15027. (lambda (a b) (< (length (car a)) (length (car b))))))
  15028. (while (setq entry (pop alist))
  15029. (setq template
  15030. (replace-regexp-in-string
  15031. (concat "%" (regexp-quote (car entry)))
  15032. (cdr entry) template t t)))
  15033. template))
  15034. (defun org-base-buffer (buffer)
  15035. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15036. (if (not buffer)
  15037. buffer
  15038. (or (buffer-base-buffer buffer)
  15039. buffer)))
  15040. (defun org-trim (s)
  15041. "Remove whitespace at beginning and end of string."
  15042. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15043. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15044. s)
  15045. (defun org-wrap (string &optional width lines)
  15046. "Wrap string to either a number of lines, or a width in characters.
  15047. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15048. that costs. If there is a word longer than WIDTH, the text is actually
  15049. wrapped to the length of that word.
  15050. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15051. many lines, whatever width that takes.
  15052. The return value is a list of lines, without newlines at the end."
  15053. (let* ((words (org-split-string string "[ \t\n]+"))
  15054. (maxword (apply 'max (mapcar 'org-string-width words)))
  15055. w ll)
  15056. (cond (width
  15057. (org-do-wrap words (max maxword width)))
  15058. (lines
  15059. (setq w maxword)
  15060. (setq ll (org-do-wrap words maxword))
  15061. (if (<= (length ll) lines)
  15062. ll
  15063. (setq ll words)
  15064. (while (> (length ll) lines)
  15065. (setq w (1+ w))
  15066. (setq ll (org-do-wrap words w)))
  15067. ll))
  15068. (t (error "Cannot wrap this")))))
  15069. (defun org-do-wrap (words width)
  15070. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15071. (let (lines line)
  15072. (while words
  15073. (setq line (pop words))
  15074. (while (and words (< (+ (length line) (length (car words))) width))
  15075. (setq line (concat line " " (pop words))))
  15076. (setq lines (push line lines)))
  15077. (nreverse lines)))
  15078. (defun org-split-string (string &optional separators)
  15079. "Splits STRING into substrings at SEPARATORS.
  15080. No empty strings are returned if there are matches at the beginning
  15081. and end of string."
  15082. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15083. (start 0)
  15084. notfirst
  15085. (list nil))
  15086. (while (and (string-match rexp string
  15087. (if (and notfirst
  15088. (= start (match-beginning 0))
  15089. (< start (length string)))
  15090. (1+ start) start))
  15091. (< (match-beginning 0) (length string)))
  15092. (setq notfirst t)
  15093. (or (eq (match-beginning 0) 0)
  15094. (and (eq (match-beginning 0) (match-end 0))
  15095. (eq (match-beginning 0) start))
  15096. (setq list
  15097. (cons (substring string start (match-beginning 0))
  15098. list)))
  15099. (setq start (match-end 0)))
  15100. (or (eq start (length string))
  15101. (setq list
  15102. (cons (substring string start)
  15103. list)))
  15104. (nreverse list)))
  15105. (defun org-quote-vert (s)
  15106. "Replace \"|\" with \"\\vert\"."
  15107. (while (string-match "|" s)
  15108. (setq s (replace-match "\\vert" t t s)))
  15109. s)
  15110. (defun org-uuidgen-p (s)
  15111. "Is S an ID created by UUIDGEN?"
  15112. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15113. (defun org-context ()
  15114. "Return a list of contexts of the current cursor position.
  15115. If several contexts apply, all are returned.
  15116. Each context entry is a list with a symbol naming the context, and
  15117. two positions indicating start and end of the context. Possible
  15118. contexts are:
  15119. :headline anywhere in a headline
  15120. :headline-stars on the leading stars in a headline
  15121. :todo-keyword on a TODO keyword (including DONE) in a headline
  15122. :tags on the TAGS in a headline
  15123. :priority on the priority cookie in a headline
  15124. :item on the first line of a plain list item
  15125. :item-bullet on the bullet/number of a plain list item
  15126. :checkbox on the checkbox in a plain list item
  15127. :table in an org-mode table
  15128. :table-special on a special filed in a table
  15129. :table-table in a table.el table
  15130. :link on a hyperlink
  15131. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15132. :target on a <<target>>
  15133. :radio-target on a <<<radio-target>>>
  15134. :latex-fragment on a LaTeX fragment
  15135. :latex-preview on a LaTeX fragment with overlayed preview image
  15136. This function expects the position to be visible because it uses font-lock
  15137. faces as a help to recognize the following contexts: :table-special, :link,
  15138. and :keyword."
  15139. (let* ((f (get-text-property (point) 'face))
  15140. (faces (if (listp f) f (list f)))
  15141. (p (point)) clist o)
  15142. ;; First the large context
  15143. (cond
  15144. ((org-on-heading-p t)
  15145. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15146. (when (progn
  15147. (beginning-of-line 1)
  15148. (looking-at org-todo-line-tags-regexp))
  15149. (push (org-point-in-group p 1 :headline-stars) clist)
  15150. (push (org-point-in-group p 2 :todo-keyword) clist)
  15151. (push (org-point-in-group p 4 :tags) clist))
  15152. (goto-char p)
  15153. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15154. (if (looking-at "\\[#[A-Z0-9]\\]")
  15155. (push (org-point-in-group p 0 :priority) clist)))
  15156. ((org-at-item-p)
  15157. (push (org-point-in-group p 2 :item-bullet) clist)
  15158. (push (list :item (point-at-bol)
  15159. (save-excursion (org-end-of-item) (point)))
  15160. clist)
  15161. (and (org-at-item-checkbox-p)
  15162. (push (org-point-in-group p 0 :checkbox) clist)))
  15163. ((org-at-table-p)
  15164. (push (list :table (org-table-begin) (org-table-end)) clist)
  15165. (if (memq 'org-formula faces)
  15166. (push (list :table-special
  15167. (previous-single-property-change p 'face)
  15168. (next-single-property-change p 'face)) clist)))
  15169. ((org-at-table-p 'any)
  15170. (push (list :table-table) clist)))
  15171. (goto-char p)
  15172. ;; Now the small context
  15173. (cond
  15174. ((org-at-timestamp-p)
  15175. (push (org-point-in-group p 0 :timestamp) clist))
  15176. ((memq 'org-link faces)
  15177. (push (list :link
  15178. (previous-single-property-change p 'face)
  15179. (next-single-property-change p 'face)) clist))
  15180. ((memq 'org-special-keyword faces)
  15181. (push (list :keyword
  15182. (previous-single-property-change p 'face)
  15183. (next-single-property-change p 'face)) clist))
  15184. ((org-on-target-p)
  15185. (push (org-point-in-group p 0 :target) clist)
  15186. (goto-char (1- (match-beginning 0)))
  15187. (if (looking-at org-radio-target-regexp)
  15188. (push (org-point-in-group p 0 :radio-target) clist))
  15189. (goto-char p))
  15190. ((setq o (car (delq nil
  15191. (mapcar
  15192. (lambda (x)
  15193. (if (memq x org-latex-fragment-image-overlays) x))
  15194. (org-overlays-at (point))))))
  15195. (push (list :latex-fragment
  15196. (org-overlay-start o) (org-overlay-end o)) clist)
  15197. (push (list :latex-preview
  15198. (org-overlay-start o) (org-overlay-end o)) clist))
  15199. ((org-inside-LaTeX-fragment-p)
  15200. ;; FIXME: positions wrong.
  15201. (push (list :latex-fragment (point) (point)) clist)))
  15202. (setq clist (nreverse (delq nil clist)))
  15203. clist))
  15204. ;; FIXME: Compare with at-regexp-p Do we need both?
  15205. (defun org-in-regexp (re &optional nlines visually)
  15206. "Check if point is inside a match of regexp.
  15207. Normally only the current line is checked, but you can include NLINES extra
  15208. lines both before and after point into the search.
  15209. If VISUALLY is set, require that the cursor is not after the match but
  15210. really on, so that the block visually is on the match."
  15211. (catch 'exit
  15212. (let ((pos (point))
  15213. (eol (point-at-eol (+ 1 (or nlines 0))))
  15214. (inc (if visually 1 0)))
  15215. (save-excursion
  15216. (beginning-of-line (- 1 (or nlines 0)))
  15217. (while (re-search-forward re eol t)
  15218. (if (and (<= (match-beginning 0) pos)
  15219. (>= (+ inc (match-end 0)) pos))
  15220. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15221. (defun org-at-regexp-p (regexp)
  15222. "Is point inside a match of REGEXP in the current line?"
  15223. (catch 'exit
  15224. (save-excursion
  15225. (let ((pos (point)) (end (point-at-eol)))
  15226. (beginning-of-line 1)
  15227. (while (re-search-forward regexp end t)
  15228. (if (and (<= (match-beginning 0) pos)
  15229. (>= (match-end 0) pos))
  15230. (throw 'exit t)))
  15231. nil))))
  15232. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15233. "Call `multi-occur' with buffers for all agenda files."
  15234. (interactive "sOrg-files matching: \np")
  15235. (let* ((files (org-agenda-files))
  15236. (tnames (mapcar 'file-truename files))
  15237. (extra org-agenda-text-search-extra-files)
  15238. f)
  15239. (when (eq (car extra) 'agenda-archives)
  15240. (setq extra (cdr extra))
  15241. (setq files (org-add-archive-files files)))
  15242. (while (setq f (pop extra))
  15243. (unless (member (file-truename f) tnames)
  15244. (add-to-list 'files f 'append)
  15245. (add-to-list 'tnames (file-truename f) 'append)))
  15246. (multi-occur
  15247. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  15248. regexp)))
  15249. (if (boundp 'occur-mode-find-occurrence-hook)
  15250. ;; Emacs 23
  15251. (add-hook 'occur-mode-find-occurrence-hook
  15252. (lambda ()
  15253. (when (org-mode-p)
  15254. (org-reveal))))
  15255. ;; Emacs 22
  15256. (defadvice occur-mode-goto-occurrence
  15257. (after org-occur-reveal activate)
  15258. (and (org-mode-p) (org-reveal)))
  15259. (defadvice occur-mode-goto-occurrence-other-window
  15260. (after org-occur-reveal activate)
  15261. (and (org-mode-p) (org-reveal)))
  15262. (defadvice occur-mode-display-occurrence
  15263. (after org-occur-reveal activate)
  15264. (when (org-mode-p)
  15265. (let ((pos (occur-mode-find-occurrence)))
  15266. (with-current-buffer (marker-buffer pos)
  15267. (save-excursion
  15268. (goto-char pos)
  15269. (org-reveal)))))))
  15270. (defun org-occur-link-in-agenda-files ()
  15271. "Create a link and search for it in the agendas.
  15272. The link is not stored in `org-stored-links', it is just created
  15273. for the search purpose."
  15274. (interactive)
  15275. (let ((link (condition-case nil
  15276. (org-store-link nil)
  15277. (error "Unable to create a link to here"))))
  15278. (org-occur-in-agenda-files (regexp-quote link))))
  15279. (defun org-uniquify (list)
  15280. "Remove duplicate elements from LIST."
  15281. (let (res)
  15282. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15283. res))
  15284. (defun org-delete-all (elts list)
  15285. "Remove all elements in ELTS from LIST."
  15286. (while elts
  15287. (setq list (delete (pop elts) list)))
  15288. list)
  15289. (defun org-back-over-empty-lines ()
  15290. "Move backwards over whitespace, to the beginning of the first empty line.
  15291. Returns the number of empty lines passed."
  15292. (let ((pos (point)))
  15293. (skip-chars-backward " \t\n\r")
  15294. (beginning-of-line 2)
  15295. (goto-char (min (point) pos))
  15296. (count-lines (point) pos)))
  15297. (defun org-skip-whitespace ()
  15298. (skip-chars-forward " \t\n\r"))
  15299. (defun org-point-in-group (point group &optional context)
  15300. "Check if POINT is in match-group GROUP.
  15301. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15302. match. If the match group does ot exist or point is not inside it,
  15303. return nil."
  15304. (and (match-beginning group)
  15305. (>= point (match-beginning group))
  15306. (<= point (match-end group))
  15307. (if context
  15308. (list context (match-beginning group) (match-end group))
  15309. t)))
  15310. (defun org-switch-to-buffer-other-window (&rest args)
  15311. "Switch to buffer in a second window on the current frame.
  15312. In particular, do not allow pop-up frames."
  15313. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15314. special-display-function)
  15315. (apply 'switch-to-buffer-other-window args)))
  15316. (defun org-combine-plists (&rest plists)
  15317. "Create a single property list from all plists in PLISTS.
  15318. The process starts by copying the first list, and then setting properties
  15319. from the other lists. Settings in the last list are the most significant
  15320. ones and overrule settings in the other lists."
  15321. (let ((rtn (copy-sequence (pop plists)))
  15322. p v ls)
  15323. (while plists
  15324. (setq ls (pop plists))
  15325. (while ls
  15326. (setq p (pop ls) v (pop ls))
  15327. (setq rtn (plist-put rtn p v))))
  15328. rtn))
  15329. (defun org-move-line-down (arg)
  15330. "Move the current line down. With prefix argument, move it past ARG lines."
  15331. (interactive "p")
  15332. (let ((col (current-column))
  15333. beg end pos)
  15334. (beginning-of-line 1) (setq beg (point))
  15335. (beginning-of-line 2) (setq end (point))
  15336. (beginning-of-line (+ 1 arg))
  15337. (setq pos (move-marker (make-marker) (point)))
  15338. (insert (delete-and-extract-region beg end))
  15339. (goto-char pos)
  15340. (org-move-to-column col)))
  15341. (defun org-move-line-up (arg)
  15342. "Move the current line up. With prefix argument, move it past ARG lines."
  15343. (interactive "p")
  15344. (let ((col (current-column))
  15345. beg end pos)
  15346. (beginning-of-line 1) (setq beg (point))
  15347. (beginning-of-line 2) (setq end (point))
  15348. (beginning-of-line (- arg))
  15349. (setq pos (move-marker (make-marker) (point)))
  15350. (insert (delete-and-extract-region beg end))
  15351. (goto-char pos)
  15352. (org-move-to-column col)))
  15353. (defun org-replace-escapes (string table)
  15354. "Replace %-escapes in STRING with values in TABLE.
  15355. TABLE is an association list with keys like \"%a\" and string values.
  15356. The sequences in STRING may contain normal field width and padding information,
  15357. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15358. so values can contain further %-escapes if they are define later in TABLE."
  15359. (let ((case-fold-search nil)
  15360. e re rpl)
  15361. (while (setq e (pop table))
  15362. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15363. (while (string-match re string)
  15364. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15365. (cdr e)))
  15366. (setq string (replace-match rpl t t string))))
  15367. string))
  15368. (defun org-sublist (list start end)
  15369. "Return a section of LIST, from START to END.
  15370. Counting starts at 1."
  15371. (let (rtn (c start))
  15372. (setq list (nthcdr (1- start) list))
  15373. (while (and list (<= c end))
  15374. (push (pop list) rtn)
  15375. (setq c (1+ c)))
  15376. (nreverse rtn)))
  15377. (defun org-find-base-buffer-visiting (file)
  15378. "Like `find-buffer-visiting' but always return the base buffer and
  15379. not an indirect buffer."
  15380. (let ((buf (or (get-file-buffer file)
  15381. (find-buffer-visiting file))))
  15382. (if buf
  15383. (or (buffer-base-buffer buf) buf)
  15384. nil)))
  15385. (defun org-image-file-name-regexp (&optional extensions)
  15386. "Return regexp matching the file names of images.
  15387. If EXTENSIONS is given, only match these."
  15388. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15389. (image-file-name-regexp)
  15390. (let ((image-file-name-extensions
  15391. (or extensions
  15392. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15393. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15394. (concat "\\."
  15395. (regexp-opt (nconc (mapcar 'upcase
  15396. image-file-name-extensions)
  15397. image-file-name-extensions)
  15398. t)
  15399. "\\'"))))
  15400. (defun org-file-image-p (file &optional extensions)
  15401. "Return non-nil if FILE is an image."
  15402. (save-match-data
  15403. (string-match (org-image-file-name-regexp extensions) file)))
  15404. (defun org-get-cursor-date ()
  15405. "Return the date at cursor in as a time.
  15406. This works in the calendar and in the agenda, anywhere else it just
  15407. returns the current time."
  15408. (let (date day defd)
  15409. (cond
  15410. ((eq major-mode 'calendar-mode)
  15411. (setq date (calendar-cursor-to-date)
  15412. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15413. ((eq major-mode 'org-agenda-mode)
  15414. (setq day (get-text-property (point) 'day))
  15415. (if day
  15416. (setq date (calendar-gregorian-from-absolute day)
  15417. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15418. (nth 2 date))))))
  15419. (or defd (current-time))))
  15420. (defvar org-agenda-action-marker (make-marker)
  15421. "Marker pointing to the entry for the next agenda action.")
  15422. (defun org-mark-entry-for-agenda-action ()
  15423. "Mark the current entry as target of an agenda action.
  15424. Agenda actions are actions executed from the agenda with the key `k',
  15425. which make use of the date at the cursor."
  15426. (interactive)
  15427. (move-marker org-agenda-action-marker
  15428. (save-excursion (org-back-to-heading t) (point))
  15429. (current-buffer))
  15430. (message
  15431. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15432. ;;; Paragraph filling stuff.
  15433. ;; We want this to be just right, so use the full arsenal.
  15434. (defun org-indent-line-function ()
  15435. "Indent line like previous, but further if previous was headline or item."
  15436. (interactive)
  15437. (let* ((pos (point))
  15438. (itemp (org-at-item-p))
  15439. (case-fold-search t)
  15440. (org-drawer-regexp (or org-drawer-regexp "\000"))
  15441. column bpos bcol tpos tcol bullet btype bullet-type)
  15442. ;; Find the previous relevant line
  15443. (beginning-of-line 1)
  15444. (cond
  15445. ((looking-at "#") (setq column 0))
  15446. ((looking-at "\\*+ ") (setq column 0))
  15447. ((and (looking-at "[ \t]*:END:")
  15448. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  15449. (save-excursion
  15450. (goto-char (1- (match-beginning 1)))
  15451. (setq column (current-column))))
  15452. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  15453. (save-excursion
  15454. (re-search-backward
  15455. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  15456. (setq column (org-get-indentation (match-string 0))))
  15457. (t
  15458. (beginning-of-line 0)
  15459. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  15460. (not (looking-at "[ \t]*:END:"))
  15461. (not (looking-at org-drawer-regexp)))
  15462. (beginning-of-line 0))
  15463. (cond
  15464. ((looking-at "\\*+[ \t]+")
  15465. (if (not org-adapt-indentation)
  15466. (setq column 0)
  15467. (goto-char (match-end 0))
  15468. (setq column (current-column))))
  15469. ((looking-at org-drawer-regexp)
  15470. (goto-char (1- (match-beginning 1)))
  15471. (setq column (current-column)))
  15472. ((looking-at "\\([ \t]*\\):END:")
  15473. (goto-char (match-end 1))
  15474. (setq column (current-column)))
  15475. ((org-in-item-p)
  15476. (org-beginning-of-item)
  15477. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  15478. (setq bpos (match-beginning 1) tpos (match-end 0)
  15479. bcol (progn (goto-char bpos) (current-column))
  15480. tcol (progn (goto-char tpos) (current-column))
  15481. bullet (match-string 1)
  15482. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  15483. (if (> tcol (+ bcol org-description-max-indent))
  15484. (setq tcol (+ bcol 5)))
  15485. (if (not itemp)
  15486. (setq column tcol)
  15487. (goto-char pos)
  15488. (beginning-of-line 1)
  15489. (if (looking-at "\\S-")
  15490. (progn
  15491. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  15492. (setq bullet (match-string 1)
  15493. btype (if (string-match "[0-9]" bullet) "n" bullet))
  15494. (setq column (if (equal btype bullet-type) bcol tcol)))
  15495. (setq column (org-get-indentation)))))
  15496. (t (setq column (org-get-indentation))))))
  15497. (goto-char pos)
  15498. (if (<= (current-column) (current-indentation))
  15499. (org-indent-line-to column)
  15500. (save-excursion (org-indent-line-to column)))
  15501. (setq column (current-column))
  15502. (beginning-of-line 1)
  15503. (if (looking-at
  15504. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  15505. (replace-match (concat (match-string 1)
  15506. (format org-property-format
  15507. (match-string 2) (match-string 3)))
  15508. t t))
  15509. (org-move-to-column column)))
  15510. (defun org-set-autofill-regexps ()
  15511. (interactive)
  15512. ;; In the paragraph separator we include headlines, because filling
  15513. ;; text in a line directly attached to a headline would otherwise
  15514. ;; fill the headline as well.
  15515. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15516. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  15517. ;; The paragraph starter includes hand-formatted lists.
  15518. (org-set-local
  15519. 'paragraph-start
  15520. (concat
  15521. "\f" "\\|"
  15522. "[ ]*$" "\\|"
  15523. "\\*+ " "\\|"
  15524. "[ \t]*#" "\\|"
  15525. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  15526. "[ \t]*[:|]" "\\|"
  15527. "\\$\\$" "\\|"
  15528. "\\\\\\(begin\\|end\\|[][]\\)"))
  15529. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15530. ;; But only if the user has not turned off tables or fixed-width regions
  15531. (org-set-local
  15532. 'auto-fill-inhibit-regexp
  15533. (concat "\\*+ \\|#\\+"
  15534. "\\|[ \t]*" org-keyword-time-regexp
  15535. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15536. (concat
  15537. "\\|[ \t]*["
  15538. (if org-enable-table-editor "|" "")
  15539. (if org-enable-fixed-width-editor ":" "")
  15540. "]"))))
  15541. ;; We use our own fill-paragraph function, to make sure that tables
  15542. ;; and fixed-width regions are not wrapped. That function will pass
  15543. ;; through to `fill-paragraph' when appropriate.
  15544. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15545. ; Adaptive filling: To get full control, first make sure that
  15546. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15547. (org-set-local 'adaptive-fill-regexp "\000")
  15548. (org-set-local 'adaptive-fill-function
  15549. 'org-adaptive-fill-function)
  15550. (org-set-local
  15551. 'align-mode-rules-list
  15552. '((org-in-buffer-settings
  15553. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15554. (modes . '(org-mode))))))
  15555. (defun org-fill-paragraph (&optional justify)
  15556. "Re-align a table, pass through to fill-paragraph if no table."
  15557. (let ((table-p (org-at-table-p))
  15558. (table.el-p (org-at-table.el-p)))
  15559. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15560. (save-excursion (goto-char (point-at-bol))
  15561. (looking-at outline-regexp)))
  15562. t) ; skip headlines
  15563. (table.el-p t) ; skip table.el tables
  15564. (table-p (org-table-align) t) ; align org-mode tables
  15565. (t nil)))) ; call paragraph-fill
  15566. ;; For reference, this is the default value of adaptive-fill-regexp
  15567. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15568. (defun org-adaptive-fill-function ()
  15569. "Return a fill prefix for org-mode files.
  15570. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15571. work correctly."
  15572. (cond ((looking-at "#[ \t]+")
  15573. (match-string 0))
  15574. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15575. (save-excursion
  15576. (if (> (match-end 1) (+ (match-beginning 1)
  15577. org-description-max-indent))
  15578. (goto-char (+ (match-beginning 1) 5))
  15579. (goto-char (match-end 0)))
  15580. (make-string (current-column) ?\ )))
  15581. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15582. (save-excursion
  15583. (goto-char (match-end 0))
  15584. (make-string (current-column) ?\ )))
  15585. (t nil)))
  15586. ;;; Other stuff.
  15587. (defun org-toggle-fixed-width-section (arg)
  15588. "Toggle the fixed-width export.
  15589. If there is no active region, the QUOTE keyword at the current headline is
  15590. inserted or removed. When present, it causes the text between this headline
  15591. and the next to be exported as fixed-width text, and unmodified.
  15592. If there is an active region, this command adds or removes a colon as the
  15593. first character of this line. If the first character of a line is a colon,
  15594. this line is also exported in fixed-width font."
  15595. (interactive "P")
  15596. (let* ((cc 0)
  15597. (regionp (org-region-active-p))
  15598. (beg (if regionp (region-beginning) (point)))
  15599. (end (if regionp (region-end)))
  15600. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15601. (case-fold-search nil)
  15602. (re "[ \t]*\\(: \\)")
  15603. off)
  15604. (if regionp
  15605. (save-excursion
  15606. (goto-char beg)
  15607. (setq cc (current-column))
  15608. (beginning-of-line 1)
  15609. (setq off (looking-at re))
  15610. (while (> nlines 0)
  15611. (setq nlines (1- nlines))
  15612. (beginning-of-line 1)
  15613. (cond
  15614. (arg
  15615. (org-move-to-column cc t)
  15616. (insert ": \n")
  15617. (forward-line -1))
  15618. ((and off (looking-at re))
  15619. (replace-match "" t t nil 1))
  15620. ((not off) (org-move-to-column cc t) (insert ": ")))
  15621. (forward-line 1)))
  15622. (save-excursion
  15623. (org-back-to-heading)
  15624. (if (looking-at (concat outline-regexp
  15625. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15626. (replace-match "" t t nil 1)
  15627. (if (looking-at outline-regexp)
  15628. (progn
  15629. (goto-char (match-end 0))
  15630. (insert org-quote-string " "))))))))
  15631. (defun org-reftex-citation ()
  15632. "Use reftex-citation to insert a citation into the buffer.
  15633. This looks for a line like
  15634. #+BIBLIOGRAPHY: foo plain option:-d
  15635. and derives from it that foo.bib is the bibliography file relevant
  15636. for this document. It then installs the necessary environment for RefTeX
  15637. to work in this buffer and calls `reftex-citation' to insert a citation
  15638. into the buffer.
  15639. Export of such citations to both LaTeX and HTML is handled by the contributed
  15640. package org-exp-bibtex by Taru Karttunen."
  15641. (interactive)
  15642. (let ((reftex-docstruct-symbol 'rds)
  15643. (reftex-cite-format "\\cite{%l}")
  15644. rds bib)
  15645. (save-excursion
  15646. (save-restriction
  15647. (widen)
  15648. (let ((case-fold-search t)
  15649. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15650. (if (not (save-excursion
  15651. (or (re-search-forward re nil t)
  15652. (re-search-backward re nil t))))
  15653. (error "No bibliography defined in file")
  15654. (setq bib (concat (match-string 1) ".bib")
  15655. rds (list (list 'bib bib)))))))
  15656. (call-interactively 'reftex-citation)))
  15657. ;;;; Functions extending outline functionality
  15658. (defun org-beginning-of-line (&optional arg)
  15659. "Go to the beginning of the current line. If that is invisible, continue
  15660. to a visible line beginning. This makes the function of C-a more intuitive.
  15661. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15662. first attempt, and only move to after the tags when the cursor is already
  15663. beyond the end of the headline."
  15664. (interactive "P")
  15665. (let ((pos (point))
  15666. (special (if (consp org-special-ctrl-a/e)
  15667. (car org-special-ctrl-a/e)
  15668. org-special-ctrl-a/e))
  15669. refpos)
  15670. (if (org-bound-and-true-p line-move-visual)
  15671. (beginning-of-visual-line 1)
  15672. (beginning-of-line 1))
  15673. (if (and arg (fboundp 'move-beginning-of-line))
  15674. (call-interactively 'move-beginning-of-line)
  15675. (if (bobp)
  15676. nil
  15677. (backward-char 1)
  15678. (if (org-invisible-p)
  15679. (while (and (not (bobp)) (org-invisible-p))
  15680. (backward-char 1)
  15681. (beginning-of-line 1))
  15682. (forward-char 1))))
  15683. (when special
  15684. (cond
  15685. ((and (looking-at org-complex-heading-regexp)
  15686. (= (char-after (match-end 1)) ?\ ))
  15687. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15688. (point-at-eol)))
  15689. (goto-char
  15690. (if (eq special t)
  15691. (cond ((> pos refpos) refpos)
  15692. ((= pos (point)) refpos)
  15693. (t (point)))
  15694. (cond ((> pos (point)) (point))
  15695. ((not (eq last-command this-command)) (point))
  15696. (t refpos)))))
  15697. ((org-at-item-p)
  15698. (goto-char
  15699. (if (eq special t)
  15700. (cond ((> pos (match-end 4)) (match-end 4))
  15701. ((= pos (point)) (match-end 4))
  15702. (t (point)))
  15703. (cond ((> pos (point)) (point))
  15704. ((not (eq last-command this-command)) (point))
  15705. (t (match-end 4))))))))
  15706. (org-no-warnings
  15707. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15708. (defun org-end-of-line (&optional arg)
  15709. "Go to the end of the line.
  15710. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15711. first attempt, and only move to after the tags when the cursor is already
  15712. beyond the end of the headline."
  15713. (interactive "P")
  15714. (let ((special (if (consp org-special-ctrl-a/e)
  15715. (cdr org-special-ctrl-a/e)
  15716. org-special-ctrl-a/e)))
  15717. (if (or (not special)
  15718. (not (org-on-heading-p))
  15719. arg)
  15720. (call-interactively
  15721. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15722. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15723. (t 'end-of-line)))
  15724. (let ((pos (point)))
  15725. (beginning-of-line 1)
  15726. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  15727. (if (eq special t)
  15728. (if (or (< pos (match-beginning 1))
  15729. (= pos (match-end 0)))
  15730. (goto-char (match-beginning 1))
  15731. (goto-char (match-end 0)))
  15732. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15733. (goto-char (match-end 0))
  15734. (goto-char (match-beginning 1))))
  15735. (call-interactively (if (fboundp 'move-end-of-line)
  15736. 'move-end-of-line
  15737. 'end-of-line)))))
  15738. (org-no-warnings
  15739. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15740. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15741. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15742. (define-key org-mode-map [home] 'org-beginning-of-line)
  15743. (define-key org-mode-map [end] 'org-end-of-line)
  15744. (defun org-backward-sentence (&optional arg)
  15745. "Go to beginning of sentence, or beginning of table field.
  15746. This will call `backward-sentence' or `org-table-beginning-of-field',
  15747. depending on context."
  15748. (interactive "P")
  15749. (cond
  15750. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15751. (t (call-interactively 'backward-sentence))))
  15752. (defun org-forward-sentence (&optional arg)
  15753. "Go to end of sentence, or end of table field.
  15754. This will call `forward-sentence' or `org-table-end-of-field',
  15755. depending on context."
  15756. (interactive "P")
  15757. (cond
  15758. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15759. (t (call-interactively 'forward-sentence))))
  15760. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15761. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15762. (defun org-kill-line (&optional arg)
  15763. "Kill line, to tags or end of line."
  15764. (interactive "P")
  15765. (cond
  15766. ((or (not org-special-ctrl-k)
  15767. (bolp)
  15768. (not (org-on-heading-p)))
  15769. (call-interactively 'kill-line))
  15770. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15771. (kill-region (point) (match-beginning 1))
  15772. (org-set-tags nil t))
  15773. (t (kill-region (point) (point-at-eol)))))
  15774. (define-key org-mode-map "\C-k" 'org-kill-line)
  15775. (defun org-yank (&optional arg)
  15776. "Yank. If the kill is a subtree, treat it specially.
  15777. This command will look at the current kill and check if is a single
  15778. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15779. cursor is at the beginning of a line or after the stars of a currently
  15780. empty headline, then the yank is handled specially. How exactly depends
  15781. on the value of the following variables, both set by default.
  15782. org-yank-folded-subtrees
  15783. When set, the subtree(s) will be folded after insertion, but only
  15784. if doing so would now swallow text after the yanked text.
  15785. org-yank-adjusted-subtrees
  15786. When set, the subtree will be promoted or demoted in order to
  15787. fit into the local outline tree structure, which means that the level
  15788. will be adjusted so that it becomes the smaller one of the two
  15789. *visible* surrounding headings.
  15790. Any prefix to this command will cause `yank' to be called directly with
  15791. no special treatment. In particular, a simple `C-u' prefix will just
  15792. plainly yank the text as it is.
  15793. \[1] The test checks if the first non-white line is a heading
  15794. and if there are no other headings with fewer stars."
  15795. (interactive "P")
  15796. (org-yank-generic 'yank arg))
  15797. (defun org-yank-generic (command arg)
  15798. "Perform some yank-like command.
  15799. This function implements the behavior described in the `org-yank'
  15800. documentation. However, it has been generalized to work for any
  15801. interactive command with similar behavior."
  15802. ;; pretend to be command COMMAND
  15803. (setq this-command command)
  15804. (if arg
  15805. (call-interactively command)
  15806. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15807. (and (org-kill-is-subtree-p)
  15808. (or (bolp)
  15809. (and (looking-at "[ \t]*$")
  15810. (string-match
  15811. "\\`\\*+\\'"
  15812. (buffer-substring (point-at-bol) (point)))))))
  15813. swallowp)
  15814. (cond
  15815. ((and subtreep org-yank-folded-subtrees)
  15816. (let ((beg (point))
  15817. end)
  15818. (if (and subtreep org-yank-adjusted-subtrees)
  15819. (org-paste-subtree nil nil 'for-yank)
  15820. (call-interactively command))
  15821. (setq end (point))
  15822. (goto-char beg)
  15823. (when (and (bolp) subtreep
  15824. (not (setq swallowp
  15825. (org-yank-folding-would-swallow-text beg end))))
  15826. (or (looking-at outline-regexp)
  15827. (re-search-forward (concat "^" outline-regexp) end t))
  15828. (while (and (< (point) end) (looking-at outline-regexp))
  15829. (hide-subtree)
  15830. (org-cycle-show-empty-lines 'folded)
  15831. (condition-case nil
  15832. (outline-forward-same-level 1)
  15833. (error (goto-char end)))))
  15834. (when swallowp
  15835. (message
  15836. "Inserted text not folded because that would swallow text"))
  15837. (goto-char end)
  15838. (skip-chars-forward " \t\n\r")
  15839. (beginning-of-line 1)
  15840. (push-mark beg 'nomsg)))
  15841. ((and subtreep org-yank-adjusted-subtrees)
  15842. (let ((beg (point-at-bol)))
  15843. (org-paste-subtree nil nil 'for-yank)
  15844. (push-mark beg 'nomsg)))
  15845. (t
  15846. (call-interactively command))))))
  15847. (defun org-yank-folding-would-swallow-text (beg end)
  15848. "Would hide-subtree at BEG swallow any text after END?"
  15849. (let (level)
  15850. (save-excursion
  15851. (goto-char beg)
  15852. (when (or (looking-at outline-regexp)
  15853. (re-search-forward (concat "^" outline-regexp) end t))
  15854. (setq level (org-outline-level)))
  15855. (goto-char end)
  15856. (skip-chars-forward " \t\r\n\v\f")
  15857. (if (or (eobp)
  15858. (and (bolp) (looking-at org-outline-regexp)
  15859. (<= (org-outline-level) level)))
  15860. nil ; Nothing would be swallowed
  15861. t)))) ; something would swallow
  15862. (define-key org-mode-map "\C-y" 'org-yank)
  15863. (defun org-invisible-p ()
  15864. "Check if point is at a character currently not visible."
  15865. ;; Early versions of noutline don't have `outline-invisible-p'.
  15866. (if (fboundp 'outline-invisible-p)
  15867. (outline-invisible-p)
  15868. (get-char-property (point) 'invisible)))
  15869. (defun org-invisible-p2 ()
  15870. "Check if point is at a character currently not visible."
  15871. (save-excursion
  15872. (if (and (eolp) (not (bobp))) (backward-char 1))
  15873. ;; Early versions of noutline don't have `outline-invisible-p'.
  15874. (if (fboundp 'outline-invisible-p)
  15875. (outline-invisible-p)
  15876. (get-char-property (point) 'invisible))))
  15877. (defun org-back-to-heading (&optional invisible-ok)
  15878. "Call `outline-back-to-heading', but provide a better error message."
  15879. (condition-case nil
  15880. (outline-back-to-heading invisible-ok)
  15881. (error (error "Before first headline at position %d in buffer %s"
  15882. (point) (current-buffer)))))
  15883. (defun org-before-first-heading-p ()
  15884. "Before first heading?"
  15885. (save-excursion
  15886. (null (re-search-backward "^\\*+ " nil t))))
  15887. (defun org-on-heading-p (&optional ignored)
  15888. (outline-on-heading-p t))
  15889. (defun org-at-heading-p (&optional ignored)
  15890. (outline-on-heading-p t))
  15891. (defun org-at-heading-or-item-p ()
  15892. (or (org-on-heading-p) (org-at-item-p)))
  15893. (defun org-on-target-p ()
  15894. (or (org-in-regexp org-radio-target-regexp)
  15895. (org-in-regexp org-target-regexp)))
  15896. (defun org-up-heading-all (arg)
  15897. "Move to the heading line of which the present line is a subheading.
  15898. This function considers both visible and invisible heading lines.
  15899. With argument, move up ARG levels."
  15900. (if (fboundp 'outline-up-heading-all)
  15901. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15902. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15903. (defun org-up-heading-safe ()
  15904. "Move to the heading line of which the present line is a subheading.
  15905. This version will not throw an error. It will return the level of the
  15906. headline found, or nil if no higher level is found.
  15907. Also, this function will be a lot faster than `outline-up-heading',
  15908. because it relies on stars being the outline starters. This can really
  15909. make a significant difference in outlines with very many siblings."
  15910. (let (start-level re)
  15911. (org-back-to-heading t)
  15912. (setq start-level (funcall outline-level))
  15913. (if (equal start-level 1)
  15914. nil
  15915. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15916. (if (re-search-backward re nil t)
  15917. (funcall outline-level)))))
  15918. (defun org-first-sibling-p ()
  15919. "Is this heading the first child of its parents?"
  15920. (interactive)
  15921. (let ((re (concat "^" outline-regexp))
  15922. level l)
  15923. (unless (org-at-heading-p t)
  15924. (error "Not at a heading"))
  15925. (setq level (funcall outline-level))
  15926. (save-excursion
  15927. (if (not (re-search-backward re nil t))
  15928. t
  15929. (setq l (funcall outline-level))
  15930. (< l level)))))
  15931. (defun org-goto-sibling (&optional previous)
  15932. "Goto the next sibling, even if it is invisible.
  15933. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15934. when a sibling was found. When none is found, return nil and don't
  15935. move point."
  15936. (let ((fun (if previous 're-search-backward 're-search-forward))
  15937. (pos (point))
  15938. (re (concat "^" outline-regexp))
  15939. level l)
  15940. (when (condition-case nil (org-back-to-heading t) (error nil))
  15941. (setq level (funcall outline-level))
  15942. (catch 'exit
  15943. (or previous (forward-char 1))
  15944. (while (funcall fun re nil t)
  15945. (setq l (funcall outline-level))
  15946. (when (< l level) (goto-char pos) (throw 'exit nil))
  15947. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15948. (goto-char pos)
  15949. nil))))
  15950. (defun org-show-siblings ()
  15951. "Show all siblings of the current headline."
  15952. (save-excursion
  15953. (while (org-goto-sibling) (org-flag-heading nil)))
  15954. (save-excursion
  15955. (while (org-goto-sibling 'previous)
  15956. (org-flag-heading nil))))
  15957. (defun org-show-hidden-entry ()
  15958. "Show an entry where even the heading is hidden."
  15959. (save-excursion
  15960. (org-show-entry)))
  15961. (defun org-flag-heading (flag &optional entry)
  15962. "Flag the current heading. FLAG non-nil means make invisible.
  15963. When ENTRY is non-nil, show the entire entry."
  15964. (save-excursion
  15965. (org-back-to-heading t)
  15966. ;; Check if we should show the entire entry
  15967. (if entry
  15968. (progn
  15969. (org-show-entry)
  15970. (save-excursion
  15971. (and (outline-next-heading)
  15972. (org-flag-heading nil))))
  15973. (outline-flag-region (max (point-min) (1- (point)))
  15974. (save-excursion (outline-end-of-heading) (point))
  15975. flag))))
  15976. (defun org-get-next-sibling ()
  15977. "Move to next heading of the same level, and return point.
  15978. If there is no such heading, return nil.
  15979. This is like outline-next-sibling, but invisible headings are ok."
  15980. (let ((level (funcall outline-level)))
  15981. (outline-next-heading)
  15982. (while (and (not (eobp)) (> (funcall outline-level) level))
  15983. (outline-next-heading))
  15984. (if (or (eobp) (< (funcall outline-level) level))
  15985. nil
  15986. (point))))
  15987. (defun org-get-last-sibling ()
  15988. "Move to previous heading of the same level, and return point.
  15989. If there is no such heading, return nil."
  15990. (let ((opoint (point))
  15991. (level (funcall outline-level)))
  15992. (outline-previous-heading)
  15993. (when (and (/= (point) opoint) (outline-on-heading-p t))
  15994. (while (and (> (funcall outline-level) level)
  15995. (not (bobp)))
  15996. (outline-previous-heading))
  15997. (if (< (funcall outline-level) level)
  15998. nil
  15999. (point)))))
  16000. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16001. ;; This contains an exact copy of the original function, but it uses
  16002. ;; `org-back-to-heading', to make it work also in invisible
  16003. ;; trees. And is uses an invisible-OK argument.
  16004. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16005. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16006. ;; with many children and grandchildren etc, this can be much faster
  16007. ;; than the outline version.
  16008. (org-back-to-heading invisible-OK)
  16009. (let ((first t)
  16010. (level (funcall outline-level)))
  16011. (if (and (org-mode-p) (< level 1000))
  16012. ;; A true heading (not a plain list item), in Org-mode
  16013. ;; This means we can easily find the end by looking
  16014. ;; only for the right number of stars. Using a regexp to do
  16015. ;; this is so much faster than using a Lisp loop.
  16016. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16017. (forward-char 1)
  16018. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16019. ;; something else, do it the slow way
  16020. (while (and (not (eobp))
  16021. (or first (> (funcall outline-level) level)))
  16022. (setq first nil)
  16023. (outline-next-heading)))
  16024. (unless to-heading
  16025. (if (memq (preceding-char) '(?\n ?\^M))
  16026. (progn
  16027. ;; Go to end of line before heading
  16028. (forward-char -1)
  16029. (if (memq (preceding-char) '(?\n ?\^M))
  16030. ;; leave blank line before heading
  16031. (forward-char -1))))))
  16032. (point))
  16033. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16034. "Use Org version in org-mode, for dramatic speed-up."
  16035. (if (eq major-mode 'org-mode)
  16036. (progn
  16037. (org-end-of-subtree nil t)
  16038. (unless (eobp) (backward-char 1)))
  16039. ad-do-it))
  16040. (defun org-forward-same-level (arg &optional invisible-ok)
  16041. "Move forward to the arg'th subheading at same level as this one.
  16042. Stop at the first and last subheadings of a superior heading."
  16043. (interactive "p")
  16044. (org-back-to-heading invisible-ok)
  16045. (org-on-heading-p)
  16046. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16047. (re (format "^\\*\\{1,%d\\} " level))
  16048. l)
  16049. (forward-char 1)
  16050. (while (> arg 0)
  16051. (while (and (re-search-forward re nil 'move)
  16052. (setq l (- (match-end 0) (match-beginning 0) 1))
  16053. (= l level)
  16054. (not invisible-ok)
  16055. (org-invisible-p))
  16056. (if (< l level) (setq arg 1)))
  16057. (setq arg (1- arg)))
  16058. (beginning-of-line 1)))
  16059. (defun org-backward-same-level (arg &optional invisible-ok)
  16060. "Move backward to the arg'th subheading at same level as this one.
  16061. Stop at the first and last subheadings of a superior heading."
  16062. (interactive "p")
  16063. (org-back-to-heading)
  16064. (org-on-heading-p)
  16065. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16066. (re (format "^\\*\\{1,%d\\} " level))
  16067. l)
  16068. (while (> arg 0)
  16069. (while (and (re-search-backward re nil 'move)
  16070. (setq l (- (match-end 0) (match-beginning 0) 1))
  16071. (= l level)
  16072. (not invisible-ok)
  16073. (org-invisible-p))
  16074. (if (< l level) (setq arg 1)))
  16075. (setq arg (1- arg)))))
  16076. (defun org-show-subtree ()
  16077. "Show everything after this heading at deeper levels."
  16078. (outline-flag-region
  16079. (point)
  16080. (save-excursion
  16081. (org-end-of-subtree t t))
  16082. nil))
  16083. (defun org-show-entry ()
  16084. "Show the body directly following this heading.
  16085. Show the heading too, if it is currently invisible."
  16086. (interactive)
  16087. (save-excursion
  16088. (condition-case nil
  16089. (progn
  16090. (org-back-to-heading t)
  16091. (outline-flag-region
  16092. (max (point-min) (1- (point)))
  16093. (save-excursion
  16094. (if (re-search-forward
  16095. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16096. (match-beginning 1)
  16097. (point-max)))
  16098. nil)
  16099. (org-cycle-hide-drawers 'children))
  16100. (error nil))))
  16101. (defun org-make-options-regexp (kwds &optional extra)
  16102. "Make a regular expression for keyword lines."
  16103. (concat
  16104. "^"
  16105. "#?[ \t]*\\+\\("
  16106. (mapconcat 'regexp-quote kwds "\\|")
  16107. (if extra (concat "\\|" extra))
  16108. "\\):[ \t]*"
  16109. "\\(.*\\)"))
  16110. ;; Make isearch reveal the necessary context
  16111. (defun org-isearch-end ()
  16112. "Reveal context after isearch exits."
  16113. (when isearch-success ; only if search was successful
  16114. (if (featurep 'xemacs)
  16115. ;; Under XEmacs, the hook is run in the correct place,
  16116. ;; we directly show the context.
  16117. (org-show-context 'isearch)
  16118. ;; In Emacs the hook runs *before* restoring the overlays.
  16119. ;; So we have to use a one-time post-command-hook to do this.
  16120. ;; (Emacs 22 has a special variable, see function `org-mode')
  16121. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16122. isearch-mode-end-hook-quit)
  16123. ;; Only when the isearch was not quitted.
  16124. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16125. 'append 'local)))))
  16126. (defun org-isearch-post-command ()
  16127. "Remove self from hook, and show context."
  16128. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16129. (org-show-context 'isearch))
  16130. ;;;; Integration with and fixes for other packages
  16131. ;;; Imenu support
  16132. (defvar org-imenu-markers nil
  16133. "All markers currently used by Imenu.")
  16134. (make-variable-buffer-local 'org-imenu-markers)
  16135. (defun org-imenu-new-marker (&optional pos)
  16136. "Return a new marker for use by Imenu, and remember the marker."
  16137. (let ((m (make-marker)))
  16138. (move-marker m (or pos (point)))
  16139. (push m org-imenu-markers)
  16140. m))
  16141. (defun org-imenu-get-tree ()
  16142. "Produce the index for Imenu."
  16143. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16144. (setq org-imenu-markers nil)
  16145. (let* ((n org-imenu-depth)
  16146. (re (concat "^" outline-regexp))
  16147. (subs (make-vector (1+ n) nil))
  16148. (last-level 0)
  16149. m level head)
  16150. (save-excursion
  16151. (save-restriction
  16152. (widen)
  16153. (goto-char (point-max))
  16154. (while (re-search-backward re nil t)
  16155. (setq level (org-reduced-level (funcall outline-level)))
  16156. (when (<= level n)
  16157. (looking-at org-complex-heading-regexp)
  16158. (setq head (org-link-display-format
  16159. (org-match-string-no-properties 4))
  16160. m (org-imenu-new-marker))
  16161. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16162. (if (>= level last-level)
  16163. (push (cons head m) (aref subs level))
  16164. (push (cons head (aref subs (1+ level))) (aref subs level))
  16165. (loop for i from (1+ level) to n do (aset subs i nil)))
  16166. (setq last-level level)))))
  16167. (aref subs 1)))
  16168. (eval-after-load "imenu"
  16169. '(progn
  16170. (add-hook 'imenu-after-jump-hook
  16171. (lambda ()
  16172. (if (eq major-mode 'org-mode)
  16173. (org-show-context 'org-goto))))))
  16174. (defun org-link-display-format (link)
  16175. "Replace a link with either the description, or the link target
  16176. if no description is present"
  16177. (save-match-data
  16178. (if (string-match org-bracket-link-analytic-regexp link)
  16179. (replace-match (if (match-end 5)
  16180. (match-string 5 link)
  16181. (concat (match-string 1 link)
  16182. (match-string 3 link)))
  16183. nil t link)
  16184. link)))
  16185. ;; Speedbar support
  16186. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16187. "Overlay marking the agenda restriction line in speedbar.")
  16188. (org-overlay-put org-speedbar-restriction-lock-overlay
  16189. 'face 'org-agenda-restriction-lock)
  16190. (org-overlay-put org-speedbar-restriction-lock-overlay
  16191. 'help-echo "Agendas are currently limited to this item.")
  16192. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16193. (defun org-speedbar-set-agenda-restriction ()
  16194. "Restrict future agenda commands to the location at point in speedbar.
  16195. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16196. (interactive)
  16197. (require 'org-agenda)
  16198. (let (p m tp np dir txt)
  16199. (cond
  16200. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16201. 'org-imenu t))
  16202. (setq m (get-text-property p 'org-imenu-marker))
  16203. (with-current-buffer (marker-buffer m)
  16204. (goto-char m)
  16205. (org-agenda-set-restriction-lock 'subtree)))
  16206. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16207. 'speedbar-function 'speedbar-find-file))
  16208. (setq tp (previous-single-property-change
  16209. (1+ p) 'speedbar-function)
  16210. np (next-single-property-change
  16211. tp 'speedbar-function)
  16212. dir (speedbar-line-directory)
  16213. txt (buffer-substring-no-properties (or tp (point-min))
  16214. (or np (point-max))))
  16215. (with-current-buffer (find-file-noselect
  16216. (let ((default-directory dir))
  16217. (expand-file-name txt)))
  16218. (unless (org-mode-p)
  16219. (error "Cannot restrict to non-Org-mode file"))
  16220. (org-agenda-set-restriction-lock 'file)))
  16221. (t (error "Don't know how to restrict Org-mode's agenda")))
  16222. (org-move-overlay org-speedbar-restriction-lock-overlay
  16223. (point-at-bol) (point-at-eol))
  16224. (setq current-prefix-arg nil)
  16225. (org-agenda-maybe-redo)))
  16226. (eval-after-load "speedbar"
  16227. '(progn
  16228. (speedbar-add-supported-extension ".org")
  16229. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16230. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16231. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16232. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16233. (add-hook 'speedbar-visiting-tag-hook
  16234. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16235. ;;; Fixes and Hacks for problems with other packages
  16236. ;; Make flyspell not check words in links, to not mess up our keymap
  16237. (defun org-mode-flyspell-verify ()
  16238. "Don't let flyspell put overlays at active buttons."
  16239. (and (not (get-text-property (point) 'keymap))
  16240. (not (get-text-property (point) 'org-no-flyspell))))
  16241. (defun org-remove-flyspell-overlays-in (beg end)
  16242. "Remove flyspell overlays in region."
  16243. (and (org-bound-and-true-p flyspell-mode)
  16244. (fboundp 'flyspell-delete-region-overlays)
  16245. (flyspell-delete-region-overlays beg end))
  16246. (add-text-properties beg end '(org-no-flyspell t)))
  16247. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16248. (eval-after-load "bookmark"
  16249. '(if (boundp 'bookmark-after-jump-hook)
  16250. ;; We can use the hook
  16251. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16252. ;; Hook not available, use advice
  16253. (defadvice bookmark-jump (after org-make-visible activate)
  16254. "Make the position visible."
  16255. (org-bookmark-jump-unhide))))
  16256. ;; Make sure saveplace shows the location if it was hidden
  16257. (eval-after-load "saveplace"
  16258. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16259. "Make the position visible."
  16260. (org-bookmark-jump-unhide)))
  16261. ;; Make sure ecb shows the location if it was hidden
  16262. (eval-after-load "ecb"
  16263. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16264. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16265. (if (eq major-mode 'org-mode)
  16266. (org-show-context))))
  16267. (defun org-bookmark-jump-unhide ()
  16268. "Unhide the current position, to show the bookmark location."
  16269. (and (org-mode-p)
  16270. (or (org-invisible-p)
  16271. (save-excursion (goto-char (max (point-min) (1- (point))))
  16272. (org-invisible-p)))
  16273. (org-show-context 'bookmark-jump)))
  16274. ;; Make session.el ignore our circular variable
  16275. (eval-after-load "session"
  16276. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16277. ;;;; Experimental code
  16278. (defun org-closed-in-range ()
  16279. "Sparse tree of items closed in a certain time range.
  16280. Still experimental, may disappear in the future."
  16281. (interactive)
  16282. ;; Get the time interval from the user.
  16283. (let* ((time1 (org-float-time
  16284. (org-read-date nil 'to-time nil "Starting date: ")))
  16285. (time2 (org-float-time
  16286. (org-read-date nil 'to-time nil "End date:")))
  16287. ;; callback function
  16288. (callback (lambda ()
  16289. (let ((time
  16290. (org-float-time
  16291. (apply 'encode-time
  16292. (org-parse-time-string
  16293. (match-string 1))))))
  16294. ;; check if time in interval
  16295. (and (>= time time1) (<= time time2))))))
  16296. ;; make tree, check each match with the callback
  16297. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16298. ;;;; Finish up
  16299. (provide 'org)
  16300. (run-hooks 'org-load-hook)
  16301. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16302. ;;; org.el ends here