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

emacs.el 71 KiB

há 6 anos
há 13 anos
há 12 anos
há 13 anos
há 13 anos
há 11 anos
há 8 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 11 anos
há 11 anos
há 13 anos
há 13 anos
há 12 anos
há 13 anos
há 6 anos
há 13 anos
há 13 anos
há 12 anos
há 13 anos
há 13 anos
há 13 anos
há 12 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 11 anos
há 11 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 11 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 6 anos
há 8 anos
há 8 anos
há 13 anos
há 6 anos
há 11 anos
há 11 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 12 anos
há 13 anos
há 13 anos
há 6 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 12 anos
há 12 anos
há 6 anos
há 12 anos
há 12 anos
há 6 anos
há 12 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 9 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 8 anos
há 13 anos
há 6 anos
há 13 anos
há 6 anos
há 13 anos
há 12 anos
há 13 anos
há 13 anos
há 13 anos
há 6 anos
há 13 anos
há 6 anos
há 13 anos
há 13 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 12 anos
há 12 anos
há 12 anos
há 6 anos
há 6 anos
há 12 anos
há 12 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 12 anos
há 11 anos
há 6 anos
há 6 anos
há 11 anos
há 6 anos
há 6 anos
há 6 anos
há 11 anos
há 6 anos
há 6 anos
há 11 anos
há 6 anos
há 5 anos
há 5 anos
há 6 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
há 5 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148
  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Code:
  3. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  4. ;; SETUP_LOAD: (and (file-readable-p file)
  5. ;; SETUP_LOAD: (byte-recompile-file file nil 0 t)))
  6. (setq debug-on-error t)
  7. ;; make directories
  8. (unless (file-directory-p (expand-file-name user-emacs-directory))
  9. (make-directory (expand-file-name user-emacs-directory)))
  10. (require 'cl-lib)
  11. (require 'simple)
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;; Some macros for internals
  14. (defun call-after-init (func)
  15. "If `after-init-hook' has been run, call FUNC immediately.
  16. Otherwize hook it."
  17. (if after-init-time
  18. (funcall func)
  19. (add-hook 'after-init-hook
  20. func)))
  21. (defmacro safe-require-or-eval (feature)
  22. "Require FEATURE if available.
  23. At compile time the feature will be loaded immediately."
  24. `(eval-and-compile
  25. (message "safe-require-or-eval: Trying to require %s" ,feature)
  26. (require ,feature nil t)))
  27. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  28. "Define autoloading FEATURE that defines FUNCTIONS.
  29. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  30. the function same as FEATURE is defined as autoloaded function. BODY is passed
  31. to `eval-after-load'.
  32. After this macro is expanded, this returns the path to library if FEATURE
  33. found, otherwise returns nil."
  34. (declare (indent 2) (debug t))
  35. (let* ((libname (symbol-name (eval feature)))
  36. (libpath (locate-library libname)))
  37. `(progn
  38. (when (locate-library ,libname)
  39. ,@(mapcar (lambda (f)
  40. `(unless (fboundp ',f)
  41. (progn
  42. (message "Autoloaded function `%S' defined (%s)"
  43. (quote ,f)
  44. ,libpath)
  45. (autoload (quote ,f)
  46. ,libname
  47. ,(concat "Autoloaded function defined in \""
  48. libpath
  49. "\".")
  50. t))))
  51. (or (eval functions)
  52. `(,(eval feature)))))
  53. (eval-after-load ,feature
  54. (quote (progn
  55. ,@body)))
  56. (locate-library ,libname))))
  57. (when (autoload-eval-lazily 'tetris nil
  58. (message "Tetris loaded!"))
  59. (message "Tetris found!"))
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ;; package
  62. (set (defvar 10sr-package-list)
  63. '(
  64. vimrc-mode
  65. markdown-mode
  66. yaml-mode
  67. gnuplot-mode
  68. php-mode
  69. erlang
  70. js2-mode
  71. js-doc
  72. git-commit
  73. gitignore-mode
  74. adoc-mode
  75. go-mode
  76. ;; It seems malabar has been merged into jdee and this package
  77. ;; already removed
  78. ;; malabar-mode
  79. gosh-mode
  80. scala-mode
  81. ;;ensime
  82. ;; ack
  83. color-moccur
  84. ggtags
  85. flycheck
  86. auto-highlight-symbol
  87. hl-todo
  88. ;; Currently not available
  89. ;; pp-c-l
  90. xclip
  91. foreign-regexp
  92. multi-term
  93. term-run
  94. editorconfig
  95. git-ps1-mode
  96. restart-emacs
  97. fill-column-indicator
  98. pkgbuild-mode
  99. minibuffer-line
  100. which-key
  101. ;; I think this works in place of my autosave lib
  102. super-save
  103. pipenv
  104. imenu-list
  105. page-break-lines
  106. ;; sync-recentf
  107. aggressive-indent
  108. ;; fancy-narrow
  109. dired-filter
  110. wgrep
  111. magit
  112. git-gutter
  113. end-mark
  114. sl
  115. ;; TODO: Configure pony-tpl-mode
  116. pony-mode
  117. editorconfig
  118. editorconfig-custom-majormode
  119. git-command
  120. prompt-text
  121. ;; 10sr repository
  122. ;; 10sr-extras
  123. terminal-title
  124. recentf-show
  125. dired-list-all-mode
  126. pack
  127. set-modeline-color
  128. read-only-only-mode
  129. smart-revert
  130. autosave
  131. ;;window-organizer
  132. ilookup
  133. pasteboard
  134. awk-preview
  135. recently
  136. ))
  137. (when (safe-require-or-eval 'package)
  138. (setq package-archives
  139. `(,@package-archives
  140. ("melpa" . "https://melpa.org/packages/")
  141. ;; Somehow fails to download via https
  142. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  143. (package-initialize)
  144. (defun my-auto-install-package ()
  145. "Install packages semi-automatically."
  146. (interactive)
  147. (package-refresh-contents)
  148. (mapc (lambda (pkg)
  149. (or (package-installed-p pkg)
  150. (package-install pkg)))
  151. 10sr-package-list))
  152. )
  153. ;; (lazy-load-eval 'sudoku)
  154. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  155. ;; my-idle-hook
  156. (defvar my-idle-hook nil
  157. "Hook run when idle for several secs.")
  158. (defvar my-idle-hook-sec 5
  159. "Second to run `my-idle-hook'.")
  160. (run-with-idle-timer my-idle-hook-sec
  161. t
  162. (lambda ()
  163. (run-hooks 'my-idle-hook)))
  164. ;; (add-hook 'my-idle-hook
  165. ;; (lambda ()
  166. ;; (message "idle hook message")))
  167. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  168. ;; start and quit
  169. (setq inhibit-startup-message t)
  170. (setq confirm-kill-emacs 'y-or-n-p)
  171. (setq gc-cons-threshold (* 1024 1024 16))
  172. (setq garbage-collection-messages nil)
  173. (when window-system
  174. (add-to-list 'default-frame-alist '(cursor-type . box))
  175. (add-to-list 'default-frame-alist '(background-color . "white"))
  176. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  177. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  178. ;; does not work?
  179. )
  180. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  181. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  182. (and (fboundp 'tool-bar-mode)
  183. (tool-bar-mode 0))
  184. (and (fboundp 'set-scroll-bar-mode)
  185. (set-scroll-bar-mode nil))
  186. (call-after-init
  187. (lambda ()
  188. (message "%s %s" invocation-name emacs-version)
  189. (message "Invocation directory: %s" default-directory)
  190. (message "%s was taken to initialize emacs." (emacs-init-time))
  191. (view-echo-area-messages)
  192. ;; (view-emacs-news)
  193. ))
  194. (cd ".") ; when using windows use / instead of \ in `default-directory'
  195. ;; locale
  196. (set-language-environment "Japanese")
  197. (set-default-coding-systems 'utf-8-unix)
  198. (prefer-coding-system 'utf-8-unix)
  199. (setq system-time-locale "C")
  200. ;; my prefix map
  201. (defvar my-prefix-map nil
  202. "My prefix map.")
  203. (define-prefix-command 'my-prefix-map)
  204. ;; (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  205. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  206. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  207. ;; (comint-show-maximum-output)
  208. ;; kill scratch
  209. (call-after-init (lambda ()
  210. (let ((buf (get-buffer "*scratch*")))
  211. (when buf
  212. (kill-buffer buf)))))
  213. ;; modifier keys
  214. ;; (setq mac-option-modifier 'control)
  215. ;; display
  216. (setq visible-bell t)
  217. (setq ring-bell-function 'ignore)
  218. (mouse-avoidance-mode 'banish)
  219. (setq echo-keystrokes 0.1)
  220. (defun reload-init-file ()
  221. "Reload Emacs init file."
  222. (interactive)
  223. (when (and user-init-file
  224. (file-readable-p user-init-file))
  225. (load-file user-init-file)))
  226. (safe-require-or-eval 'session)
  227. ;; server
  228. (set-variable 'server-name (concat "server"
  229. (number-to-string (emacs-pid))))
  230. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  231. ;; In Darwin environment, init fails with message like 'Service name too long'
  232. ;; when server-use-tcp is nil
  233. (when (or (eq system-type
  234. 'cygwin)
  235. (eq system-type
  236. 'darwin))
  237. (set-variable 'server-use-tcp t))
  238. ;; MSYS2 fix
  239. (when (eq system-type
  240. 'windows-nt)
  241. (setq shell-file-name
  242. (executable-find "bash"))
  243. '(setq function-key-map
  244. `(,@function-key-map ([pause] . [?\C-c])
  245. ))
  246. (define-key key-translation-map
  247. (kbd "<pause>")
  248. (kbd "C-c"))
  249. '(keyboard-translate [pause]
  250. (kbd "C-c")p)
  251. ;; TODO: move to other place later
  252. (when (not window-system)
  253. (setq interprogram-paste-function nil)
  254. (setq interprogram-cut-function nil)))
  255. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  256. ;; global keys
  257. (global-set-key (kbd "<up>") 'scroll-down-line)
  258. (global-set-key (kbd "<down>") 'scroll-up-line)
  259. (global-set-key (kbd "<left>") 'scroll-down)
  260. (global-set-key (kbd "<right>") 'scroll-up)
  261. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  262. (global-set-key (kbd "C-\\") help-map)
  263. (define-key ctl-x-map (kbd "DEL") help-map)
  264. (define-key ctl-x-map (kbd "C-h") help-map)
  265. (define-key help-map "a" 'apropos)
  266. ;; disable annoying keys
  267. (global-set-key [prior] 'ignore)
  268. (global-set-key (kbd "<next>") 'ignore)
  269. (global-set-key [menu] 'ignore)
  270. (global-set-key [down-mouse-1] 'ignore)
  271. (global-set-key [down-mouse-2] 'ignore)
  272. (global-set-key [down-mouse-3] 'ignore)
  273. (global-set-key [mouse-1] 'ignore)
  274. (global-set-key [mouse-2] 'ignore)
  275. (global-set-key [mouse-3] 'ignore)
  276. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  277. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  278. (when (safe-require-or-eval 'which-key)
  279. (which-key-mode))
  280. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  281. ;; editor
  282. (setq kill-whole-line t)
  283. (setq scroll-conservatively 35
  284. scroll-margin 2)
  285. (setq-default major-mode 'text-mode)
  286. (setq next-line-add-newlines nil)
  287. (setq kill-read-only-ok t)
  288. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  289. ;; (setq-default line-spacing 0.2)
  290. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  291. ;; (setq-default tab-width 4)
  292. (setq-default indent-tabs-mode nil)
  293. (setq-default indent-line-function 'indent-to-left-margin)
  294. ;; (setq-default indent-line-function nil)
  295. (setq-default truncate-lines nil)
  296. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  297. (delete-selection-mode 1)
  298. (cua-mode 0)
  299. (setq line-move-visual nil)
  300. (setq create-lockfiles nil)
  301. (add-hook 'before-save-hook
  302. 'time-stamp)
  303. ;; Add Time-stamp: <> to insert timestamp there
  304. (set-variable 'time-stamp-format
  305. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  306. ;; key bindings
  307. ;; moving around
  308. ;;(keyboard-translate ?\M-j ?\C-j)
  309. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  310. (define-key esc-map "p" 'backward-paragraph)
  311. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  312. (define-key esc-map "n" 'forward-paragraph)
  313. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  314. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  315. (global-set-key (kbd "C-<left>") 'scroll-down)
  316. (global-set-key (kbd "C-<right>") 'scroll-up)
  317. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  318. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  319. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  320. ;; C-h and DEL
  321. (global-set-key (kbd "C-h") (kbd "DEL"))
  322. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  323. (global-set-key (kbd "C-m") 'newline-and-indent)
  324. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  325. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  326. (define-key esc-map "u" 'undo)
  327. (define-key esc-map "i" (kbd "ESC TAB"))
  328. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  329. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  330. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  331. (require 'page-ext nil t)
  332. (when (safe-require-or-eval 'page-break-lines)
  333. (global-page-break-lines-mode 1))
  334. (when (safe-require-or-eval 'git-gutter)
  335. (custom-set-variables
  336. '(git-gutter:lighter " Gttr"))
  337. (custom-set-variables
  338. '(git-gutter:update-interval 2))
  339. (custom-set-variables
  340. '(git-gutter:unchanged-sign " "))
  341. (when (= (display-color-cells)
  342. 256)
  343. (let ((c "color-233"))
  344. (set-face-background 'git-gutter:modified c)
  345. (set-face-background 'git-gutter:added c)
  346. (set-face-background 'git-gutter:deleted c)
  347. (set-face-background 'git-gutter:unchanged c)))
  348. (global-git-gutter-mode 1)
  349. )
  350. ;; (when (safe-require-or-eval 'fancy-narrow)
  351. ;; (fancy-narrow-mode 1))
  352. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  353. ;; title and mode-line
  354. (when (safe-require-or-eval 'terminal-title)
  355. ;; if TERM is not screen use default value
  356. (if (getenv "TMUX")
  357. ;; if use tmux locally just basename of current dir
  358. (set-variable 'terminal-title-format
  359. '((file-name-nondirectory (directory-file-name
  360. default-directory))))
  361. (if (and (let ((tty-type (frame-parameter nil
  362. 'tty-type)))
  363. (and tty-type
  364. (equal (car (split-string tty-type
  365. "-"))
  366. "screen")))
  367. (not (getenv "SSH_CONNECTION")))
  368. (set-variable 'terminal-title-format
  369. '((file-name-nondirectory (directory-file-name
  370. default-directory))))
  371. ;; seems that TMUX is used locally and ssh to remote host
  372. (set-variable 'terminal-title-format
  373. `("em:"
  374. ,user-login-name
  375. "@"
  376. ,(car (split-string (system-name)
  377. "\\."))
  378. ":"
  379. default-directory))
  380. )
  381. )
  382. (terminal-title-mode))
  383. (setq eol-mnemonic-dos "\\r\\n")
  384. (setq eol-mnemonic-mac "\\r")
  385. (setq eol-mnemonic-unix "")
  386. (which-function-mode 1)
  387. (line-number-mode 0)
  388. (column-number-mode 0)
  389. (size-indication-mode 0)
  390. (setq mode-line-position
  391. '(:eval (format "L%%l/%d%s:C%%c"
  392. (count-lines (point-max)
  393. (point-min))
  394. (if (buffer-narrowed-p)
  395. "[N]"
  396. "")
  397. )))
  398. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  399. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  400. ;; minibuffer
  401. (setq insert-default-directory t)
  402. (setq completion-ignore-case t
  403. read-file-name-completion-ignore-case t
  404. read-buffer-completion-ignore-case t)
  405. (setq resize-mini-windows t)
  406. (temp-buffer-resize-mode 1)
  407. (savehist-mode 1)
  408. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  409. (set-variable 'help-at-pt-display-when-idle t)
  410. (fset 'yes-or-no-p 'y-or-n-p)
  411. ;; complete symbol when `eval'
  412. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  413. (define-key minibuffer-local-map (kbd "C-u")
  414. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  415. ;; I dont know these bindings are good
  416. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  417. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  418. (when (safe-require-or-eval 'minibuffer-line)
  419. (set-face-underline 'minibuffer-line nil)
  420. (set-variable 'minibuffer-line-refresh-interval
  421. 25)
  422. ;; Set idle timer
  423. (defvar my-minibuffer-line--idle-timer nil)
  424. (defvar minibuffer-line-mode)
  425. (add-hook 'minibuffer-line-mode-hook
  426. (lambda ()
  427. (when my-minibuffer-line--idle-timer
  428. (cancel-timer my-minibuffer-line--idle-timer)
  429. (setq my-minibuffer-line--idle-timer nil))
  430. (when minibuffer-line-mode
  431. (setq my-minibuffer-line--idle-timer
  432. (run-with-idle-timer 0.5
  433. t
  434. 'minibuffer-line--update)))))
  435. (set-variable 'minibuffer-line-format
  436. `(,(concat user-login-name
  437. "@"
  438. (car (split-string (system-name)
  439. "\\."))
  440. ":")
  441. (:eval (abbreviate-file-name (or buffer-file-name
  442. default-directory)))
  443. (:eval (and (fboundp 'git-ps1-mode-get-current)
  444. (git-ps1-mode-get-current " [GIT:%s]")))
  445. " "
  446. (:eval (format-time-string display-time-format))))
  447. (minibuffer-line-mode 1)
  448. )
  449. (when (safe-require-or-eval 'prompt-text)
  450. (set-variable 'prompt-text-format
  451. `(,(concat ""
  452. user-login-name
  453. "@"
  454. (car (split-string (system-name)
  455. "\\."))
  456. ":")
  457. (:eval (abbreviate-file-name (or buffer-file-name
  458. default-directory)))
  459. (:eval (and (fboundp 'git-ps1-mode-get-current)
  460. (git-ps1-mode-get-current " [GIT:%s]")))
  461. " "
  462. (:eval (format-time-string display-time-format))
  463. "\n"
  464. (:eval (symbol-name this-command))
  465. ": "))
  466. (prompt-text-mode 1))
  467. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  468. ;; letters, font-lock mode and fonts
  469. (setq text-quoting-style 'grave)
  470. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  471. ;; (set-window-margins (selected-window) 1 1)
  472. (unless window-system
  473. (setq frame-background-mode 'dark))
  474. (and (or (eq system-type 'Darwin)
  475. (eq system-type 'darwin))
  476. (fboundp 'mac-set-input-method-parameter)
  477. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  478. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  479. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  480. (boundp 'input-method-inactivate-hook))
  481. (add-hook 'input-method-activate-hook
  482. (lambda () (set-cursor-color "red")))
  483. (add-hook 'input-method-inactivate-hook
  484. (lambda () (set-cursor-color "black"))))
  485. (when (safe-require-or-eval 'paren)
  486. (show-paren-mode 1)
  487. (setq show-paren-delay 0.5
  488. show-paren-style 'parenthesis) ; mixed is hard to read
  489. ;; (set-face-background 'show-paren-match
  490. ;; "black")
  491. ;; ;; (face-foreground 'default))
  492. ;; (set-face-foreground 'show-paren-match
  493. ;; "white")
  494. ;; (set-face-inverse-video-p 'show-paren-match
  495. ;; t)
  496. )
  497. (transient-mark-mode 1)
  498. (global-font-lock-mode 1)
  499. (setq font-lock-global-modes
  500. '(not
  501. help-mode
  502. eshell-mode
  503. ;;term-mode
  504. Man-mode
  505. magit-diff-mode
  506. magit-revision-mode))
  507. ;; (standard-display-ascii ?\n "$\n")
  508. ;; (defvar my-eol-face
  509. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  510. ;; )
  511. ;; (defvar my-tab-face
  512. ;; '(("\t" . '(0 highlight t nil))))
  513. (defvar my-jspace-face
  514. '(("\u3000" . '(0 highlight t nil))))
  515. (add-hook 'font-lock-mode-hook
  516. (lambda ()
  517. ;; (font-lock-add-keywords nil my-eol-face)
  518. (font-lock-add-keywords nil my-jspace-face)
  519. ))
  520. (when (safe-require-or-eval 'whitespace)
  521. (add-to-list 'whitespace-display-mappings
  522. ;; We need t since last one takes precedence
  523. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  524. ;; (add-to-list 'whitespace-display-mappings
  525. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  526. (setq whitespace-style '(face
  527. trailing ; trailing blanks
  528. ;; tabs
  529. ;; spaces
  530. ;; lines
  531. lines-tail ; lines over 80
  532. newline ; newlines
  533. empty ; empty lines at beg or end of buffer
  534. ;; big-indent
  535. ;; space-mark
  536. tab-mark
  537. newline-mark ; use display table for newline
  538. ))
  539. ;; (setq whitespace-newline 'font-lock-comment-face)
  540. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  541. (defun my-whitesspace-mode-reload ()
  542. "Reload whitespace-mode config."
  543. (interactive)
  544. (when whitespace-mode
  545. (whitespace-mode 0)
  546. (whitespace-mode 1)))
  547. (set-variable 'whitespace-line-column nil)
  548. (global-whitespace-mode t)
  549. (add-hook 'dired-mode-hook
  550. (lambda ()
  551. (set (make-local-variable 'whitespace-style) nil)))
  552. (if (= (display-color-cells)
  553. 256)
  554. (set-face-foreground 'whitespace-newline "color-109")
  555. ;; (progn
  556. ;; (set-face-bold-p 'whitespace-newline
  557. ;; t))
  558. ))
  559. (and nil
  560. '(safe-require-or-eval 'fill-column-indicator)
  561. (setq fill-column-indicator))
  562. ;; highlight current line
  563. ;; http://wiki.riywo.com/index.php?Meadow
  564. (face-spec-set 'hl-line
  565. '((((min-colors 256)
  566. (background dark))
  567. (:background "color-234"))
  568. (((min-colors 256)
  569. (background light))
  570. (:background "color-234"))
  571. (t
  572. (:underline "black"))))
  573. (set-variable 'hl-line-global-modes
  574. '(not
  575. term-mode))
  576. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  577. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  578. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  579. ;;(safe-require-or-eval 'set-modeline-color)
  580. ;; (let ((fg (face-foreground 'default))
  581. ;; (bg (face-background 'default)))
  582. ;; (set-face-background 'mode-line-inactive
  583. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  584. ;; (set-face-foreground 'mode-line-inactive
  585. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  586. ;; (set-face-underline 'mode-line-inactive
  587. ;; t)
  588. ;; (set-face-underline 'vertical-border
  589. ;; nil)
  590. ;; (when (safe-require-or-eval 'end-mark)
  591. ;; (global-end-mark-mode))
  592. ;; M-x highlight-* to highlight things
  593. (global-hi-lock-mode 1)
  594. (unless (fboundp 'highlight-region-text)
  595. (defun highlight-region-text (beg end)
  596. "Highlight text between BEG and END."
  597. (interactive "r")
  598. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  599. end)))
  600. (setq deactivate-mark t)))
  601. (when (safe-require-or-eval 'auto-highlight-symbol)
  602. (set-variable 'ahs-idle-interval 0.6)
  603. (global-auto-highlight-symbol-mode 1))
  604. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  605. ;; file handling
  606. (when (safe-require-or-eval 'recently)
  607. (recently-mode 1))
  608. (when (safe-require-or-eval 'editorconfig)
  609. ;; (set-variable 'editorconfig-get-properties-function
  610. ;; 'editorconfig-core-get-properties-hash)
  611. (editorconfig-mode 1)
  612. (set-variable 'editorconfig-mode-lighter " EC")
  613. (when (fboundp 'ws-butler-mode)
  614. (set-variable 'editorconfig-trim-whitespaces-mode
  615. 'ws-butler-mode))
  616. (with-eval-after-load 'org-src
  617. ;; [*.org\[\*Org Src*\[ c \]*\]]
  618. (add-hook 'org-src-mode-hook
  619. 'editorconfig-mode-apply t)))
  620. (when (fboundp 'editorconfig-custom-majormode)
  621. (add-hook 'editorconfig-after-apply-functions
  622. 'editorconfig-custom-majormode))
  623. ;; Add readonly=true to set read-only-mode
  624. (add-hook 'editorconfig-after-apply-functions
  625. (lambda (props)
  626. (let ((r (gethash 'readonly props)))
  627. (when (and (string= r "true")
  628. (not buffer-read-only))
  629. (read-only-mode 1)))))
  630. (add-hook 'editorconfig-hack-properties-functions
  631. '(lambda (props)
  632. (when (derived-mode-p 'makefile-mode)
  633. (puthash 'indent_style "tab" props))))
  634. ;; (when (fboundp 'editorconfig-charset-extras)
  635. ;; (add-hook 'editorconfig-custom-hooks
  636. ;; 'editorconfig-charset-extras))
  637. (setq revert-without-query '(".+"))
  638. ;; save cursor position
  639. (when (safe-require-or-eval 'saveplace)
  640. (setq-default save-place t)
  641. (setq save-place-file (concat user-emacs-directory
  642. "places")))
  643. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  644. (setq make-backup-files t)
  645. (setq vc-make-backup-files t)
  646. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  647. (setq backup-directory-alist
  648. (cons (cons "." (expand-file-name (concat user-emacs-directory
  649. "backup")))
  650. backup-directory-alist))
  651. (setq version-control 't)
  652. (setq delete-old-versions t)
  653. (setq kept-new-versions 20)
  654. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  655. "auto-save/")))
  656. ;; (setq delete-auto-save-files t)
  657. (setq auto-save-visited-interval 8)
  658. (auto-save-visited-mode 1)
  659. (add-to-list 'completion-ignored-extensions ".bak")
  660. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  661. (setq delete-by-moving-to-trash t)
  662. ;; trash-directory "~/.emacs.d/trash")
  663. (add-hook 'after-save-hook
  664. 'executable-make-buffer-file-executable-if-script-p)
  665. (set-variable 'bookmark-default-file
  666. (expand-file-name (concat user-emacs-directory
  667. "bmk")))
  668. (set-variable 'bookmark-save-flag
  669. 1)
  670. (with-eval-after-load 'recentf
  671. (defvar recentf-exclude)
  672. (defvar bookmark-default-file)
  673. (add-to-list 'recentf-exclude
  674. (regexp-quote bookmark-default-file)))
  675. (when (safe-require-or-eval 'smart-revert)
  676. (smart-revert-on))
  677. ;; autosave
  678. ;; auto-save-visited-mode can be used instead?
  679. ;; (when (safe-require-or-eval 'autosave)
  680. ;; (autosave-set 8))
  681. ;; bookmarks
  682. (define-key ctl-x-map "m" 'list-bookmarks)
  683. ;; vc
  684. (set-variable 'vc-handled-backends '(RCS))
  685. (set-variable 'vc-rcs-register-switches "-l")
  686. (set-variable 'vc-rcs-checkin-switches "-l")
  687. (set-variable 'vc-command-messages t)
  688. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  689. ;; share clipboard with x
  690. ;; this page describes this in details, but only these sexps seem to be needed
  691. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  692. (and nil
  693. (not window-system)
  694. (not (eq window-system 'mac))
  695. (getenv "DISPLAY")
  696. (not (equal (getenv "DISPLAY") ""))
  697. (executable-find "xclip")
  698. ;; (< emacs-major-version 24)
  699. '(safe-require-or-eval 'xclip)
  700. nil
  701. (turn-on-xclip))
  702. (and (eq system-type 'darwin)
  703. (safe-require-or-eval 'pasteboard)
  704. (turn-on-pasteboard))
  705. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  706. ;; some modes and hooks
  707. ;; Include some extra modes
  708. (require 'generic-x)
  709. (when (autoload-eval-lazily 'wgrep)
  710. (set-variable 'wgrep-auto-save-buffer t)
  711. (with-eval-after-load 'grep
  712. (defvar grep-mode-map)
  713. (define-key grep-mode-map
  714. "e"
  715. 'wgrep-change-to-wgrep-mode)))
  716. (with-eval-after-load 'remember
  717. (defvar remember-mode-map (make-sparse-keymap))
  718. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  719. (with-eval-after-load 'magit-files
  720. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  721. ;; existing keybindings.
  722. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  723. ;; to disable thie mode here, but do anyway.
  724. ;; See also https://github.com/magit/magit/issues/3517
  725. (global-magit-file-mode -1))
  726. (with-eval-after-load 'magit-section
  727. (set-face-background 'magit-section-highlight
  728. nil))
  729. (with-eval-after-load 'magit-diff
  730. (set-face-background 'magit-diff-added-highlight
  731. nil)
  732. (set-face-background 'magit-diff-removed-highlight
  733. nil)
  734. (set-face-background 'magit-diff-context-highlight
  735. nil)
  736. )
  737. (when (boundp 'git-rebase-filename-regexp)
  738. (add-to-list 'auto-mode-alist
  739. `(,git-rebase-filename-regexp . text-mode)))
  740. (when (safe-require-or-eval 'aggressive-indent)
  741. (defvar aggressive-indent-excluded-modes)
  742. (setq aggressive-indent-excluded-modes
  743. `(diff-mode
  744. toml-mode
  745. conf-mode
  746. dockerfile-mode
  747. groovy-mode
  748. ,@aggressive-indent-excluded-modes))
  749. (global-aggressive-indent-mode 1))
  750. (when (autoload-eval-lazily 'ggtags)
  751. (add-hook 'c-mode-common-hook
  752. (lambda ()
  753. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  754. (ggtags-mode 1))))
  755. (add-hook 'python-mode-hook
  756. (lambda ()
  757. (ggtags-mode 1))))
  758. (when (autoload-eval-lazily 'imenu-list)
  759. ;; (set-variable 'imenu-list-auto-resize t)
  760. (set-variable 'imenu-list-focus-after-activation t)
  761. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  762. (add-hook 'emacs-lisp-mode-hook
  763. (lambda ()
  764. (setq imenu-generic-expression
  765. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  766. ,@imenu-generic-expression))))
  767. (with-eval-after-load 'compile
  768. (defvar compilation-filter-start)
  769. (defvar compilation-error-regexp-alist)
  770. (require 'ansi-color)
  771. (add-hook 'compilation-filter-hook
  772. (lambda ()
  773. (let ((inhibit-read-only t))
  774. (ansi-color-apply-on-region compilation-filter-start
  775. (point)))))
  776. (add-to-list 'compilation-error-regexp-alist
  777. ;; ansible-lint
  778. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  779. ;; Workaround to avoid ensime error
  780. (defvar ensime-mode-key-prefix nil)
  781. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  782. (when (safe-require-or-eval 'company)
  783. (global-company-mode)
  784. (set-variable 'company-idle-delay 0.5)
  785. (set-variable 'company-minimum-prefix-length 2)
  786. (set-variable 'company-selection-wrap-around t))
  787. ;; https://github.com/lunaryorn/flycheck
  788. (when (safe-require-or-eval 'flycheck)
  789. (call-after-init 'global-flycheck-mode))
  790. (when (autoload-eval-lazily 'ilookup)
  791. (define-key ctl-x-map "d" 'ilookup-open-word))
  792. (set-variable 'ac-ignore-case nil)
  793. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  794. (define-key ctl-x-map "t" 'term-run-shell-command))
  795. (add-to-list 'safe-local-variable-values
  796. '(encoding utf-8))
  797. (setq enable-local-variables :safe)
  798. ;; Detect file type from shebang and set major-mode.
  799. (add-to-list 'interpreter-mode-alist
  800. '("python3" . python-mode))
  801. (add-to-list 'interpreter-mode-alist
  802. '("python2" . python-mode))
  803. (with-eval-after-load 'python
  804. (defvar python-mode-map (make-sparse-keymap))
  805. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  806. (when (autoload-eval-lazily 'pipenv)
  807. (add-hook 'python-mode-hook
  808. (lambda ()
  809. (pipenv-mode 1)
  810. (pipenv-projectile-after-switch-default)))
  811. )
  812. (set-variable 'flycheck-python-pycompile-executable "python3")
  813. (set-variable 'python-indent-guess-indent-offset nil)
  814. ;; http://fukuyama.co/foreign-regexp
  815. '(and (safe-require-or-eval 'foreign-regexp)
  816. (progn
  817. (setq foreign-regexp/regexp-type 'perl)
  818. '(setq reb-re-syntax 'foreign-regexp)
  819. ))
  820. (autoload-eval-lazily 'sql '(sql-mode)
  821. (require 'sql-indent nil t))
  822. (when (autoload-eval-lazily 'git-command)
  823. (define-key ctl-x-map "g" 'git-command))
  824. (when (safe-require-or-eval 'git-commit)
  825. (global-git-commit-mode 1))
  826. (with-eval-after-load 'git-commit
  827. (add-hook 'git-commit-setup-hook
  828. 'turn-off-auto-fill t))
  829. (autoload-eval-lazily 'sl)
  830. (with-eval-after-load 'rst
  831. (defvar rst-mode-map)
  832. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  833. (with-eval-after-load 'jdee
  834. (add-hook 'jdee-mode-hook
  835. (lambda ()
  836. (make-local-variable 'global-mode-string)
  837. (add-to-list 'global-mode-string
  838. mode-line-position))))
  839. ;; Cannot enable error thrown. Why???
  840. ;; https://github.com/m0smith/malabar-mode#Installation
  841. ;; (when (autoload-eval-lazily 'malabar-mode)
  842. ;; (add-to-list 'load-path
  843. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  844. ;; (safe-require-or-eval 'cedet-devel-load)
  845. ;; (call-after-init 'activate-malabar-mode))
  846. (with-eval-after-load 'make-mode
  847. (defvar makefile-mode-map (make-sparse-keymap))
  848. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  849. ;; this functions is set in write-file-functions, i cannot find any
  850. ;; good way to remove this.
  851. (fset 'makefile-warn-suspicious-lines 'ignore))
  852. (with-eval-after-load 'verilog-mode
  853. (defvar verilog-mode-map (make-sparse-keymap))
  854. (define-key verilog-mode-map ";" 'self-insert-command))
  855. (setq diff-switches "-u")
  856. (with-eval-after-load 'diff-mode
  857. ;; (when (and (eq major-mode
  858. ;; 'diff-mode)
  859. ;; (not buffer-file-name))
  860. ;; ;; do not pass when major-mode is derived mode of diff-mode
  861. ;; (view-mode 1))
  862. (set-face-attribute 'diff-header nil
  863. :foreground nil
  864. :background nil
  865. :weight 'bold)
  866. (set-face-attribute 'diff-file-header nil
  867. :foreground nil
  868. :background nil
  869. :weight 'bold)
  870. (set-face-foreground 'diff-index "blue")
  871. (set-face-attribute 'diff-hunk-header nil
  872. :foreground "cyan"
  873. :weight 'normal)
  874. (set-face-attribute 'diff-context nil
  875. ;; :foreground "white"
  876. :foreground nil
  877. :weight 'normal)
  878. (set-face-foreground 'diff-removed "red")
  879. (set-face-foreground 'diff-added "green")
  880. (set-face-background 'diff-removed nil)
  881. (set-face-background 'diff-added nil)
  882. (set-face-attribute 'diff-changed nil
  883. :foreground "magenta"
  884. :weight 'normal)
  885. (set-face-attribute 'diff-refine-changed nil
  886. :foreground nil
  887. :background nil
  888. :weight 'bold
  889. :inverse-video t)
  890. ;; Annoying !
  891. ;;(diff-auto-refine-mode)
  892. )
  893. ;; (ffap-bindings)
  894. (set-variable 'browse-url-browser-function
  895. 'eww-browse-url)
  896. (set-variable 'sh-here-document-word "__EOC__")
  897. (when (autoload-eval-lazily 'adoc-mode
  898. nil
  899. (defvar adoc-mode-map (make-sparse-keymap))
  900. (define-key adoc-mode-map (kbd "C-m") 'newline))
  901. (setq auto-mode-alist
  902. `(("\\.adoc\\'" . adoc-mode)
  903. ("\\.asciidoc\\'" . adoc-mode)
  904. ,@auto-mode-alist)))
  905. (with-eval-after-load 'markup-faces
  906. ;; Is this too match ?
  907. (set-face-foreground 'markup-meta-face
  908. "color-245")
  909. (set-face-foreground 'markup-meta-hide-face
  910. "color-245")
  911. )
  912. ;; TODO: check if this is required
  913. (when (autoload-eval-lazily 'groovy-mode nil
  914. (defvar groovy-mode-map (make-sparse-keymap))
  915. (define-key groovy-mode-map "(" 'self-insert-command)
  916. (define-key groovy-mode-map ")" 'self-insert-command)
  917. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  918. )
  919. (add-to-list 'auto-mode-alist
  920. '("build\\.gradle\\'" . groovy-mode)))
  921. (add-to-list 'auto-mode-alist
  922. '("\\.gawk\\'" . awk-mode))
  923. (with-eval-after-load 'yaml-mode
  924. (defvar yaml-mode-map (make-sparse-keymap))
  925. (define-key yaml-mode-map (kbd "C-m") 'newline))
  926. (with-eval-after-load 'html-mode
  927. (defvar html-mode-map (make-sparse-keymap))
  928. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  929. (with-eval-after-load 'text-mode
  930. (define-key text-mode-map (kbd "C-m") 'newline))
  931. (autoload-eval-lazily 'info nil
  932. (defvar Info-additional-directory-list)
  933. (dolist (dir (directory-files (concat user-emacs-directory
  934. "info")
  935. t
  936. "^[^.].*"))
  937. (when (file-directory-p dir)
  938. (add-to-list 'Info-additional-directory-list
  939. dir)))
  940. (let ((dir (expand-file-name "~/.brew/share/info")))
  941. (when (file-directory-p dir)
  942. (add-to-list 'Info-additional-directory-list
  943. dir))))
  944. (with-eval-after-load 'apropos
  945. (defvar apropos-mode-map (make-sparse-keymap))
  946. (define-key apropos-mode-map "n" 'next-line)
  947. (define-key apropos-mode-map "p" 'previous-line))
  948. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  949. ;; (define-key isearch-mode-map
  950. ;; (kbd "C-j") 'isearch-other-control-char)
  951. ;; (define-key isearch-mode-map
  952. ;; (kbd "C-k") 'isearch-other-control-char)
  953. ;; (define-key isearch-mode-map
  954. ;; (kbd "C-h") 'isearch-other-control-char)
  955. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  956. (define-key isearch-mode-map (kbd "M-r")
  957. 'isearch-query-replace-regexp)
  958. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  959. (setq lazy-highlight-cleanup nil)
  960. ;; face for isearch highlighing
  961. (set-face-attribute 'lazy-highlight
  962. nil
  963. :foreground `unspecified
  964. :background `unspecified
  965. :underline t
  966. ;; :weight `bold
  967. )
  968. (add-hook 'outline-mode-hook
  969. (lambda ()
  970. (when (string-match "\\.md\\'" buffer-file-name)
  971. (set (make-local-variable 'outline-regexp) "#+ "))))
  972. (add-hook 'outline-mode-hook
  973. 'outline-show-all)
  974. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  975. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  976. (when (autoload-eval-lazily 'markdown-mode
  977. '(markdown-mode gfm-mode)
  978. (defvar gfm-mode-map (make-sparse-keymap))
  979. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  980. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  981. (set-variable 'markdown-command (or (executable-find "markdown")
  982. (executable-find "markdown.pl")
  983. ""))
  984. (add-hook 'markdown-mode-hook
  985. (lambda ()
  986. (outline-minor-mode 1)
  987. (flyspell-mode)
  988. (set (make-local-variable 'comment-start) ";")))
  989. )
  990. ;; c-mode
  991. ;; http://www.emacswiki.org/emacs/IndentingC
  992. ;; http://en.wikipedia.org/wiki/Indent_style
  993. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  994. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  995. (with-eval-after-load 'cc-vars
  996. (defvar c-default-style nil)
  997. (add-to-list 'c-default-style
  998. '(c-mode . "k&r"))
  999. (add-to-list 'c-default-style
  1000. '(c++-mode . "k&r")))
  1001. (autoload-eval-lazily 'js2-mode nil
  1002. ;; currently do not use js2-mode
  1003. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1004. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1005. ;; (defvar js2-mode-map (make-sparse-keymap))
  1006. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1007. ;; (interactive)
  1008. ;; (js2-enter-key)
  1009. ;; (indent-for-tab-command)))
  1010. ;; (add-hook (kill-local-variable 'before-save-hook)
  1011. ;; 'js2-before-save)
  1012. ;; (add-hook 'before-save-hook
  1013. ;; 'my-indent-buffer
  1014. ;; nil
  1015. ;; t)
  1016. )
  1017. (add-to-list 'interpreter-mode-alist
  1018. '("node" . js-mode))
  1019. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1020. (with-eval-after-load 'uniquify
  1021. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1022. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1023. (setq uniquify-min-dir-content 1))
  1024. (with-eval-after-load 'view
  1025. (defvar view-mode-map (make-sparse-keymap))
  1026. (define-key view-mode-map "j" 'scroll-up-line)
  1027. (define-key view-mode-map "k" 'scroll-down-line)
  1028. (define-key view-mode-map "v" 'toggle-read-only)
  1029. (define-key view-mode-map "q" 'bury-buffer)
  1030. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1031. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1032. ;; (define-key view-mode-map
  1033. ;; "n" 'nonincremental-repeat-search-forward)
  1034. ;; (define-key view-mode-map
  1035. ;; "N" 'nonincremental-repeat-search-backward)
  1036. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1037. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1038. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1039. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1040. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1041. (global-set-key "\M-r" 'view-mode)
  1042. ;; (setq view-read-only t)
  1043. (with-eval-after-load 'term
  1044. (defvar term-raw-map (make-sparse-keymap))
  1045. (define-key term-raw-map (kbd "C-x")
  1046. (lookup-key (current-global-map)
  1047. (kbd "C-x"))))
  1048. (add-hook 'term-mode-hook
  1049. (lambda ()
  1050. ;; Stop current line highlighting
  1051. (set (make-local-variable (defvar hl-line-range-function))
  1052. (lambda () '(0 . 0)))
  1053. (set (make-local-variable 'scroll-margin)
  1054. 0)))
  1055. (add-hook 'Man-mode-hook
  1056. (lambda ()
  1057. (view-mode 1)
  1058. (setq truncate-lines nil)))
  1059. (set-variable 'Man-notify-method (if window-system
  1060. 'newframe
  1061. 'aggressive))
  1062. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1063. "woman_cache.el")))
  1064. ;; not work because man.el will be loaded when man called
  1065. (defalias 'man 'woman)
  1066. (add-to-list 'auto-mode-alist
  1067. '("tox\\.ini\\'" . conf-unix-mode))
  1068. (when (autoload-eval-lazily 'toml-mode)
  1069. (add-to-list 'auto-mode-alist
  1070. '("/tox\\.ini\\'" . toml-mode))
  1071. (add-to-list 'auto-mode-alist
  1072. '("/Pipfile\\'" . toml-mode))
  1073. (add-to-list 'auto-mode-alist
  1074. '("/poetry\\.lock\\'" . toml-mode))
  1075. )
  1076. (when (autoload-eval-lazily 'json-mode)
  1077. (add-to-list 'auto-mode-alist
  1078. '("/Pipfile\\.lock\\'" . json-mode)))
  1079. (add-hook 'go-mode-hook
  1080. (lambda()
  1081. (defvar go-mode-map)
  1082. (add-hook 'before-save-hook' 'gofmt-before-save)
  1083. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1084. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1085. ;; buffers
  1086. (defvar bs-configurations)
  1087. (when (autoload-eval-lazily 'bs '(bs-show)
  1088. (add-to-list 'bs-configurations
  1089. '("specials" "^\\*" nil ".*" nil nil))
  1090. (add-to-list 'bs-configurations
  1091. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1092. (defvar bs-mode-map)
  1093. (defvar bs-current-configuration)
  1094. (define-key bs-mode-map (kbd "t")
  1095. ;; TODO: fix toggle feature
  1096. (lambda ()
  1097. (interactive)
  1098. (if (string= "specials"
  1099. bs-current-configuration)
  1100. (bs-set-configuration "files")
  1101. (bs-set-configuration "specials"))
  1102. (bs-refresh)
  1103. (bs-message-without-log "%s"
  1104. (bs--current-config-message))))
  1105. ;; (setq bs-configurations (list
  1106. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1107. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1108. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1109. )
  1110. (defalias 'list-buffers 'bs-show)
  1111. (set-variable 'bs-default-configuration "files-and-specials")
  1112. (set-variable 'bs-default-sort-name "by nothing")
  1113. (add-hook 'bs-mode-hook
  1114. (lambda ()
  1115. (set (make-local-variable 'scroll-margin) 0))))
  1116. ;;(iswitchb-mode 1)
  1117. (icomplete-mode)
  1118. (defun iswitchb-buffer-display-other-window ()
  1119. "Do iswitchb in other window."
  1120. (interactive)
  1121. (let ((iswitchb-default-method 'display))
  1122. (call-interactively 'iswitchb-buffer)))
  1123. ;; buffer killing
  1124. ;; (defun my-delete-window-killing-buffer () nil)
  1125. (defun my-query-kill-current-buffer ()
  1126. "Interactively kill current buffer."
  1127. (interactive)
  1128. (if (y-or-n-p (concat "kill current buffer? :"))
  1129. (kill-buffer (current-buffer))))
  1130. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1131. (substitute-key-definition 'kill-buffer
  1132. 'my-query-kill-current-buffer
  1133. global-map)
  1134. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1135. ;; recentf-mode
  1136. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1137. "recentf")))
  1138. (set-variable 'recentf-max-menu-items 20)
  1139. (set-variable 'recentf-max-saved-items 30)
  1140. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1141. (set-variable 'recentf-auto-cleanup 3)
  1142. ;; (safe-require-or-eval 'sync-recentf)
  1143. ;; (when (safe-require-or-eval 'recentf)
  1144. ;; (add-to-list 'recentf-exclude
  1145. ;; (regexp-quote recentf-save-file))
  1146. ;; (add-to-list 'recentf-exclude
  1147. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1148. ;; (add-to-list 'recentf-exclude
  1149. ;; "/sync-recentf-marker\\'")
  1150. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1151. ;; (remove-hook 'find-file-hook
  1152. ;; 'recentf-track-opened-file)
  1153. ;; (defun my-recentf-load-track-save-list ()
  1154. ;; "Load current recentf list from file, track current visiting file, then save
  1155. ;; the list."
  1156. ;; (recentf-load-list)
  1157. ;; (recentf-track-opened-file)
  1158. ;; (recentf-save-list))
  1159. ;; (add-hook 'find-file-hook
  1160. ;; 'my-recentf-load-track-save-list)
  1161. ;; (add-hook 'kill-emacs-hook
  1162. ;; 'recentf-load-list)
  1163. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1164. ;; ;; (add-hook 'find-file-hook
  1165. ;; ;; (lambda ()
  1166. ;; ;; (recentf-add-file default-directory)))
  1167. ;; (when (autoload-eval-lazily 'recentf-show)
  1168. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1169. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1170. ;; ;; 'recentf-load-list)
  1171. ;; )
  1172. ;; (recentf-mode 1)
  1173. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1174. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1175. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1176. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1177. ;; (add-hook 'recentf-dialog-mode-hook
  1178. ;; (lambda ()
  1179. ;; ;; (recentf-save-list)
  1180. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1181. ;; ;; 'my-recentf-cd-and-find-file)
  1182. ;; (cd "~/"))))
  1183. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1184. ;; dired
  1185. (defun my-file-head (filename &optional n)
  1186. "Return list of first N lines of file FILENAME."
  1187. ;; TODO: Fix for janapese text
  1188. ;; TODO: Fix for short text
  1189. (let ((num (or n 10))
  1190. (size 100)
  1191. (beg 0)
  1192. (end 0)
  1193. (result '())
  1194. (read -1))
  1195. (with-temp-buffer
  1196. (erase-buffer)
  1197. (while (or (<= (count-lines (point-min)
  1198. (point-max))
  1199. num)
  1200. (eq read 0))
  1201. (setq end (+ beg size))
  1202. (setq read (nth 1 (insert-file-contents-literally filename
  1203. nil
  1204. beg
  1205. end)))
  1206. (goto-char (point-max))
  1207. (setq beg (+ beg size)))
  1208. (goto-char (point-min))
  1209. (while (< (length result) num)
  1210. (let ((start (point)))
  1211. (forward-line 1)
  1212. (setq result
  1213. `(,@result ,(buffer-substring-no-properties start
  1214. (point))))))
  1215. result
  1216. ;; (buffer-substring-no-properties (point-min)
  1217. ;; (progn
  1218. ;; (forward-line num)
  1219. ;; (point)))
  1220. )))
  1221. ;; (apply 'concat (my-file-head "./shrc" 10)
  1222. (defun my-dired-echo-file-head (arg)
  1223. "Echo head of current file.
  1224. ARG is num to show, or defaults to 7."
  1225. (interactive "P")
  1226. (let ((f (dired-get-filename)))
  1227. (message "%s"
  1228. (apply 'concat
  1229. (my-file-head f
  1230. 7)))))
  1231. (defun my-dired-diff ()
  1232. "Show diff of marked file and file of current line."
  1233. (interactive)
  1234. (let ((files (dired-get-marked-files nil nil nil t)))
  1235. (if (eq (car files)
  1236. t)
  1237. (diff (cadr files) (dired-get-filename))
  1238. (message "One file must be marked!"))))
  1239. (defun dired-get-file-info ()
  1240. "Print information of current line file."
  1241. (interactive)
  1242. (let ((f (shell-quote-argument (dired-get-filename t))))
  1243. (if (file-directory-p f)
  1244. (progn
  1245. (message "Calculating disk usage...")
  1246. (shell-command (concat "du -hsD "
  1247. f)))
  1248. (shell-command (concat "file "
  1249. f)))))
  1250. (defun my-dired-scroll-up ()
  1251. "Scroll up."
  1252. (interactive)
  1253. (my-dired-previous-line (- (window-height) 1)))
  1254. (defun my-dired-scroll-down ()
  1255. "Scroll down."
  1256. (interactive)
  1257. (my-dired-next-line (- (window-height) 1)))
  1258. ;; (defun my-dired-forward-line (arg)
  1259. ;; ""
  1260. ;; (interactive "p"))
  1261. (defun my-dired-previous-line (arg)
  1262. "Move ARG lines up."
  1263. (interactive "p")
  1264. (if (> arg 0)
  1265. (progn
  1266. (if (eq (line-number-at-pos)
  1267. 1)
  1268. (goto-char (point-max))
  1269. (forward-line -1))
  1270. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1271. (dired-get-subdir))
  1272. (- arg 1)
  1273. arg)))
  1274. (dired-move-to-filename)))
  1275. (defun my-dired-next-line (arg)
  1276. "Move ARG lines down."
  1277. (interactive "p")
  1278. (if (> arg 0)
  1279. (progn
  1280. (if (eq (point)
  1281. (point-max))
  1282. (goto-char (point-min))
  1283. (forward-line 1))
  1284. (my-dired-next-line (if (or (dired-get-filename nil t)
  1285. (dired-get-subdir))
  1286. (- arg 1)
  1287. arg)))
  1288. (dired-move-to-filename)))
  1289. (defun my-tramp-remote-find-file (f)
  1290. "Open F."
  1291. (interactive (list (read-file-name "My Find File Tramp: "
  1292. "/scp:"
  1293. nil ;; "/scp:"
  1294. (confirm-nonexistent-file-or-buffer))))
  1295. (find-file f))
  1296. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1297. (if (eq window-system 'mac)
  1298. (setq dired-listing-switches "-lhF")
  1299. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1300. )
  1301. (setq dired-listing-switches "-lhF")
  1302. ;; when using dired-find-alternate-file
  1303. ;; reuse current dired buffer for the file to open
  1304. ;; (put 'dired-find-alternate-file 'disabled nil)
  1305. (set-variable 'dired-ls-F-marks-symlinks t)
  1306. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1307. (set-variable 'ls-lisp-dirs-first t)
  1308. (set-variable 'ls-lisp-use-localized-time-format t)
  1309. (set-variable 'ls-lisp-format-time-list
  1310. '("%Y-%m-%d %H:%M"
  1311. "%Y-%m-%d "))
  1312. (set-variable 'dired-dwim-target t)
  1313. (set-variable 'dired-isearch-filenames t)
  1314. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1315. (set-variable 'dired-hide-details-hide-information-lines nil)
  1316. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1317. (set-variable 'dired-recursive-deletes 'always)
  1318. ;; (add-hook 'dired-after-readin-hook
  1319. ;; 'my-replace-nasi-none)
  1320. (with-eval-after-load 'dired
  1321. (safe-require-or-eval 'ls-lisp)
  1322. (defvar dired-mode-map (make-sparse-keymap))
  1323. ;; dired-do-chgrp sometimes cause system hung
  1324. (define-key dired-mode-map "G" 'ignore)
  1325. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1326. (define-key dired-mode-map "i" 'dired-get-file-info)
  1327. (define-key dired-mode-map "f" 'find-file)
  1328. (define-key dired-mode-map "!" 'shell-command)
  1329. (define-key dired-mode-map "&" 'async-shell-command)
  1330. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1331. (define-key dired-mode-map "=" 'my-dired-diff)
  1332. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1333. (define-key dired-mode-map "b" 'gtkbm)
  1334. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1335. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1336. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1337. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1338. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1339. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1340. (substitute-key-definition 'dired-next-line
  1341. 'my-dired-next-line
  1342. dired-mode-map)
  1343. (substitute-key-definition 'dired-previous-line
  1344. 'my-dired-previous-line
  1345. dired-mode-map)
  1346. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1347. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1348. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1349. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1350. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1351. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1352. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1353. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1354. (add-hook 'dired-mode-hook
  1355. (lambda ()
  1356. (when (fboundp 'dired-hide-details-mode)
  1357. (dired-hide-details-mode t)
  1358. (local-set-key "l" 'dired-hide-details-mode))
  1359. (let ((file "._Icon\015"))
  1360. (when nil
  1361. '(file-readable-p file)
  1362. (delete-file file)))))
  1363. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1364. (with-eval-after-load 'dired
  1365. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1366. (when (autoload-eval-lazily 'dired-list-all-mode)
  1367. (setq dired-listing-switches "-lhF")
  1368. (with-eval-after-load 'dired
  1369. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1370. (when (autoload-eval-lazily 'dired-filter)
  1371. (add-hook 'dired-mode-hook
  1372. 'dired-filter-mode))
  1373. ;; Currently disabled in favor of dired-from-git-ls-files
  1374. ;; (define-key ctl-x-map "f" 'find-dired)
  1375. ;; It works!
  1376. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1377. (defun my-dired-git-ls-files (args)
  1378. "Dired from git ls-files."
  1379. (interactive "sgit ls-files args: ")
  1380. (pop-to-buffer-same-window
  1381. (dired-noselect `(,default-directory
  1382. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1383. "\0" t))
  1384. ""))
  1385. )
  1386. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1387. (with-eval-after-load 'dired
  1388. (defvar dired-mode-map (make-sparse-keymap))
  1389. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1390. ;; (define-minor-mode my-dired-glob-filter)
  1391. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1392. ;; misc funcs
  1393. (defalias 'qcalc 'quick-calc)
  1394. (defun memo (&optional dir)
  1395. "Open memo.txt in DIR."
  1396. (interactive)
  1397. (pop-to-buffer (find-file-noselect (concat (if dir
  1398. (file-name-as-directory dir)
  1399. "")
  1400. "memo.txt"))))
  1401. (set (defvar my-rgrep-alist nil
  1402. "Alist of rgrep command.
  1403. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1404. condition to choose COMMAND when evaluated.")
  1405. `(
  1406. ;; ripgrep
  1407. ("rg"
  1408. (executable-find "rg")
  1409. "rg --hidden --no-heading --smart-case ")
  1410. ;; git grep
  1411. ("gitgrep"
  1412. (eq 0
  1413. (shell-command "git rev-parse --git-dir"))
  1414. "git --no-pager -c color.grep=always grep -nH -e ")
  1415. ;; sift
  1416. ("sift"
  1417. (executable-find "sift")
  1418. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1419. ;; the silver searcher
  1420. ("ag"
  1421. (executable-find "ag")
  1422. "ag --nogroup --nopager --filename ")
  1423. ;; ack
  1424. ("ack"
  1425. (executable-find "ack")
  1426. "ack --nogroup --nopager --with-filename ")
  1427. ;; gnu global
  1428. ("global"
  1429. (and (require 'ggtags nil t)
  1430. (executable-find "global")
  1431. (ggtags-current-project-root))
  1432. "global --result grep ")
  1433. ;; grep
  1434. ("grep"
  1435. t
  1436. ,(concat "find . "
  1437. "-path '*/.git' -prune -o "
  1438. "-path '*/.svn' -prune -o "
  1439. "-type f -print0 | "
  1440. "xargs -0 grep -nH -e "))
  1441. )
  1442. )
  1443. (defvar my-rgrep-default nil
  1444. "Default command name for my-rgrep.")
  1445. (defun my-rgrep-grep-command (&optional name alist)
  1446. "Return recursive grep command for current directory or nil.
  1447. If NAME is given, use that without testing.
  1448. Commands are searched from ALIST."
  1449. (if alist
  1450. (if name
  1451. ;; if name is given search that from alist and return the command
  1452. (nth 2 (assoc name
  1453. alist))
  1454. ;; if name is not given try test in 1th elem
  1455. (let ((car (car alist))
  1456. (cdr (cdr alist)))
  1457. (if (eval (nth 1 car))
  1458. ;; if the condition is true return the command
  1459. (nth 2 car)
  1460. ;; try next one
  1461. (and cdr
  1462. (my-rgrep-grep-command name cdr)))))
  1463. ;; if alist is not given set default value
  1464. (my-rgrep-grep-command name my-rgrep-alist)))
  1465. (defun my-rgrep (command-args)
  1466. "My recursive grep. Run COMMAND-ARGS.
  1467. If prefix argument is given, use current symbol as default search target
  1468. and search from projectile root (if projectile is available)."
  1469. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1470. nil)))
  1471. (if cmd
  1472. (list (read-shell-command "grep command: "
  1473. (concat cmd
  1474. (if current-prefix-arg
  1475. (thing-at-point 'symbol t)
  1476. ""))
  1477. 'grep-find-history))
  1478. (error "My-Rgrep: Command for rgrep not found")
  1479. )))
  1480. (if (and current-prefix-arg
  1481. (safe-require-or-eval 'projectile)
  1482. (projectile-project-p))
  1483. (projectile-with-default-dir (projectile-project-root)
  1484. (compilation-start command-args
  1485. 'grep-mode))
  1486. (compilation-start command-args
  1487. 'grep-mode)))
  1488. (defun my-rgrep-thing-at-point-projectile-root ()
  1489. "My recursive grep to find thing at point from project root."
  1490. (interactive)
  1491. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1492. nil))
  1493. (command-args
  1494. (if cmd
  1495. (concat cmd
  1496. (or (thing-at-point 'symbol t)
  1497. (error "No symbol at point")))
  1498. (error "My-Rgrep: Command for rgrep not found"))))
  1499. (if (safe-require-or-eval 'projectile)
  1500. (projectile-with-default-dir (or (projectile-project-root)
  1501. default-directory)
  1502. (compilation-start command-args
  1503. 'grep-mode))
  1504. (compilation-start command-args
  1505. 'grep-mode))))
  1506. (defmacro define-my-rgrep (name)
  1507. "Define rgrep for NAME."
  1508. `(defun ,(intern (concat "my-rgrep-"
  1509. name)) ()
  1510. ,(format "My recursive grep by %s."
  1511. name)
  1512. (interactive)
  1513. (let ((my-rgrep-default ,name))
  1514. (if (called-interactively-p 'any)
  1515. (call-interactively 'my-rgrep)
  1516. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1517. )
  1518. (define-my-rgrep "ack")
  1519. (define-my-rgrep "ag")
  1520. (define-my-rgrep "rg")
  1521. (define-my-rgrep "sift")
  1522. (define-my-rgrep "gitgrep")
  1523. (define-my-rgrep "grep")
  1524. (define-my-rgrep "global")
  1525. (define-key ctl-x-map "s" 'my-rgrep)
  1526. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1527. (defun my-occur (regexp &optional region)
  1528. "My occur command to search REGEXP."
  1529. (interactive (list (read-string "List lines matching regexp: "
  1530. (thing-at-point 'symbol t))))
  1531. (occur regexp nil region))
  1532. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1533. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1534. (defalias 'make 'compile)
  1535. (define-key ctl-x-map "c" 'compile)
  1536. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1537. ;; editorconfig-auto-apply
  1538. (define-minor-mode editorconfig-auto-apply-mode
  1539. "When saving .editorconfig file update buffer configs."
  1540. :global t
  1541. :lighter ""
  1542. (if editorconfig-auto-apply-mode
  1543. (add-hook 'after-save-hook
  1544. 'editorconfig-auto-apply-mode--run)
  1545. (remove-hook 'after-save-hook
  1546. 'editorconfig-auto-apply-mode--run)))
  1547. (defun editorconfig-auto-apply-mode--run ()
  1548. "When saving .editorconfig file walk all buffers and update configs."
  1549. (when (eq major-mode
  1550. 'editorconfig-conf-mode)
  1551. (let ((dir (file-name-directory buffer-file-name)))
  1552. (cl-dolist (buf (buffer-list))
  1553. (when (and (buffer-file-name buf)
  1554. (file-in-directory-p (buffer-file-name buf)
  1555. dir))
  1556. (with-current-buffer buf
  1557. (editorconfig-mode-apply)))))))
  1558. (editorconfig-auto-apply-mode 1)
  1559. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1560. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1561. ;; git-worktree
  1562. (defun git-worktree-get-current-trees ()
  1563. "Get current worktree list."
  1564. (with-temp-buffer
  1565. (git-worktree--call-process "worktree" "list" "--porcelain")
  1566. (goto-char (point-min))
  1567. (let ((trees nil))
  1568. (save-match-data
  1569. (while (not (eq (point) (point-max)))
  1570. (let ((worktree nil)
  1571. (head nil)
  1572. (branch nil))
  1573. (while (re-search-forward "^\\([^ ]+\\) \\(.*\\)$" (point-at-eol) t)
  1574. (pcase (match-string 1)
  1575. ("worktree" (setq worktree (match-string 2)))
  1576. ("HEAD" (setq head (match-string 2)))
  1577. ("branch" (setq branch (match-string 2))))
  1578. (forward-line 1)
  1579. (goto-char (point-at-bol)))
  1580. (setq trees `(,@trees
  1581. (:worktree ,worktree :head ,head :branch ,branch)))
  1582. (forward-line 1)
  1583. (goto-char (point-at-bol)))
  1584. ))
  1585. trees)))
  1586. (defun git-worktree--call-process (&rest args)
  1587. "Start git process synchronously with ARGS.
  1588. Raise error when git process ends with non-zero status.
  1589. Any output will be written to current buffer."
  1590. (let ((status (apply 'call-process
  1591. "git"
  1592. nil
  1593. t
  1594. nil
  1595. args)))
  1596. (cl-assert (eq status 0)
  1597. nil
  1598. (buffer-substring-no-properties (point-min) (point-max)))))
  1599. (defun git-worktree--get-repository-root (dir)
  1600. "Resolve repository root of DIR.
  1601. If DIR is not inside of any git repository, signal an error."
  1602. (cl-assert (file-directory-p dir))
  1603. (with-temp-buffer
  1604. (cd dir)
  1605. (git-worktree--call-process "rev-parse" "--show-toplevel")
  1606. (goto-char (point-min))
  1607. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  1608. ;;(git-worktree--get-repository-root default-directory)
  1609. (defun git-worktree-open-noselect (&optional directory)
  1610. "Open git worktree list buffer.
  1611. If optional arg DIRECTORY is given change current directory to there before
  1612. initializing."
  1613. (setq directory (expand-file-name (or directory
  1614. default-directory)))
  1615. (cl-assert (file-directory-p directory))
  1616. (let* ((root (git-worktree--get-repository-root directory))
  1617. (name (file-name-nondirectory root))
  1618. (bname (format "*GitWorktree<%s>*" name)))
  1619. (with-current-buffer (get-buffer-create bname)
  1620. (cd root)
  1621. (git-worktree--set-tabulated-list-mode-variables)
  1622. (git-worktree-mode)
  1623. (current-buffer))))
  1624. ;; ((:worktree "/Users/10sr/.dotfiles" :head "5e7457a8d49ef6a517cdf39d038ba5fdf98dc68e" :branch "refs/heads/master") (:worktree "/Users/10sr/.dotfiles/b1" :head "fa7d868076d807692e35f82ae23596c903fd1117" :branch "refs/heads/b1"))
  1625. (defun git-worktree--set-tabulated-list-mode-variables ()
  1626. "Set variables for `tabulated-list-mode'."
  1627. (let ((trees (git-worktree-get-current-trees)))
  1628. (setq tabulated-list-entries
  1629. (mapcar (lambda (e)
  1630. (list e
  1631. (vector
  1632. (concat (file-relative-name (plist-get e :worktree))
  1633. "/")
  1634. (or (plist-get e :branch) "")
  1635. (plist-get e :head)
  1636. )))
  1637. trees))
  1638. (let ((branch-max-size
  1639. (apply 'max
  1640. (cl-loop for e in tabulated-list-entries
  1641. collect (length (elt (cadr e) 1)))))
  1642. (worktree-max-size
  1643. (apply 'max
  1644. (length "Worktree")
  1645. (cl-loop for e in tabulated-list-entries
  1646. collect (length (elt (cadr e) 0))))))
  1647. (setq tabulated-list-format
  1648. `[
  1649. ("Worktree" ,worktree-max-size t)
  1650. ("Branch" ,branch-max-size t)
  1651. ("Head" -1 t)
  1652. ]))))
  1653. (defun git-worktree-open (&optional directory)
  1654. "Open git worktree list buffer.
  1655. If optional arg DIRECTORY is given change current directory to there before
  1656. initializing."
  1657. (interactive)
  1658. (let ((bf (git-worktree-open-noselect directory)))
  1659. (pop-to-buffer bf)))
  1660. (defalias 'git-worktree 'git-worktree-open)
  1661. (defun git-worktree-mode-go ()
  1662. "Go to worktree directory at point."
  1663. (interactive)
  1664. (let* ((id (tabulated-list-get-id))
  1665. (path (plist-get id :worktree)))
  1666. (cl-assert path nil "No worktree info at point")
  1667. (cl-assert (file-directory-p path) t "Directory not found")
  1668. (dired path)))
  1669. (defun git-worktree-mode-move ()
  1670. "Move worktree at point to a new location."
  1671. (interactive)
  1672. (let* ((id (tabulated-list-get-id))
  1673. (path (plist-get id :worktree)))
  1674. (cl-assert path nil "No worktree info at point")
  1675. (cl-assert (file-directory-p path) t "Directory not found")
  1676. (let ((new (read-file-name (format "New name for worktree \"%s\": "
  1677. path))))
  1678. (with-temp-buffer
  1679. (git-worktree--call-process "worktree"
  1680. "move"
  1681. path
  1682. (expand-file-name new)))
  1683. (revert-buffer))))
  1684. (defun git-worktree-mode-add ()
  1685. "Add new git worktree."
  1686. (interactive)
  1687. (let* ((path (read-file-name "Path of new worktree: "))
  1688. (commitish (read-string (format "Commitish to checkout to \"%s\" (Empty to omit): "
  1689. path)))
  1690. (args (append '("worktree" "add")
  1691. (if (string= "" commitish)
  1692. (list (expand-file-name path))
  1693. (list (expand-file-name path) commitish)))))
  1694. (with-temp-buffer
  1695. (apply 'git-worktree--call-process args))
  1696. (revert-buffer)))
  1697. (defun git-worktree-mode-remove ()
  1698. "Remove worktree at point."
  1699. (interactive)
  1700. (let* ((id (tabulated-list-get-id))
  1701. (path (plist-get id :worktree)))
  1702. (cl-assert path nil "No worktree info at point")
  1703. (cl-assert (file-directory-p path) t "Directory not found")
  1704. (when (yes-or-no-p (format "Remove workking directory \"%s\": "
  1705. path))
  1706. (with-temp-buffer
  1707. (git-worktree--call-process "worktree"
  1708. "remove"
  1709. path))
  1710. (revert-buffer))))
  1711. (defvar git-worktree-mode-map
  1712. (let ((map (make-sparse-keymap)))
  1713. (suppress-keymap map)
  1714. (define-key map "A" 'git-worktree-mode-add)
  1715. (define-key map (kbd "C-m") 'git-worktree-mode-go)
  1716. (define-key map "R" 'git-worktree-mode-move)
  1717. (define-key map "D" 'git-worktree-mode-remove)
  1718. ;; (define-key map (kbd "C-g") 'git-worktree-mode-close)
  1719. (define-key map "/" 'isearch-forward)
  1720. map))
  1721. (define-derived-mode git-worktree-mode tabulated-list-mode "Git-Worktrees"
  1722. "Major mode for browsing recently opened files and directories."
  1723. (setq tabulated-list-padding 2)
  1724. (add-hook 'tabulated-list-revert-hook
  1725. 'git-worktree--set-tabulated-list-mode-variables
  1726. nil
  1727. t)
  1728. (tabulated-list-init-header)
  1729. (tabulated-list-print nil nil))
  1730. ;;;;;;;;;;;;;;;;;;;;;;;
  1731. ;; with-venv
  1732. (defvar-local with-venv-venv-dir
  1733. nil
  1734. "Venv directory path.
  1735. This variable is intended to be explicitly set by user.
  1736. When nil, `with-venv' tries to find suitable venv dir.
  1737. When this variable is set , use this value without checking if it is a valid
  1738. python environment.")
  1739. (defmacro with-venv-dir (dir &rest body)
  1740. "Set python environment to DIR and execute BODY.
  1741. This macro does not check if DIR is a valid python environemnt.
  1742. If dir is nil, execute BODY as usual."
  1743. `(let ((--with-venv-process-environment-orig (cl-copy-list process-environment))
  1744. (--with-venv-exec-path-orig (cl-copy-list exec-path)))
  1745. (unwind-protect
  1746. (progn
  1747. (when ,dir
  1748. (let* ((dir (file-name-as-directory ,dir))
  1749. (bin (expand-file-name "bin" dir)))
  1750. ;; Do the same thing that bin/activate does
  1751. (setq exec-path
  1752. (cons bin
  1753. exec-path))
  1754. (setenv "VIRTUAL_ENV" dir)
  1755. (setenv "PATH" (concat bin ":" (or (getenv "PATH") "")))
  1756. (setenv "PYTHONHOME")))
  1757. ,@body)
  1758. (setq process-environment
  1759. --with-venv-process-environment-orig)
  1760. (setq exec-path
  1761. --with-venv-exec-path-orig))))
  1762. (defvar-local with-venv-previously-used nil
  1763. "Previously used venv dir path.")
  1764. (defmacro with-venv (&rest body)
  1765. "Execute BODY with venv enabled.
  1766. This function tries to find suitable venv dir, or run BODY as usual when no
  1767. suitable environment was found."
  1768. `(with-venv-dir
  1769. ;; If set explicitly use it
  1770. (or with-venv-venv-dir
  1771. ;; Check previously used directory
  1772. (with-venv-check-exists with-venv-previously-used)
  1773. (setq with-venv-previously-used (with-venv-find-venv-dir)))
  1774. ,@body))
  1775. (defun with-venv-find-venv-dir (&optional dir)
  1776. "Try to find venv dir for DIR.
  1777. If none found return nil."
  1778. (with-temp-buffer
  1779. (when dir
  1780. (cd dir))
  1781. (or
  1782. ;; Check pipenv
  1783. (with-venv-check-exists (with-venv--find-venv-dir-pipenv))
  1784. ;; Check poetry
  1785. (with-venv-check-exists (with-venv--find-venv-dir-poetry))
  1786. ;; Search for .venv dir
  1787. (with-venv-check-exists (with-venv--find-venv-dir-by-name)))))
  1788. (defun with-venv--find-venv-dir-pipenv ()
  1789. "Try to find venv dir via pipenv."
  1790. (with-temp-buffer
  1791. (let ((status (call-process "pipenv" nil t nil "--venv")))
  1792. (when (eq status 0)
  1793. (goto-char (point-min))
  1794. (buffer-substring-no-properties (point-at-bol)
  1795. (point-at-eol))))))
  1796. (defun with-venv--find-venv-dir-poetry ()
  1797. "Try to find venv dir via poetry."
  1798. (with-temp-buffer
  1799. ;; TODO: Use poetry env info --path
  1800. (let ((status (call-process "poetry" nil t nil "debug:info")))
  1801. (when (eq status 0)
  1802. (goto-char (point-min))
  1803. (save-match-data
  1804. (when (re-search-forward "^ \\* Path: *\\(.*\\)$")
  1805. (match-string 1)))))))
  1806. (defun with-venv--find-venv-dir-by-name ()
  1807. "Try to find venv dir by its name."
  1808. (let ((dir (locate-dominating-file default-directory
  1809. ".venv")))
  1810. (when dir
  1811. ;; TODO: check with -check-exists
  1812. (expand-file-name ".venv"
  1813. dir))))
  1814. (defun with-venv-check-exists (dir)
  1815. "Return DIR as is if \"bin\" directory was found under DIR."
  1816. (and dir
  1817. (file-directory-p (expand-file-name "bin"
  1818. dir))
  1819. dir))
  1820. (defun with-venv-advice-add (func)
  1821. "Setup advice so that FUNC uses `with-env' macro when executing."
  1822. (advice-add func
  1823. :around
  1824. 'with-venv--advice-around))
  1825. (defun with-venv-advice-remove (func)
  1826. "Remove advice added by `with-venv-advice-add'."
  1827. (advice-remove func
  1828. 'with-venv--advice-around))
  1829. (defun with-venv--advice-around (orig-func &rest args)
  1830. "Function to be used to advice functions with `with-venv-advice-add'.
  1831. When a function is adviced with this function, it is wrapped with `with-venv'."
  1832. (with-venv
  1833. (apply orig-func args)))
  1834. (with-eval-after-load 'blacken
  1835. (with-venv-advice-add 'blacken-call-bin))
  1836. ;; (with-venv (:dir default-directory) (message "a"))
  1837. ;; (with-venv () (message "a"))
  1838. ;; Local Variables:
  1839. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1840. ;; flycheck-checker: emacs-lisp
  1841. ;; End:
  1842. ;;; emancs.el ends here