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

2267 linhas
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)
  935. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  936. (set-variable 'remember-notes-initial-major-mode
  937. 'change-log-mode)
  938. (with-eval-after-load 'magit-files
  939. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  940. ;; existing keybindings.
  941. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  942. ;; to disable thie mode here, but do anyway.
  943. ;; See also https://github.com/magit/magit/issues/3517
  944. (global-magit-file-mode -1))
  945. (with-eval-after-load 'magit-section
  946. (set-face-background 'magit-section-highlight
  947. nil))
  948. (with-eval-after-load 'magit-diff
  949. (set-face-background 'magit-diff-added-highlight
  950. nil)
  951. (set-face-background 'magit-diff-removed-highlight
  952. nil)
  953. (set-face-background 'magit-diff-context-highlight
  954. nil)
  955. )
  956. (when (boundp 'git-rebase-filename-regexp)
  957. (add-to-list 'auto-mode-alist
  958. `(,git-rebase-filename-regexp . text-mode)))
  959. (when (safe-require-or-eval 'aggressive-indent)
  960. (defvar aggressive-indent-excluded-modes)
  961. (setq aggressive-indent-excluded-modes
  962. `(diff-mode
  963. toml-mode
  964. conf-mode
  965. dockerfile-mode
  966. groovy-mode
  967. scala-mode
  968. ,@aggressive-indent-excluded-modes))
  969. (global-aggressive-indent-mode 1))
  970. (when (autoload-eval-lazily 'ggtags)
  971. (add-hook 'c-mode-common-hook
  972. (lambda ()
  973. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  974. (ggtags-mode 1))))
  975. (add-hook 'python-mode-hook
  976. (lambda ()
  977. (ggtags-mode 1))))
  978. (when (autoload-eval-lazily 'imenu-list)
  979. ;; (set-variable 'imenu-list-auto-resize t)
  980. (set-variable 'imenu-list-focus-after-activation t)
  981. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  982. (add-hook 'emacs-lisp-mode-hook
  983. (lambda ()
  984. (setq imenu-generic-expression
  985. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  986. ,@imenu-generic-expression))))
  987. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  988. (with-eval-after-load 'compile
  989. (defvar compilation-filter-start)
  990. (defvar compilation-error-regexp-alist)
  991. (require 'ansi-color)
  992. (add-hook 'compilation-filter-hook
  993. (lambda ()
  994. (let ((inhibit-read-only t))
  995. (ansi-color-apply-on-region compilation-filter-start
  996. (point)))))
  997. (add-to-list 'compilation-error-regexp-alist
  998. ;; ansible-lint
  999. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2))
  1000. (add-to-list 'compilation-error-regexp-alist
  1001. ;; pydocstyle
  1002. '("^\\([^ \n]+\\):\\([0-9]+\\) " 1 2))
  1003. )
  1004. ;; Workaround to avoid ensime error
  1005. (defvar ensime-mode-key-prefix nil)
  1006. (when (safe-require-or-eval 'company)
  1007. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  1008. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  1009. (global-company-mode)
  1010. (set-variable 'company-idle-delay nil)
  1011. (set-variable 'company-minimum-prefix-length 2)
  1012. (set-variable 'company-selection-wrap-around t)
  1013. (defvar company-mode-map)
  1014. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  1015. (with-eval-after-load 'python
  1016. (defvar python-indent-trigger-commands)
  1017. ;; TODO: This disables completion in puthon?
  1018. (add-to-list 'python-indent-trigger-commands
  1019. 'company-indent-or-complete-common))
  1020. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  1021. (defvar company-active-map)
  1022. (define-key company-active-map (kbd "C-n") 'company-select-next)
  1023. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  1024. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  1025. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  1026. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  1027. (defvar company-mode)
  1028. (defvar company-candidates)
  1029. (defvar company-candidates-length)
  1030. ;; (popup-tip "Hello, World!")
  1031. (defun my-company-lighter-current-length ()
  1032. "Get current candidate length."
  1033. (interactive)
  1034. (let ((l nil)
  1035. (inhibit-message t))
  1036. (when (and company-mode
  1037. (not (minibufferp))
  1038. ;; Do nothing when already in company completion
  1039. (not company-candidates))
  1040. ;; FIXME: Somehow it cannto catch errors from ggtags
  1041. (ignore-errors
  1042. ;; (company-auto-begin)
  1043. (company-manual-begin)
  1044. (setq l company-candidates-length)
  1045. (company-cancel)))
  1046. (if l
  1047. (format "[%d]" l)
  1048. "")))
  1049. (defvar company-lighter)
  1050. (set-variable 'company-lighter-base "Cmp")
  1051. ;; (add-to-list 'company-lighter
  1052. ;; '(:eval (my-company-lighter-current-length))
  1053. ;; t)
  1054. ;; This breaks japanese text input
  1055. ;; (set-variable 'my-company-length-popup-tip-timer
  1056. ;; (run-with-idle-timer 0.2 t
  1057. ;; 'my-company-length-popup-tip))
  1058. ;; (current-active-maps)
  1059. ;; (lookup-key)
  1060. '(mapcar (lambda (map)
  1061. (lookup-key map (kbd "C-i")))
  1062. (current-active-maps))
  1063. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  1064. (set-face-attribute 'company-tooltip nil
  1065. :foreground "black" :background "lightgrey")
  1066. (set-face-attribute 'company-tooltip-common nil
  1067. :foreground "black" :background "lightgrey")
  1068. (set-face-attribute 'company-tooltip-common-selection nil
  1069. :foreground "white" :background "steelblue")
  1070. (set-face-attribute 'company-tooltip-selection nil
  1071. :foreground "black" :background "steelblue")
  1072. (set-face-attribute 'company-preview-common nil
  1073. :background nil :foreground "lightgrey" :underline t)
  1074. (set-face-attribute 'company-scrollbar-fg nil
  1075. :background "orange")
  1076. (set-face-attribute 'company-scrollbar-bg nil
  1077. :background "gray40")
  1078. )
  1079. ;; https://github.com/lunaryorn/flycheck
  1080. (when (safe-require-or-eval 'flycheck)
  1081. (call-after-init (global-flycheck-mode)))
  1082. (with-eval-after-load 'flycheck
  1083. (when (fboundp 'flycheck-black-check-setup)
  1084. (flycheck-black-check-setup)))
  1085. (set-variable 'flycheck-python-mypy-ini
  1086. ".mypy.ini")
  1087. (when (autoload-eval-lazily 'ilookup)
  1088. (define-key ctl-x-map "d" 'ilookup-open-word))
  1089. (set-variable 'ac-ignore-case nil)
  1090. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  1091. (define-key ctl-x-map "t" 'term-run-shell-command))
  1092. (add-to-list 'safe-local-variable-values
  1093. '(encoding utf-8))
  1094. (setq enable-local-variables :safe)
  1095. ;; Detect file type from shebang and set major-mode.
  1096. (add-to-list 'interpreter-mode-alist
  1097. '("python3" . python-mode))
  1098. (add-to-list 'interpreter-mode-alist
  1099. '("python2" . python-mode))
  1100. (with-eval-after-load 'python
  1101. (defvar python-mode-map (make-sparse-keymap))
  1102. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1103. (when (autoload-eval-lazily 'pipenv)
  1104. ;; (declare-function pipenv-projectile-after-switch-default "pipenv")
  1105. ;; (add-hook 'python-mode-hook
  1106. ;; (lambda ()
  1107. ;; (pipenv-mode 1)
  1108. ;; (pipenv-projectile-after-switch-default)))
  1109. )
  1110. (set-variable 'flycheck-python-pycompile-executable "python3")
  1111. (set-variable 'python-indent-guess-indent-offset nil)
  1112. (with-eval-after-load 'blacken
  1113. (when (require 'with-venv nil t)
  1114. (with-venv-advice-add 'blacken-buffer)))
  1115. ;; http://fukuyama.co/foreign-regexp
  1116. '(and (safe-require-or-eval 'foreign-regexp)
  1117. (progn
  1118. (setq foreign-regexp/regexp-type 'perl)
  1119. '(setq reb-re-syntax 'foreign-regexp)
  1120. ))
  1121. (autoload-eval-lazily 'sql '(sql-mode)
  1122. (require 'sql-indent nil t))
  1123. (add-to-list 'auto-mode-alist
  1124. '("\\.hql\\'" . sql-mode))
  1125. (when (autoload-eval-lazily 'git-command)
  1126. (define-key ctl-x-map "g" 'git-command))
  1127. (when (autoload-eval-lazily 'gited)
  1128. (define-key ctl-x-map (kbd "C-g") 'gited-list))
  1129. (when (safe-require-or-eval 'git-commit)
  1130. (global-git-commit-mode 1))
  1131. (with-eval-after-load 'git-commit
  1132. (add-hook 'git-commit-setup-hook
  1133. 'turn-off-auto-fill t))
  1134. (autoload-eval-lazily 'sl)
  1135. (with-eval-after-load 'rst
  1136. (defvar rst-mode-map)
  1137. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1138. (with-eval-after-load 'jdee
  1139. (add-hook 'jdee-mode-hook
  1140. (lambda ()
  1141. (make-local-variable 'global-mode-string)
  1142. (add-to-list 'global-mode-string
  1143. mode-line-position))))
  1144. ;; Cannot enable error thrown. Why???
  1145. ;; https://github.com/m0smith/malabar-mode#Installation
  1146. ;; (when (autoload-eval-lazily 'malabar-mode)
  1147. ;; (add-to-list 'load-path
  1148. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1149. ;; (safe-require-or-eval 'cedet-devel-load)
  1150. ;; (call-after-init (activate-malabar-mode)))
  1151. (with-eval-after-load 'make-mode
  1152. (defvar makefile-mode-map (make-sparse-keymap))
  1153. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1154. ;; this functions is set in write-file-functions, i cannot find any
  1155. ;; good way to remove this.
  1156. (fset 'makefile-warn-suspicious-lines 'ignore))
  1157. (with-eval-after-load 'verilog-mode
  1158. (defvar verilog-mode-map (make-sparse-keymap))
  1159. (define-key verilog-mode-map ";" 'self-insert-command))
  1160. (setq diff-switches "-u")
  1161. (with-eval-after-load 'diff-mode
  1162. ;; (when (and (eq major-mode
  1163. ;; 'diff-mode)
  1164. ;; (not buffer-file-name))
  1165. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1166. ;; (view-mode 1))
  1167. (set-face-attribute 'diff-header nil
  1168. :foreground nil
  1169. :background nil
  1170. :weight 'bold)
  1171. (set-face-attribute 'diff-file-header nil
  1172. :foreground nil
  1173. :background nil
  1174. :weight 'bold)
  1175. (set-face-foreground 'diff-index "blue")
  1176. (set-face-attribute 'diff-hunk-header nil
  1177. :foreground "cyan"
  1178. :weight 'normal)
  1179. (set-face-attribute 'diff-context nil
  1180. ;; :foreground "white"
  1181. :foreground nil
  1182. :weight 'normal)
  1183. (set-face-foreground 'diff-removed "red")
  1184. (set-face-foreground 'diff-added "green")
  1185. (set-face-background 'diff-removed nil)
  1186. (set-face-background 'diff-added nil)
  1187. (set-face-attribute 'diff-changed nil
  1188. :foreground "magenta"
  1189. :weight 'normal)
  1190. (set-face-attribute 'diff-refine-changed nil
  1191. :foreground nil
  1192. :background nil
  1193. :weight 'bold
  1194. :inverse-video t)
  1195. ;; Annoying !
  1196. ;;(diff-auto-refine-mode)
  1197. )
  1198. ;; (ffap-bindings)
  1199. (set-variable 'browse-url-browser-function
  1200. 'eww-browse-url)
  1201. (set-variable 'sh-here-document-word "__EOC__")
  1202. (when (autoload-eval-lazily 'adoc-mode
  1203. nil
  1204. (defvar adoc-mode-map (make-sparse-keymap))
  1205. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1206. (setq auto-mode-alist
  1207. `(("\\.adoc\\'" . adoc-mode)
  1208. ("\\.asciidoc\\'" . adoc-mode)
  1209. ,@auto-mode-alist)))
  1210. (with-eval-after-load 'markup-faces
  1211. ;; Is this too match ?
  1212. (set-face-foreground 'markup-meta-face
  1213. "color-245")
  1214. (set-face-foreground 'markup-meta-hide-face
  1215. "color-245")
  1216. )
  1217. ;; TODO: check if this is required
  1218. (when (autoload-eval-lazily 'groovy-mode nil
  1219. (defvar groovy-mode-map (make-sparse-keymap))
  1220. (define-key groovy-mode-map "(" 'self-insert-command)
  1221. (define-key groovy-mode-map ")" 'self-insert-command)
  1222. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1223. )
  1224. (add-to-list 'auto-mode-alist
  1225. '("build\\.gradle\\'" . groovy-mode)))
  1226. (add-to-list 'auto-mode-alist
  1227. '("\\.gawk\\'" . awk-mode))
  1228. (with-eval-after-load 'yaml-mode
  1229. (defvar yaml-mode-map (make-sparse-keymap))
  1230. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1231. (with-eval-after-load 'html-mode
  1232. (defvar html-mode-map (make-sparse-keymap))
  1233. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1234. (with-eval-after-load 'text-mode
  1235. (define-key text-mode-map (kbd "C-m") 'newline))
  1236. (autoload-eval-lazily 'info nil
  1237. (defvar Info-additional-directory-list)
  1238. (dolist (dir (directory-files (concat user-emacs-directory
  1239. "info")
  1240. t
  1241. "^[^.].*"))
  1242. (when (file-directory-p dir)
  1243. (add-to-list 'Info-additional-directory-list
  1244. dir)))
  1245. (let ((dir (expand-file-name "~/.brew/share/info")))
  1246. (when (file-directory-p dir)
  1247. (add-to-list 'Info-additional-directory-list
  1248. dir))))
  1249. (with-eval-after-load 'apropos
  1250. (defvar apropos-mode-map (make-sparse-keymap))
  1251. (define-key apropos-mode-map "n" 'next-line)
  1252. (define-key apropos-mode-map "p" 'previous-line))
  1253. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1254. ;; (define-key isearch-mode-map
  1255. ;; (kbd "C-j") 'isearch-other-control-char)
  1256. ;; (define-key isearch-mode-map
  1257. ;; (kbd "C-k") 'isearch-other-control-char)
  1258. ;; (define-key isearch-mode-map
  1259. ;; (kbd "C-h") 'isearch-other-control-char)
  1260. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1261. (define-key isearch-mode-map (kbd "M-r")
  1262. 'isearch-query-replace-regexp)
  1263. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1264. (setq lazy-highlight-cleanup nil)
  1265. ;; face for isearch highlighing
  1266. (set-face-attribute 'lazy-highlight
  1267. nil
  1268. :foreground `unspecified
  1269. :background `unspecified
  1270. :underline t
  1271. ;; :weight `bold
  1272. )
  1273. (add-hook 'outline-mode-hook
  1274. (lambda ()
  1275. (when (string-match "\\.md\\'" buffer-file-name)
  1276. (set (make-local-variable 'outline-regexp) "#+ "))))
  1277. (add-hook 'outline-mode-hook
  1278. 'outline-show-all)
  1279. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1280. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1281. (when (autoload-eval-lazily 'markdown-mode
  1282. '(markdown-mode gfm-mode)
  1283. (defvar gfm-mode-map (make-sparse-keymap))
  1284. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  1285. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1286. (set-variable 'markdown-command (or (executable-find "markdown")
  1287. (executable-find "markdown.pl")
  1288. ""))
  1289. (add-hook 'markdown-mode-hook
  1290. (lambda ()
  1291. (outline-minor-mode 1)
  1292. (flyspell-mode)
  1293. (set (make-local-variable 'comment-start) ";")))
  1294. )
  1295. ;; c-mode
  1296. ;; http://www.emacswiki.org/emacs/IndentingC
  1297. ;; http://en.wikipedia.org/wiki/Indent_style
  1298. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1299. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1300. (with-eval-after-load 'cc-vars
  1301. (defvar c-default-style nil)
  1302. (add-to-list 'c-default-style
  1303. '(c-mode . "k&r"))
  1304. (add-to-list 'c-default-style
  1305. '(c++-mode . "k&r")))
  1306. (autoload-eval-lazily 'js2-mode nil
  1307. ;; currently do not use js2-mode
  1308. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1309. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1310. ;; (defvar js2-mode-map (make-sparse-keymap))
  1311. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1312. ;; (interactive)
  1313. ;; (js2-enter-key)
  1314. ;; (indent-for-tab-command)))
  1315. ;; (add-hook (kill-local-variable 'before-save-hook)
  1316. ;; 'js2-before-save)
  1317. ;; (add-hook 'before-save-hook
  1318. ;; 'my-indent-buffer
  1319. ;; nil
  1320. ;; t)
  1321. )
  1322. (add-to-list 'interpreter-mode-alist
  1323. '("node" . js-mode))
  1324. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1325. (with-eval-after-load 'uniquify
  1326. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1327. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1328. (setq uniquify-min-dir-content 1))
  1329. (with-eval-after-load 'view
  1330. (defvar view-mode-map (make-sparse-keymap))
  1331. (define-key view-mode-map "j" 'scroll-up-line)
  1332. (define-key view-mode-map "k" 'scroll-down-line)
  1333. (define-key view-mode-map "v" 'toggle-read-only)
  1334. (define-key view-mode-map "q" 'bury-buffer)
  1335. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1336. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1337. ;; (define-key view-mode-map
  1338. ;; "n" 'nonincremental-repeat-search-forward)
  1339. ;; (define-key view-mode-map
  1340. ;; "N" 'nonincremental-repeat-search-backward)
  1341. ;; N conflicts with git-walktree
  1342. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1343. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1344. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1345. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1346. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1347. (global-set-key "\M-r" 'view-mode)
  1348. ;; (setq view-read-only t)
  1349. (with-eval-after-load 'term
  1350. (defvar term-raw-map (make-sparse-keymap))
  1351. (define-key term-raw-map (kbd "C-x")
  1352. (lookup-key (current-global-map)
  1353. (kbd "C-x"))))
  1354. (add-hook 'term-mode-hook
  1355. (lambda ()
  1356. ;; Stop current line highlighting
  1357. (set (make-local-variable (defvar hl-line-range-function))
  1358. (lambda () '(0 . 0)))
  1359. (set (make-local-variable 'scroll-margin)
  1360. 0)
  1361. (set-variable 'term-buffer-maximum-size 20480)
  1362. ))
  1363. (add-hook 'Man-mode-hook
  1364. (lambda ()
  1365. (view-mode 1)
  1366. (setq truncate-lines nil)))
  1367. (set-variable 'Man-notify-method (if window-system
  1368. 'newframe
  1369. 'aggressive))
  1370. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1371. "woman_cache.el")))
  1372. ;; not work because man.el will be loaded when man called
  1373. (defalias 'man 'woman)
  1374. (add-to-list 'auto-mode-alist
  1375. '("tox\\.ini\\'" . conf-unix-mode))
  1376. (when (autoload-eval-lazily 'toml-mode)
  1377. (add-to-list 'auto-mode-alist
  1378. '("/tox\\.ini\\'" . toml-mode))
  1379. (add-to-list 'auto-mode-alist
  1380. '("/Pipfile\\'" . toml-mode))
  1381. (add-to-list 'auto-mode-alist
  1382. '("/poetry\\.lock\\'" . toml-mode))
  1383. )
  1384. (when (autoload-eval-lazily 'json-mode)
  1385. (add-to-list 'auto-mode-alist
  1386. '("/Pipfile\\.lock\\'" . json-mode)))
  1387. (add-hook 'go-mode-hook
  1388. (lambda()
  1389. (defvar go-mode-map)
  1390. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1391. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1392. (when (autoload-eval-lazily 'k8s-mode)
  1393. (add-to-list 'auto-mode-alist
  1394. '("\\.k8s\\'" . k8s-mode)))
  1395. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1396. ;; buffers
  1397. (defvar bs-configurations)
  1398. (declare-function bs-set-configuration "bs")
  1399. (declare-function bs-refresh "bs")
  1400. (declare-function bs-message-without-log "bs")
  1401. (declare-function bs--current-config-message "bs")
  1402. (when (autoload-eval-lazily 'bs '(bs-show)
  1403. (add-to-list 'bs-configurations
  1404. '("specials" "^\\*" nil ".*" nil nil))
  1405. (add-to-list 'bs-configurations
  1406. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1407. (defvar bs-mode-map)
  1408. (defvar bs-current-configuration)
  1409. (define-key bs-mode-map (kbd "t")
  1410. ;; TODO: fix toggle feature
  1411. (lambda ()
  1412. (interactive)
  1413. (if (string= "specials"
  1414. bs-current-configuration)
  1415. (bs-set-configuration "files")
  1416. (bs-set-configuration "specials"))
  1417. (bs-refresh)
  1418. (bs-message-without-log "%s"
  1419. (bs--current-config-message))))
  1420. ;; (setq bs-configurations (list
  1421. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1422. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1423. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1424. )
  1425. (defalias 'list-buffers 'bs-show)
  1426. (set-variable 'bs-default-configuration "files-and-specials")
  1427. (set-variable 'bs-default-sort-name "by nothing")
  1428. (add-hook 'bs-mode-hook
  1429. (lambda ()
  1430. (set (make-local-variable 'scroll-margin) 0))))
  1431. ;;(iswitchb-mode 1)
  1432. (icomplete-mode)
  1433. (defun iswitchb-buffer-display-other-window ()
  1434. "Do iswitchb in other window."
  1435. (interactive)
  1436. (let ((iswitchb-default-method 'display))
  1437. (call-interactively 'iswitchb-buffer)))
  1438. ;; buffer killing
  1439. ;; (defun my-delete-window-killing-buffer () nil)
  1440. (defun my-query-kill-current-buffer ()
  1441. "Interactively kill current buffer."
  1442. (interactive)
  1443. (if (y-or-n-p (concat "kill current buffer? :"))
  1444. (kill-buffer (current-buffer))))
  1445. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1446. (substitute-key-definition 'kill-buffer
  1447. 'my-query-kill-current-buffer
  1448. global-map)
  1449. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1450. ;; dired
  1451. (defun my-file-head (filename &optional n)
  1452. "Return list of first N lines of file FILENAME."
  1453. ;; TODO: Fix for janapese text
  1454. ;; TODO: Fix for short text
  1455. (let ((num (or n 10))
  1456. (size 100)
  1457. (beg 0)
  1458. (end 0)
  1459. (result '())
  1460. (read -1))
  1461. (with-temp-buffer
  1462. (erase-buffer)
  1463. (while (or (<= (count-lines (point-min)
  1464. (point-max))
  1465. num)
  1466. (eq read 0))
  1467. (setq end (+ beg size))
  1468. (setq read (nth 1 (insert-file-contents-literally filename
  1469. nil
  1470. beg
  1471. end)))
  1472. (goto-char (point-max))
  1473. (setq beg (+ beg size)))
  1474. (goto-char (point-min))
  1475. (while (< (length result) num)
  1476. (let ((start (point)))
  1477. (forward-line 1)
  1478. (setq result
  1479. `(,@result ,(buffer-substring-no-properties start
  1480. (point))))))
  1481. result
  1482. ;; (buffer-substring-no-properties (point-min)
  1483. ;; (progn
  1484. ;; (forward-line num)
  1485. ;; (point)))
  1486. )))
  1487. ;; (apply 'concat (my-file-head "./shrc" 10)
  1488. (defun my-dired-echo-file-head (arg)
  1489. "Echo head of current file.
  1490. ARG is num to show, or defaults to 7."
  1491. (interactive "P")
  1492. (let ((f (dired-get-filename)))
  1493. (message "%s"
  1494. (apply 'concat
  1495. (my-file-head f
  1496. 7)))))
  1497. (defun my-dired-diff ()
  1498. "Show diff of marked file and file of current line."
  1499. (interactive)
  1500. (let ((files (dired-get-marked-files nil nil nil t)))
  1501. (if (eq (car files)
  1502. t)
  1503. (diff (cadr files) (dired-get-filename))
  1504. (message "One file must be marked!"))))
  1505. (defun dired-get-file-info ()
  1506. "Print information of current line file."
  1507. (interactive)
  1508. (let ((f (shell-quote-argument (dired-get-filename t))))
  1509. (if (file-directory-p f)
  1510. (progn
  1511. (message "Calculating disk usage...")
  1512. (shell-command (concat "du -hsD "
  1513. f)))
  1514. (shell-command (concat "file "
  1515. f)))))
  1516. (defun my-dired-scroll-up ()
  1517. "Scroll up."
  1518. (interactive)
  1519. (my-dired-previous-line (- (window-height) 1)))
  1520. (defun my-dired-scroll-down ()
  1521. "Scroll down."
  1522. (interactive)
  1523. (my-dired-next-line (- (window-height) 1)))
  1524. ;; (defun my-dired-forward-line (arg)
  1525. ;; ""
  1526. ;; (interactive "p"))
  1527. (defun my-dired-previous-line (arg)
  1528. "Move ARG lines up."
  1529. (interactive "p")
  1530. (if (> arg 0)
  1531. (progn
  1532. (if (eq (line-number-at-pos)
  1533. 1)
  1534. (goto-char (point-max))
  1535. (forward-line -1))
  1536. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1537. (dired-get-subdir))
  1538. (- arg 1)
  1539. arg)))
  1540. (dired-move-to-filename)))
  1541. (defun my-dired-next-line (arg)
  1542. "Move ARG lines down."
  1543. (interactive "p")
  1544. (if (> arg 0)
  1545. (progn
  1546. (if (eq (point)
  1547. (point-max))
  1548. (goto-char (point-min))
  1549. (forward-line 1))
  1550. (my-dired-next-line (if (or (dired-get-filename nil t)
  1551. (dired-get-subdir))
  1552. (- arg 1)
  1553. arg)))
  1554. (dired-move-to-filename)))
  1555. (defun my-tramp-remote-find-file (f)
  1556. "Open F."
  1557. (interactive (list (read-file-name "My Find File Tramp: "
  1558. "/scp:"
  1559. nil ;; "/scp:"
  1560. (confirm-nonexistent-file-or-buffer))))
  1561. (find-file f))
  1562. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1563. (if (eq window-system 'mac)
  1564. (setq dired-listing-switches "-lhF")
  1565. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1566. )
  1567. (setq dired-listing-switches "-lhF")
  1568. ;; when using dired-find-alternate-file
  1569. ;; reuse current dired buffer for the file to open
  1570. ;; (put 'dired-find-alternate-file 'disabled nil)
  1571. (set-variable 'dired-ls-F-marks-symlinks t)
  1572. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1573. (set-variable 'ls-lisp-dirs-first t)
  1574. (set-variable 'ls-lisp-use-localized-time-format t)
  1575. (set-variable 'ls-lisp-format-time-list
  1576. '("%Y-%m-%d %H:%M"
  1577. "%Y-%m-%d "))
  1578. (set-variable 'dired-dwim-target t)
  1579. (set-variable 'dired-isearch-filenames t)
  1580. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1581. (set-variable 'dired-hide-details-hide-information-lines nil)
  1582. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1583. (set-variable 'dired-recursive-deletes 'always)
  1584. ;; (add-hook 'dired-after-readin-hook
  1585. ;; 'my-replace-nasi-none)
  1586. (with-eval-after-load 'dired
  1587. (safe-require-or-eval 'ls-lisp)
  1588. (defvar dired-mode-map (make-sparse-keymap))
  1589. ;; dired-do-chgrp sometimes cause system hung
  1590. (define-key dired-mode-map "G" 'ignore)
  1591. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1592. (define-key dired-mode-map "i" 'dired-get-file-info)
  1593. ;; (define-key dired-mode-map "f" 'find-file)
  1594. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1595. (define-key dired-mode-map "z" 'fzf)
  1596. (define-key dired-mode-map "!" 'shell-command)
  1597. (define-key dired-mode-map "&" 'async-shell-command)
  1598. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1599. (define-key dired-mode-map "=" 'my-dired-diff)
  1600. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1601. (define-key dired-mode-map "b" 'gtkbm)
  1602. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1603. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1604. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1605. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1606. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1607. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1608. (substitute-key-definition 'dired-next-line
  1609. 'my-dired-next-line
  1610. dired-mode-map)
  1611. (substitute-key-definition 'dired-previous-line
  1612. 'my-dired-previous-line
  1613. dired-mode-map)
  1614. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1615. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1616. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1617. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1618. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1619. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1620. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1621. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1622. (add-hook 'dired-mode-hook
  1623. (lambda ()
  1624. (when (fboundp 'dired-hide-details-mode)
  1625. (dired-hide-details-mode t)
  1626. (local-set-key "l" 'dired-hide-details-mode))
  1627. (let ((file "._Icon\015"))
  1628. (when nil
  1629. '(file-readable-p file)
  1630. (delete-file file)))))
  1631. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack)
  1632. (defvar pack-program-alist)
  1633. (add-to-list 'pack-program-alist
  1634. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf")))
  1635. (set-variable 'pack-silence
  1636. t)
  1637. (with-eval-after-load 'dired
  1638. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1639. (when (autoload-eval-lazily 'dired-list-all-mode)
  1640. (setq dired-listing-switches "-lhF")
  1641. (with-eval-after-load 'dired
  1642. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1643. (when (autoload-eval-lazily 'dired-filter)
  1644. (add-hook 'dired-mode-hook
  1645. 'dired-filter-mode))
  1646. (set-variable 'dired-filter-stack nil)
  1647. ;; Currently disabled in favor of dired-from-git-ls-files
  1648. ;; (define-key ctl-x-map "f" 'find-dired)
  1649. (defvar my-dired-git-ls-files-history
  1650. "History for `my-dired-git-ls-files'." nil)
  1651. (defun my-dired-git-ls-files (arg)
  1652. "Dired from git ls-files."
  1653. (interactive (list
  1654. (read-shell-command "git ls-files: "
  1655. "git ls-files -z ")))
  1656. (pop-to-buffer-same-window
  1657. (dired-noselect `(,default-directory
  1658. ,@(split-string (shell-command-to-string arg)
  1659. "\0" t))
  1660. ""))
  1661. )
  1662. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  1663. (with-eval-after-load 'dired
  1664. (defvar dired-mode-map (make-sparse-keymap))
  1665. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1666. ;; (define-minor-mode my-dired-glob-filter)
  1667. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1668. ;; misc funcs
  1669. (define-key ctl-x-map "T" 'git-worktree)
  1670. (define-key ctl-x-map "W" 'git-walktree)
  1671. (when (fboundp 'browse-url-default-macosx-browser)
  1672. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1673. (defalias 'qcalc 'quick-calc)
  1674. (defun memo (&optional dir)
  1675. "Open memo.txt in DIR."
  1676. (interactive)
  1677. (pop-to-buffer (find-file-noselect (concat (if dir
  1678. (file-name-as-directory dir)
  1679. "")
  1680. "memo.txt"))))
  1681. ;; TODO: remember-projectile
  1682. (set (defvar my-privnotes-path nil
  1683. "My privnotes repository path.")
  1684. (expand-file-name "~/my/privnotes"))
  1685. (defun my-privnotes-readme (dir)
  1686. "Open my privnotes DIR."
  1687. (interactive (list
  1688. (read-file-name "Privnotes: "
  1689. (expand-file-name (format-time-string "%Y%m%d_")
  1690. my-privnotes-path))))
  1691. (let ((path (expand-file-name "README.md" dir)))
  1692. (with-current-buffer (find-file path)
  1693. (unless (file-exists-p path)
  1694. (insert (file-name-base dir)
  1695. "\n"
  1696. "=======\n"
  1697. "\n\n")))))
  1698. (define-key ctl-x-map "p" 'my-privnotes-readme)
  1699. (set (defvar my-rgrep-alist nil
  1700. "Alist of rgrep command.
  1701. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1702. condition to choose COMMAND when evaluated.")
  1703. `(
  1704. ;; ripgrep
  1705. ("rg"
  1706. (executable-find "rg")
  1707. "rg -nH --no-heading --color=always --hidden --glob '!.git/' --smart-case -M 1280 ")
  1708. ;; git grep
  1709. ("gitgrep"
  1710. (eq 0
  1711. (shell-command "git rev-parse --git-dir"))
  1712. "git --no-pager -c color.grep=always grep -nH -e ")
  1713. ;; sift
  1714. ("sift"
  1715. (executable-find "sift")
  1716. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1717. ;; the silver searcher
  1718. ("ag"
  1719. (executable-find "ag")
  1720. "ag --nogroup --nopager --filename ")
  1721. ;; ack
  1722. ("ack"
  1723. (executable-find "ack")
  1724. "ack --nogroup --nopager --with-filename ")
  1725. ;; gnu global
  1726. ("global"
  1727. (and (require 'ggtags nil t)
  1728. (executable-find "global")
  1729. (ggtags-current-project-root))
  1730. "global --result grep ")
  1731. ;; grep
  1732. ("grep"
  1733. t
  1734. ,(concat "find . "
  1735. "-path '*/.git' -prune -o "
  1736. "-path '*/.svn' -prune -o "
  1737. "-type f -print0 | "
  1738. "xargs -0 grep -nH -e "))
  1739. )
  1740. )
  1741. (defvar my-rgrep-default nil
  1742. "Default command name for my-rgrep.")
  1743. (defun my-rgrep-grep-command (&optional name alist)
  1744. "Return recursive grep command for current directory or nil.
  1745. If NAME is given, use that without testing.
  1746. Commands are searched from ALIST."
  1747. (if alist
  1748. (if name
  1749. ;; if name is given search that from alist and return the command
  1750. (nth 2 (assoc name
  1751. alist))
  1752. ;; if name is not given try test in 1th elem
  1753. (let ((car (car alist))
  1754. (cdr (cdr alist)))
  1755. (if (eval (nth 1 car))
  1756. ;; if the condition is true return the command
  1757. (nth 2 car)
  1758. ;; try next one
  1759. (and cdr
  1760. (my-rgrep-grep-command name cdr)))))
  1761. ;; if alist is not given set default value
  1762. (my-rgrep-grep-command name my-rgrep-alist)))
  1763. (defun my-rgrep (command-args)
  1764. "My recursive grep. Run COMMAND-ARGS.
  1765. If prefix argument is given, use current symbol as default search target
  1766. and search from projectile root (if projectile is available)."
  1767. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1768. nil)))
  1769. (if cmd
  1770. (list (read-shell-command "grep command: "
  1771. (concat cmd
  1772. (if current-prefix-arg
  1773. (thing-at-point 'symbol t)
  1774. ""))
  1775. 'grep-find-history))
  1776. (error "My-Rgrep: Command for rgrep not found")
  1777. )))
  1778. (if (and current-prefix-arg
  1779. (safe-require-or-eval 'projectile)
  1780. (projectile-project-p))
  1781. (projectile-with-default-dir (projectile-project-root)
  1782. (compilation-start command-args
  1783. 'grep-mode))
  1784. (compilation-start command-args
  1785. 'grep-mode)))
  1786. (defun my-rgrep-thing-at-point-projectile-root ()
  1787. "My recursive grep to find thing at point from project root."
  1788. (interactive)
  1789. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1790. nil))
  1791. (command-args
  1792. (if cmd
  1793. (concat cmd
  1794. (or (thing-at-point 'symbol t)
  1795. (error "No symbol at point")))
  1796. (error "My-Rgrep: Command for rgrep not found"))))
  1797. (if (safe-require-or-eval 'projectile)
  1798. (projectile-with-default-dir (or (projectile-project-root)
  1799. default-directory)
  1800. (compilation-start command-args
  1801. 'grep-mode))
  1802. (compilation-start command-args
  1803. 'grep-mode))))
  1804. (defmacro define-my-rgrep (name)
  1805. "Define rgrep for NAME."
  1806. `(defun ,(intern (concat "my-rgrep-"
  1807. name)) ()
  1808. ,(format "My recursive grep by %s."
  1809. name)
  1810. (interactive)
  1811. (let ((my-rgrep-default ,name))
  1812. (if (called-interactively-p 'any)
  1813. (call-interactively 'my-rgrep)
  1814. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1815. )
  1816. (define-my-rgrep "ack")
  1817. (define-my-rgrep "ag")
  1818. (define-my-rgrep "rg")
  1819. (define-my-rgrep "sift")
  1820. (define-my-rgrep "gitgrep")
  1821. (define-my-rgrep "grep")
  1822. (define-my-rgrep "global")
  1823. (define-key ctl-x-map "s" 'my-rgrep)
  1824. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1825. (defun my-occur (regexp &optional region)
  1826. "My occur command to search REGEXP."
  1827. (interactive (list (read-string "List lines matching regexp: "
  1828. (thing-at-point 'symbol t))))
  1829. (occur regexp nil region))
  1830. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1831. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1832. (defalias 'make 'compile)
  1833. (define-key ctl-x-map "c" 'compile)
  1834. (defun my-pushbullet-note (text &optional title)
  1835. "Push TEXT."
  1836. (interactive "sText to Push: ")
  1837. (pb/push-item '("") text "note" (or title "")))
  1838. ;;;;;;;;;;;;;;;;;;;;;
  1839. ;; git-bug
  1840. (defconst git-bug-ls-regexp
  1841. (eval-when-compile
  1842. (rx bol
  1843. (submatch (one-or-more alphanumeric)) ; id
  1844. ;; (one-or-more any)
  1845. (one-or-more space)
  1846. (submatch (or "open" "close")) ; status
  1847. (one-or-more space)
  1848. (submatch (maximal-match (zero-or-more print))) ; title
  1849. "\t"
  1850. (submatch (one-or-more alphanumeric)) ; user
  1851. (one-or-more space)
  1852. "C:"
  1853. (submatch (one-or-more digit)) ; Comment num
  1854. (one-or-more space)
  1855. "L:"
  1856. (submatch (one-or-more digit)) ; Label num
  1857. eol
  1858. ))
  1859. "Regexp to parse line of output of git-bug ls.
  1860. Used by `git-bug-ls'.")
  1861. (defun git-bug-bugs ()
  1862. "Get list of git-bug bugs."
  1863. (with-temp-buffer
  1864. (git-bug--call-process "bug" "ls")
  1865. (goto-char (point-min))
  1866. (let ((bugs nil))
  1867. (while (not (eq (point) (point-max)))
  1868. (save-match-data
  1869. (when (re-search-forward git-bug-ls-regexp (point-at-eol) t)
  1870. (setq bugs `(,@bugs
  1871. ,(list
  1872. :id (match-string 1)
  1873. :status (match-string 2)
  1874. :title (string-trim (match-string 3))
  1875. :user (match-string 4)
  1876. :comment-num (match-string 5)
  1877. :label-num (match-string 6)
  1878. )))))
  1879. (forward-line 1)
  1880. (goto-char (point-at-bol)))
  1881. bugs)))
  1882. (defun git-bug-ls-noselect (&optional directory)
  1883. "Open git bug list buffer.
  1884. If optional arg DIRECTORY is given change current directory to there before
  1885. initializing."
  1886. (setq directory (expand-file-name (or directory
  1887. default-directory)))
  1888. (cl-assert (file-directory-p directory))
  1889. (let* ((root (git-bug--get-repository-root directory))
  1890. (name (file-name-nondirectory root))
  1891. (bname (format "*GitBug<%s>*" name)))
  1892. (with-current-buffer (get-buffer-create bname)
  1893. (cd root)
  1894. (git-bug-ls--set-tabulated-list-mode-variables)
  1895. (git-bug-ls-mode)
  1896. (current-buffer))))
  1897. (defun git-bug--get-repository-root (dir)
  1898. "Resolve repository root of DIR.
  1899. If DIR is not inside of any git repository, signal an error."
  1900. (cl-assert (file-directory-p dir))
  1901. (with-temp-buffer
  1902. (cd dir)
  1903. (git-bug--call-process "rev-parse" "--show-toplevel")
  1904. (goto-char (point-min))
  1905. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  1906. (defun git-bug--call-process (&rest args)
  1907. "Start git process synchronously with ARGS.
  1908. Raise error when git process ends with non-zero status.
  1909. Any output will be written to current buffer."
  1910. (let ((status (apply 'call-process
  1911. "git"
  1912. nil
  1913. t
  1914. nil
  1915. args)))
  1916. (cl-assert (eq status 0)
  1917. nil
  1918. (buffer-substring-no-properties (point-min) (point-max)))))
  1919. ;;;;;;;;;;;;;;;;;;;
  1920. ;; peek-file-mode
  1921. (defun peek-file (file)
  1922. "Peek FILE."
  1923. (interactive)
  1924. (message "%s" file))
  1925. ;;;;;;;;;;;;;;;;;;;;
  1926. ;; remember-projectile
  1927. ;; TODO: Add global-minor-mode
  1928. (defvar remember-data-file)
  1929. (defun my-set-remember-data-file-buffer-local ()
  1930. "Set `remember-data-file'."
  1931. (when (require 'projectile nil t)
  1932. (setq-local remember-data-file
  1933. (expand-file-name ".remember.notes"
  1934. (projectile-project-root)))))
  1935. (add-hook 'after-change-major-mode-hook
  1936. 'my-set-remember-data-file-buffer-local)
  1937. ;; Local Variables:
  1938. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1939. ;; flycheck-checker: emacs-lisp
  1940. ;; End:
  1941. ;;; emancs.el ends here