Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

emacs.el 107 KiB

6 anos atrás
13 anos atrás
12 anos atrás
13 anos atrás
13 anos atrás
9 anos atrás
11 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
6 anos atrás
8 anos atrás
8 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
11 anos atrás
8 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
13 anos atrás
13 anos atrás
12 anos atrás
13 anos atrás
9 anos atrás
9 anos atrás
6 anos atrás
6 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
12 anos atrás
13 anos atrás
9 anos atrás
13 anos atrás
13 anos atrás
8 anos atrás
8 anos atrás
12 anos atrás
8 anos atrás
6 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
8 anos atrás
8 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
12 anos atrás
12 anos atrás
11 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
11 anos atrás
11 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
11 anos atrás
13 anos atrás
12 anos atrás
13 anos atrás
13 anos atrás
6 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
11 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
12 anos atrás
13 anos atrás
12 anos atrás
13 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
6 anos atrás
6 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
13 anos atrás
6 anos atrás
11 anos atrás
11 anos atrás
6 anos atrás
6 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
13 anos atrás
13 anos atrás
6 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
12 anos atrás
6 anos atrás
12 anos atrás
6 anos atrás
12 anos atrás
12 anos atrás
6 anos atrás
12 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
11 anos atrás
9 anos atrás
11 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
12 anos atrás
6 anos atrás
13 anos atrás
13 anos atrás
8 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
13 anos atrás
8 anos atrás
13 anos atrás
9 anos atrás
11 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
8 anos atrás
10 anos atrás
13 anos atrás
6 anos atrás
6 anos atrás
13 anos atrás
6 anos atrás
13 anos atrás
12 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
6 anos atrás
13 anos atrás
8 anos atrás
6 anos atrás
6 anos atrás
13 anos atrás
13 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
12 anos atrás
12 anos atrás
8 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
12 anos atrás
8 anos atrás
11 anos atrás
6 anos atrás
6 anos atrás
11 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
11 anos atrás
11 anos atrás
6 anos atrás
6 anos atrás
11 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104
  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Code:
  3. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  4. ;; SETUP_LOAD: (and (file-readable-p file)
  5. ;; SETUP_LOAD: (byte-recompile-file file nil 0 t)))
  6. (setq debug-on-error t)
  7. ;; make directories
  8. (unless (file-directory-p (expand-file-name user-emacs-directory))
  9. (make-directory (expand-file-name user-emacs-directory)))
  10. (require 'cl-lib)
  11. (require 'simple)
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;; Some macros for internals
  14. (defun call-after-init (func)
  15. "If `after-init-hook' has been run, call FUNC immediately.
  16. Otherwize hook it."
  17. (if after-init-time
  18. (funcall func)
  19. (add-hook 'after-init-hook
  20. func)))
  21. (defmacro safe-require-or-eval (feature)
  22. "Require FEATURE if available.
  23. At compile time the feature will be loaded immediately."
  24. `(eval-and-compile
  25. (message "safe-require-or-eval: Trying to require %s" ,feature)
  26. (require ,feature nil t)))
  27. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  28. "Define autoloading FEATURE that defines FUNCTIONS.
  29. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  30. the function same as FEATURE is defined as autoloaded function. BODY is passed
  31. to `eval-after-load'.
  32. After this macro is expanded, this returns the path to library if FEATURE
  33. found, otherwise returns nil."
  34. (declare (indent 2) (debug t))
  35. (let* ((libname (symbol-name (eval feature)))
  36. (libpath (locate-library libname)))
  37. `(progn
  38. (when (locate-library ,libname)
  39. ,@(mapcar (lambda (f)
  40. `(unless (fboundp ',f)
  41. (progn
  42. (message "Autoloaded function `%S' defined (%s)"
  43. (quote ,f)
  44. ,libpath)
  45. (autoload (quote ,f)
  46. ,libname
  47. ,(concat "Autoloaded function defined in \""
  48. libpath
  49. "\".")
  50. t))))
  51. (or (eval functions)
  52. `(,(eval feature)))))
  53. (eval-after-load ,feature
  54. (quote (progn
  55. ,@body)))
  56. (locate-library ,libname))))
  57. (when (autoload-eval-lazily 'tetris nil
  58. (message "Tetris loaded!"))
  59. (message "Tetris found!"))
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ;; package
  62. (set (defvar 10sr-package-list)
  63. '(
  64. vimrc-mode
  65. markdown-mode
  66. yaml-mode
  67. gnuplot-mode
  68. php-mode
  69. erlang
  70. js2-mode
  71. js-doc
  72. git-commit
  73. gitignore-mode
  74. adoc-mode
  75. go-mode
  76. ;; It seems malabar has been merged into jdee and this package
  77. ;; already removed
  78. ;; malabar-mode
  79. gosh-mode
  80. scala-mode
  81. ;;ensime
  82. ;; ack
  83. color-moccur
  84. ggtags
  85. flycheck
  86. auto-highlight-symbol
  87. hl-todo
  88. ;; Currently not available
  89. ;; pp-c-l
  90. xclip
  91. foreign-regexp
  92. multi-term
  93. term-run
  94. editorconfig
  95. git-ps1-mode
  96. restart-emacs
  97. fill-column-indicator
  98. pkgbuild-mode
  99. minibuffer-line
  100. which-key
  101. ;; I think this works in place of my autosave lib
  102. super-save
  103. pipenv
  104. imenu-list
  105. page-break-lines
  106. ;; sync-recentf
  107. aggressive-indent
  108. ;; fancy-narrow
  109. dired-filter
  110. wgrep
  111. magit
  112. git-gutter
  113. end-mark
  114. sl
  115. editorconfig
  116. editorconfig-custom-majormode
  117. git-command
  118. prompt-text
  119. ;; 10sr repository
  120. ;; 10sr-extras
  121. terminal-title
  122. recentf-show
  123. dired-list-all-mode
  124. pack
  125. set-modeline-color
  126. read-only-only-mode
  127. smart-revert
  128. autosave
  129. ;;window-organizer
  130. ilookup
  131. pasteboard
  132. ))
  133. (when (safe-require-or-eval 'package)
  134. (setq package-archives
  135. `(,@package-archives
  136. ("melpa" . "https://melpa.org/packages/")
  137. ;; Somehow fails to download via https
  138. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  139. (package-initialize)
  140. (defun my-auto-install-package ()
  141. "Install packages semi-automatically."
  142. (interactive)
  143. (package-refresh-contents)
  144. (mapc (lambda (pkg)
  145. (or (package-installed-p pkg)
  146. (package-install pkg)))
  147. 10sr-package-list))
  148. )
  149. ;; (lazy-load-eval 'sudoku)
  150. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  151. ;; my-idle-hook
  152. (defvar my-idle-hook nil
  153. "Hook run when idle for several secs.")
  154. (defvar my-idle-hook-sec 5
  155. "Second to run `my-idle-hook'.")
  156. (run-with-idle-timer my-idle-hook-sec
  157. t
  158. (lambda ()
  159. (run-hooks 'my-idle-hook)))
  160. ;; (add-hook 'my-idle-hook
  161. ;; (lambda ()
  162. ;; (message "idle hook message")))
  163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  164. ;; start and quit
  165. (setq inhibit-startup-message t)
  166. (setq confirm-kill-emacs 'y-or-n-p)
  167. (setq gc-cons-threshold (* 1024 1024 16))
  168. (setq garbage-collection-messages nil)
  169. (when window-system
  170. (add-to-list 'default-frame-alist '(cursor-type . box))
  171. (add-to-list 'default-frame-alist '(background-color . "white"))
  172. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  173. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  174. ;; does not work?
  175. )
  176. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  177. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  178. (and (fboundp 'tool-bar-mode)
  179. (tool-bar-mode 0))
  180. (and (fboundp 'set-scroll-bar-mode)
  181. (set-scroll-bar-mode nil))
  182. (call-after-init
  183. (lambda ()
  184. (message "%s %s" invocation-name emacs-version)
  185. (message "Invocation directory: %s" default-directory)
  186. (message "%s was taken to initialize emacs." (emacs-init-time))
  187. (view-echo-area-messages)
  188. ;; (view-emacs-news)
  189. ))
  190. (cd ".") ; when using windows use / instead of \ in `default-directory'
  191. ;; locale
  192. (set-language-environment "Japanese")
  193. (set-default-coding-systems 'utf-8-unix)
  194. (prefer-coding-system 'utf-8-unix)
  195. (setq system-time-locale "C")
  196. ;; my prefix map
  197. (defvar my-prefix-map nil
  198. "My prefix map.")
  199. (define-prefix-command 'my-prefix-map)
  200. ;; (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  201. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  202. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  203. ;; (comint-show-maximum-output)
  204. ;; kill scratch
  205. (call-after-init (lambda ()
  206. (let ((buf (get-buffer "*scratch*")))
  207. (when buf
  208. (kill-buffer buf)))))
  209. ;; modifier keys
  210. ;; (setq mac-option-modifier 'control)
  211. ;; display
  212. (setq visible-bell t)
  213. (setq ring-bell-function 'ignore)
  214. (mouse-avoidance-mode 'banish)
  215. (setq echo-keystrokes 0.1)
  216. (defun reload-init-file ()
  217. "Reload Emacs init file."
  218. (interactive)
  219. (when (and user-init-file
  220. (file-readable-p user-init-file))
  221. (load-file user-init-file)))
  222. (safe-require-or-eval 'session)
  223. ;; server
  224. (set-variable 'server-name (concat "server"
  225. (number-to-string (emacs-pid))))
  226. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  227. ;; In Darwin environment, init fails with message like 'Service name too long'
  228. ;; when server-use-tcp is nil
  229. (when (or (eq system-type
  230. 'cygwin)
  231. (eq system-type
  232. 'darwin))
  233. (set-variable 'server-use-tcp t))
  234. ;; MSYS2 fix
  235. (when (eq system-type
  236. 'windows-nt)
  237. (setq shell-file-name
  238. (executable-find "bash"))
  239. '(setq function-key-map
  240. `(,@function-key-map ([pause] . [?\C-c])
  241. ))
  242. (define-key key-translation-map
  243. (kbd "<pause>")
  244. (kbd "C-c"))
  245. '(keyboard-translate [pause]
  246. (kbd "C-c")p)
  247. ;; TODO: move to other place later
  248. (when (not window-system)
  249. (setq interprogram-paste-function nil)
  250. (setq interprogram-cut-function nil)))
  251. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  252. ;; global keys
  253. (global-set-key (kbd "<up>") 'scroll-down-line)
  254. (global-set-key (kbd "<down>") 'scroll-up-line)
  255. (global-set-key (kbd "<left>") 'scroll-down)
  256. (global-set-key (kbd "<right>") 'scroll-up)
  257. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  258. (global-set-key (kbd "C-\\") help-map)
  259. (define-key ctl-x-map (kbd "DEL") help-map)
  260. (define-key ctl-x-map (kbd "C-h") help-map)
  261. (define-key help-map "a" 'apropos)
  262. ;; disable annoying keys
  263. (global-set-key [prior] 'ignore)
  264. (global-set-key (kbd "<next>") 'ignore)
  265. (global-set-key [menu] 'ignore)
  266. (global-set-key [down-mouse-1] 'ignore)
  267. (global-set-key [down-mouse-2] 'ignore)
  268. (global-set-key [down-mouse-3] 'ignore)
  269. (global-set-key [mouse-1] 'ignore)
  270. (global-set-key [mouse-2] 'ignore)
  271. (global-set-key [mouse-3] 'ignore)
  272. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  273. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  274. (when (safe-require-or-eval 'which-key)
  275. (which-key-mode))
  276. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  277. ;; editor
  278. (setq kill-whole-line t)
  279. (setq scroll-conservatively 35
  280. scroll-margin 2)
  281. (setq-default major-mode 'text-mode)
  282. (setq next-line-add-newlines nil)
  283. (setq kill-read-only-ok t)
  284. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  285. ;; (setq-default line-spacing 0.2)
  286. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  287. ;; (setq-default tab-width 4)
  288. (setq-default indent-tabs-mode nil)
  289. (setq-default indent-line-function 'indent-to-left-margin)
  290. ;; (setq-default indent-line-function nil)
  291. (setq-default truncate-lines nil)
  292. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  293. (delete-selection-mode 1)
  294. (cua-mode 0)
  295. (setq line-move-visual nil)
  296. (setq create-lockfiles nil)
  297. (add-hook 'before-save-hook
  298. 'time-stamp)
  299. ;; Add Time-stamp: <> to insert timestamp there
  300. (set-variable 'time-stamp-format
  301. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  302. ;; key bindings
  303. ;; moving around
  304. ;;(keyboard-translate ?\M-j ?\C-j)
  305. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  306. (define-key esc-map "p" 'backward-paragraph)
  307. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  308. (define-key esc-map "n" 'forward-paragraph)
  309. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  310. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  311. (global-set-key (kbd "C-<left>") 'scroll-down)
  312. (global-set-key (kbd "C-<right>") 'scroll-up)
  313. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  314. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  315. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  316. ;; C-h and DEL
  317. (global-set-key (kbd "C-h") (kbd "DEL"))
  318. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  319. (global-set-key (kbd "C-m") 'newline-and-indent)
  320. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  321. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  322. (define-key esc-map "u" 'undo)
  323. (define-key esc-map "i" (kbd "ESC TAB"))
  324. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  325. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  326. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  327. (require 'page-ext nil t)
  328. (when (safe-require-or-eval 'page-break-lines)
  329. (global-page-break-lines-mode 1))
  330. (when (safe-require-or-eval 'git-gutter)
  331. (custom-set-variables
  332. '(git-gutter:update-interval 2))
  333. (custom-set-variables
  334. '(git-gutter:unchanged-sign " "))
  335. (when (= (display-color-cells)
  336. 256)
  337. (let ((c "color-233"))
  338. (set-face-background 'git-gutter:modified c)
  339. (set-face-background 'git-gutter:added c)
  340. (set-face-background 'git-gutter:deleted c)
  341. (set-face-background 'git-gutter:unchanged c)))
  342. (global-git-gutter-mode 1)
  343. )
  344. ;; (when (safe-require-or-eval 'fancy-narrow)
  345. ;; (fancy-narrow-mode 1))
  346. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  347. ;; title and mode-line
  348. (when (safe-require-or-eval 'terminal-title)
  349. ;; if TERM is not screen use default value
  350. (if (getenv "TMUX")
  351. ;; if use tmux locally just basename of current dir
  352. (set-variable 'terminal-title-format
  353. '((file-name-nondirectory (directory-file-name
  354. default-directory))))
  355. (if (and (let ((tty-type (frame-parameter nil
  356. 'tty-type)))
  357. (and tty-type
  358. (equal (car (split-string tty-type
  359. "-"))
  360. "screen")))
  361. (not (getenv "SSH_CONNECTION")))
  362. (set-variable 'terminal-title-format
  363. '((file-name-nondirectory (directory-file-name
  364. default-directory))))
  365. ;; seems that TMUX is used locally and ssh to remote host
  366. (set-variable 'terminal-title-format
  367. `("em:"
  368. ,user-login-name
  369. "@"
  370. ,(car (split-string (system-name)
  371. "\\."))
  372. ":"
  373. default-directory))
  374. )
  375. )
  376. (terminal-title-mode))
  377. (setq eol-mnemonic-dos "\\r\\n")
  378. (setq eol-mnemonic-mac "\\r")
  379. (setq eol-mnemonic-unix "")
  380. (which-function-mode 1)
  381. (line-number-mode 0)
  382. (column-number-mode 0)
  383. (size-indication-mode 0)
  384. (setq mode-line-position
  385. '(:eval (format "L%%l/%d%s:C%%c"
  386. (count-lines (point-max)
  387. (point-min))
  388. (if (buffer-narrowed-p)
  389. "[N]"
  390. "")
  391. )))
  392. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  393. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  394. ;; minibuffer
  395. (setq insert-default-directory t)
  396. (setq completion-ignore-case t
  397. read-file-name-completion-ignore-case t
  398. read-buffer-completion-ignore-case t)
  399. (setq resize-mini-windows t)
  400. (temp-buffer-resize-mode 1)
  401. (savehist-mode 1)
  402. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  403. (set-variable 'help-at-pt-display-when-idle t)
  404. (fset 'yes-or-no-p 'y-or-n-p)
  405. ;; complete symbol when `eval'
  406. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  407. (define-key minibuffer-local-map (kbd "C-u")
  408. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  409. ;; I dont know these bindings are good
  410. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  411. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  412. (when (safe-require-or-eval 'minibuffer-line)
  413. (set-face-underline 'minibuffer-line nil)
  414. (set-variable 'minibuffer-line-refresh-interval
  415. 25)
  416. ;; Set idle timer
  417. (defvar my-minibuffer-line--idle-timer nil)
  418. (defvar minibuffer-line-mode)
  419. (add-hook 'minibuffer-line-mode-hook
  420. (lambda ()
  421. (when my-minibuffer-line--idle-timer
  422. (cancel-timer my-minibuffer-line--idle-timer)
  423. (setq my-minibuffer-line--idle-timer nil))
  424. (when minibuffer-line-mode
  425. (setq my-minibuffer-line--idle-timer
  426. (run-with-idle-timer 0.5
  427. t
  428. 'minibuffer-line--update)))))
  429. (set-variable 'minibuffer-line-format
  430. `(,(concat user-login-name
  431. "@"
  432. (car (split-string (system-name)
  433. "\\."))
  434. ":")
  435. (:eval (abbreviate-file-name (or buffer-file-name
  436. default-directory)))
  437. (:eval (and (fboundp 'git-ps1-mode-get-current)
  438. (git-ps1-mode-get-current " [GIT:%s]")))
  439. " "
  440. (:eval (format-time-string display-time-format))))
  441. (minibuffer-line-mode 1)
  442. )
  443. (when (safe-require-or-eval 'prompt-text)
  444. (set-variable 'prompt-text-format
  445. `(,(concat ""
  446. user-login-name
  447. "@"
  448. (car (split-string (system-name)
  449. "\\."))
  450. ":")
  451. (:eval (abbreviate-file-name (or buffer-file-name
  452. default-directory)))
  453. (:eval (and (fboundp 'git-ps1-mode-get-current)
  454. (git-ps1-mode-get-current " [GIT:%s]")))
  455. " "
  456. (:eval (format-time-string display-time-format))
  457. "\n"
  458. (:eval (symbol-name this-command))
  459. ": "))
  460. (prompt-text-mode 1))
  461. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  462. ;; letters, font-lock mode and fonts
  463. (setq text-quoting-style 'grave)
  464. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  465. ;; (set-window-margins (selected-window) 1 1)
  466. (unless window-system
  467. (setq frame-background-mode 'dark))
  468. (and (or (eq system-type 'Darwin)
  469. (eq system-type 'darwin))
  470. (fboundp 'mac-set-input-method-parameter)
  471. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  472. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  473. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  474. (boundp 'input-method-inactivate-hook))
  475. (add-hook 'input-method-activate-hook
  476. (lambda () (set-cursor-color "red")))
  477. (add-hook 'input-method-inactivate-hook
  478. (lambda () (set-cursor-color "black"))))
  479. (when (safe-require-or-eval 'paren)
  480. (show-paren-mode 1)
  481. (setq show-paren-delay 0.5
  482. show-paren-style 'parenthesis) ; mixed is hard to read
  483. ;; (set-face-background 'show-paren-match
  484. ;; "black")
  485. ;; ;; (face-foreground 'default))
  486. ;; (set-face-foreground 'show-paren-match
  487. ;; "white")
  488. ;; (set-face-inverse-video-p 'show-paren-match
  489. ;; t)
  490. )
  491. (transient-mark-mode 1)
  492. (global-font-lock-mode 1)
  493. (setq font-lock-global-modes
  494. '(not
  495. help-mode
  496. eshell-mode
  497. ;;term-mode
  498. Man-mode
  499. magit-diff-mode
  500. magit-revision-mode))
  501. ;; (standard-display-ascii ?\n "$\n")
  502. ;; (defvar my-eol-face
  503. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  504. ;; )
  505. ;; (defvar my-tab-face
  506. ;; '(("\t" . '(0 highlight t nil))))
  507. (defvar my-jspace-face
  508. '(("\u3000" . '(0 highlight t nil))))
  509. (add-hook 'font-lock-mode-hook
  510. (lambda ()
  511. ;; (font-lock-add-keywords nil my-eol-face)
  512. (font-lock-add-keywords nil my-jspace-face)
  513. ))
  514. (when (safe-require-or-eval 'whitespace)
  515. (add-to-list 'whitespace-display-mappings
  516. ;; We need t since last one takes precedence
  517. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  518. ;; (add-to-list 'whitespace-display-mappings
  519. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  520. (setq whitespace-style '(face
  521. trailing ; trailing blanks
  522. ;; tabs
  523. ;; spaces
  524. ;; lines
  525. lines-tail ; lines over 80
  526. newline ; newlines
  527. empty ; empty lines at beg or end of buffer
  528. ;; big-indent
  529. ;; space-mark
  530. tab-mark
  531. newline-mark ; use display table for newline
  532. ))
  533. ;; (setq whitespace-newline 'font-lock-comment-face)
  534. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  535. (defun my-whitesspace-mode-reload ()
  536. "Reload whitespace-mode config."
  537. (interactive)
  538. (when whitespace-mode
  539. (whitespace-mode 0)
  540. (whitespace-mode 1)))
  541. (set-variable 'whitespace-line-column nil)
  542. (global-whitespace-mode t)
  543. (add-hook 'dired-mode-hook
  544. (lambda ()
  545. (set (make-local-variable 'whitespace-style) nil)))
  546. (if (= (display-color-cells)
  547. 256)
  548. (set-face-foreground 'whitespace-newline "color-109")
  549. ;; (progn
  550. ;; (set-face-bold-p 'whitespace-newline
  551. ;; t))
  552. ))
  553. (and nil
  554. '(safe-require-or-eval 'fill-column-indicator)
  555. (setq fill-column-indicator))
  556. ;; highlight current line
  557. ;; http://wiki.riywo.com/index.php?Meadow
  558. (face-spec-set 'hl-line
  559. '((((min-colors 256)
  560. (background dark))
  561. (:background "color-234"))
  562. (((min-colors 256)
  563. (background light))
  564. (:background "color-234"))
  565. (t
  566. (:underline "black"))))
  567. (set-variable 'hl-line-global-modes
  568. '(not
  569. term-mode))
  570. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  571. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  572. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  573. ;;(safe-require-or-eval 'set-modeline-color)
  574. ;; (let ((fg (face-foreground 'default))
  575. ;; (bg (face-background 'default)))
  576. ;; (set-face-background 'mode-line-inactive
  577. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  578. ;; (set-face-foreground 'mode-line-inactive
  579. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  580. ;; (set-face-underline 'mode-line-inactive
  581. ;; t)
  582. ;; (set-face-underline 'vertical-border
  583. ;; nil)
  584. ;; (when (safe-require-or-eval 'end-mark)
  585. ;; (global-end-mark-mode))
  586. ;; M-x highlight-* to highlight things
  587. (global-hi-lock-mode 1)
  588. (unless (fboundp 'highlight-region-text)
  589. (defun highlight-region-text (beg end)
  590. "Highlight text between BEG and END."
  591. (interactive "r")
  592. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  593. end)))
  594. (setq deactivate-mark t)))
  595. (when (safe-require-or-eval 'auto-highlight-symbol)
  596. (set-variable 'ahs-idle-interval 0.6)
  597. (global-auto-highlight-symbol-mode 1))
  598. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  599. ;; file handling
  600. (when (safe-require-or-eval 'editorconfig)
  601. ;; (set-variable 'editorconfig-get-properties-function
  602. ;; 'editorconfig-core-get-properties-hash)
  603. (editorconfig-mode 1)
  604. (set-variable 'editorconfig-mode-lighter " EC")
  605. (with-eval-after-load 'org-src
  606. ;; [*.org\[\*Org Src*\[ c \]*\]]
  607. (add-hook 'org-src-mode-hook
  608. 'editorconfig-mode-apply t)))
  609. (when (fboundp 'editorconfig-custom-majormode)
  610. (add-hook 'editorconfig-custom-hooks
  611. 'editorconfig-custom-majormode))
  612. ;; Add readonly=true to set read-only-mode
  613. (add-hook 'editorconfig-custom-hooks
  614. (lambda (props)
  615. (let ((r (gethash 'readonly props)))
  616. (when (and (string= r "true")
  617. (not buffer-read-only))
  618. (read-only-mode 1)))))
  619. ;; (when (fboundp 'editorconfig-charset-extras)
  620. ;; (add-hook 'editorconfig-custom-hooks
  621. ;; 'editorconfig-charset-extras))
  622. (setq revert-without-query '(".+"))
  623. ;; save cursor position
  624. (when (safe-require-or-eval 'saveplace)
  625. (setq-default save-place t)
  626. (setq save-place-file (concat user-emacs-directory
  627. "places")))
  628. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  629. (setq make-backup-files t)
  630. (setq vc-make-backup-files t)
  631. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  632. (setq backup-directory-alist
  633. (cons (cons "." (expand-file-name (concat user-emacs-directory
  634. "backup")))
  635. backup-directory-alist))
  636. (setq version-control 't)
  637. (setq delete-old-versions t)
  638. (setq kept-new-versions 20)
  639. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  640. "auto-save/")))
  641. ;; (setq delete-auto-save-files t)
  642. (setq auto-save-visited-interval 8)
  643. (auto-save-visited-mode 1)
  644. (add-to-list 'completion-ignored-extensions ".bak")
  645. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  646. (setq delete-by-moving-to-trash t)
  647. ;; trash-directory "~/.emacs.d/trash")
  648. (add-hook 'after-save-hook
  649. 'executable-make-buffer-file-executable-if-script-p)
  650. (set-variable 'bookmark-default-file
  651. (expand-file-name (concat user-emacs-directory
  652. "bmk")))
  653. (set-variable 'bookmark-save-flag
  654. 1)
  655. (with-eval-after-load 'recentf
  656. (defvar recentf-exclude)
  657. (defvar bookmark-default-file)
  658. (add-to-list 'recentf-exclude
  659. (regexp-quote bookmark-default-file)))
  660. (when (safe-require-or-eval 'smart-revert)
  661. (smart-revert-on))
  662. ;; autosave
  663. ;; auto-save-visited-mode can be used instead?
  664. ;; (when (safe-require-or-eval 'autosave)
  665. ;; (autosave-set 8))
  666. ;; bookmarks
  667. (define-key ctl-x-map "m" 'list-bookmarks)
  668. ;; vc
  669. (set-variable 'vc-handled-backends '(RCS))
  670. (set-variable 'vc-rcs-register-switches "-l")
  671. (set-variable 'vc-rcs-checkin-switches "-l")
  672. (set-variable 'vc-command-messages t)
  673. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  674. ;; share clipboard with x
  675. ;; this page describes this in details, but only these sexps seem to be needed
  676. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  677. (and nil
  678. (not window-system)
  679. (not (eq window-system 'mac))
  680. (getenv "DISPLAY")
  681. (not (equal (getenv "DISPLAY") ""))
  682. (executable-find "xclip")
  683. ;; (< emacs-major-version 24)
  684. '(safe-require-or-eval 'xclip)
  685. nil
  686. (turn-on-xclip))
  687. (and (eq system-type 'darwin)
  688. (safe-require-or-eval 'pasteboard)
  689. (turn-on-pasteboard))
  690. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  691. ;; some modes and hooks
  692. ;; Include some extra modes
  693. (require 'generic-x)
  694. (when (autoload-eval-lazily 'wgrep)
  695. (set-variable 'wgrep-auto-save-buffer t)
  696. (with-eval-after-load 'grep
  697. (defvar grep-mode-map)
  698. (define-key grep-mode-map
  699. "e"
  700. 'wgrep-change-to-wgrep-mode)))
  701. (with-eval-after-load 'remember
  702. (defvar remember-mode-map (make-sparse-keymap))
  703. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  704. (with-eval-after-load 'magit-files
  705. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  706. ;; existing keybindings.
  707. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  708. ;; to disable thie mode here, but do anyway.
  709. ;; See also https://github.com/magit/magit/issues/3517
  710. (global-magit-file-mode -1))
  711. (with-eval-after-load 'magit-section
  712. (set-face-background 'magit-section-highlight
  713. nil))
  714. (with-eval-after-load 'magit-diff
  715. (set-face-background 'magit-diff-added-highlight
  716. nil)
  717. (set-face-background 'magit-diff-removed-highlight
  718. nil)
  719. (set-face-background 'magit-diff-context-highlight
  720. nil)
  721. )
  722. (when (boundp 'git-rebase-filename-regexp)
  723. (add-to-list 'auto-mode-alist
  724. `(,git-rebase-filename-regexp . text-mode)))
  725. (when (safe-require-or-eval 'aggressive-indent)
  726. (defvar aggressive-indent-excluded-modes)
  727. (setq aggressive-indent-excluded-modes
  728. `(diff-mode
  729. toml-mode
  730. conf-mode
  731. dockerfile-mode
  732. groovy-mode
  733. ,@aggressive-indent-excluded-modes))
  734. (global-aggressive-indent-mode 1))
  735. (when (autoload-eval-lazily 'ggtags)
  736. (add-hook 'c-mode-common-hook
  737. (lambda ()
  738. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  739. (ggtags-mode 1))))
  740. (add-hook 'python-mode-hook
  741. (lambda ()
  742. (ggtags-mode 1))))
  743. (when (autoload-eval-lazily 'imenu-list)
  744. ;; (set-variable 'imenu-list-auto-resize t)
  745. (set-variable 'imenu-list-focus-after-activation t)
  746. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  747. (add-hook 'emacs-lisp-mode-hook
  748. (lambda ()
  749. (setq imenu-generic-expression
  750. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  751. ,@imenu-generic-expression))))
  752. (with-eval-after-load 'compile
  753. (defvar compilation-filter-start)
  754. (defvar compilation-error-regexp-alist)
  755. (require 'ansi-color)
  756. (add-hook 'compilation-filter-hook
  757. (lambda ()
  758. (let ((inhibit-read-only t))
  759. (ansi-color-apply-on-region compilation-filter-start
  760. (point)))))
  761. (add-to-list 'compilation-error-regexp-alist
  762. ;; ansible-lint
  763. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  764. ;; Workaround to avoid ensime error
  765. (defvar ensime-mode-key-prefix nil)
  766. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  767. (when (safe-require-or-eval 'company)
  768. (global-company-mode)
  769. (set-variable 'company-idle-delay 0.5)
  770. (set-variable 'company-minimum-prefix-length 2)
  771. (set-variable 'company-selection-wrap-around t))
  772. ;; https://github.com/lunaryorn/flycheck
  773. (when (safe-require-or-eval 'flycheck)
  774. (call-after-init 'global-flycheck-mode))
  775. (when (autoload-eval-lazily 'ilookup)
  776. (define-key ctl-x-map "d" 'ilookup-open-word))
  777. (set-variable 'ac-ignore-case nil)
  778. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  779. (define-key ctl-x-map "t" 'term-run-shell-command))
  780. (add-to-list 'safe-local-variable-values
  781. '(encoding utf-8))
  782. (setq enable-local-variables :safe)
  783. ;; Detect file type from shebang and set major-mode.
  784. (add-to-list 'interpreter-mode-alist
  785. '("python3" . python-mode))
  786. (add-to-list 'interpreter-mode-alist
  787. '("python2" . python-mode))
  788. (with-eval-after-load 'python
  789. (defvar python-mode-map (make-sparse-keymap))
  790. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  791. (when (autoload-eval-lazily 'pipenv)
  792. (add-hook 'python-mode-hook
  793. (lambda ()
  794. (pipenv-mode 1)
  795. (pipenv-projectile-after-switch-default)))
  796. )
  797. (set-variable 'flycheck-python-pycompile-executable "python3")
  798. (set-variable 'python-indent-guess-indent-offset nil)
  799. ;; http://fukuyama.co/foreign-regexp
  800. '(and (safe-require-or-eval 'foreign-regexp)
  801. (progn
  802. (setq foreign-regexp/regexp-type 'perl)
  803. '(setq reb-re-syntax 'foreign-regexp)
  804. ))
  805. (autoload-eval-lazily 'sql '(sql-mode)
  806. (require 'sql-indent nil t))
  807. (when (autoload-eval-lazily 'git-command)
  808. (define-key ctl-x-map "g" 'git-command))
  809. (when (safe-require-or-eval 'git-commit)
  810. (global-git-commit-mode 1))
  811. (with-eval-after-load 'git-commit
  812. (add-hook 'git-commit-setup-hook
  813. 'turn-off-auto-fill t))
  814. (autoload-eval-lazily 'sl)
  815. (with-eval-after-load 'rst
  816. (defvar rst-mode-map)
  817. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  818. (with-eval-after-load 'jdee
  819. (add-hook 'jdee-mode-hook
  820. (lambda ()
  821. (make-local-variable 'global-mode-string)
  822. (add-to-list 'global-mode-string
  823. mode-line-position))))
  824. ;; Cannot enable error thrown. Why???
  825. ;; https://github.com/m0smith/malabar-mode#Installation
  826. ;; (when (autoload-eval-lazily 'malabar-mode)
  827. ;; (add-to-list 'load-path
  828. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  829. ;; (safe-require-or-eval 'cedet-devel-load)
  830. ;; (call-after-init 'activate-malabar-mode))
  831. (with-eval-after-load 'make-mode
  832. (defvar makefile-mode-map (make-sparse-keymap))
  833. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  834. ;; this functions is set in write-file-functions, i cannot find any
  835. ;; good way to remove this.
  836. (fset 'makefile-warn-suspicious-lines 'ignore))
  837. (with-eval-after-load 'verilog-mode
  838. (defvar verilog-mode-map (make-sparse-keymap))
  839. (define-key verilog-mode-map ";" 'self-insert-command))
  840. (setq diff-switches "-u")
  841. (with-eval-after-load 'diff-mode
  842. ;; (when (and (eq major-mode
  843. ;; 'diff-mode)
  844. ;; (not buffer-file-name))
  845. ;; ;; do not pass when major-mode is derived mode of diff-mode
  846. ;; (view-mode 1))
  847. (set-face-attribute 'diff-header nil
  848. :foreground nil
  849. :background nil
  850. :weight 'bold)
  851. (set-face-attribute 'diff-file-header nil
  852. :foreground nil
  853. :background nil
  854. :weight 'bold)
  855. (set-face-foreground 'diff-index "blue")
  856. (set-face-attribute 'diff-hunk-header nil
  857. :foreground "cyan"
  858. :weight 'normal)
  859. (set-face-attribute 'diff-context nil
  860. ;; :foreground "white"
  861. :foreground nil
  862. :weight 'normal)
  863. (set-face-foreground 'diff-removed "red")
  864. (set-face-foreground 'diff-added "green")
  865. (set-face-background 'diff-removed nil)
  866. (set-face-background 'diff-added nil)
  867. (set-face-attribute 'diff-changed nil
  868. :foreground "magenta"
  869. :weight 'normal)
  870. (set-face-attribute 'diff-refine-changed nil
  871. :foreground nil
  872. :background nil
  873. :weight 'bold
  874. :inverse-video t)
  875. ;; Annoying !
  876. ;;(diff-auto-refine-mode)
  877. )
  878. ;; (ffap-bindings)
  879. (set-variable 'browse-url-browser-function
  880. 'eww-browse-url)
  881. (set-variable 'sh-here-document-word "__EOC__")
  882. (when (autoload-eval-lazily 'adoc-mode
  883. nil
  884. (defvar adoc-mode-map (make-sparse-keymap))
  885. (define-key adoc-mode-map (kbd "C-m") 'newline))
  886. (setq auto-mode-alist
  887. `(("\\.adoc\\'" . adoc-mode)
  888. ("\\.asciidoc\\'" . adoc-mode)
  889. ,@auto-mode-alist)))
  890. (with-eval-after-load 'markup-faces
  891. ;; Is this too match ?
  892. (set-face-foreground 'markup-meta-face
  893. "color-245")
  894. (set-face-foreground 'markup-meta-hide-face
  895. "color-245")
  896. )
  897. ;; TODO: check if this is required
  898. (when (autoload-eval-lazily 'groovy-mode nil
  899. (defvar groovy-mode-map (make-sparse-keymap))
  900. (define-key groovy-mode-map "(" 'self-insert-command)
  901. (define-key groovy-mode-map ")" 'self-insert-command)
  902. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  903. )
  904. (add-to-list 'auto-mode-alist
  905. '("build\\.gradle\\'" . groovy-mode)))
  906. (with-eval-after-load 'yaml-mode
  907. (defvar yaml-mode-map (make-sparse-keymap))
  908. (define-key yaml-mode-map (kbd "C-m") 'newline))
  909. (with-eval-after-load 'html-mode
  910. (defvar html-mode-map (make-sparse-keymap))
  911. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  912. (with-eval-after-load 'text-mode
  913. (define-key text-mode-map (kbd "C-m") 'newline))
  914. (autoload-eval-lazily 'info nil
  915. (defvar Info-additional-directory-list)
  916. (dolist (dir (directory-files (concat user-emacs-directory
  917. "info")
  918. t
  919. "^[^.].*"))
  920. (when (file-directory-p dir)
  921. (add-to-list 'Info-additional-directory-list
  922. dir)))
  923. (let ((dir (expand-file-name "~/.brew/share/info")))
  924. (when (file-directory-p dir)
  925. (add-to-list 'Info-additional-directory-list
  926. dir))))
  927. (with-eval-after-load 'apropos
  928. (defvar apropos-mode-map (make-sparse-keymap))
  929. (define-key apropos-mode-map "n" 'next-line)
  930. (define-key apropos-mode-map "p" 'previous-line))
  931. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  932. ;; (define-key isearch-mode-map
  933. ;; (kbd "C-j") 'isearch-other-control-char)
  934. ;; (define-key isearch-mode-map
  935. ;; (kbd "C-k") 'isearch-other-control-char)
  936. ;; (define-key isearch-mode-map
  937. ;; (kbd "C-h") 'isearch-other-control-char)
  938. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  939. (define-key isearch-mode-map (kbd "M-r")
  940. 'isearch-query-replace-regexp)
  941. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  942. (setq lazy-highlight-cleanup nil)
  943. ;; face for isearch highlighing
  944. (set-face-attribute 'lazy-highlight
  945. nil
  946. :foreground `unspecified
  947. :background `unspecified
  948. :underline t
  949. ;; :weight `bold
  950. )
  951. (add-hook 'outline-mode-hook
  952. (lambda ()
  953. (when (string-match "\\.md\\'" buffer-file-name)
  954. (set (make-local-variable 'outline-regexp) "#+ "))))
  955. (add-hook 'outline-mode-hook
  956. 'outline-show-all)
  957. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  958. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  959. (when (autoload-eval-lazily 'markdown-mode
  960. '(markdown-mode gfm-mode)
  961. (defvar gfm-mode-map (make-sparse-keymap))
  962. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  963. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  964. (set-variable 'markdown-command (or (executable-find "markdown")
  965. (executable-find "markdown.pl")
  966. ""))
  967. (add-hook 'markdown-mode-hook
  968. (lambda ()
  969. (outline-minor-mode 1)
  970. (flyspell-mode)
  971. (set (make-local-variable 'comment-start) ";")))
  972. )
  973. ;; c-mode
  974. ;; http://www.emacswiki.org/emacs/IndentingC
  975. ;; http://en.wikipedia.org/wiki/Indent_style
  976. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  977. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  978. (with-eval-after-load 'cc-vars
  979. (defvar c-default-style nil)
  980. (add-to-list 'c-default-style
  981. '(c-mode . "k&r"))
  982. (add-to-list 'c-default-style
  983. '(c++-mode . "k&r")))
  984. (autoload-eval-lazily 'js2-mode nil
  985. ;; currently do not use js2-mode
  986. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  987. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  988. ;; (defvar js2-mode-map (make-sparse-keymap))
  989. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  990. ;; (interactive)
  991. ;; (js2-enter-key)
  992. ;; (indent-for-tab-command)))
  993. ;; (add-hook (kill-local-variable 'before-save-hook)
  994. ;; 'js2-before-save)
  995. ;; (add-hook 'before-save-hook
  996. ;; 'my-indent-buffer
  997. ;; nil
  998. ;; t)
  999. )
  1000. (add-to-list 'interpreter-mode-alist
  1001. '("node" . js-mode))
  1002. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1003. (with-eval-after-load 'uniquify
  1004. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1005. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1006. (setq uniquify-min-dir-content 1))
  1007. (with-eval-after-load 'view
  1008. (defvar view-mode-map (make-sparse-keymap))
  1009. (define-key view-mode-map "j" 'scroll-up-line)
  1010. (define-key view-mode-map "k" 'scroll-down-line)
  1011. (define-key view-mode-map "v" 'toggle-read-only)
  1012. (define-key view-mode-map "q" 'bury-buffer)
  1013. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1014. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1015. ;; (define-key view-mode-map
  1016. ;; "n" 'nonincremental-repeat-search-forward)
  1017. ;; (define-key view-mode-map
  1018. ;; "N" 'nonincremental-repeat-search-backward)
  1019. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1020. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1021. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1022. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1023. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1024. (global-set-key "\M-r" 'view-mode)
  1025. ;; (setq view-read-only t)
  1026. (with-eval-after-load 'term
  1027. (defvar term-raw-map (make-sparse-keymap))
  1028. (define-key term-raw-map (kbd "C-x")
  1029. (lookup-key (current-global-map)
  1030. (kbd "C-x"))))
  1031. (add-hook 'term-mode-hook
  1032. (lambda ()
  1033. ;; Stop current line highlighting
  1034. (set (make-local-variable (defvar hl-line-range-function))
  1035. (lambda () '(0 . 0)))
  1036. (set (make-local-variable 'scroll-margin)
  1037. 0)))
  1038. (add-hook 'Man-mode-hook
  1039. (lambda ()
  1040. (view-mode 1)
  1041. (setq truncate-lines nil)))
  1042. (set-variable 'Man-notify-method (if window-system
  1043. 'newframe
  1044. 'aggressive))
  1045. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1046. "woman_cache.el")))
  1047. ;; not work because man.el will be loaded when man called
  1048. (defalias 'man 'woman)
  1049. (add-to-list 'auto-mode-alist
  1050. '("tox\\.ini\\'" . conf-unix-mode))
  1051. (when (autoload-eval-lazily 'toml-mode)
  1052. (add-to-list 'auto-mode-alist
  1053. '("/tox\\.ini\\'" . toml-mode))
  1054. (add-to-list 'auto-mode-alist
  1055. '("/Pipfile\\'" . toml-mode))
  1056. (add-to-list 'auto-mode-alist
  1057. '("/poetry\\.lock\\'" . toml-mode))
  1058. )
  1059. (when (autoload-eval-lazily 'json-mode)
  1060. (add-to-list 'auto-mode-alist
  1061. '("/Pipfile\\.lock\\'" . json-mode)))
  1062. (add-hook 'go-mode-hook
  1063. (lambda()
  1064. (defvar go-mode-map)
  1065. (add-hook 'before-save-hook' 'gofmt-before-save)
  1066. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1067. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1068. ;; buffers
  1069. (defvar bs-configurations)
  1070. (when (autoload-eval-lazily 'bs '(bs-show)
  1071. (add-to-list 'bs-configurations
  1072. '("specials" "^\\*" nil ".*" nil nil))
  1073. (add-to-list 'bs-configurations
  1074. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1075. (defvar bs-mode-map)
  1076. (defvar bs-current-configuration)
  1077. (define-key bs-mode-map (kbd "t")
  1078. ;; TODO: fix toggle feature
  1079. (lambda ()
  1080. (interactive)
  1081. (if (string= "specials"
  1082. bs-current-configuration)
  1083. (bs-set-configuration "files")
  1084. (bs-set-configuration "specials"))
  1085. (bs-refresh)
  1086. (bs-message-without-log "%s"
  1087. (bs--current-config-message))))
  1088. ;; (setq bs-configurations (list
  1089. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1090. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1091. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1092. )
  1093. (defalias 'list-buffers 'bs-show)
  1094. (set-variable 'bs-default-configuration "files-and-specials")
  1095. (set-variable 'bs-default-sort-name "by nothing")
  1096. (add-hook 'bs-mode-hook
  1097. (lambda ()
  1098. (set (make-local-variable 'scroll-margin) 0))))
  1099. ;;(iswitchb-mode 1)
  1100. (icomplete-mode)
  1101. (defun iswitchb-buffer-display-other-window ()
  1102. "Do iswitchb in other window."
  1103. (interactive)
  1104. (let ((iswitchb-default-method 'display))
  1105. (call-interactively 'iswitchb-buffer)))
  1106. ;; buffer killing
  1107. ;; (defun my-delete-window-killing-buffer () nil)
  1108. (defun my-query-kill-current-buffer ()
  1109. "Interactively kill current buffer."
  1110. (interactive)
  1111. (if (y-or-n-p (concat "kill current buffer? :"))
  1112. (kill-buffer (current-buffer))))
  1113. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1114. (substitute-key-definition 'kill-buffer
  1115. 'my-query-kill-current-buffer
  1116. global-map)
  1117. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1118. ;; recentf-mode
  1119. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1120. "recentf")))
  1121. (set-variable 'recentf-max-menu-items 20)
  1122. (set-variable 'recentf-max-saved-items 30)
  1123. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1124. (set-variable 'recentf-auto-cleanup 3)
  1125. ;; (safe-require-or-eval 'sync-recentf)
  1126. ;; (when (safe-require-or-eval 'recentf)
  1127. ;; (add-to-list 'recentf-exclude
  1128. ;; (regexp-quote recentf-save-file))
  1129. ;; (add-to-list 'recentf-exclude
  1130. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1131. ;; (add-to-list 'recentf-exclude
  1132. ;; "/sync-recentf-marker\\'")
  1133. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1134. ;; (remove-hook 'find-file-hook
  1135. ;; 'recentf-track-opened-file)
  1136. ;; (defun my-recentf-load-track-save-list ()
  1137. ;; "Load current recentf list from file, track current visiting file, then save
  1138. ;; the list."
  1139. ;; (recentf-load-list)
  1140. ;; (recentf-track-opened-file)
  1141. ;; (recentf-save-list))
  1142. ;; (add-hook 'find-file-hook
  1143. ;; 'my-recentf-load-track-save-list)
  1144. ;; (add-hook 'kill-emacs-hook
  1145. ;; 'recentf-load-list)
  1146. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1147. ;; ;; (add-hook 'find-file-hook
  1148. ;; ;; (lambda ()
  1149. ;; ;; (recentf-add-file default-directory)))
  1150. ;; (when (autoload-eval-lazily 'recentf-show)
  1151. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1152. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1153. ;; ;; 'recentf-load-list)
  1154. ;; )
  1155. ;; (recentf-mode 1)
  1156. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1157. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1158. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1159. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1160. ;; (add-hook 'recentf-dialog-mode-hook
  1161. ;; (lambda ()
  1162. ;; ;; (recentf-save-list)
  1163. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1164. ;; ;; 'my-recentf-cd-and-find-file)
  1165. ;; (cd "~/"))))
  1166. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1167. ;; dired
  1168. (defun my-file-head (filename &optional n)
  1169. "Return list of first N lines of file FILENAME."
  1170. ;; TODO: Fix for janapese text
  1171. ;; TODO: Fix for short text
  1172. (let ((num (or n 10))
  1173. (size 100)
  1174. (beg 0)
  1175. (end 0)
  1176. (result '())
  1177. (read -1))
  1178. (with-temp-buffer
  1179. (erase-buffer)
  1180. (while (or (<= (count-lines (point-min)
  1181. (point-max))
  1182. num)
  1183. (eq read 0))
  1184. (setq end (+ beg size))
  1185. (setq read (nth 1 (insert-file-contents-literally filename
  1186. nil
  1187. beg
  1188. end)))
  1189. (goto-char (point-max))
  1190. (setq beg (+ beg size)))
  1191. (goto-char (point-min))
  1192. (while (< (length result) num)
  1193. (let ((start (point)))
  1194. (forward-line 1)
  1195. (setq result
  1196. `(,@result ,(buffer-substring-no-properties start
  1197. (point))))))
  1198. result
  1199. ;; (buffer-substring-no-properties (point-min)
  1200. ;; (progn
  1201. ;; (forward-line num)
  1202. ;; (point)))
  1203. )))
  1204. ;; (apply 'concat (my-file-head "./shrc" 10)
  1205. (defun my-dired-echo-file-head (arg)
  1206. "Echo head of current file.
  1207. ARG is num to show, or defaults to 7."
  1208. (interactive "P")
  1209. (let ((f (dired-get-filename)))
  1210. (message "%s"
  1211. (apply 'concat
  1212. (my-file-head f
  1213. 7)))))
  1214. (defun my-dired-diff ()
  1215. "Show diff of marked file and file of current line."
  1216. (interactive)
  1217. (let ((files (dired-get-marked-files nil nil nil t)))
  1218. (if (eq (car files)
  1219. t)
  1220. (diff (cadr files) (dired-get-filename))
  1221. (message "One file must be marked!"))))
  1222. (defun dired-get-file-info ()
  1223. "Print information of current line file."
  1224. (interactive)
  1225. (let ((f (shell-quote-argument (dired-get-filename t))))
  1226. (if (file-directory-p f)
  1227. (progn
  1228. (message "Calculating disk usage...")
  1229. (shell-command (concat "du -hsD "
  1230. f)))
  1231. (shell-command (concat "file "
  1232. f)))))
  1233. (defun my-dired-scroll-up ()
  1234. "Scroll up."
  1235. (interactive)
  1236. (my-dired-previous-line (- (window-height) 1)))
  1237. (defun my-dired-scroll-down ()
  1238. "Scroll down."
  1239. (interactive)
  1240. (my-dired-next-line (- (window-height) 1)))
  1241. ;; (defun my-dired-forward-line (arg)
  1242. ;; ""
  1243. ;; (interactive "p"))
  1244. (defun my-dired-previous-line (arg)
  1245. "Move ARG lines up."
  1246. (interactive "p")
  1247. (if (> arg 0)
  1248. (progn
  1249. (if (eq (line-number-at-pos)
  1250. 1)
  1251. (goto-char (point-max))
  1252. (forward-line -1))
  1253. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1254. (dired-get-subdir))
  1255. (- arg 1)
  1256. arg)))
  1257. (dired-move-to-filename)))
  1258. (defun my-dired-next-line (arg)
  1259. "Move ARG lines down."
  1260. (interactive "p")
  1261. (if (> arg 0)
  1262. (progn
  1263. (if (eq (point)
  1264. (point-max))
  1265. (goto-char (point-min))
  1266. (forward-line 1))
  1267. (my-dired-next-line (if (or (dired-get-filename nil t)
  1268. (dired-get-subdir))
  1269. (- arg 1)
  1270. arg)))
  1271. (dired-move-to-filename)))
  1272. (defun my-tramp-remote-find-file (f)
  1273. "Open F."
  1274. (interactive (list (read-file-name "My Find File Tramp: "
  1275. "/scp:"
  1276. nil ;; "/scp:"
  1277. (confirm-nonexistent-file-or-buffer))))
  1278. (find-file f))
  1279. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1280. (if (eq window-system 'mac)
  1281. (setq dired-listing-switches "-lhF")
  1282. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1283. )
  1284. (setq dired-listing-switches "-lhF")
  1285. ;; when using dired-find-alternate-file
  1286. ;; reuse current dired buffer for the file to open
  1287. ;; (put 'dired-find-alternate-file 'disabled nil)
  1288. (set-variable 'dired-ls-F-marks-symlinks t)
  1289. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1290. (set-variable 'ls-lisp-dirs-first t)
  1291. (set-variable 'ls-lisp-use-localized-time-format t)
  1292. (set-variable 'ls-lisp-format-time-list
  1293. '("%Y-%m-%d %H:%M"
  1294. "%Y-%m-%d "))
  1295. (set-variable 'dired-dwim-target t)
  1296. (set-variable 'dired-isearch-filenames t)
  1297. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1298. (set-variable 'dired-hide-details-hide-information-lines nil)
  1299. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1300. (set-variable 'dired-recursive-deletes 'always)
  1301. ;; (add-hook 'dired-after-readin-hook
  1302. ;; 'my-replace-nasi-none)
  1303. (with-eval-after-load 'dired
  1304. (safe-require-or-eval 'ls-lisp)
  1305. (defvar dired-mode-map (make-sparse-keymap))
  1306. ;; dired-do-chgrp sometimes cause system hung
  1307. (define-key dired-mode-map "G" 'ignore)
  1308. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1309. (define-key dired-mode-map "i" 'dired-get-file-info)
  1310. (define-key dired-mode-map "f" 'find-file)
  1311. (define-key dired-mode-map "!" 'shell-command)
  1312. (define-key dired-mode-map "&" 'async-shell-command)
  1313. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1314. (define-key dired-mode-map "=" 'my-dired-diff)
  1315. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1316. (define-key dired-mode-map "b" 'gtkbm)
  1317. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1318. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1319. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1320. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1321. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1322. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1323. (substitute-key-definition 'dired-next-line
  1324. 'my-dired-next-line
  1325. dired-mode-map)
  1326. (substitute-key-definition 'dired-previous-line
  1327. 'my-dired-previous-line
  1328. dired-mode-map)
  1329. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1330. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1331. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1332. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1333. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1334. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1335. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1336. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1337. (add-hook 'dired-mode-hook
  1338. (lambda ()
  1339. (when (fboundp 'dired-hide-details-mode)
  1340. (dired-hide-details-mode t)
  1341. (local-set-key "l" 'dired-hide-details-mode))
  1342. (let ((file "._Icon\015"))
  1343. (when nil
  1344. '(file-readable-p file)
  1345. (delete-file file)))))
  1346. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1347. (with-eval-after-load 'dired
  1348. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1349. (when (autoload-eval-lazily 'dired-list-all-mode)
  1350. (setq dired-listing-switches "-lhF")
  1351. (with-eval-after-load 'dired
  1352. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1353. (when (autoload-eval-lazily 'dired-filter)
  1354. (add-hook 'dired-mode-hook
  1355. 'dired-filter-mode))
  1356. ;; Currently disabled in favor of dired-from-git-ls-files
  1357. ;; (define-key ctl-x-map "f" 'find-dired)
  1358. ;; It works!
  1359. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1360. (defun my-dired-git-ls-files (args)
  1361. "Dired from git ls-files."
  1362. (interactive "sgit ls-files args: ")
  1363. (pop-to-buffer-same-window
  1364. (dired-noselect `(,default-directory
  1365. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1366. "\0" t))
  1367. ""))
  1368. )
  1369. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1370. (with-eval-after-load 'dired
  1371. (defvar dired-mode-map (make-sparse-keymap))
  1372. (define-key dired-mode-map "f" 'my-dired-git-ls-files))
  1373. ;; (define-minor-mode my-dired-glob-filter)
  1374. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1375. ;; misc funcs
  1376. (defalias 'qcalc 'quick-calc)
  1377. (defun memo (&optional dir)
  1378. "Open memo.txt in DIR."
  1379. (interactive)
  1380. (pop-to-buffer (find-file-noselect (concat (if dir
  1381. (file-name-as-directory dir)
  1382. "")
  1383. "memo.txt"))))
  1384. (set (defvar my-rgrep-alist nil
  1385. "Alist of rgrep command.
  1386. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1387. condition to choose COMMAND when evaluated.")
  1388. `(
  1389. ;; ripgrep
  1390. ("rg"
  1391. (executable-find "rg")
  1392. "rg --hidden --no-heading --smart-case ")
  1393. ;; git grep
  1394. ("gitgrep"
  1395. (eq 0
  1396. (shell-command "git rev-parse --git-dir"))
  1397. "git --no-pager -c color.grep=always grep -nH -e ")
  1398. ;; sift
  1399. ("sift"
  1400. (executable-find "sift")
  1401. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1402. ;; the silver searcher
  1403. ("ag"
  1404. (executable-find "ag")
  1405. "ag --nogroup --nopager --filename ")
  1406. ;; ack
  1407. ("ack"
  1408. (executable-find "ack")
  1409. "ack --nogroup --nopager --with-filename ")
  1410. ;; gnu global
  1411. ("global"
  1412. (and (require 'ggtags nil t)
  1413. (executable-find "global")
  1414. (ggtags-current-project-root))
  1415. "global --result grep ")
  1416. ;; grep
  1417. ("grep"
  1418. t
  1419. ,(concat "find . "
  1420. "-path '*/.git' -prune -o "
  1421. "-path '*/.svn' -prune -o "
  1422. "-type f -print0 | "
  1423. "xargs -0 grep -nH -e "))
  1424. )
  1425. )
  1426. (defvar my-rgrep-default nil
  1427. "Default command name for my-rgrep.")
  1428. (defun my-rgrep-grep-command (&optional name alist)
  1429. "Return recursive grep command for current directory or nil.
  1430. If NAME is given, use that without testing.
  1431. Commands are searched from ALIST."
  1432. (if alist
  1433. (if name
  1434. ;; if name is given search that from alist and return the command
  1435. (nth 2 (assoc name
  1436. alist))
  1437. ;; if name is not given try test in 1th elem
  1438. (let ((car (car alist))
  1439. (cdr (cdr alist)))
  1440. (if (eval (nth 1 car))
  1441. ;; if the condition is true return the command
  1442. (nth 2 car)
  1443. ;; try next one
  1444. (and cdr
  1445. (my-rgrep-grep-command name cdr)))))
  1446. ;; if alist is not given set default value
  1447. (my-rgrep-grep-command name my-rgrep-alist)))
  1448. (defun my-rgrep (command-args)
  1449. "My recursive grep. Run COMMAND-ARGS.
  1450. If prefix argument is given, use current symbol as default search target
  1451. and search from projectile root (if projectile is available)."
  1452. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1453. nil)))
  1454. (if cmd
  1455. (list (read-shell-command "grep command: "
  1456. (concat cmd
  1457. (if current-prefix-arg
  1458. (thing-at-point 'symbol t)
  1459. ""))
  1460. 'grep-find-history))
  1461. (error "My-Rgrep: Command for rgrep not found")
  1462. )))
  1463. (if (and current-prefix-arg
  1464. (safe-require-or-eval 'projectile)
  1465. (projectile-project-p))
  1466. (projectile-with-default-dir (projectile-project-root)
  1467. (compilation-start command-args
  1468. 'grep-mode))
  1469. (compilation-start command-args
  1470. 'grep-mode)))
  1471. (defun my-rgrep-thing-at-point-projectile-root ()
  1472. "My recursive grep to find thing at point from project root."
  1473. (interactive)
  1474. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1475. nil))
  1476. (command-args
  1477. (if cmd
  1478. (concat cmd
  1479. (or (thing-at-point 'symbol t)
  1480. (error "No symbol at point")))
  1481. (error "My-Rgrep: Command for rgrep not found"))))
  1482. (if (safe-require-or-eval 'projectile)
  1483. (projectile-with-default-dir (or (projectile-project-root)
  1484. default-directory)
  1485. (compilation-start command-args
  1486. 'grep-mode))
  1487. (compilation-start command-args
  1488. 'grep-mode))))
  1489. (defmacro define-my-rgrep (name)
  1490. "Define rgrep for NAME."
  1491. `(defun ,(intern (concat "my-rgrep-"
  1492. name)) ()
  1493. ,(format "My recursive grep by %s."
  1494. name)
  1495. (interactive)
  1496. (let ((my-rgrep-default ,name))
  1497. (if (called-interactively-p 'any)
  1498. (call-interactively 'my-rgrep)
  1499. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1500. )
  1501. (define-my-rgrep "ack")
  1502. (define-my-rgrep "ag")
  1503. (define-my-rgrep "rg")
  1504. (define-my-rgrep "sift")
  1505. (define-my-rgrep "gitgrep")
  1506. (define-my-rgrep "grep")
  1507. (define-my-rgrep "global")
  1508. (define-key ctl-x-map "s" 'my-rgrep)
  1509. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1510. (defun my-occur (regexp &optional region)
  1511. "My occur command to search REGEXP."
  1512. (interactive (list (read-string "List lines matching regexp: "
  1513. (thing-at-point 'symbol t))))
  1514. (occur regexp nil region))
  1515. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1516. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1517. (defalias 'make 'compile)
  1518. (define-key ctl-x-map "c" 'compile)
  1519. ;;;;;;;;;;;;;;;;;;;;;;;
  1520. ;; adoc-simple-mode
  1521. (when (safe-require-or-eval 'adoc-mode)
  1522. (defvar adoc-simple-font-lock-keywords
  1523. nil)
  1524. (define-derived-mode adoc-simple-mode adoc-mode
  1525. "Adoc-Simple"
  1526. "Major mode for editing AsciiDoc text files.
  1527. This mode is a simplified version of `adoc-mode'."
  1528. '(set (make-local-variable 'font-lock-defaults)
  1529. '(adoc-simple-font-lock-keywords
  1530. nil nil nil nil
  1531. (font-lock-multiline . t)
  1532. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1533. (add-to-list 'auto-mode-alist
  1534. '("\\.adoc\\'" . adoc-simple-mode)))
  1535. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1536. ;; awk-preview
  1537. (defgroup awk-preview nil
  1538. "Awk previewer."
  1539. :tag "Awk Preview"
  1540. :prefix "awk-preview-"
  1541. :group 'tools)
  1542. (defcustom awk-preview-program
  1543. (or (executable-find "gawk")
  1544. (executable-find "awk")
  1545. "awk")
  1546. "Awk program to execute."
  1547. :type 'string
  1548. :group 'awk-preview)
  1549. (defcustom awk-preview-switches
  1550. ;; '("--sandbox")
  1551. nil
  1552. "String of awk options appended when running awk preview."
  1553. :type '(repeat string)
  1554. :group 'awk-preview)
  1555. (defcustom awk-preview-default-program
  1556. ;; TODO: Use `substitute-command-keys'
  1557. "# C-c C-l: Update preview C-c C-c: Commit and exit
  1558. # C-c C-r: Resest to original C-c C-k: Abort
  1559. {
  1560. print NR, $0
  1561. }
  1562. "
  1563. "Default awk command."
  1564. :type 'string
  1565. :group 'awk-preview)
  1566. (cl-defstruct awk-preview--env
  1567. (running-p nil)
  1568. ;; Point of beg in source buffer
  1569. (point-beg nil)
  1570. ;; Point of end in source buffer
  1571. (point-end nil)
  1572. ;; Point of beginning
  1573. ;; Used by preview buffer and always same as awk-preview--point-beg
  1574. (preview-point-beg nil)
  1575. ;; Point of beginning
  1576. ;; Used by preview buffer and may defferent from awk-preview--point-end
  1577. (preview-point-end nil)
  1578. ;; Awk preview program file name
  1579. (program-filename nil)
  1580. ;; Source buffer
  1581. (source-buffer nil)
  1582. ;; Preview buffer
  1583. (preview-buffer nil)
  1584. ;; Program buffer
  1585. (program-buffer nil)
  1586. ;; Window configuration when entering awk-review
  1587. (window-configuration nil)
  1588. )
  1589. (defvar awk-preview--env nil
  1590. "`awk-preview--env' struct object of currently running.")
  1591. (make-variable-buffer-local 'awk-preview--env)
  1592. (put 'awk-preview--env
  1593. 'permanent-local
  1594. t)
  1595. (defun awk-preview--invoke-awk (buf beg end progfile output &optional delete display)
  1596. "Execute PROGFILE awk process with BEG and END input and output to OUTPUT buffer.
  1597. When OUTPUT is t the result will be output to BUF.
  1598. Delete the text between BEG and END when DELETE is non-nil.
  1599. DISPLAY non-nil means redisplay buffer as output is inserted."
  1600. (with-current-buffer buf
  1601. (let ((status (apply 'call-process-region
  1602. beg
  1603. end
  1604. awk-preview-program
  1605. delete
  1606. output
  1607. display
  1608. `(,@awk-preview-switches "-f" ,progfile))))
  1609. (unless (eq status
  1610. 0)
  1611. (error "awk-preview: Awk program exited abnormally."))
  1612. output)))
  1613. (defvar awk-preview-program-buffer-name
  1614. "*AWK Preview Program<%s>*"
  1615. "Buffer name of awk preview program.")
  1616. (defvar awk-preview-preview-buffer-name
  1617. "*AWK Preview<%s>*"
  1618. "Buffer name of awk preview.")
  1619. (defun awk-preview--create-program-buffer (e)
  1620. "Create program buffer for `awk-preview--env' object E."
  1621. (let ((source-name (buffer-name (awk-preview--env-source-buffer e))))
  1622. (with-current-buffer (generate-new-buffer (format awk-preview-program-buffer-name
  1623. source-name))
  1624. (erase-buffer)
  1625. (insert awk-preview-default-program)
  1626. (awk-mode)
  1627. (awk-preview-program-mode 1)
  1628. (setf (awk-preview--env-program-buffer e) (current-buffer))
  1629. (setf (awk-preview--env-program-filename e) (make-temp-file "awk-preview-"
  1630. nil
  1631. ".awk"))
  1632. (setq awk-preview--env e)
  1633. (current-buffer))))
  1634. (defun awk-preview-program-buffer-kill-hook ()
  1635. "Cleanup for awk-preview program buffer."
  1636. (when (and awk-preview--env
  1637. (awk-preview--env-program-filename awk-preview--env))
  1638. (delete-file (awk-preview--env-program-filename awk-preview--env))))
  1639. (add-hook 'kill-buffer-hook
  1640. 'awk-preview-program-buffer-kill-hook)
  1641. (defun awk-preview--create-preview-buffer (e)
  1642. "Create preview buffer for `awk-preview--env' object E."
  1643. (with-current-buffer (awk-preview--env-source-buffer e)
  1644. ;; TODO: Do not unset buffer-file-name in let, use unwind-protect
  1645. (let ((buffer-file-name nil)
  1646. (beg (awk-preview--env-point-beg e))
  1647. (end (awk-preview--env-point-end e)))
  1648. (with-current-buffer (clone-buffer (format awk-preview-preview-buffer-name
  1649. (buffer-name)))
  1650. (awk-preview-program-mode 1)
  1651. (setf (awk-preview--env-preview-point-beg e) beg)
  1652. (setf (awk-preview--env-preview-point-end e) end)
  1653. (setf (awk-preview--env-preview-buffer e) (current-buffer))
  1654. (goto-char end)
  1655. (setq buffer-read-only t)
  1656. (setq awk-preview--env e)
  1657. (current-buffer)))))
  1658. (defun awk-preview (beg end)
  1659. "Run awk and preview result."
  1660. (interactive (if (use-region-p)
  1661. (list (region-beginning)
  1662. (region-end))
  1663. (list (point-min)
  1664. (point-max))))
  1665. (when (and awk-preview--env
  1666. (awk-preview--env-running-p awk-preview--env))
  1667. (error "AWK-Preview already running"))
  1668. (let ((e (make-awk-preview--env)))
  1669. (setf (awk-preview--env-point-beg e) beg)
  1670. (setf (awk-preview--env-point-end e) end)
  1671. (setf (awk-preview--env-source-buffer e) (current-buffer))
  1672. (awk-preview--create-preview-buffer e)
  1673. (awk-preview--create-program-buffer e)
  1674. (setf (awk-preview--env-window-configuration e)
  1675. (current-window-configuration))
  1676. (cl-assert (awk-preview--env-point-beg e))
  1677. (cl-assert (awk-preview--env-point-end e))
  1678. (cl-assert (awk-preview--env-preview-point-beg e))
  1679. (cl-assert (awk-preview--env-preview-point-end e))
  1680. (cl-assert (awk-preview--env-program-filename e))
  1681. (cl-assert (awk-preview--env-source-buffer e))
  1682. (cl-assert (awk-preview--env-preview-buffer e))
  1683. (cl-assert (awk-preview--env-program-buffer e))
  1684. (cl-assert (awk-preview--env-window-configuration e))
  1685. (setq awk-preview--env e)
  1686. (set-window-buffer (get-buffer-window (awk-preview--env-source-buffer e))
  1687. (awk-preview--env-preview-buffer e))
  1688. (pop-to-buffer (awk-preview--env-program-buffer e))
  1689. (switch-to-buffer (awk-preview--env-program-buffer e))
  1690. (setf (awk-preview--env-running-p e) t)
  1691. (awk-preview-update-preview)
  1692. ))
  1693. (defun awk-preview-update-preview ()
  1694. "Update awk-preview."
  1695. (interactive)
  1696. (with-current-buffer (awk-preview--env-program-buffer awk-preview--env)
  1697. (write-region (point-min)
  1698. (point-max)
  1699. (awk-preview--env-program-filename awk-preview--env)))
  1700. (let ((output (with-current-buffer (get-buffer-create " *awk-preview output*")
  1701. (erase-buffer)
  1702. (current-buffer)))
  1703. (progfile (awk-preview--env-program-filename awk-preview--env)))
  1704. (awk-preview--invoke-awk (awk-preview--env-source-buffer awk-preview--env)
  1705. (awk-preview--env-point-beg awk-preview--env)
  1706. (awk-preview--env-point-end awk-preview--env)
  1707. progfile
  1708. output)
  1709. (with-current-buffer (awk-preview--env-preview-buffer awk-preview--env)
  1710. (let ((inhibit-read-only t))
  1711. (goto-char (awk-preview--env-preview-point-end awk-preview--env))
  1712. (delete-region (awk-preview--env-preview-point-beg awk-preview--env)
  1713. (point))
  1714. (insert-buffer-substring output)
  1715. (setf (awk-preview--env-preview-point-end awk-preview--env)
  1716. (point)))
  1717. )))
  1718. (defun awk-preview-reset ()
  1719. "Show original input in preview buffer."
  1720. (interactive)
  1721. (cl-assert awk-preview--env)
  1722. (with-current-buffer (awk-preview--env-preview-buffer awk-preview--env)
  1723. (let ((inhibit-read-only t))
  1724. (goto-char (awk-preview--env-preview-point-end awk-preview--env))
  1725. (delete-region (awk-preview--env-preview-point-beg awk-preview--env)
  1726. (point))
  1727. (insert-buffer-substring (awk-preview--env-source-buffer awk-preview--env)
  1728. (awk-preview--env-point-beg awk-preview--env)
  1729. (awk-preview--env-point-end awk-preview--env))
  1730. (setf (awk-preview--env-preview-point-end awk-preview--env)
  1731. (point)))))
  1732. (defun awk-preview-commit ()
  1733. "Exit awk-preview and update buffer."
  1734. (interactive)
  1735. (cl-assert awk-preview--env)
  1736. (awk-preview--invoke-awk (awk-preview--env-source-buffer awk-preview--env)
  1737. (awk-preview--env-point-beg awk-preview--env)
  1738. (awk-preview--env-point-end awk-preview--env)
  1739. (awk-preview--env-program-filename awk-preview--env)
  1740. t
  1741. t
  1742. t)
  1743. (awk-preview--cleanup))
  1744. (defun awk-preview-abort ()
  1745. "Discard result and exit awk-preview."
  1746. (interactive)
  1747. (awk-preview--cleanup))
  1748. (defun awk-preview--cleanup()
  1749. "Cleanup awk preview buffers and variables."
  1750. (with-current-buffer (awk-preview--env-source-buffer awk-preview--env)
  1751. (cl-assert awk-preview--env)
  1752. (kill-buffer (awk-preview--env-preview-buffer awk-preview--env))
  1753. ;; TODO: Ask if it should be killed
  1754. (kill-buffer (awk-preview--env-program-buffer awk-preview--env))
  1755. (setf (awk-preview--env-running-p awk-preview--env) nil))
  1756. (set-window-configuration (awk-preview--env-window-configuration awk-preview--env)))
  1757. (defvar awk-preview-program-mode-map
  1758. (let ((map (make-sparse-keymap)))
  1759. (define-key map (kbd "C-c C-l") 'awk-preview-update-preview)
  1760. (define-key map (kbd "C-c C-k") 'awk-preview-abort)
  1761. (define-key map (kbd "C-c C-c") 'awk-preview-commit)
  1762. (define-key map (kbd "C-c C-r") 'awk-preview-reset)
  1763. map)
  1764. "Keymap for `awk-preview-program-mode'.")
  1765. (define-minor-mode awk-preview-program-mode
  1766. "Minor mode for awk-preview program buffer."
  1767. :lighter " AWKPrev"
  1768. :keymap awk-preview-program-mode-map)
  1769. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1770. ;; editorconfig-auto-apply
  1771. (define-minor-mode editorconfig-auto-apply-mode
  1772. "When saving .editorconfig file update buffer configs."
  1773. :global t
  1774. :lighter ""
  1775. (if editorconfig-auto-apply-mode
  1776. (add-hook 'after-save-hook
  1777. 'editorconfig-auto-apply-mode--run)
  1778. (remove-hook 'after-save-hook
  1779. 'editorconfig-auto-apply-mode--run)))
  1780. (defun editorconfig-auto-apply-mode--run ()
  1781. "When saving .editorconfig file walk all buffers and update configs."
  1782. (when (eq major-mode
  1783. 'editorconfig-conf-mode)
  1784. (let ((dir (file-name-directory buffer-file-name)))
  1785. (cl-dolist (buf (buffer-list))
  1786. (when (and (buffer-file-name buf)
  1787. (file-in-directory-p (buffer-file-name buf)
  1788. dir))
  1789. (with-current-buffer buf
  1790. (editorconfig-mode-apply)))))))
  1791. (editorconfig-auto-apply-mode 1)
  1792. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1793. ;; recently
  1794. (defgroup recently nil
  1795. "Recently visited files"
  1796. :tag "Recently"
  1797. :prefix "recently-"
  1798. :group 'tools)
  1799. (defcustom recently-file
  1800. (concat user-emacs-directory
  1801. "recently.el")
  1802. "Recently file."
  1803. :type 'string
  1804. :group 'recently)
  1805. (defcustom recently-max
  1806. 100
  1807. "Recently list max length."
  1808. :type 'int
  1809. :group 'recently)
  1810. (defcustom recently-excludes
  1811. '()
  1812. "List of regexps for filenames excluded from the recent list."
  1813. :type '(repeat string)
  1814. :group 'recently)
  1815. (add-to-list 'recently-excludes
  1816. (eval-when-compile (rx "/COMMIT_EDITMSG" eot)))
  1817. (defvar recently-list
  1818. '()
  1819. "Recently list.")
  1820. (defvar recently-file-mtime
  1821. nil
  1822. "Modified time of file when last read file.")
  1823. (defun recently-write ()
  1824. "Write to file."
  1825. (with-temp-buffer
  1826. (prin1 recently-list
  1827. (current-buffer))
  1828. (write-region (point-min)
  1829. (point-max)
  1830. recently-file)))
  1831. (defun recently-read ()
  1832. "Read file."
  1833. (when (file-readable-p recently-file)
  1834. (with-temp-buffer
  1835. (insert-file-contents recently-file)
  1836. (goto-char (point-min))
  1837. (setq recently-list
  1838. (read (current-buffer))))
  1839. (setq recently-file-mtime
  1840. (nth 5
  1841. (file-attributes recently-file)))))
  1842. (defun recently-reload ()
  1843. "Reload file."
  1844. (when (and (file-readable-p recently-file)
  1845. (not (equal recently-file-mtime
  1846. (nth 5
  1847. (file-attributes recently-file)))))
  1848. (recently-read)
  1849. (cl-assert (equal recently-file-mtime
  1850. (nth 5
  1851. (file-attributes recently-file))))))
  1852. (defun recently-add (path)
  1853. "Add PATH to list."
  1854. (cl-assert (string= path
  1855. (expand-file-name path)))
  1856. (when (cl-loop for re in recently-excludes
  1857. if (string-match re path) return nil
  1858. finally return t)
  1859. (recently-reload)
  1860. (let* ((l (copy-list recently-list))
  1861. (l (delete path
  1862. l))
  1863. (l (cl-loop for e in l
  1864. unless (file-in-directory-p path e)
  1865. collect e))
  1866. (l (cons path
  1867. l))
  1868. (l (recently--truncate l
  1869. recently-max)))
  1870. (unless (equal recently-list
  1871. l)
  1872. (setq recently-list l)
  1873. (recently-write)))))
  1874. (defun recently--truncate (list len)
  1875. "Truncate LIST to LEN."
  1876. (if (> (length list)
  1877. len)
  1878. (cl-subseq list
  1879. 0
  1880. len)
  1881. list))
  1882. (defun recently-find-file-hook ()
  1883. "Add current file."
  1884. (when buffer-file-name
  1885. (recently-add buffer-file-name)))
  1886. (defun recently-dired-mode-hook ()
  1887. "Add current directory."
  1888. (recently-add (expand-file-name default-directory)))
  1889. (add-hook 'find-file-hook
  1890. 'recently-find-file-hook)
  1891. (add-hook 'dired-mode-hook
  1892. 'recently-dired-mode-hook)
  1893. ;;;;;;;;;;;;;;;;
  1894. ;; recently-show
  1895. (defvar recently-show-window-height 10
  1896. "Max height of window of `recently-show'")
  1897. ;; (defvar recently-show-mode-map
  1898. ;; (let ((map (make-sparse-keymap)))
  1899. ;; (suppress-keymap map)
  1900. ;; (define-key map "n" 'next-line)
  1901. ;; (define-key map "p" 'previous-line)
  1902. ;; (define-key map (kbd "C-m") 'recently-show-find-file)
  1903. ;; (define-key map (kbd "SPC") 'recently-show-find-file)
  1904. ;; (define-key map "v" 'recently-show-view-file)
  1905. ;; (define-key map "@" 'recently-show-dired)
  1906. ;; (define-key map "q" 'recently-show-close)
  1907. ;; (define-key map (kbd "C-g") 'recently-show-close)
  1908. ;; (define-key map "?" 'describe-mode)
  1909. ;; (define-key map "/" 'isearch-forward)
  1910. ;; map))
  1911. ;; (defvar recently-show-before-listing-hook nil
  1912. ;; "Hook run before creating buffer of `recently-show'.")
  1913. (defvar recently-show-window-configuration nil
  1914. "Used for internal")
  1915. (defvar recently-show-abbreviate t
  1916. "Non-nil means use `abbreviate-file-name' when listing recently opened files.")
  1917. ;; (define-derived-mode recently-show-mode special-mode "recently-show"
  1918. ;; "Major mode for `recently-show'."
  1919. ;; ;; (set (make-local-variable 'scroll-margin)
  1920. ;; ;; 0)
  1921. ;; )
  1922. ;;;###autoload
  1923. (defun recently-show (&optional files buffer-name)
  1924. "Show simplified list of recently opened files.
  1925. If optional argument FILES is non-nil, it is a list of recently-opened
  1926. files to choose from. It defaults to the whole recent list.
  1927. If optional argument BUFFER-NAME is non-nil, it is a buffer name to
  1928. use for the buffer. It defaults to \"*recetf-show*\"."
  1929. (interactive)
  1930. (let ((bf (recently-show--create-buffer-tabulated files buffer-name)))
  1931. (if bf
  1932. (progn
  1933. ;; (recently-save-list)
  1934. (setq recently-show-window-configuration (current-window-configuration))
  1935. ;;(pop-to-buffer bf t t)
  1936. (display-buffer bf)
  1937. ;; (set-window-text-height (selected-window)
  1938. ;; recently-show-window-height)
  1939. ;; (shrink-window-if-larger-than-buffer (selected-window))
  1940. )
  1941. (message "No recent file!"))))
  1942. (defun recently-show--create-buffer-tabulated (&optional files buffer-name)
  1943. "Create buffer listing recently files FILES."
  1944. (let ((bname (or buffer-name
  1945. "*recently-show-tabulated*"))
  1946. (list (or files
  1947. (progn
  1948. (recently-reload)
  1949. recently-list))))
  1950. (when list
  1951. (when (get-buffer bname)
  1952. (kill-buffer bname))
  1953. (with-current-buffer (get-buffer-create bname)
  1954. ;; (setq tabulated-list-sort-key (cons "Name" nil))
  1955. (setq tabulated-list-entries
  1956. (mapcar (lambda (f)
  1957. (list f
  1958. (vector (file-name-nondirectory f)
  1959. (if recently-show-abbreviate
  1960. (abbreviate-file-name f)
  1961. f))))
  1962. ;; list
  1963. recently-list
  1964. ))
  1965. (let ((max
  1966. (apply 'max
  1967. (mapcar (lambda (l)
  1968. (length (elt (cadr l) 0)))
  1969. tabulated-list-entries))))
  1970. (setq tabulated-list-format
  1971. `[("Name"
  1972. ,(min max
  1973. 30)
  1974. t)
  1975. ("Full Path" 0 t)])
  1976. )
  1977. (recently-show-tabulated-mode)
  1978. (current-buffer)))))
  1979. (defun recently-show-tabulated-find-file ()
  1980. "Find-file in `recently-show-tabulated-mode'."
  1981. (interactive)
  1982. (let ((f (tabulated-list-get-id)))
  1983. (when f
  1984. (recently-show-tabulated-close)
  1985. (find-file f))))
  1986. (defvar recently-show-tabulated-mode-map
  1987. (let ((map (make-sparse-keymap)))
  1988. (suppress-keymap map)
  1989. (define-key map (kbd "C-m") 'recently-show-tabulated-find-file)
  1990. ;; TODO: implement
  1991. (define-key map "v" 'recently-show-tabulated-view-file)
  1992. ;; TODO: implement
  1993. (define-key map "@" 'recently-show-tabulated-dired)
  1994. (define-key map (kbd "C-g") 'recently-show-tabulated-close)
  1995. (define-key map "/" 'isearch-forward)
  1996. map))
  1997. (define-derived-mode recently-show-tabulated-mode tabulated-list-mode "Recently Show"
  1998. "Major mode for browsing recently opened files and directories."
  1999. (setq tabulated-list-padding 2)
  2000. ;; TODO: Implement revert
  2001. ;; (add-hook 'tabulated-list-revert-hook 'recently-reload nil t)
  2002. (tabulated-list-init-header)
  2003. (tabulated-list-print nil nil))
  2004. ;; (defun recently-show-create-buffer (&optional files buffer-name)
  2005. ;; "Create buffer listing recently files."
  2006. ;; (let ((bname (or buffer-name
  2007. ;; "*recently-show*"))
  2008. ;; (list (or files
  2009. ;; (progn
  2010. ;; (recently-reload)
  2011. ;; recently-list))))
  2012. ;; (when list
  2013. ;; (and (get-buffer bname)
  2014. ;; (kill-buffer bname))
  2015. ;; (let ((bf (get-buffer-create bname)))
  2016. ;; (with-current-buffer bf
  2017. ;; (recently-show-mode)
  2018. ;; (let ((inhibit-read-only t))
  2019. ;; (mapc (lambda (f)
  2020. ;; (insert (if recently-show-abbreviate
  2021. ;; (abbreviate-file-name f)
  2022. ;; f)
  2023. ;; "\n"))
  2024. ;; list))
  2025. ;; (goto-char (point-min))
  2026. ;; ;; (setq buffer-read-only t)
  2027. ;; )
  2028. ;; bf))))
  2029. ;; (defun recently-show-close ()
  2030. ;; "Close recently-show window."
  2031. ;; (interactive)
  2032. ;; (kill-buffer (current-buffer))
  2033. ;; (set-window-configuration recently-show-window-configuration))
  2034. (defun recently-show-tabulated-close ()
  2035. "Close recently-show window."
  2036. (interactive)
  2037. (kill-buffer (current-buffer))
  2038. (set-window-configuration recently-show-window-configuration))
  2039. ;; (defun recently-show-find-file ()
  2040. ;; "Fine file of current line."
  2041. ;; (interactive)
  2042. ;; (let ((f (recently-show-get-filename)))
  2043. ;; (recently-show-close)
  2044. ;; (find-file f)))
  2045. ;; (defun recently-show-view-file ()
  2046. ;; "view file of current line."
  2047. ;; (interactive)
  2048. ;; (let ((f (recently-show-get-filename)))
  2049. ;; (recently-show-close)
  2050. ;; (view-file f)))
  2051. ;; (defun recently-show-get-filename ()
  2052. ;; "Get filename of current line."
  2053. ;; (buffer-substring-no-properties (point-at-bol)
  2054. ;; (point-at-eol)))
  2055. ;; (defun recently-show-dired()
  2056. ;; "Open dired buffer of directory containing file of current line."
  2057. ;; (interactive)
  2058. ;; (let ((f (recently-show-get-filename)))
  2059. ;; (recently-show-close)
  2060. ;; (dired (if (file-directory-p f)
  2061. ;; f
  2062. ;; (or (file-name-directory f)
  2063. ;; ".")))))
  2064. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  2065. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2066. ;; git walktree
  2067. (defgroup git-walktree nil
  2068. "Git Walktree."
  2069. :tag "GitWalktree"
  2070. :prefix "git-walktree-"
  2071. :group 'tools)
  2072. (defvar git-walktree-current-committish nil
  2073. "Committish name of currently browsing.")
  2074. (make-variable-buffer-local 'git-walktree-current-committish)
  2075. (defvar git-walktree-current-path nil
  2076. "Path name currently visiting without leading and trailing slash.
  2077. This path is always relative to repository root.")
  2078. (make-variable-buffer-local 'git-walktree-current-path)
  2079. (defvar git-walktree-buffer-file-name nil
  2080. "Psudo filename of current buffer.")
  2081. (make-variable-buffer-local 'git-walktree-buffer-file-name)
  2082. (defvar git-walktree-object-full-sha1 nil
  2083. "Object name in full sha1 format of current buffer.")
  2084. (make-variable-buffer-local 'git-walktree-object-full-sha1)
  2085. (defvar git-walktree-repository-root nil
  2086. "Repository root path of current buffer.")
  2087. (make-variable-buffer-local 'git-walktree-repository-root)
  2088. (put 'git-walktree-repository-root
  2089. 'permanent-local
  2090. t)
  2091. (defun git-walktree--committish-fordisplay (committish)
  2092. "Convert COMMITTISH and return is a suitable format for displaying."
  2093. (if (and committish
  2094. (string-match-p "\\`[0-9a-f]+\\'"
  2095. committish)
  2096. (>= (length committish) 32))
  2097. (git-walktree--git-plumbing "rev-parse"
  2098. "--short"
  2099. committish)
  2100. committish))
  2101. (defun git-walktree--create-buffer (committish name type)
  2102. "Create and return buffer for COMMITTISH:NAME.
  2103. TYPE is target object type."
  2104. (let* ((root (git-walktree--git-plumbing "rev-parse"
  2105. "--show-toplevel"))
  2106. (committish-display (git-walktree--committish-fordisplay committish))
  2107. (name (format "%s:%s"
  2108. (or committish-display "")
  2109. name)))
  2110. (if (and git-walktree-reuse-tree-buffer
  2111. (string= type "tree"))
  2112. (with-current-buffer (or git-walktree-tree-buffer-for-reuse
  2113. (setq git-walktree-tree-buffer-for-reuse
  2114. (generate-new-buffer "gitwalktreebuf")))
  2115. (setq git-walktree-repository-root root)
  2116. (rename-buffer name t)
  2117. (current-buffer))
  2118. (with-current-buffer (get-buffer-create name)
  2119. (if git-walktree-repository-root
  2120. (if (string= root
  2121. git-walktree-repository-root)
  2122. (current-buffer)
  2123. ;; If the buffer is for another repository, create new buffer
  2124. (with-current-buffer (generate-new-buffer name)
  2125. (setq git-walktree-repository-root root)
  2126. (current-buffer)))
  2127. ;; New buffer
  2128. (setq git-walktree-repository-root root)
  2129. (current-buffer))))))
  2130. (defun git-walktree--replace-into-buffer (target)
  2131. "Replace TARGET buffer contents with that of current buffer.
  2132. It also copy text overlays."
  2133. (let ((src (current-buffer)))
  2134. (with-current-buffer target
  2135. (replace-buffer-contents src)))
  2136. ;; Copy color overlays
  2137. (let ((overlays (overlays-in (point-min) (point-max))))
  2138. (dolist (o overlays)
  2139. (let ((beg (overlay-start o))
  2140. (end (overlay-end o)))
  2141. (move-overlay (copy-overlay o)
  2142. beg
  2143. end
  2144. target)))))
  2145. (require 'ansi-color)
  2146. (defun git-walktree--open-treeish (committish path treeish)
  2147. "Open git tree buffer of COMMITISH:PATH.
  2148. TREEISH should be a tree-ish object full-sha1 of COMMITISH:PATH."
  2149. (cl-assert path)
  2150. (cl-assert treeish)
  2151. (let* (point-tree-start
  2152. (type (git-walktree--git-plumbing "cat-file"
  2153. "-t"
  2154. treeish))
  2155. (buf (git-walktree--create-buffer committish path type))
  2156. )
  2157. (cl-assert (member type
  2158. '("commit" "tree")))
  2159. (with-current-buffer buf
  2160. (unless (and (string= treeish
  2161. git-walktree-object-full-sha1)
  2162. (or (eq committish
  2163. git-walktree-current-committish)
  2164. (string= committish
  2165. git-walktree-current-committish)))
  2166. (buffer-disable-undo)
  2167. ;; For running git command go back to repository root
  2168. (cd git-walktree-repository-root)
  2169. (save-excursion
  2170. (let ((inhibit-read-only t))
  2171. ;; Remove existing overlays generated by ansi-color-apply-on-region
  2172. (remove-overlays)
  2173. (with-temp-buffer
  2174. (if committish
  2175. (progn (git-walktree--call-process nil
  2176. "show"
  2177. ;; TODO: Make this args configurable
  2178. ;; "--no-patch"
  2179. "--color=always"
  2180. "--pretty=short"
  2181. "--decorate"
  2182. "--stat"
  2183. committish)
  2184. (ansi-color-apply-on-region (point-min)
  2185. (point))
  2186. (insert "\n")
  2187. (insert (format "Contents of '%s:%s':\n"
  2188. (git-walktree--committish-fordisplay committish)
  2189. path)))
  2190. (insert (format "Contents of treeish object '%s:\n"
  2191. treeish)))
  2192. (setq point-tree-start (point))
  2193. (git-walktree--call-process nil
  2194. "ls-tree"
  2195. ;; "-r"
  2196. "--abbrev"
  2197. treeish)
  2198. (git-walktree--replace-into-buffer buf))
  2199. ))
  2200. (git-walktree-mode)
  2201. (set-buffer-modified-p nil)
  2202. (setq git-walktree-current-committish committish)
  2203. (setq git-walktree-current-path path)
  2204. (setq git-walktree-object-full-sha1 treeish)
  2205. (let ((dir (expand-file-name path git-walktree-repository-root)))
  2206. (when (and git-walktree-try-cd
  2207. (file-directory-p dir))
  2208. (cd dir)))
  2209. (when (= (point) (point-min))
  2210. (goto-char point-tree-start)
  2211. (git-walktree-mode--move-to-file)
  2212. )
  2213. ))
  2214. buf))
  2215. (defun git-walktree--call-process (&optional infile &rest args)
  2216. "Call git command with input from INFILE and args ARGS.
  2217. Result will be inserted into current buffer."
  2218. (let ((status (apply 'call-process
  2219. git-walktree-git-executable
  2220. infile
  2221. t
  2222. nil
  2223. args)))
  2224. (unless (eq 0
  2225. status)
  2226. (error "Failed to call git process %S %S"
  2227. infile
  2228. args))))
  2229. ?w
  2230. (defun git-walktree--open-blob (committish path blob)
  2231. "Open blob object of COMMITISH:PATH.
  2232. BLOB should be a object full sha1 of COMMITISH:PATH."
  2233. (cl-assert committish)
  2234. (cl-assert path)
  2235. (cl-assert blob)
  2236. (let* ((type (git-walktree--git-plumbing "cat-file"
  2237. "-t"
  2238. blob))
  2239. (buf (git-walktree--create-buffer committish path type)))
  2240. (cl-assert (string= type "blob"))
  2241. (with-current-buffer buf
  2242. (unless (string= blob
  2243. git-walktree-object-full-sha1)
  2244. ;; For running git command go back to repository root
  2245. (cd git-walktree-repository-root)
  2246. (let ((inhibit-read-only t))
  2247. (with-temp-buffer
  2248. (git-walktree--call-process nil
  2249. "cat-file"
  2250. "-p"
  2251. blob)
  2252. (git-walktree--replace-into-buffer buf)))
  2253. (setq git-walktree-buffer-file-name
  2254. (concat git-walktree-repository-root "/git@" committish ":" path))
  2255. (setq buffer-file-name
  2256. (concat git-walktree-repository-root "/" path))
  2257. (normal-mode t)
  2258. ;; For asking filename when C-xC-s
  2259. (setq buffer-file-name nil)
  2260. (set-buffer-modified-p t)
  2261. (setq git-walktree-current-committish committish)
  2262. (setq git-walktree-current-path path)
  2263. (setq git-walktree-object-full-sha1 blob)
  2264. (let ((dir (expand-file-name (or (file-name-directory path)
  2265. ".")
  2266. git-walktree-repository-root)))
  2267. (when (and git-walktree-try-cd
  2268. (file-directory-p dir))
  2269. (cd dir)))
  2270. (view-mode 1)
  2271. ))
  2272. buf))
  2273. (defun git-walktree--open-noselect-safe-path (committish &optional path)
  2274. "Open git object of COMMITTISH:PATH.
  2275. If PATH not found in COMMITTISH tree, go up path and try again until found.
  2276. When PATH is omitted or nil, it is calculated from current file or directory."
  2277. (cl-assert committish)
  2278. (let ((type (git-walktree--git-plumbing "cat-file"
  2279. "-t"
  2280. committish)))
  2281. (cl-assert (string= type "commit")))
  2282. (setq path
  2283. (or path
  2284. (git-walktree--path-in-repository (or buffer-file-name
  2285. default-directory))))
  2286. ;; PATH must not start with and end with slashes
  2287. (cl-assert (not (string-match-p "\\`/" path)))
  2288. (cl-assert (not (string-match-p "/\\'" path)))
  2289. (let ((obj (git-walktree--resolve-object committish path)))
  2290. (while (not obj)
  2291. (setq path
  2292. (git-walktree--parent-directory path))
  2293. (setq obj
  2294. (git-walktree--resolve-object committish path)))
  2295. (git-walktree--open-noselect committish
  2296. path
  2297. obj)))
  2298. ;; TODO: Store view history
  2299. (defun git-walktree--open-noselect (committish path object)
  2300. "Open buffer to view git object of COMMITTISH:PATH.
  2301. When PATH was given and non-nil open that, otherwise open root tree.
  2302. When OBJECT was given and non-nil, assume that is the object full sha1 of
  2303. COMMITTISH:PATH without checking it."
  2304. (cl-assert committish)
  2305. (let ((type (git-walktree--git-plumbing "cat-file"
  2306. "-t"
  2307. committish)))
  2308. (cl-assert (string= type "commit")))
  2309. (setq path (or path
  2310. "."))
  2311. ;; PATH must not start with and end with slashes
  2312. (cl-assert (not (string-match-p "\\`/" path)))
  2313. (cl-assert (not (string-match-p "/\\'" path)))
  2314. (setq object (or object
  2315. (git-walktree--resolve-object committish path)))
  2316. (setq object (git-walktree--git-plumbing "rev-parse"
  2317. object))
  2318. (cl-assert object)
  2319. (let ((type (git-walktree--git-plumbing "cat-file"
  2320. "-t"
  2321. object)))
  2322. (pcase type
  2323. ((or "commit" "tree")
  2324. (git-walktree--open-treeish committish path object))
  2325. ("blob"
  2326. (git-walktree--open-blob committish path object))
  2327. (_
  2328. (error "Type cannot handle: %s" type)))))
  2329. (defun git-walktree--resolve-object (committish path)
  2330. "Return object full sha1 name of COMMITISIH:PATH.
  2331. If path is equal to \".\" return COMMITTISH's root tree object.
  2332. PATH will be always treated as relative to repository root."
  2333. (cl-assert committish)
  2334. (cl-assert path)
  2335. (cl-assert (not (string-match-p "\\`/" path)))
  2336. (cl-assert (not (string-match-p "/\\'" path)))
  2337. (if (string= path ".")
  2338. (git-walktree--git-plumbing "show"
  2339. "--no-patch"
  2340. "--pretty=format:%T"
  2341. committish)
  2342. (let ((info (git-walktree--parse-lstree-line (git-walktree--git-plumbing "ls-tree"
  2343. "--full-tree"
  2344. committish
  2345. path))))
  2346. (plist-get info :object))))
  2347. (defun git-walktree-open (committish &optional path)
  2348. "Open git tree buffer of COMMITTISH.
  2349. When PATH was given and non-nil open that, otherwise try to open current path.
  2350. If target path is not found in COMMITISH tree, go up path and try again until found."
  2351. ;; TODO: Add fallback method for cases where magit is not available
  2352. (interactive (list (magit-read-branch-or-commit "Revision: ")))
  2353. (switch-to-buffer (git-walktree--open-noselect-safe-path committish path)))
  2354. (defalias 'git-walktree 'git-walktree-open)
  2355. (defun git-walktree--path-in-repository (path)
  2356. "Convert PATH into relative path to repository root.
  2357. Result will not have leading and trailing slashes."
  2358. (with-temp-buffer
  2359. (cd (if (file-directory-p path)
  2360. path
  2361. (file-name-directory path)))
  2362. (let ((root (git-walktree--git-plumbing "rev-parse"
  2363. "--show-toplevel")))
  2364. (file-relative-name (directory-file-name path)
  2365. root))))
  2366. (defcustom git-walktree-git-executable "git"
  2367. "Git executable."
  2368. :type 'string
  2369. :group 'git-walktree)
  2370. (defcustom git-walktree-try-cd t
  2371. "Try to cd if directory exists in current working directory if non-nil.
  2372. Otherwise use repository root for gitwalktree buffer's `default-directory'."
  2373. :type 'boolean
  2374. :group 'git-walktree)
  2375. (defcustom git-walktree-reuse-tree-buffer t
  2376. "Non-nil to reuse buffer for treeish object."
  2377. :type 'boolean
  2378. :group 'git-walktree)
  2379. (defvar git-walktree-tree-buffer-for-reuse nil
  2380. "Buffer to use when `git-walktree-reuse-tree-buffer' is non-nil.")
  2381. (defun git-walktree--git-plumbing (&rest args)
  2382. "Run git plubming command with ARGS.
  2383. Returns first line of output without newline."
  2384. (with-temp-buffer
  2385. (let ((status (apply 'call-process
  2386. git-walktree-git-executable
  2387. nil
  2388. t
  2389. nil
  2390. args)))
  2391. (unless (eq 0
  2392. status)
  2393. (error "Faild to run git %S:\n%s"
  2394. args
  2395. (buffer-substring-no-properties (point-min)
  2396. (point-max))))
  2397. (buffer-substring-no-properties (point-min)
  2398. (progn
  2399. (goto-char (point-min))
  2400. (point-at-eol))))))
  2401. (defconst git-walktree-ls-tree-line-regexp
  2402. "^\\([0-9]\\{6\\}\\) \\(\\w+\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2403. "Regexp for one line of output of git ls-tree.")
  2404. (defconst git-walktree-ls-tree-line-tree-regexp
  2405. "^\\([0-9]\\{6\\}\\) \\(tree\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2406. "Regexp for tree line of output of git ls-tree.")
  2407. (defconst git-walktree-ls-tree-line-commit-regexp
  2408. "^\\([0-9]\\{6\\}\\) \\(commit\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2409. "Regexp for commit line of output of git ls-tree.")
  2410. (defun git-walktree--parse-lstree-line (str)
  2411. "Extract object info from STR.
  2412. STR should be a string like following without newline.:
  2413. 100644 blob 6fd4d58202d0b46547c6fe43de0f8c878456f966 .editorconfig
  2414. Returns property list like (:mode MODE :type TYPE :object OBJECT :file FILE)."
  2415. (let (result mode type object file)
  2416. (save-match-data
  2417. (with-temp-buffer
  2418. (insert str)
  2419. (goto-char (point-min))
  2420. (and (re-search-forward git-walktree-ls-tree-line-regexp
  2421. nil
  2422. t)
  2423. (list :mode (match-string 1)
  2424. :type (match-string 2)
  2425. :object (match-string 3)
  2426. :file (match-string 4)))))))
  2427. (defun git-walktree-mode-open-this ()
  2428. "Open git object of current line."
  2429. (interactive)
  2430. (let ((info (git-walktree--parse-lstree-line (buffer-substring-no-properties (point-at-bol)
  2431. (point-at-eol)))))
  2432. (if info
  2433. (switch-to-buffer
  2434. (if (string= (plist-get info
  2435. :type)
  2436. "commit")
  2437. ;; For submodule cd to that directory and intialize
  2438. ;; TODO: Provide way to go back to known "parent" repository
  2439. (with-temp-buffer
  2440. (cd (plist-get info :file))
  2441. (git-walktree--open-noselect (plist-get info
  2442. :object)
  2443. nil
  2444. (plist-get info
  2445. :object)))
  2446. (git-walktree--open-noselect git-walktree-current-committish
  2447. (git-walktree--join-path (plist-get info
  2448. :file))
  2449. (plist-get info
  2450. :object))))
  2451. (message "No object on current line."))))
  2452. (defun git-walktree--join-path (name &optional base)
  2453. "Make path from NAME and BASE.
  2454. If base is omitted or nil use value of `git-walktree-current-path'."
  2455. (setq base (or base
  2456. git-walktree-current-path))
  2457. (cl-assert base)
  2458. (if (string= base ".")
  2459. name
  2460. (concat base "/" name)))
  2461. (defun git-walktree--parent-directory (path)
  2462. "Return parent directory of PATH without trailing slash.
  2463. For root directory return \".\".
  2464. If PATH is equal to \".\", return nil."
  2465. (if (string-match-p "/" path)
  2466. (directory-file-name (file-name-directory path))
  2467. (if (string= "." path)
  2468. nil
  2469. ".")))
  2470. (defun git-walktree-up (&optional committish path)
  2471. "Open parent directory of COMMITTISH and PATH.
  2472. If not given, value of current buffer will be used."
  2473. (interactive)
  2474. (setq committish
  2475. (or committish git-walktree-current-committish))
  2476. (setq path
  2477. (or path git-walktree-current-path))
  2478. (let ((parent (git-walktree--parent-directory path)))
  2479. (if parent
  2480. (switch-to-buffer (git-walktree--open-noselect committish
  2481. parent
  2482. nil))
  2483. (message "Cannot find parent directory for current tree."))))
  2484. (defun git-walktree-mode--move-to-file ()
  2485. "Move point to file field of ls-tree output in current line.
  2486. This function do nothing when current line is not ls-tree output."
  2487. (interactive)
  2488. (save-match-data
  2489. (when (save-excursion
  2490. (goto-char (point-at-bol))
  2491. (re-search-forward git-walktree-ls-tree-line-regexp
  2492. (point-at-eol) t))
  2493. (goto-char (match-beginning 4)))))
  2494. (defun git-walktree-mode-next-line (&optional arg try-vscroll)
  2495. "Move cursor vertically down ARG lines and move to file field if found."
  2496. (interactive "^p\np")
  2497. (or arg (setq arg 1))
  2498. (line-move arg nil nil try-vscroll)
  2499. (git-walktree-mode--move-to-file)
  2500. )
  2501. (defun git-walktree-mode-previous-line (&optional arg try-vscroll)
  2502. "Move cursor vertically up ARG lines and move to file field if found."
  2503. (interactive "^p\np")
  2504. (or arg (setq arg 1))
  2505. (line-move (- arg) nil nil try-vscroll)
  2506. (git-walktree-mode--move-to-file)
  2507. )
  2508. (defgroup git-walktree-faces nil
  2509. "Faces used by git-walktree."
  2510. :group 'git-walktree
  2511. :group 'faces)
  2512. (defface git-walktree-tree-face
  2513. ;; Same as dired-directory
  2514. '((t (:inherit font-lock-function-name-face)))
  2515. "Face used for tree objects."
  2516. :group 'git-walktree-faces)
  2517. (defface git-walktree-commit-face
  2518. ;; Same as dired-symlink face
  2519. '((t (:inherit font-lock-keyword-face)))
  2520. "Face used for commit objects."
  2521. :group 'git-walktree-faces)
  2522. (defvar git-walktree-known-child-revisions (make-hash-table :test 'equal)
  2523. "Hash of already known pair of commitid -> list of child commitid.
  2524. Both values should be object full sha1 names.")
  2525. (defun git-walktree--put-child (parent child)
  2526. "Register PARENT and CHILD relationship.
  2527. PARENT should be a full sha1 object name."
  2528. ;; Any way to check if PARENT is a full SHA-1 object name?
  2529. (let ((current (gethash parent git-walktree-known-child-revisions)))
  2530. (unless (member child current)
  2531. (puthash parent
  2532. (cons child
  2533. current)
  2534. git-walktree-known-child-revisions))))
  2535. ;; TODO: Add aggressive search mode
  2536. ;; https://stackoverflow.com/a/9870218
  2537. ;; git log --reverse --pretty=format:%H -n 1 --ancestry-path <PARENT>..HEAD
  2538. (defun git-walktree--get-children (parent)
  2539. "Get known children list of PARENT commit.
  2540. PARENT should be a full sha1 object name."
  2541. (gethash parent git-walktree-known-child-revisions))
  2542. (defun git-walktree--choose-committish (prompt-format collection)
  2543. "Emit PROMPT-FORMAT and ask user to which committish of COLLECTION to use.
  2544. When collection has just one element, return the first element without asking."
  2545. (cl-assert collection)
  2546. (if (< (length collection) 2)
  2547. (car collection)
  2548. (completing-read (format prompt-format
  2549. (mapconcat 'git-walktree--committish-fordisplay
  2550. collection
  2551. " "))
  2552. collection
  2553. nil
  2554. t)))
  2555. (defun git-walktree-parent-revision ()
  2556. "Open parent revision of current path.
  2557. If current path was not found in the parent revision try to go up path."
  2558. (interactive)
  2559. (cl-assert git-walktree-current-committish)
  2560. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2561. git-walktree-current-committish))
  2562. (parents (git-walktree--parent-full-sha1 commit-full-sha1)))
  2563. (dolist (parent parents)
  2564. (git-walktree--put-child parent
  2565. commit-full-sha1))
  2566. (if (< (length parents)
  2567. 1)
  2568. (message "This revision has no parent revision")
  2569. (let* ((parent (git-walktree--choose-committish "This revision has multiple parents. Which to open? (%s) "
  2570. parents))
  2571. (path git-walktree-current-path))
  2572. (cl-assert path)
  2573. (switch-to-buffer (git-walktree--open-noselect-safe-path parent
  2574. path))))))
  2575. (defun git-walktree--parent-full-sha1 (committish)
  2576. "Return list of parent commits of COMMITTISH in sha1 string."
  2577. (let ((type (git-walktree--git-plumbing "cat-file"
  2578. "-t"
  2579. committish)))
  2580. (cl-assert (string= type "commit")))
  2581. (let ((parents (git-walktree--git-plumbing "show"
  2582. "--no-patch"
  2583. "--pretty=format:%P"
  2584. committish)))
  2585. (split-string parents)))
  2586. (defun git-walktree-known-child-revision ()
  2587. "Open known revision of current path."
  2588. (interactive)
  2589. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2590. git-walktree-current-committish))
  2591. (children (git-walktree--get-children commit-full-sha1)))
  2592. (if (< (length children)
  2593. 1)
  2594. (message "There are no known child revision")
  2595. (let* ((child (git-walktree--choose-committish "There are multiple known childrens. Which to open? (%s)"
  2596. children))
  2597. (path git-walktree-current-path))
  2598. (cl-assert path)
  2599. (switch-to-buffer (git-walktree--open-noselect-safe-path child
  2600. path))))))
  2601. (defvar git-walktree-mode-map
  2602. (let ((map (make-sparse-keymap)))
  2603. (define-key map "n" 'git-walktree-mode-next-line)
  2604. (define-key map "p" 'git-walktree-mode-previous-line)
  2605. (define-key map (kbd "C-n") 'git-walktree-mode-next-line)
  2606. (define-key map (kbd "C-p") 'git-walktree-mode-previous-line)
  2607. ;; TODO: Review keybind
  2608. (define-key map "P" 'git-walktree-parent-revision)
  2609. (define-key map "N" 'git-walktree-known-child-revision)
  2610. (define-key map "^" 'git-walktree-up)
  2611. ;; TODO: implement
  2612. (define-key map (kbd "DEL") 'git-walktree-back)
  2613. (define-key map (kbd "C-m") 'git-walktree-mode-open-this)
  2614. map))
  2615. (defvar git-walktree-mode-font-lock-keywords
  2616. `(
  2617. (,git-walktree-ls-tree-line-regexp . (
  2618. (1 'shadow)
  2619. (3 'shadow)
  2620. ))
  2621. (,git-walktree-ls-tree-line-tree-regexp . (
  2622. (2 'git-walktree-tree-face)
  2623. (4 'git-walktree-tree-face)
  2624. ))
  2625. (,git-walktree-ls-tree-line-commit-regexp . (
  2626. (2 'git-walktree-commit-face)
  2627. (4 'git-walktree-commit-face)
  2628. ))
  2629. )
  2630. "Syntax highlighting for git-walktree mode.")
  2631. (define-derived-mode git-walktree-mode special-mode "GitWalktree"
  2632. "Major-mode for `git-walktree-open'."
  2633. (set (make-local-variable 'font-lock-defaults)
  2634. '(git-walktree-mode-font-lock-keywords
  2635. nil nil nil nil
  2636. ))
  2637. )
  2638. (require 'magit nil t)
  2639. ;; (git-revision--git-plumbing "cat-file" "-t" "HEAD")
  2640. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2641. ;; git-worktree
  2642. (defun git-worktree-get-current-list ()
  2643. "Get current worktree list."
  2644. (with-temp-buffer
  2645. (let ((trees nil)
  2646. (status (call-process "git"
  2647. nil
  2648. t
  2649. nil
  2650. "worktree" "list" "--porcelain")))
  2651. (cl-assert (eq status 0))
  2652. (goto-char (point-min))
  2653. (save-match-data
  2654. (while (not (eq (point) (point-max)))
  2655. (let ((worktree nil)
  2656. (head nil)
  2657. (branch nil))
  2658. (while (re-search-forward "^\\([^ ]+\\) \\(.*\\)$" (point-at-eol) t)
  2659. (pcase (match-string 1)
  2660. ("worktree" (setq worktree (match-string 2)))
  2661. ("HEAD" (setq head (match-string 2)))
  2662. ("branch" (setq branch (match-string 2))))
  2663. (forward-line 1)
  2664. (goto-char (point-at-bol)))
  2665. (setq trees `(,@trees
  2666. (:worktree ,worktree :head ,head :branch ,branch)))
  2667. (forward-line 1)
  2668. (goto-char (point-at-bol)))
  2669. ))
  2670. trees)))
  2671. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2672. ;; j2-mode jinja2-mmm-mode?
  2673. (define-derived-mode jinja2-mmm-mode prog-mode
  2674. "Jinja2 MMM"
  2675. "Major mode to setup mmm-mode for jinja2 files."
  2676. (require 'mmm-mode)
  2677. (require 'mmm-jinja2)
  2678. (let* ((origname buffer-file-name)
  2679. (withoutj2 buffer-file-name)
  2680. (withoutj2 (replace-regexp-in-string "\\.j2\\'"
  2681. ""
  2682. withoutj2))
  2683. (withoutj2 (replace-regexp-in-string "\\.j2\\'"
  2684. ""
  2685. withoutj2)))
  2686. (unwind-protect
  2687. (progn
  2688. (let ((mode (assoc-default withoutj2
  2689. auto-mode-alist
  2690. 'string-match)))
  2691. (when mode
  2692. (funcall mode)))
  2693. (add-to-list 'mmm-classes
  2694. 'jinja2)
  2695. (mmm-mode-on))
  2696. (setq buffer-file-name origname))))
  2697. ;; (add-to-list 'auto-mode-alist
  2698. ;; '("\\.j2\\'" . jinja2-mmm-mode))
  2699. ;; Local Variables:
  2700. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2701. ;; flycheck-checker: emacs-lisp
  2702. ;; End:
  2703. ;;; emancs.el ends here