Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

2015 Zeilen
64 KiB

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