Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

emacs.el 105 KiB

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