Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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