You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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