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.
 
 
 
 
 
 

2253 lines
74 KiB

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