You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

emacs.el 97 KiB

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