No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

emacs.el 108 KiB

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