Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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