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.
 
 
 
 
 
 

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