您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

1638 行
53 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: (load-file file)))
  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. (defun call-after-init (func)
  15. "If `after-init-hook' has been run, call FUNC immediately.
  16. Otherwize hook it."
  17. (if after-init-time
  18. (funcall func)
  19. (add-hook 'after-init-hook
  20. func)))
  21. (defmacro safe-require-or-eval (feature)
  22. "Require FEATURE if available.
  23. At compile time the feature will be loaded immediately."
  24. `(eval-and-compile
  25. (message "safe-require-or-eval: Trying to require %s" ,feature)
  26. (require ,feature nil t)))
  27. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  28. "Define autoloading FEATURE that defines FUNCTIONS.
  29. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  30. the function same as FEATURE is defined as autoloaded function. BODY is passed
  31. to `eval-after-load'.
  32. After this macro is expanded, this returns the path to library if FEATURE
  33. found, otherwise returns nil."
  34. (declare (indent 2) (debug t))
  35. (let* ((libname (symbol-name (eval feature)))
  36. (libpath (locate-library libname)))
  37. `(progn
  38. (when (locate-library ,libname)
  39. ,@(mapcar (lambda (f)
  40. `(unless (fboundp ',f)
  41. (progn
  42. (message "Autoloaded function `%S' defined (%s)"
  43. (quote ,f)
  44. ,libpath)
  45. (autoload (quote ,f)
  46. ,libname
  47. ,(concat "Autoloaded function defined in \""
  48. libpath
  49. "\".")
  50. t))))
  51. (or (eval functions)
  52. `(,(eval feature)))))
  53. (eval-after-load ,feature
  54. (quote (progn
  55. ,@body)))
  56. (locate-library ,libname))))
  57. (when (autoload-eval-lazily 'tetris nil
  58. (message "Tetris loaded!"))
  59. (message "Tetris found!"))
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ;; package
  62. (set (defvar 10sr-package-list)
  63. '(
  64. vimrc-mode
  65. markdown-mode
  66. yaml-mode
  67. gnuplot-mode
  68. php-mode
  69. erlang
  70. js2-mode
  71. js-doc
  72. git-commit
  73. gitignore-mode
  74. adoc-mode
  75. go-mode
  76. ;; It seems malabar has been merged into jdee and this package
  77. ;; already removed
  78. ;; malabar-mode
  79. ;; ack
  80. color-moccur
  81. ggtags
  82. flycheck
  83. auto-highlight-symbol
  84. hl-todo
  85. ;; Currently not available
  86. ;; pp-c-l
  87. ;; is flymake installs are required?
  88. ;;flymake-jshint
  89. ;;flymake-python-pyflakes
  90. xclip
  91. foreign-regexp
  92. multi-term
  93. term-run
  94. editorconfig
  95. git-ps1-mode
  96. restart-emacs
  97. fill-column-indicator
  98. pkgbuild-mode
  99. minibuffer-line
  100. which-key
  101. ;; I think this works in place of my autosave lib
  102. super-save
  103. pipenv
  104. imenu-list
  105. scala-mode
  106. ;;ensime
  107. editorconfig
  108. editorconfig-custom-majormode
  109. git-command
  110. prompt-text
  111. ;; 10sr repository
  112. ;; 10sr-extras
  113. terminal-title
  114. recentf-show
  115. dired-list-all-mode
  116. pack
  117. set-modeline-color
  118. read-only-only-mode
  119. smart-revert
  120. autosave
  121. ;;window-organizer
  122. remember-major-modes-mode
  123. ilookup
  124. pasteboard
  125. end-mark
  126. sl
  127. gosh-mode
  128. ))
  129. (when (safe-require-or-eval 'package)
  130. (setq package-archives
  131. `(,@package-archives
  132. ("melpa" . "https://melpa.org/packages/")
  133. ;; Somehow fails to download via https
  134. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  135. (package-initialize)
  136. (defun my-auto-install-package ()
  137. "Install packages semi-automatically."
  138. (interactive)
  139. (package-refresh-contents)
  140. (mapc (lambda (pkg)
  141. (or (package-installed-p pkg)
  142. (package-install pkg)))
  143. 10sr-package-list))
  144. )
  145. ;; (lazy-load-eval 'sudoku)
  146. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  147. ;; my-idle-hook
  148. (defvar my-idle-hook nil
  149. "Hook run when idle for several secs.")
  150. (defvar my-idle-hook-sec 5
  151. "Second to run `my-idle-hook'.")
  152. (run-with-idle-timer my-idle-hook-sec
  153. t
  154. (lambda ()
  155. (run-hooks 'my-idle-hook)))
  156. ;; (add-hook 'my-idle-hook
  157. ;; (lambda ()
  158. ;; (message "idle hook message")))
  159. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  160. ;; start and quit
  161. (setq inhibit-startup-message t)
  162. (setq confirm-kill-emacs 'y-or-n-p)
  163. (setq gc-cons-threshold (* 1024 1024 4))
  164. (when window-system
  165. (add-to-list 'default-frame-alist '(cursor-type . box))
  166. (add-to-list 'default-frame-alist '(background-color . "white"))
  167. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  168. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  169. ;; does not work?
  170. )
  171. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  172. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  173. (and (fboundp 'tool-bar-mode)
  174. (tool-bar-mode 0))
  175. (and (fboundp 'set-scroll-bar-mode)
  176. (set-scroll-bar-mode nil))
  177. (call-after-init
  178. (lambda ()
  179. (message "%s %s" invocation-name emacs-version)
  180. (message "Invocation directory: %s" default-directory)
  181. (message "%s was taken to initialize emacs." (emacs-init-time))
  182. (switch-to-buffer "*Messages*")))
  183. (cd ".") ; when using windows use / instead of \ in `default-directory'
  184. ;; locale
  185. (set-language-environment "Japanese")
  186. (set-default-coding-systems 'utf-8-unix)
  187. (prefer-coding-system 'utf-8-unix)
  188. (setq system-time-locale "C")
  189. ;; my prefix map
  190. (defvar my-prefix-map nil
  191. "My prefix map.")
  192. (define-prefix-command 'my-prefix-map)
  193. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  194. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  195. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  196. ;; (comint-show-maximum-output)
  197. ;; kill scratch
  198. (call-after-init (lambda ()
  199. (let ((buf (get-buffer "*scratch*")))
  200. (when buf
  201. (kill-buffer buf)))))
  202. ;; modifier keys
  203. ;; (setq mac-option-modifier 'control)
  204. ;; display
  205. (setq visible-bell t)
  206. (setq ring-bell-function 'ignore)
  207. (mouse-avoidance-mode 'banish)
  208. (setq echo-keystrokes 0.1)
  209. (defun reload-init-file ()
  210. "Reload Emacs init file."
  211. (interactive)
  212. (when (and user-init-file
  213. (file-readable-p user-init-file))
  214. (load-file user-init-file)))
  215. (safe-require-or-eval 'session)
  216. ;; server
  217. (set-variable 'server-name (concat "server"
  218. (number-to-string (emacs-pid))))
  219. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  220. ;; In Darwin environment, init fails with message like 'Service name too long'
  221. ;; when server-use-tcp is nil
  222. (when (or (eq system-type
  223. 'cygwin)
  224. (eq system-type
  225. 'darwin))
  226. (set-variable 'server-use-tcp t))
  227. ;; MSYS2 fix
  228. (when (eq system-type
  229. 'windows-nt)
  230. (setq shell-file-name
  231. (executable-find "bash"))
  232. '(setq function-key-map
  233. `(,@function-key-map ([pause] . [?\C-c])
  234. ))
  235. (define-key key-translation-map
  236. (kbd "<pause>")
  237. (kbd "C-c"))
  238. '(keyboard-translate [pause]
  239. (kbd "C-c")p)
  240. ;; TODO: move to other place later
  241. (when (not window-system)
  242. (setq interprogram-paste-function nil)
  243. (setq interprogram-cut-function nil)))
  244. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  245. ;; global keys
  246. (global-set-key (kbd "<up>") 'scroll-down-line)
  247. (global-set-key (kbd "<down>") 'scroll-up-line)
  248. (global-set-key (kbd "<left>") 'scroll-down)
  249. (global-set-key (kbd "<right>") 'scroll-up)
  250. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  251. (global-set-key (kbd "C-\\") help-map)
  252. (define-key ctl-x-map (kbd "DEL") help-map)
  253. (define-key ctl-x-map (kbd "C-h") help-map)
  254. (define-key help-map "a" 'apropos)
  255. ;; disable annoying keys
  256. (global-set-key [prior] 'ignore)
  257. (global-set-key (kbd "<next>") 'ignore)
  258. (global-set-key [menu] 'ignore)
  259. (global-set-key [down-mouse-1] 'ignore)
  260. (global-set-key [down-mouse-2] 'ignore)
  261. (global-set-key [down-mouse-3] 'ignore)
  262. (global-set-key [mouse-1] 'ignore)
  263. (global-set-key [mouse-2] 'ignore)
  264. (global-set-key [mouse-3] 'ignore)
  265. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  266. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  267. (when (safe-require-or-eval 'which-key)
  268. (which-key-mode))
  269. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  270. ;; editting
  271. (setq kill-whole-line t)
  272. (setq scroll-conservatively 35
  273. scroll-margin 2
  274. scroll-step 0)
  275. (setq-default major-mode 'text-mode)
  276. (setq next-line-add-newlines nil)
  277. (setq kill-read-only-ok t)
  278. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  279. ;; (setq-default line-spacing 0.2)
  280. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  281. ;; (setq-default tab-width 4)
  282. (setq-default indent-tabs-mode nil)
  283. (setq-default indent-line-function 'indent-to-left-margin)
  284. ;; (setq-default indent-line-function nil)
  285. (setq-default truncate-lines nil)
  286. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  287. (delete-selection-mode 1)
  288. (cua-mode 0)
  289. (setq line-move-visual nil)
  290. ;; key bindings
  291. ;; moving around
  292. ;;(keyboard-translate ?\M-j ?\C-j)
  293. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  294. (define-key esc-map "p" 'backward-paragraph)
  295. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  296. (define-key esc-map "n" 'forward-paragraph)
  297. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  298. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  299. (global-set-key (kbd "C-<left>") 'scroll-down)
  300. (global-set-key (kbd "C-<right>") 'scroll-up)
  301. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  302. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  303. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  304. ;; C-h and DEL
  305. (global-set-key (kbd "C-h") (kbd "DEL"))
  306. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  307. (global-set-key (kbd "C-m") 'newline-and-indent)
  308. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  309. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  310. (define-key esc-map "u" 'undo)
  311. (define-key esc-map "i" (kbd "ESC TAB"))
  312. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  313. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  314. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  315. (define-key my-prefix-map (kbd "C-o") 'occur)
  316. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  317. ;; title and mode-line
  318. (when (safe-require-or-eval 'terminal-title)
  319. ;; if TERM is not screen use default value
  320. (if (getenv "TMUX")
  321. ;; if use tmux locally just basename of current dir
  322. (set-variable 'terminal-title-format
  323. '((file-name-nondirectory (directory-file-name
  324. default-directory))))
  325. (if (and (let ((tty-type (frame-parameter nil
  326. 'tty-type)))
  327. (and tty-type
  328. (equal (car (split-string tty-type
  329. "-"))
  330. "screen")))
  331. (not (getenv "SSH_CONNECTION")))
  332. (set-variable 'terminal-title-format
  333. '((file-name-nondirectory (directory-file-name
  334. default-directory))))
  335. ;; seems that TMUX is used locally and ssh to remote host
  336. (set-variable 'terminal-title-format
  337. `("em:"
  338. ,user-login-name
  339. "@"
  340. ,(car (split-string (system-name)
  341. "\\."))
  342. ":"
  343. default-directory))
  344. )
  345. )
  346. (terminal-title-mode))
  347. (setq eol-mnemonic-dos "\\r\\n")
  348. (setq eol-mnemonic-mac "\\r")
  349. (setq eol-mnemonic-unix "")
  350. (which-function-mode 0)
  351. (line-number-mode 0)
  352. (column-number-mode 0)
  353. (size-indication-mode 0)
  354. (setq mode-line-position
  355. '(:eval (format "L%%l/%d:C%%c"
  356. (count-lines (point-max)
  357. (point-min)))))
  358. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  359. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  360. ;; minibuffer
  361. (setq insert-default-directory t)
  362. (setq completion-ignore-case t
  363. read-file-name-completion-ignore-case t
  364. read-buffer-completion-ignore-case t)
  365. (setq resize-mini-windows t)
  366. (temp-buffer-resize-mode 1)
  367. (savehist-mode 1)
  368. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  369. (fset 'yes-or-no-p 'y-or-n-p)
  370. ;; complete symbol when `eval'
  371. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  372. (define-key minibuffer-local-map (kbd "C-u")
  373. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  374. ;; I dont know these bindings are good
  375. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  376. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  377. (when (safe-require-or-eval 'minibuffer-line)
  378. (set-face-underline 'minibuffer-line nil)
  379. (set-variable 'minibuffer-line-refresh-interval
  380. 25)
  381. ;; Set idle timer
  382. (defvar my-minibuffer-line--idle-timer nil)
  383. (defvar minibuffer-line-mode)
  384. (add-hook 'minibuffer-line-mode-hook
  385. (lambda ()
  386. (when my-minibuffer-line--idle-timer
  387. (cancel-timer my-minibuffer-line--idle-timer)
  388. (setq my-minibuffer-line--idle-timer nil))
  389. (when minibuffer-line-mode
  390. (setq my-minibuffer-line--idle-timer
  391. (run-with-idle-timer 0.5
  392. t
  393. 'minibuffer-line--update)))))
  394. (set-variable 'minibuffer-line-format
  395. `(,(concat user-login-name
  396. "@"
  397. (car (split-string (system-name)
  398. "\\."))
  399. ":")
  400. (:eval (abbreviate-file-name (or buffer-file-name
  401. default-directory)))
  402. (:eval (and (fboundp 'git-ps1-mode-get-current)
  403. (git-ps1-mode-get-current " [GIT:%s]")))
  404. " "
  405. (:eval (format-time-string display-time-format))))
  406. (minibuffer-line-mode 1)
  407. )
  408. (when (safe-require-or-eval 'prompt-text)
  409. (set-variable 'prompt-text-format
  410. `(,(concat ""
  411. user-login-name
  412. "@"
  413. (car (split-string (system-name)
  414. "\\."))
  415. ":")
  416. (:eval (abbreviate-file-name (or buffer-file-name
  417. default-directory)))
  418. (:eval (and (fboundp 'git-ps1-mode-get-current)
  419. (git-ps1-mode-get-current " [GIT:%s]")))
  420. " "
  421. (:eval (format-time-string display-time-format))
  422. "\n"
  423. (:eval (symbol-name this-command))
  424. ": "))
  425. (prompt-text-mode 1))
  426. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  427. ;; letters, font-lock mode and fonts
  428. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  429. ;; (set-window-margins (selected-window) 1 1)
  430. (and (or (eq system-type 'Darwin)
  431. (eq system-type 'darwin))
  432. (fboundp 'mac-set-input-method-parameter)
  433. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  434. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  435. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  436. (boundp 'input-method-inactivate-hook))
  437. (add-hook 'input-method-activate-hook
  438. (lambda () (set-cursor-color "red")))
  439. (add-hook 'input-method-inactivate-hook
  440. (lambda () (set-cursor-color "black"))))
  441. (when (safe-require-or-eval 'paren)
  442. (show-paren-mode 1)
  443. (setq show-paren-delay 0.5
  444. show-paren-style 'parenthesis) ; mixed is hard to read
  445. ;; (set-face-background 'show-paren-match
  446. ;; "black")
  447. ;; ;; (face-foreground 'default))
  448. ;; (set-face-foreground 'show-paren-match
  449. ;; "white")
  450. ;; (set-face-inverse-video-p 'show-paren-match
  451. ;; t)
  452. )
  453. (transient-mark-mode 1)
  454. (global-font-lock-mode 1)
  455. (setq font-lock-global-modes
  456. '(not
  457. help-mode
  458. eshell-mode
  459. ;;term-mode
  460. Man-mode))
  461. ;; (standard-display-ascii ?\n "$\n")
  462. ;; (defvar my-eol-face
  463. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  464. ;; )
  465. ;; (defvar my-tab-face
  466. ;; '(("\t" . '(0 highlight t nil))))
  467. (defvar my-jspace-face
  468. '(("\u3000" . '(0 highlight t nil))))
  469. (add-hook 'font-lock-mode-hook
  470. (lambda ()
  471. ;; (font-lock-add-keywords nil my-eol-face)
  472. (font-lock-add-keywords nil my-jspace-face)
  473. ))
  474. (when (safe-require-or-eval 'whitespace)
  475. (add-to-list 'whitespace-display-mappings
  476. ;; We need t since last one takes precedence
  477. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  478. ;; (add-to-list 'whitespace-display-mappings
  479. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  480. (setq whitespace-style '(face
  481. trailing ; trailing blanks
  482. newline ; newlines
  483. newline-mark ; use display table for newline
  484. tab-mark
  485. empty ; empty lines at beg or end of buffer
  486. lines-tail ; lines over 80
  487. ))
  488. ;; (setq whitespace-newline 'font-lock-comment-face)
  489. (set-variable 'whitespace-line-column nil)
  490. (global-whitespace-mode t)
  491. (add-hook 'dired-mode-hook
  492. (lambda ()
  493. (set (make-local-variable 'whitespace-style) nil)))
  494. (if (eq (display-color-cells)
  495. 256)
  496. (set-face-foreground 'whitespace-newline "color-109")
  497. ;; (progn
  498. ;; (set-face-bold-p 'whitespace-newline
  499. ;; t))
  500. ))
  501. (and nil
  502. '(safe-require-or-eval 'fill-column-indicator)
  503. (setq fill-column-indicator))
  504. ;; highlight current line
  505. ;; http://wiki.riywo.com/index.php?Meadow
  506. (face-spec-set 'hl-line
  507. '((((min-colors 256)
  508. (background dark))
  509. (:background "color-234"))
  510. (((min-colors 256)
  511. (background light))
  512. (:background "color-234"))
  513. (t
  514. (:underline "black"))))
  515. (set-variable 'hl-line-global-modes
  516. '(not
  517. term-mode))
  518. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  519. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  520. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  521. ;;(safe-require-or-eval 'set-modeline-color)
  522. ;; (let ((fg (face-foreground 'default))
  523. ;; (bg (face-background 'default)))
  524. ;; (set-face-background 'mode-line-inactive
  525. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  526. ;; (set-face-foreground 'mode-line-inactive
  527. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  528. ;; (set-face-underline 'mode-line-inactive
  529. ;; t)
  530. ;; (set-face-underline 'vertical-border
  531. ;; nil)
  532. (when (safe-require-or-eval 'end-mark)
  533. (global-end-mark-mode))
  534. (when (safe-require-or-eval 'auto-highlight-symbol)
  535. (set-variable 'ahs-idle-interval 0.6)
  536. (global-auto-highlight-symbol-mode 1))
  537. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  538. ;; file handling
  539. (when (safe-require-or-eval 'editorconfig)
  540. ;; (set-variable 'editorconfig-get-properties-function
  541. ;; 'editorconfig-core-get-properties-hash)
  542. (editorconfig-mode 1)
  543. (set-variable 'editorconfig-mode-lighter " EC")
  544. (with-eval-after-load 'org-src
  545. ;; [*.org\[\*Org Src*\[ c \]*\]]
  546. (add-hook 'org-src-mode-hook
  547. 'editorconfig-mode-apply t)))
  548. (when (fboundp 'editorconfig-custom-majormode)
  549. (add-hook 'editorconfig-custom-hooks
  550. 'editorconfig-custom-majormode))
  551. ;; Add readonly=true to set read-only-mode
  552. (add-hook 'editorconfig-custom-hooks
  553. (lambda (props)
  554. (let ((r (gethash 'readonly props)))
  555. (when (and (string= r "true")
  556. (not buffer-read-only))
  557. (read-only-mode 1)))))
  558. ;; (when (fboundp 'editorconfig-charset-extras)
  559. ;; (add-hook 'editorconfig-custom-hooks
  560. ;; 'editorconfig-charset-extras))
  561. (setq revert-without-query '(".+"))
  562. ;; save cursor position
  563. (when (safe-require-or-eval 'saveplace)
  564. (setq-default save-place t)
  565. (setq save-place-file (concat user-emacs-directory
  566. "places")))
  567. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  568. (setq make-backup-files t)
  569. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  570. (setq backup-directory-alist
  571. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  572. "backup")))
  573. backup-directory-alist))
  574. (setq version-control 'never)
  575. (setq delete-old-versions t)
  576. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  577. "auto-save/")))
  578. (setq delete-auto-save-files t)
  579. (add-to-list 'completion-ignored-extensions ".bak")
  580. (setq delete-by-moving-to-trash t)
  581. ;; trash-directory "~/.emacs.d/trash")
  582. (add-hook 'after-save-hook
  583. 'executable-make-buffer-file-executable-if-script-p)
  584. (set (defvar bookmark-default-file)
  585. (expand-file-name (concat user-emacs-directory
  586. "bmk")))
  587. (with-eval-after-load 'recentf
  588. (defvar recentf-exclude nil)
  589. (add-to-list 'recentf-exclude
  590. (regexp-quote bookmark-default-file)))
  591. (when (safe-require-or-eval 'smart-revert)
  592. (smart-revert-on))
  593. ;; autosave
  594. (when (safe-require-or-eval 'autosave)
  595. (autosave-set 8))
  596. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  597. ;; buffer killing
  598. ;; (defun my-delete-window-killing-buffer () nil)
  599. (defun my-query-kill-current-buffer ()
  600. "Interactively kill current buffer."
  601. (interactive)
  602. (if (y-or-n-p (concat "kill current buffer? :"))
  603. (kill-buffer (current-buffer))))
  604. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  605. (substitute-key-definition 'kill-buffer
  606. 'my-query-kill-current-buffer
  607. global-map)
  608. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  609. ;; share clipboard with x
  610. ;; this page describes this in details, but only these sexps seem to be needed
  611. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  612. (and nil
  613. (not window-system)
  614. (not (eq window-system 'mac))
  615. (getenv "DISPLAY")
  616. (not (equal (getenv "DISPLAY") ""))
  617. (executable-find "xclip")
  618. ;; (< emacs-major-version 24)
  619. '(safe-require-or-eval 'xclip)
  620. nil
  621. (turn-on-xclip))
  622. (and (eq system-type 'darwin)
  623. (safe-require-or-eval 'pasteboard)
  624. (turn-on-pasteboard))
  625. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  626. ;; some modes and hooks
  627. ;; Include some extra modes
  628. (require 'generic-x)
  629. (when (autoload-eval-lazily 'ggtags)
  630. (add-hook 'c-mode-common-hook
  631. (lambda ()
  632. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  633. (ggtags-mode 1))))
  634. (add-hook 'python-mode-hook
  635. (lambda ()
  636. (ggtags-mode 1))))
  637. (when (autoload-eval-lazily 'imenu-list)
  638. (set-variable 'imenu-list-auto-resize t)
  639. (set-variable 'imenu-list-focus-after-activation t)
  640. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  641. (with-eval-after-load 'compile
  642. (defvar compilation-filter-start)
  643. (defvar compilation-error-regexp-alist)
  644. (require 'ansi-color)
  645. (add-hook 'compilation-filter-hook
  646. (lambda ()
  647. (let ((inhibit-read-only t))
  648. (ansi-color-apply-on-region compilation-filter-start
  649. (point)))))
  650. (add-to-list 'compilation-error-regexp-alist
  651. ;; ansible-lint
  652. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  653. ;; Workaround to avoid ensime error
  654. (defvar ensime-mode-key-prefix nil)
  655. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  656. (when (safe-require-or-eval 'company)
  657. (global-company-mode)
  658. (set-variable 'company-idle-delay 0.5)
  659. (set-variable 'company-minimum-prefix-length 2)
  660. (set-variable 'company-selection-wrap-around t))
  661. ;; https://github.com/lunaryorn/flycheck
  662. (when (safe-require-or-eval 'flycheck)
  663. (call-after-init 'global-flycheck-mode))
  664. (when (autoload-eval-lazily 'ilookup)
  665. (define-key ctl-x-map "d" 'ilookup-open-word))
  666. (set-variable 'ac-ignore-case nil)
  667. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  668. (define-key ctl-x-map "t" 'term-run-shell-command))
  669. (add-to-list 'safe-local-variable-values
  670. '(encoding utf-8))
  671. (setq enable-local-variables :safe)
  672. ;; (when (safe-require-or-eval 'remember-major-modes-mode)
  673. ;; (remember-major-modes-mode 1))
  674. ;; Detect file type from shebang and set major-mode.
  675. (add-to-list 'interpreter-mode-alist
  676. '("python3" . python-mode))
  677. (add-to-list 'interpreter-mode-alist
  678. '("python2" . python-mode))
  679. (with-eval-after-load 'python
  680. (defvar python-mode-map (make-sparse-keymap))
  681. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  682. ;; http://fukuyama.co/foreign-regexp
  683. '(and (safe-require-or-eval 'foreign-regexp)
  684. (progn
  685. (setq foreign-regexp/regexp-type 'perl)
  686. '(setq reb-re-syntax 'foreign-regexp)
  687. ))
  688. (autoload-eval-lazily 'sql '(sql-mode)
  689. (require 'sql-indent nil t))
  690. (when (autoload-eval-lazily 'git-command)
  691. (define-key ctl-x-map "g" 'git-command))
  692. (when (safe-require-or-eval 'git-commit)
  693. (global-git-commit-mode 1))
  694. (with-eval-after-load 'git-commit
  695. (add-hook 'git-commit-setup-hook
  696. 'turn-off-auto-fill t))
  697. (autoload-eval-lazily 'sl)
  698. (with-eval-after-load 'rst
  699. (defvar rst-mode-map)
  700. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  701. (with-eval-after-load 'jdee
  702. (add-hook 'jdee-mode-hook
  703. (lambda ()
  704. (make-local-variable 'global-mode-string)
  705. (add-to-list 'global-mode-string
  706. mode-line-position))))
  707. ;; Cannot enable error thrown. Why???
  708. ;; https://github.com/m0smith/malabar-mode#Installation
  709. ;; (when (autoload-eval-lazily 'malabar-mode)
  710. ;; (add-to-list 'load-path
  711. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  712. ;; (safe-require-or-eval 'cedet-devel-load)
  713. ;; (call-after-init 'activate-malabar-mode))
  714. (with-eval-after-load 'make-mode
  715. (defvar makefile-mode-map (make-sparse-keymap))
  716. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  717. ;; this functions is set in write-file-functions, i cannot find any
  718. ;; good way to remove this.
  719. (fset 'makefile-warn-suspicious-lines 'ignore))
  720. (with-eval-after-load 'verilog-mode
  721. (defvar verilog-mode-map (make-sparse-keymap))
  722. (define-key verilog-mode-map ";" 'self-insert-command))
  723. (setq diff-switches "-u")
  724. (with-eval-after-load 'diff-mode
  725. ;; (when (and (eq major-mode
  726. ;; 'diff-mode)
  727. ;; (not buffer-file-name))
  728. ;; ;; do not pass when major-mode is derived mode of diff-mode
  729. ;; (view-mode 1))
  730. (set-face-attribute 'diff-header nil
  731. :foreground nil
  732. :background nil
  733. :weight 'bold)
  734. (set-face-attribute 'diff-file-header nil
  735. :foreground nil
  736. :background nil
  737. :weight 'bold)
  738. (set-face-foreground 'diff-index "blue")
  739. (set-face-attribute 'diff-hunk-header nil
  740. :foreground "cyan"
  741. :weight 'normal)
  742. (set-face-attribute 'diff-context nil
  743. ;; :foreground "white"
  744. :foreground nil
  745. :weight 'normal)
  746. (set-face-foreground 'diff-removed "red")
  747. (set-face-foreground 'diff-added "green")
  748. (set-face-background 'diff-removed nil)
  749. (set-face-background 'diff-added nil)
  750. (set-face-attribute 'diff-changed nil
  751. :foreground "magenta"
  752. :weight 'normal)
  753. (set-face-attribute 'diff-refine-changed nil
  754. :foreground nil
  755. :background nil
  756. :weight 'bold
  757. :inverse-video t)
  758. ;; Annoying !
  759. ;;(diff-auto-refine-mode)
  760. )
  761. ;; (ffap-bindings)
  762. (set-variable 'browse-url-browser-function
  763. 'eww-browse-url)
  764. (set-variable 'sh-here-document-word "__EOC__")
  765. (when (autoload-eval-lazily 'adoc-mode
  766. nil
  767. (defvar adoc-mode-map (make-sparse-keymap))
  768. (define-key adoc-mode-map (kbd "C-m") 'newline))
  769. (setq auto-mode-alist
  770. `(("\\.adoc\\'" . adoc-mode)
  771. ("\\.asciidoc\\'" . adoc-mode)
  772. ,@auto-mode-alist)))
  773. (with-eval-after-load 'markup-faces
  774. ;; Is this too match ?
  775. (set-face-foreground 'markup-meta-face
  776. "color-245")
  777. (set-face-foreground 'markup-meta-hide-face
  778. "color-245")
  779. )
  780. ;; TODO: check if this is required
  781. (when (autoload-eval-lazily 'groovy-mode nil
  782. (defvar groovy-mode-map (make-sparse-keymap))
  783. (define-key groovy-mode-map "(" 'self-insert-command)
  784. (define-key groovy-mode-map ")" 'self-insert-command)
  785. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  786. )
  787. (add-to-list 'auto-mode-alist
  788. '("build\\.gradle\\'" . groovy-mode)))
  789. (with-eval-after-load 'yaml-mode
  790. (defvar yaml-mode-map (make-sparse-keymap))
  791. (define-key yaml-mode-map (kbd "C-m") 'newline))
  792. (with-eval-after-load 'html-mode
  793. (defvar html-mode-map (make-sparse-keymap))
  794. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  795. (with-eval-after-load 'text-mode
  796. (define-key text-mode-map (kbd "C-m") 'newline))
  797. (with-eval-after-load 'info
  798. (defvar Info-directory-list nil)
  799. (add-to-list 'Info-directory-list
  800. (expand-file-name "~/.brew/share/info")))
  801. (with-eval-after-load 'apropos
  802. (defvar apropos-mode-map (make-sparse-keymap))
  803. (define-key apropos-mode-map "n" 'next-line)
  804. (define-key apropos-mode-map "p" 'previous-line))
  805. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  806. ;; (define-key isearch-mode-map
  807. ;; (kbd "C-j") 'isearch-other-control-char)
  808. ;; (define-key isearch-mode-map
  809. ;; (kbd "C-k") 'isearch-other-control-char)
  810. ;; (define-key isearch-mode-map
  811. ;; (kbd "C-h") 'isearch-other-control-char)
  812. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  813. (define-key isearch-mode-map (kbd "M-r")
  814. 'isearch-query-replace-regexp)
  815. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  816. (setq lazy-highlight-cleanup nil)
  817. ;; face for isearch highlighing
  818. (set-face-attribute 'lazy-highlight
  819. nil
  820. :foreground `unspecified
  821. :background `unspecified
  822. :underline t
  823. ;; :weight `bold
  824. )
  825. (add-hook 'outline-mode-hook
  826. (lambda ()
  827. (when (string-match "\\.md\\'" buffer-file-name)
  828. (set (make-local-variable 'outline-regexp) "#+ "))))
  829. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  830. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  831. (when (autoload-eval-lazily 'markdown-mode
  832. '(markdown-mode gfm-mode)
  833. (defvar gfm-mode-map (make-sparse-keymap))
  834. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  835. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  836. (set-variable 'markdown-command (or (executable-find "markdown")
  837. (executable-find "markdown.pl")
  838. ""))
  839. (add-hook 'markdown-mode-hook
  840. (lambda ()
  841. (outline-minor-mode 1)
  842. (flyspell-mode)
  843. (set (make-local-variable 'comment-start) ";")))
  844. )
  845. ;; c-mode
  846. ;; http://www.emacswiki.org/emacs/IndentingC
  847. ;; http://en.wikipedia.org/wiki/Indent_style
  848. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  849. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  850. (with-eval-after-load 'cc-vars
  851. (defvar c-default-style nil)
  852. (add-to-list 'c-default-style
  853. '(c-mode . "k&r"))
  854. (add-to-list 'c-default-style
  855. '(c++-mode . "k&r")))
  856. (autoload-eval-lazily 'js2-mode nil
  857. ;; currently do not use js2-mode
  858. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  859. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  860. ;; (defvar js2-mode-map (make-sparse-keymap))
  861. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  862. ;; (interactive)
  863. ;; (js2-enter-key)
  864. ;; (indent-for-tab-command)))
  865. ;; (add-hook (kill-local-variable 'before-save-hook)
  866. ;; 'js2-before-save)
  867. ;; (add-hook 'before-save-hook
  868. ;; 'my-indent-buffer
  869. ;; nil
  870. ;; t)
  871. )
  872. (add-to-list 'interpreter-mode-alist
  873. '("node" . js-mode))
  874. (when (autoload-eval-lazily 'flymake-jslint
  875. '(flymake-jslint-load))
  876. (autoload-eval-lazily 'js nil
  877. (add-hook 'js-mode-hook
  878. 'flymake-jslint-load)))
  879. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  880. (with-eval-after-load 'uniquify
  881. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  882. (setq uniquify-ignore-buffers-re "*[^*]+*")
  883. (setq uniquify-min-dir-content 1))
  884. (with-eval-after-load 'view
  885. (defvar view-mode-map (make-sparse-keymap))
  886. (define-key view-mode-map "j" 'scroll-up-line)
  887. (define-key view-mode-map "k" 'scroll-down-line)
  888. (define-key view-mode-map "v" 'toggle-read-only)
  889. (define-key view-mode-map "q" 'bury-buffer)
  890. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  891. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  892. ;; (define-key view-mode-map
  893. ;; "n" 'nonincremental-repeat-search-forward)
  894. ;; (define-key view-mode-map
  895. ;; "N" 'nonincremental-repeat-search-backward)
  896. (define-key view-mode-map "/" 'isearch-forward-regexp)
  897. (define-key view-mode-map "?" 'isearch-backward-regexp)
  898. (define-key view-mode-map "n" 'isearch-repeat-forward)
  899. (define-key view-mode-map "N" 'isearch-repeat-backward)
  900. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  901. (global-set-key "\M-r" 'view-mode)
  902. ;; (setq view-read-only t)
  903. (with-eval-after-load 'term
  904. (defvar term-raw-map (make-sparse-keymap))
  905. (define-key term-raw-map (kbd "C-x")
  906. (lookup-key (current-global-map)
  907. (kbd "C-x"))))
  908. (add-hook 'term-mode-hook
  909. (lambda ()
  910. ;; Stop current line highlighting
  911. (set (make-local-variable (defvar hl-line-range-function))
  912. (lambda () '(0 . 0)))
  913. (set (make-local-variable 'scroll-margin)
  914. 0)))
  915. (add-hook 'Man-mode-hook
  916. (lambda ()
  917. (view-mode 1)
  918. (setq truncate-lines nil)))
  919. (set-variable 'Man-notify-method (if window-system
  920. 'newframe
  921. 'aggressive))
  922. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  923. "woman_cache.el")))
  924. ;; not work because man.el will be loaded when man called
  925. (defalias 'man 'woman)
  926. (add-to-list 'auto-mode-alist
  927. '("tox\\.ini\\'" . conf-unix-mode))
  928. (when (autoload-eval-lazily 'toml-mode)
  929. (add-to-list 'auto-mode-alist
  930. '("/Pipfile\\'" . toml-mode)))
  931. (when (autoload-eval-lazily 'json-mode)
  932. (add-to-list 'auto-mode-alist
  933. '("/Pipfile\\.lock\\'" . json-mode)))
  934. (add-hook 'go-mode-hook
  935. (lambda()
  936. (defvar go-mode-map)
  937. (add-hook 'before-save-hook' 'gofmt-before-save)
  938. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  939. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  940. ;; buffer switching
  941. (defvar bs-configurations)
  942. (when (autoload-eval-lazily 'bs '(bs-show)
  943. (add-to-list 'bs-configurations
  944. '("specials" "^\\*" nil ".*" nil nil))
  945. (add-to-list 'bs-configurations
  946. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  947. (defvar bs-mode-map)
  948. (defvar bs-current-configuration)
  949. (define-key bs-mode-map (kbd "t")
  950. ;; TODO: fix toggle feature
  951. (lambda ()
  952. (interactive)
  953. (if (string= "specials"
  954. bs-current-configuration)
  955. (bs-set-configuration "files")
  956. (bs-set-configuration "specials"))
  957. (bs-refresh)
  958. (bs-message-without-log "%s"
  959. (bs--current-config-message))))
  960. ;; (setq bs-configurations (list
  961. ;; '("processes" nil get-buffer-process ".*" nil nil)
  962. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  963. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  964. )
  965. (defalias 'list-buffers 'bs-show)
  966. (set-variable 'bs-default-configuration "files-and-specials")
  967. (set-variable 'bs-default-sort-name "by nothing")
  968. (add-hook 'bs-mode-hook
  969. (lambda ()
  970. (set (make-local-variable 'scroll-margin) 0))))
  971. ;;(iswitchb-mode 1)
  972. (icomplete-mode)
  973. (defun iswitchb-buffer-display-other-window ()
  974. "Do iswitchb in other window."
  975. (interactive)
  976. (let ((iswitchb-default-method 'display))
  977. (call-interactively 'iswitchb-buffer)))
  978. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  979. ;; vc
  980. (set-variable 'vc-handled-backends '())
  981. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  982. ;; recentf-mode
  983. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  984. "recentf")))
  985. (set-variable 'recentf-max-menu-items 20)
  986. (set-variable 'recentf-max-saved-items 30)
  987. (set-variable 'recentf-show-file-shortcuts-flag nil)
  988. (when (safe-require-or-eval 'recentf)
  989. (add-to-list 'recentf-exclude
  990. (regexp-quote recentf-save-file))
  991. (add-to-list 'recentf-exclude
  992. (regexp-quote (expand-file-name user-emacs-directory)))
  993. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  994. (remove-hook 'find-file-hook
  995. 'recentf-track-opened-file)
  996. (defun my-recentf-load-track-save-list ()
  997. "Load current recentf list from file, track current visiting file, then save
  998. the list."
  999. (recentf-load-list)
  1000. (recentf-track-opened-file)
  1001. (recentf-save-list))
  1002. (add-hook 'find-file-hook
  1003. 'my-recentf-load-track-save-list)
  1004. (add-hook 'kill-emacs-hook
  1005. 'recentf-load-list)
  1006. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1007. ;; (add-hook 'find-file-hook
  1008. ;; (lambda ()
  1009. ;; (recentf-add-file default-directory)))
  1010. (and (autoload-eval-lazily 'recentf-show)
  1011. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1012. (add-hook 'recentf-show-before-listing-hook
  1013. 'recentf-load-list))
  1014. (recentf-mode 1)
  1015. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1016. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1017. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1018. (define-key recentf-dialog-mode-map "n" 'next-line)
  1019. (add-hook 'recentf-dialog-mode-hook
  1020. (lambda ()
  1021. ;; (recentf-save-list)
  1022. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1023. ;; 'my-recentf-cd-and-find-file)
  1024. (cd "~/"))))
  1025. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1026. ;; dired
  1027. (defun my-dired-echo-file-head (arg)
  1028. "Echo head of current file.
  1029. ARG is num to show, or defaults to 7."
  1030. (interactive "P")
  1031. (let ((f (dired-get-filename)))
  1032. (message "%s"
  1033. (with-temp-buffer
  1034. (insert-file-contents f)
  1035. (buffer-substring-no-properties
  1036. (point-min)
  1037. (progn (goto-char (point-min))
  1038. (forward-line (1- (if arg
  1039. (prefix-numeric-value arg)
  1040. 7)))
  1041. (point-at-eol)))))))
  1042. (defun my-dired-diff ()
  1043. "Show diff of marked file and file of current line."
  1044. (interactive)
  1045. (let ((files (dired-get-marked-files nil nil nil t)))
  1046. (if (eq (car files)
  1047. t)
  1048. (diff (cadr files) (dired-get-filename))
  1049. (message "One file must be marked!"))))
  1050. (defun dired-get-file-info ()
  1051. "Print information of current line file."
  1052. (interactive)
  1053. (let ((f (shell-quote-argument (dired-get-filename t))))
  1054. (if (file-directory-p f)
  1055. (progn
  1056. (message "Calculating disk usage...")
  1057. (shell-command (concat "du -hsD "
  1058. f)))
  1059. (shell-command (concat "file "
  1060. f)))))
  1061. (defun my-dired-scroll-up ()
  1062. "Scroll up."
  1063. (interactive)
  1064. (my-dired-previous-line (- (window-height) 1)))
  1065. (defun my-dired-scroll-down ()
  1066. "Scroll down."
  1067. (interactive)
  1068. (my-dired-next-line (- (window-height) 1)))
  1069. ;; (defun my-dired-forward-line (arg)
  1070. ;; ""
  1071. ;; (interactive "p"))
  1072. (defun my-dired-previous-line (arg)
  1073. "Move ARG lines up."
  1074. (interactive "p")
  1075. (if (> arg 0)
  1076. (progn
  1077. (if (eq (line-number-at-pos)
  1078. 1)
  1079. (goto-char (point-max))
  1080. (forward-line -1))
  1081. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1082. (dired-get-subdir))
  1083. (- arg 1)
  1084. arg)))
  1085. (dired-move-to-filename)))
  1086. (defun my-dired-next-line (arg)
  1087. "Move ARG lines down."
  1088. (interactive "p")
  1089. (if (> arg 0)
  1090. (progn
  1091. (if (eq (point)
  1092. (point-max))
  1093. (goto-char (point-min))
  1094. (forward-line 1))
  1095. (my-dired-next-line (if (or (dired-get-filename nil t)
  1096. (dired-get-subdir))
  1097. (- arg 1)
  1098. arg)))
  1099. (dired-move-to-filename)))
  1100. (defun my-dired-tramp-open (host)
  1101. "Open HOST home directory."
  1102. (interactive "sHostname: ")
  1103. (find-file (format "/scp:%s:" host)))
  1104. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1105. (if (eq window-system 'mac)
  1106. (setq dired-listing-switches "-lhF")
  1107. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1108. )
  1109. (setq dired-listing-switches "-lhF")
  1110. (put 'dired-find-alternate-file 'disabled nil)
  1111. ;; when using dired-find-alternate-file
  1112. ;; reuse current dired buffer for the file to open
  1113. (set-variable 'dired-ls-F-marks-symlinks t)
  1114. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1115. (set-variable 'ls-lisp-dirs-first t)
  1116. (set-variable 'ls-lisp-use-localized-time-format t)
  1117. (set-variable 'ls-lisp-format-time-list
  1118. '("%Y-%m-%d %H:%M"
  1119. "%Y-%m-%d "))
  1120. (set-variable 'dired-dwim-target t)
  1121. (set-variable 'dired-isearch-filenames t)
  1122. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1123. (set-variable 'dired-hide-details-hide-information-lines nil)
  1124. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1125. (set-variable 'dired-recursive-deletes 'always)
  1126. ;; (add-hook 'dired-after-readin-hook
  1127. ;; 'my-replace-nasi-none)
  1128. (with-eval-after-load 'dired
  1129. (safe-require-or-eval 'ls-lisp)
  1130. (defvar dired-mode-map (make-sparse-keymap))
  1131. (define-key dired-mode-map "i" 'dired-get-file-info)
  1132. (define-key dired-mode-map "f" 'find-file)
  1133. (define-key dired-mode-map "!" 'shell-command)
  1134. (define-key dired-mode-map "&" 'async-shell-command)
  1135. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1136. (define-key dired-mode-map "=" 'my-dired-diff)
  1137. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1138. (define-key dired-mode-map "b" 'gtkbm)
  1139. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1140. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1141. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1142. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1143. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1144. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1145. (substitute-key-definition 'dired-next-line
  1146. 'my-dired-next-line
  1147. dired-mode-map)
  1148. (substitute-key-definition 'dired-previous-line
  1149. 'my-dired-previous-line
  1150. dired-mode-map)
  1151. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1152. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1153. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1154. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1155. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1156. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1157. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1158. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1159. (add-hook 'dired-mode-hook
  1160. (lambda ()
  1161. (when (fboundp 'dired-hide-details-mode)
  1162. (dired-hide-details-mode t)
  1163. (local-set-key "l" 'dired-hide-details-mode))
  1164. (let ((file "._Icon\015"))
  1165. (when nil
  1166. '(file-readable-p file)
  1167. (delete-file file)))))
  1168. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1169. (with-eval-after-load 'dired
  1170. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1171. (when (autoload-eval-lazily 'dired-list-all-mode)
  1172. (setq dired-listing-switches "-lhF")
  1173. (with-eval-after-load 'dired
  1174. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1175. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1176. ;; misc funcs
  1177. (defalias 'qcalc 'quick-calc)
  1178. (defun memo (&optional dir)
  1179. "Open memo.txt in DIR."
  1180. (interactive)
  1181. (pop-to-buffer (find-file-noselect (concat (if dir
  1182. (file-name-as-directory dir)
  1183. "")
  1184. "memo.txt"))))
  1185. (set (defvar my-rgrep-alist nil
  1186. "Alist of rgrep command.
  1187. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1188. condition to choose COMMAND when evaluated.")
  1189. `(
  1190. ;; git grep
  1191. ("gitgrep"
  1192. (eq 0
  1193. (shell-command "git rev-parse --git-dir"))
  1194. "git --no-pager -c color.grep=always grep -nH -e ")
  1195. ;; ripgrep
  1196. ("rg"
  1197. (executable-find "rg")
  1198. "rg --no-heading --smart-case ")
  1199. ;; sift
  1200. ("sift"
  1201. (executable-find "sift")
  1202. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1203. ;; the silver searcher
  1204. ("ag"
  1205. (executable-find "ag")
  1206. "ag --nogroup --nopager --filename ")
  1207. ;; ack
  1208. ("ack"
  1209. (executable-find "ack")
  1210. "ack --nogroup --nopager --with-filename ")
  1211. ;; gnu global
  1212. ("global"
  1213. (and (require 'gtags nil t)
  1214. (executable-find "global")
  1215. (gtags-get-rootpath))
  1216. "global --result grep ")
  1217. ;; grep
  1218. ("grep"
  1219. t
  1220. ,(concat "find . "
  1221. "-path '*/.git' -prune -o "
  1222. "-path '*/.svn' -prune -o "
  1223. "-type f -print0 | "
  1224. "xargs -0 grep -nH -e "))
  1225. )
  1226. )
  1227. (defvar my-rgrep-default nil
  1228. "Default command name for my-rgrep.")
  1229. (defun my-rgrep-grep-command (&optional name alist)
  1230. "Return recursive grep command for current directory or nil.
  1231. If NAME is given, use that without testing.
  1232. Commands are searched from ALIST."
  1233. (if alist
  1234. (if name
  1235. ;; if name is given search that from alist and return the command
  1236. (nth 2 (assoc name
  1237. alist))
  1238. ;; if name is not given try test in 1th elem
  1239. (let ((car (car alist))
  1240. (cdr (cdr alist)))
  1241. (if (eval (nth 1 car))
  1242. ;; if the condition is true return the command
  1243. (nth 2 car)
  1244. ;; try next one
  1245. (and cdr
  1246. (my-rgrep-grep-command name cdr)))))
  1247. ;; if alist is not given set default value
  1248. (my-rgrep-grep-command name my-rgrep-alist)))
  1249. (defun my-rgrep (command-args)
  1250. "My recursive grep. Run COMMAND-ARGS.
  1251. If prefix argument is given, use current symbol as default search target
  1252. and search from projectile root (if projectile is available)."
  1253. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1254. nil)))
  1255. (if cmd
  1256. (list (read-shell-command "grep command: "
  1257. (concat cmd
  1258. (if current-prefix-arg
  1259. (thing-at-point 'symbol t)
  1260. ""))
  1261. 'grep-find-history))
  1262. (error "My-Rgrep: Command for rgrep not found")
  1263. )))
  1264. (if (and current-prefix-arg
  1265. (safe-require-or-eval 'projectile))
  1266. (projectile-with-default-dir (projectile-project-root)
  1267. (compilation-start command-args
  1268. 'grep-mode))
  1269. (compilation-start command-args
  1270. 'grep-mode)))
  1271. (defun my-rgrep-thing-at-point-projectile-root ()
  1272. "My recursive grep to find thing at point from project root."
  1273. (interactive)
  1274. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1275. nil))
  1276. (command-args
  1277. (if cmd
  1278. (concat cmd
  1279. (thing-at-point 'symbol t))
  1280. (error "My-Rgrep: Command for rgrep not found"))))
  1281. (if (safe-require-or-eval 'projectile)
  1282. (projectile-with-default-dir (projectile-project-root)
  1283. (compilation-start command-args
  1284. 'grep-mode))
  1285. (compilation-start command-args
  1286. 'grep-mode))))
  1287. (defmacro define-my-rgrep (name)
  1288. "Define rgrep for NAME."
  1289. `(defun ,(intern (concat "my-rgrep-"
  1290. name)) ()
  1291. ,(format "My recursive grep by %s."
  1292. name)
  1293. (interactive)
  1294. (let ((my-rgrep-default ,name))
  1295. (if (called-interactively-p 'any)
  1296. (call-interactively 'my-rgrep)
  1297. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1298. )
  1299. (define-my-rgrep "ack")
  1300. (define-my-rgrep "ag")
  1301. (define-my-rgrep "rg")
  1302. (define-my-rgrep "sift")
  1303. (define-my-rgrep "gitgrep")
  1304. (define-my-rgrep "grep")
  1305. (define-my-rgrep "global")
  1306. (define-key ctl-x-map "s" 'my-rgrep)
  1307. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1308. (defalias 'make 'compile)
  1309. (define-key ctl-x-map "c" 'compile)
  1310. ;;;;;;;;;;;;;;;;;;;;;;;
  1311. ;; adoc-simple-mode
  1312. (when (safe-require-or-eval 'adoc-mode)
  1313. (defvar adoc-simple-font-lock-keywords
  1314. nil)
  1315. (define-derived-mode adoc-simple-mode adoc-mode
  1316. "Adoc-Simple"
  1317. "Major mode for editing AsciiDoc text files.
  1318. This mode is a simplified version of `adoc-mode'."
  1319. '(set (make-local-variable 'font-lock-defaults)
  1320. '(adoc-simple-font-lock-keywords
  1321. nil nil nil nil
  1322. (font-lock-multiline . t)
  1323. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1324. (add-to-list 'auto-mode-alist
  1325. '("\\.adoc\\'" . adoc-simple-mode)))
  1326. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1327. ;; awk-preview
  1328. (defgroup awk-preview nil
  1329. "Awk previewer."
  1330. :tag "Awk Preview"
  1331. :prefix "awk-preview-"
  1332. :group 'tools)
  1333. (defcustom awk-preview-program
  1334. (or (executable-find "gawk")
  1335. (executable-find "awk")
  1336. "awk")
  1337. "Awk program to execute."
  1338. :type 'string
  1339. :group 'awk-preview)
  1340. (defcustom awk-preview-switches
  1341. '("--sandbox" "--" "{print}")
  1342. "String of awk options appended when running awk preview."
  1343. :type '(repeat string)
  1344. :group 'awk-preview)
  1345. (defun awk-preview--invoke (start end output)
  1346. "Execute awk process and get result."
  1347. (let ((proc (apply 'start-process
  1348. "awk-preview"
  1349. output
  1350. awk-preview-program
  1351. awk-preview-switches)))
  1352. (message "%S" proc)
  1353. (process-send-region proc start end)
  1354. (process-send-eof proc)
  1355. (accept-process-output proc)
  1356. )
  1357. output)
  1358. (defun awk-preview (start end)
  1359. "Run awk and preview result."
  1360. (interactive "r")
  1361. (let ((output (with-current-buffer (get-buffer-create "*awk-preview output*")
  1362. (erase-buffer)
  1363. (current-buffer))))
  1364. (awk-preview--invoke start end output)
  1365. (message "awk-preview: %s" (with-current-buffer output
  1366. (buffer-substring-no-properties (point-min) (point-max))))
  1367. ))
  1368. (defun my-file-head (filename &optional n)
  1369. "Return list of first N lines of file FILENAME."
  1370. (let ((num (or n 10))
  1371. (size 100)
  1372. (beg 0)
  1373. (end 0)
  1374. (result '()))
  1375. (with-temp-buffer
  1376. (while (<= (count-lines (point-min)
  1377. (point-max))
  1378. num)
  1379. (setq end (+ beg size))
  1380. (insert-file-contents filename
  1381. nil
  1382. beg
  1383. end)
  1384. (setq beg end))
  1385. (goto-char (point-min))
  1386. (while (< (length result) num)
  1387. (let ((start (point)))
  1388. (forward-line 1)
  1389. (setq result
  1390. `(,@result ,(buffer-substring-no-properties start
  1391. (point))))))
  1392. result)))
  1393. ;; (apply 'concat (car (my-file-head "./emacs.el"))
  1394. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1395. ;; Local Variables:
  1396. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1397. ;; flycheck-checker: emacs-lisp
  1398. ;; End:
  1399. ;;; emancs.el ends here