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 72 KiB

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