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

2920 lines
99 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. gosh-mode
  80. scala-mode
  81. ;;ensime
  82. ;; ack
  83. color-moccur
  84. ggtags
  85. flycheck
  86. auto-highlight-symbol
  87. hl-todo
  88. ;; Currently not available
  89. ;; pp-c-l
  90. xclip
  91. foreign-regexp
  92. multi-term
  93. term-run
  94. editorconfig
  95. git-ps1-mode
  96. restart-emacs
  97. fill-column-indicator
  98. pkgbuild-mode
  99. minibuffer-line
  100. which-key
  101. ;; I think this works in place of my autosave lib
  102. super-save
  103. pipenv
  104. imenu-list
  105. page-break-lines
  106. ;; sync-recentf
  107. aggressive-indent
  108. ;; fancy-narrow
  109. dired-filter
  110. wgrep
  111. magit
  112. git-gutter
  113. end-mark
  114. sl
  115. editorconfig
  116. editorconfig-custom-majormode
  117. git-command
  118. prompt-text
  119. ;; 10sr repository
  120. ;; 10sr-extras
  121. terminal-title
  122. recentf-show
  123. dired-list-all-mode
  124. pack
  125. set-modeline-color
  126. read-only-only-mode
  127. smart-revert
  128. autosave
  129. ;;window-organizer
  130. ilookup
  131. pasteboard
  132. ))
  133. (when (safe-require-or-eval 'package)
  134. (setq package-archives
  135. `(,@package-archives
  136. ("melpa" . "https://melpa.org/packages/")
  137. ;; Somehow fails to download via https
  138. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  139. (package-initialize)
  140. (defun my-auto-install-package ()
  141. "Install packages semi-automatically."
  142. (interactive)
  143. (package-refresh-contents)
  144. (mapc (lambda (pkg)
  145. (or (package-installed-p pkg)
  146. (package-install pkg)))
  147. 10sr-package-list))
  148. )
  149. ;; (lazy-load-eval 'sudoku)
  150. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  151. ;; my-idle-hook
  152. (defvar my-idle-hook nil
  153. "Hook run when idle for several secs.")
  154. (defvar my-idle-hook-sec 5
  155. "Second to run `my-idle-hook'.")
  156. (run-with-idle-timer my-idle-hook-sec
  157. t
  158. (lambda ()
  159. (run-hooks 'my-idle-hook)))
  160. ;; (add-hook 'my-idle-hook
  161. ;; (lambda ()
  162. ;; (message "idle hook message")))
  163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  164. ;; start and quit
  165. (setq inhibit-startup-message t)
  166. (setq confirm-kill-emacs 'y-or-n-p)
  167. (setq gc-cons-threshold (* 1024 1024 16))
  168. (setq garbage-collection-messages nil)
  169. (when window-system
  170. (add-to-list 'default-frame-alist '(cursor-type . box))
  171. (add-to-list 'default-frame-alist '(background-color . "white"))
  172. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  173. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  174. ;; does not work?
  175. )
  176. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  177. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  178. (and (fboundp 'tool-bar-mode)
  179. (tool-bar-mode 0))
  180. (and (fboundp 'set-scroll-bar-mode)
  181. (set-scroll-bar-mode nil))
  182. (call-after-init
  183. (lambda ()
  184. (message "%s %s" invocation-name emacs-version)
  185. (message "Invocation directory: %s" default-directory)
  186. (message "%s was taken to initialize emacs." (emacs-init-time))
  187. (view-echo-area-messages)
  188. ;; (view-emacs-news)
  189. ))
  190. (cd ".") ; when using windows use / instead of \ in `default-directory'
  191. ;; locale
  192. (set-language-environment "Japanese")
  193. (set-default-coding-systems 'utf-8-unix)
  194. (prefer-coding-system 'utf-8-unix)
  195. (setq system-time-locale "C")
  196. ;; my prefix map
  197. (defvar my-prefix-map nil
  198. "My prefix map.")
  199. (define-prefix-command 'my-prefix-map)
  200. ;; (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  201. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  202. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  203. ;; (comint-show-maximum-output)
  204. ;; kill scratch
  205. (call-after-init (lambda ()
  206. (let ((buf (get-buffer "*scratch*")))
  207. (when buf
  208. (kill-buffer buf)))))
  209. ;; modifier keys
  210. ;; (setq mac-option-modifier 'control)
  211. ;; display
  212. (setq visible-bell t)
  213. (setq ring-bell-function 'ignore)
  214. (mouse-avoidance-mode 'banish)
  215. (setq echo-keystrokes 0.1)
  216. (defun reload-init-file ()
  217. "Reload Emacs init file."
  218. (interactive)
  219. (when (and user-init-file
  220. (file-readable-p user-init-file))
  221. (load-file user-init-file)))
  222. (safe-require-or-eval 'session)
  223. ;; server
  224. (set-variable 'server-name (concat "server"
  225. (number-to-string (emacs-pid))))
  226. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  227. ;; In Darwin environment, init fails with message like 'Service name too long'
  228. ;; when server-use-tcp is nil
  229. (when (or (eq system-type
  230. 'cygwin)
  231. (eq system-type
  232. 'darwin))
  233. (set-variable 'server-use-tcp t))
  234. ;; MSYS2 fix
  235. (when (eq system-type
  236. 'windows-nt)
  237. (setq shell-file-name
  238. (executable-find "bash"))
  239. '(setq function-key-map
  240. `(,@function-key-map ([pause] . [?\C-c])
  241. ))
  242. (define-key key-translation-map
  243. (kbd "<pause>")
  244. (kbd "C-c"))
  245. '(keyboard-translate [pause]
  246. (kbd "C-c")p)
  247. ;; TODO: move to other place later
  248. (when (not window-system)
  249. (setq interprogram-paste-function nil)
  250. (setq interprogram-cut-function nil)))
  251. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  252. ;; global keys
  253. (global-set-key (kbd "<up>") 'scroll-down-line)
  254. (global-set-key (kbd "<down>") 'scroll-up-line)
  255. (global-set-key (kbd "<left>") 'scroll-down)
  256. (global-set-key (kbd "<right>") 'scroll-up)
  257. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  258. (global-set-key (kbd "C-\\") help-map)
  259. (define-key ctl-x-map (kbd "DEL") help-map)
  260. (define-key ctl-x-map (kbd "C-h") help-map)
  261. (define-key help-map "a" 'apropos)
  262. ;; disable annoying keys
  263. (global-set-key [prior] 'ignore)
  264. (global-set-key (kbd "<next>") 'ignore)
  265. (global-set-key [menu] 'ignore)
  266. (global-set-key [down-mouse-1] 'ignore)
  267. (global-set-key [down-mouse-2] 'ignore)
  268. (global-set-key [down-mouse-3] 'ignore)
  269. (global-set-key [mouse-1] 'ignore)
  270. (global-set-key [mouse-2] 'ignore)
  271. (global-set-key [mouse-3] 'ignore)
  272. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  273. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  274. (when (safe-require-or-eval 'which-key)
  275. (which-key-mode))
  276. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  277. ;; editor
  278. (setq kill-whole-line t)
  279. (setq scroll-conservatively 35
  280. scroll-margin 2)
  281. (setq-default major-mode 'text-mode)
  282. (setq next-line-add-newlines nil)
  283. (setq kill-read-only-ok t)
  284. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  285. ;; (setq-default line-spacing 0.2)
  286. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  287. ;; (setq-default tab-width 4)
  288. (setq-default indent-tabs-mode nil)
  289. (setq-default indent-line-function 'indent-to-left-margin)
  290. ;; (setq-default indent-line-function nil)
  291. (setq-default truncate-lines nil)
  292. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  293. (delete-selection-mode 1)
  294. (cua-mode 0)
  295. (setq line-move-visual nil)
  296. (setq create-lockfiles nil)
  297. (add-hook 'before-save-hook
  298. 'time-stamp)
  299. ;; Add Time-stamp: <> to insert timestamp there
  300. (set-variable 'time-stamp-format
  301. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  302. ;; key bindings
  303. ;; moving around
  304. ;;(keyboard-translate ?\M-j ?\C-j)
  305. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  306. (define-key esc-map "p" 'backward-paragraph)
  307. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  308. (define-key esc-map "n" 'forward-paragraph)
  309. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  310. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  311. (global-set-key (kbd "C-<left>") 'scroll-down)
  312. (global-set-key (kbd "C-<right>") 'scroll-up)
  313. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  314. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  315. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  316. ;; C-h and DEL
  317. (global-set-key (kbd "C-h") (kbd "DEL"))
  318. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  319. (global-set-key (kbd "C-m") 'newline-and-indent)
  320. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  321. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  322. (define-key esc-map "u" 'undo)
  323. (define-key esc-map "i" (kbd "ESC TAB"))
  324. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  325. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  326. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  327. (require 'page-ext nil t)
  328. (when (safe-require-or-eval 'page-break-lines)
  329. (global-page-break-lines-mode 1))
  330. (when (safe-require-or-eval 'git-gutter)
  331. (custom-set-variables
  332. '(git-gutter:update-interval 2))
  333. (custom-set-variables
  334. '(git-gutter:unchanged-sign " "))
  335. (when (= (display-color-cells)
  336. 256)
  337. (let ((c "color-233"))
  338. (set-face-background 'git-gutter:modified c)
  339. (set-face-background 'git-gutter:added c)
  340. (set-face-background 'git-gutter:deleted c)
  341. (set-face-background 'git-gutter:unchanged c)))
  342. (global-git-gutter-mode 1)
  343. )
  344. ;; (when (safe-require-or-eval 'fancy-narrow)
  345. ;; (fancy-narrow-mode 1))
  346. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  347. ;; title and mode-line
  348. (when (safe-require-or-eval 'terminal-title)
  349. ;; if TERM is not screen use default value
  350. (if (getenv "TMUX")
  351. ;; if use tmux locally just basename of current dir
  352. (set-variable 'terminal-title-format
  353. '((file-name-nondirectory (directory-file-name
  354. default-directory))))
  355. (if (and (let ((tty-type (frame-parameter nil
  356. 'tty-type)))
  357. (and tty-type
  358. (equal (car (split-string tty-type
  359. "-"))
  360. "screen")))
  361. (not (getenv "SSH_CONNECTION")))
  362. (set-variable 'terminal-title-format
  363. '((file-name-nondirectory (directory-file-name
  364. default-directory))))
  365. ;; seems that TMUX is used locally and ssh to remote host
  366. (set-variable 'terminal-title-format
  367. `("em:"
  368. ,user-login-name
  369. "@"
  370. ,(car (split-string (system-name)
  371. "\\."))
  372. ":"
  373. default-directory))
  374. )
  375. )
  376. (terminal-title-mode))
  377. (setq eol-mnemonic-dos "\\r\\n")
  378. (setq eol-mnemonic-mac "\\r")
  379. (setq eol-mnemonic-unix "")
  380. (which-function-mode 1)
  381. (line-number-mode 0)
  382. (column-number-mode 0)
  383. (size-indication-mode 0)
  384. (setq mode-line-position
  385. '(:eval (format "L%%l/%d%s:C%%c"
  386. (count-lines (point-max)
  387. (point-min))
  388. (if (buffer-narrowed-p)
  389. "[N]"
  390. "")
  391. )))
  392. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  393. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  394. ;; minibuffer
  395. (setq insert-default-directory t)
  396. (setq completion-ignore-case t
  397. read-file-name-completion-ignore-case t
  398. read-buffer-completion-ignore-case t)
  399. (setq resize-mini-windows t)
  400. (temp-buffer-resize-mode 1)
  401. (savehist-mode 1)
  402. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  403. (set-variable 'help-at-pt-display-when-idle t)
  404. (fset 'yes-or-no-p 'y-or-n-p)
  405. ;; complete symbol when `eval'
  406. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  407. (define-key minibuffer-local-map (kbd "C-u")
  408. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  409. ;; I dont know these bindings are good
  410. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  411. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  412. (when (safe-require-or-eval 'minibuffer-line)
  413. (set-face-underline 'minibuffer-line nil)
  414. (set-variable 'minibuffer-line-refresh-interval
  415. 25)
  416. ;; Set idle timer
  417. (defvar my-minibuffer-line--idle-timer nil)
  418. (defvar minibuffer-line-mode)
  419. (add-hook 'minibuffer-line-mode-hook
  420. (lambda ()
  421. (when my-minibuffer-line--idle-timer
  422. (cancel-timer my-minibuffer-line--idle-timer)
  423. (setq my-minibuffer-line--idle-timer nil))
  424. (when minibuffer-line-mode
  425. (setq my-minibuffer-line--idle-timer
  426. (run-with-idle-timer 0.5
  427. t
  428. 'minibuffer-line--update)))))
  429. (set-variable 'minibuffer-line-format
  430. `(,(concat user-login-name
  431. "@"
  432. (car (split-string (system-name)
  433. "\\."))
  434. ":")
  435. (:eval (abbreviate-file-name (or buffer-file-name
  436. default-directory)))
  437. (:eval (and (fboundp 'git-ps1-mode-get-current)
  438. (git-ps1-mode-get-current " [GIT:%s]")))
  439. " "
  440. (:eval (format-time-string display-time-format))))
  441. (minibuffer-line-mode 1)
  442. )
  443. (when (safe-require-or-eval 'prompt-text)
  444. (set-variable 'prompt-text-format
  445. `(,(concat ""
  446. user-login-name
  447. "@"
  448. (car (split-string (system-name)
  449. "\\."))
  450. ":")
  451. (:eval (abbreviate-file-name (or buffer-file-name
  452. default-directory)))
  453. (:eval (and (fboundp 'git-ps1-mode-get-current)
  454. (git-ps1-mode-get-current " [GIT:%s]")))
  455. " "
  456. (:eval (format-time-string display-time-format))
  457. "\n"
  458. (:eval (symbol-name this-command))
  459. ": "))
  460. (prompt-text-mode 1))
  461. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  462. ;; letters, font-lock mode and fonts
  463. (setq text-quoting-style 'grave)
  464. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  465. ;; (set-window-margins (selected-window) 1 1)
  466. (unless window-system
  467. (setq frame-background-mode 'dark))
  468. (and (or (eq system-type 'Darwin)
  469. (eq system-type 'darwin))
  470. (fboundp 'mac-set-input-method-parameter)
  471. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  472. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  473. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  474. (boundp 'input-method-inactivate-hook))
  475. (add-hook 'input-method-activate-hook
  476. (lambda () (set-cursor-color "red")))
  477. (add-hook 'input-method-inactivate-hook
  478. (lambda () (set-cursor-color "black"))))
  479. (when (safe-require-or-eval 'paren)
  480. (show-paren-mode 1)
  481. (setq show-paren-delay 0.5
  482. show-paren-style 'parenthesis) ; mixed is hard to read
  483. ;; (set-face-background 'show-paren-match
  484. ;; "black")
  485. ;; ;; (face-foreground 'default))
  486. ;; (set-face-foreground 'show-paren-match
  487. ;; "white")
  488. ;; (set-face-inverse-video-p 'show-paren-match
  489. ;; t)
  490. )
  491. (transient-mark-mode 1)
  492. (global-font-lock-mode 1)
  493. (setq font-lock-global-modes
  494. '(not
  495. help-mode
  496. eshell-mode
  497. ;;term-mode
  498. Man-mode
  499. magit-diff-mode
  500. magit-revision-mode))
  501. ;; (standard-display-ascii ?\n "$\n")
  502. ;; (defvar my-eol-face
  503. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  504. ;; )
  505. ;; (defvar my-tab-face
  506. ;; '(("\t" . '(0 highlight t nil))))
  507. (defvar my-jspace-face
  508. '(("\u3000" . '(0 highlight t nil))))
  509. (add-hook 'font-lock-mode-hook
  510. (lambda ()
  511. ;; (font-lock-add-keywords nil my-eol-face)
  512. (font-lock-add-keywords nil my-jspace-face)
  513. ))
  514. (when (safe-require-or-eval 'whitespace)
  515. (add-to-list 'whitespace-display-mappings
  516. ;; We need t since last one takes precedence
  517. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  518. ;; (add-to-list 'whitespace-display-mappings
  519. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  520. (setq whitespace-style '(face
  521. trailing ; trailing blanks
  522. ;; tabs
  523. ;; spaces
  524. ;; lines
  525. lines-tail ; lines over 80
  526. newline ; newlines
  527. empty ; empty lines at beg or end of buffer
  528. ;; big-indent
  529. ;; space-mark
  530. tab-mark
  531. newline-mark ; use display table for newline
  532. ))
  533. ;; (setq whitespace-newline 'font-lock-comment-face)
  534. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  535. (defun my-whitesspace-mode-reload ()
  536. "Reload whitespace-mode config."
  537. (interactive)
  538. (when whitespace-mode
  539. (whitespace-mode 0)
  540. (whitespace-mode 1)))
  541. (set-variable 'whitespace-line-column nil)
  542. (global-whitespace-mode t)
  543. (add-hook 'dired-mode-hook
  544. (lambda ()
  545. (set (make-local-variable 'whitespace-style) nil)))
  546. (if (= (display-color-cells)
  547. 256)
  548. (set-face-foreground 'whitespace-newline "color-109")
  549. ;; (progn
  550. ;; (set-face-bold-p 'whitespace-newline
  551. ;; t))
  552. ))
  553. (and nil
  554. '(safe-require-or-eval 'fill-column-indicator)
  555. (setq fill-column-indicator))
  556. ;; highlight current line
  557. ;; http://wiki.riywo.com/index.php?Meadow
  558. (face-spec-set 'hl-line
  559. '((((min-colors 256)
  560. (background dark))
  561. (:background "color-234"))
  562. (((min-colors 256)
  563. (background light))
  564. (:background "color-234"))
  565. (t
  566. (:underline "black"))))
  567. (set-variable 'hl-line-global-modes
  568. '(not
  569. term-mode))
  570. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  571. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  572. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  573. ;;(safe-require-or-eval 'set-modeline-color)
  574. ;; (let ((fg (face-foreground 'default))
  575. ;; (bg (face-background 'default)))
  576. ;; (set-face-background 'mode-line-inactive
  577. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  578. ;; (set-face-foreground 'mode-line-inactive
  579. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  580. ;; (set-face-underline 'mode-line-inactive
  581. ;; t)
  582. ;; (set-face-underline 'vertical-border
  583. ;; nil)
  584. ;; (when (safe-require-or-eval 'end-mark)
  585. ;; (global-end-mark-mode))
  586. ;; M-x highlight-* to highlight things
  587. (global-hi-lock-mode 1)
  588. (unless (fboundp 'highlight-region-text)
  589. (defun highlight-region-text (beg end)
  590. "Highlight text between BEG and END."
  591. (interactive "r")
  592. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  593. end)))
  594. (setq deactivate-mark t)))
  595. (when (safe-require-or-eval 'auto-highlight-symbol)
  596. (set-variable 'ahs-idle-interval 0.6)
  597. (global-auto-highlight-symbol-mode 1))
  598. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  599. ;; file handling
  600. (when (safe-require-or-eval 'editorconfig)
  601. ;; (set-variable 'editorconfig-get-properties-function
  602. ;; 'editorconfig-core-get-properties-hash)
  603. (editorconfig-mode 1)
  604. (set-variable 'editorconfig-mode-lighter " EC")
  605. (with-eval-after-load 'org-src
  606. ;; [*.org\[\*Org Src*\[ c \]*\]]
  607. (add-hook 'org-src-mode-hook
  608. 'editorconfig-mode-apply t)))
  609. (when (fboundp 'editorconfig-custom-majormode)
  610. (add-hook 'editorconfig-custom-hooks
  611. 'editorconfig-custom-majormode))
  612. ;; Add readonly=true to set read-only-mode
  613. (add-hook 'editorconfig-custom-hooks
  614. (lambda (props)
  615. (let ((r (gethash 'readonly props)))
  616. (when (and (string= r "true")
  617. (not buffer-read-only))
  618. (read-only-mode 1)))))
  619. ;; (when (fboundp 'editorconfig-charset-extras)
  620. ;; (add-hook 'editorconfig-custom-hooks
  621. ;; 'editorconfig-charset-extras))
  622. (setq revert-without-query '(".+"))
  623. ;; save cursor position
  624. (when (safe-require-or-eval 'saveplace)
  625. (setq-default save-place t)
  626. (setq save-place-file (concat user-emacs-directory
  627. "places")))
  628. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  629. (setq make-backup-files t)
  630. (setq vc-make-backup-files t)
  631. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  632. (setq backup-directory-alist
  633. (cons (cons "." (expand-file-name (concat user-emacs-directory
  634. "backup")))
  635. backup-directory-alist))
  636. (setq version-control 't)
  637. (setq delete-old-versions t)
  638. (setq kept-new-versions 20)
  639. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  640. "auto-save/")))
  641. ;; (setq delete-auto-save-files t)
  642. (setq auto-save-visited-interval 8)
  643. (auto-save-visited-mode 1)
  644. (add-to-list 'completion-ignored-extensions ".bak")
  645. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  646. (setq delete-by-moving-to-trash t)
  647. ;; trash-directory "~/.emacs.d/trash")
  648. (add-hook 'after-save-hook
  649. 'executable-make-buffer-file-executable-if-script-p)
  650. (set-variable 'bookmark-default-file
  651. (expand-file-name (concat user-emacs-directory
  652. "bmk")))
  653. (set-variable 'bookmark-save-flag
  654. 1)
  655. (with-eval-after-load 'recentf
  656. (defvar recentf-exclude)
  657. (defvar bookmark-default-file)
  658. (add-to-list 'recentf-exclude
  659. (regexp-quote bookmark-default-file)))
  660. (when (safe-require-or-eval 'smart-revert)
  661. (smart-revert-on))
  662. ;; autosave
  663. ;; auto-save-visited-mode can be used instead?
  664. ;; (when (safe-require-or-eval 'autosave)
  665. ;; (autosave-set 8))
  666. ;; bookmarks
  667. (define-key ctl-x-map "m" 'list-bookmarks)
  668. ;; vc
  669. (set-variable 'vc-handled-backends '(RCS))
  670. (set-variable 'vc-rcs-register-switches "-l")
  671. (set-variable 'vc-rcs-checkin-switches "-l")
  672. (set-variable 'vc-command-messages t)
  673. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  674. ;; share clipboard with x
  675. ;; this page describes this in details, but only these sexps seem to be needed
  676. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  677. (and nil
  678. (not window-system)
  679. (not (eq window-system 'mac))
  680. (getenv "DISPLAY")
  681. (not (equal (getenv "DISPLAY") ""))
  682. (executable-find "xclip")
  683. ;; (< emacs-major-version 24)
  684. '(safe-require-or-eval 'xclip)
  685. nil
  686. (turn-on-xclip))
  687. (and (eq system-type 'darwin)
  688. (safe-require-or-eval 'pasteboard)
  689. (turn-on-pasteboard))
  690. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  691. ;; some modes and hooks
  692. ;; Include some extra modes
  693. (require 'generic-x)
  694. (when (safe-require-or-eval 'wgrep)
  695. (defvar grep-mode-map)
  696. (define-key grep-mode-map
  697. "e"
  698. 'wgrep-change-to-wgrep-mode))
  699. (with-eval-after-load 'remember
  700. (defvar remember-mode-map (make-sparse-keymap))
  701. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  702. (with-eval-after-load 'magit-files
  703. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  704. ;; existing keybindings.
  705. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  706. ;; to disable thie mode here, but do anyway.
  707. ;; See also https://github.com/magit/magit/issues/3517
  708. (global-magit-file-mode -1))
  709. (with-eval-after-load 'magit-section
  710. (set-face-background 'magit-section-highlight
  711. nil))
  712. (with-eval-after-load 'magit-diff
  713. (set-face-background 'magit-diff-added-highlight
  714. nil)
  715. (set-face-background 'magit-diff-removed-highlight
  716. nil)
  717. (set-face-background 'magit-diff-context-highlight
  718. nil)
  719. )
  720. (when (boundp 'git-rebase-filename-regexp)
  721. (add-to-list 'auto-mode-alist
  722. `(,git-rebase-filename-regexp . text-mode)))
  723. (when (safe-require-or-eval 'aggressive-indent)
  724. (defvar aggressive-indent-excluded-modes)
  725. (setq aggressive-indent-excluded-modes
  726. `(diff-mode
  727. toml-mode
  728. conf-mode
  729. dockerfile-mode
  730. ,@aggressive-indent-excluded-modes))
  731. (global-aggressive-indent-mode 1))
  732. (when (autoload-eval-lazily 'ggtags)
  733. (add-hook 'c-mode-common-hook
  734. (lambda ()
  735. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  736. (ggtags-mode 1))))
  737. (add-hook 'python-mode-hook
  738. (lambda ()
  739. (ggtags-mode 1))))
  740. (when (autoload-eval-lazily 'imenu-list)
  741. ;; (set-variable 'imenu-list-auto-resize t)
  742. (set-variable 'imenu-list-focus-after-activation t)
  743. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  744. (add-hook 'emacs-lisp-mode-hook
  745. (lambda ()
  746. (setq imenu-generic-expression
  747. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  748. ,@imenu-generic-expression))))
  749. (with-eval-after-load 'compile
  750. (defvar compilation-filter-start)
  751. (defvar compilation-error-regexp-alist)
  752. (require 'ansi-color)
  753. (add-hook 'compilation-filter-hook
  754. (lambda ()
  755. (let ((inhibit-read-only t))
  756. (ansi-color-apply-on-region compilation-filter-start
  757. (point)))))
  758. (add-to-list 'compilation-error-regexp-alist
  759. ;; ansible-lint
  760. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  761. ;; Workaround to avoid ensime error
  762. (defvar ensime-mode-key-prefix nil)
  763. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  764. (when (safe-require-or-eval 'company)
  765. (global-company-mode)
  766. (set-variable 'company-idle-delay 0.5)
  767. (set-variable 'company-minimum-prefix-length 2)
  768. (set-variable 'company-selection-wrap-around t))
  769. ;; https://github.com/lunaryorn/flycheck
  770. (when (safe-require-or-eval 'flycheck)
  771. (call-after-init 'global-flycheck-mode))
  772. (when (autoload-eval-lazily 'ilookup)
  773. (define-key ctl-x-map "d" 'ilookup-open-word))
  774. (set-variable 'ac-ignore-case nil)
  775. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  776. (define-key ctl-x-map "t" 'term-run-shell-command))
  777. (add-to-list 'safe-local-variable-values
  778. '(encoding utf-8))
  779. (setq enable-local-variables :safe)
  780. ;; Detect file type from shebang and set major-mode.
  781. (add-to-list 'interpreter-mode-alist
  782. '("python3" . python-mode))
  783. (add-to-list 'interpreter-mode-alist
  784. '("python2" . python-mode))
  785. (with-eval-after-load 'python
  786. (defvar python-mode-map (make-sparse-keymap))
  787. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  788. (when (autoload-eval-lazily 'pipenv)
  789. (add-hook 'python-mode-hook
  790. (lambda ()
  791. (pipenv-mode 1)
  792. (pipenv-projectile-after-switch-default)))
  793. )
  794. (set-variable 'flycheck-python-pycompile-executable "python3")
  795. ;; http://fukuyama.co/foreign-regexp
  796. '(and (safe-require-or-eval 'foreign-regexp)
  797. (progn
  798. (setq foreign-regexp/regexp-type 'perl)
  799. '(setq reb-re-syntax 'foreign-regexp)
  800. ))
  801. (autoload-eval-lazily 'sql '(sql-mode)
  802. (require 'sql-indent nil t))
  803. (when (autoload-eval-lazily 'git-command)
  804. (define-key ctl-x-map "g" 'git-command))
  805. (when (safe-require-or-eval 'git-commit)
  806. (global-git-commit-mode 1))
  807. (with-eval-after-load 'git-commit
  808. (add-hook 'git-commit-setup-hook
  809. 'turn-off-auto-fill t))
  810. (autoload-eval-lazily 'sl)
  811. (with-eval-after-load 'rst
  812. (defvar rst-mode-map)
  813. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  814. (with-eval-after-load 'jdee
  815. (add-hook 'jdee-mode-hook
  816. (lambda ()
  817. (make-local-variable 'global-mode-string)
  818. (add-to-list 'global-mode-string
  819. mode-line-position))))
  820. ;; Cannot enable error thrown. Why???
  821. ;; https://github.com/m0smith/malabar-mode#Installation
  822. ;; (when (autoload-eval-lazily 'malabar-mode)
  823. ;; (add-to-list 'load-path
  824. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  825. ;; (safe-require-or-eval 'cedet-devel-load)
  826. ;; (call-after-init 'activate-malabar-mode))
  827. (with-eval-after-load 'make-mode
  828. (defvar makefile-mode-map (make-sparse-keymap))
  829. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  830. ;; this functions is set in write-file-functions, i cannot find any
  831. ;; good way to remove this.
  832. (fset 'makefile-warn-suspicious-lines 'ignore))
  833. (with-eval-after-load 'verilog-mode
  834. (defvar verilog-mode-map (make-sparse-keymap))
  835. (define-key verilog-mode-map ";" 'self-insert-command))
  836. (setq diff-switches "-u")
  837. (with-eval-after-load 'diff-mode
  838. ;; (when (and (eq major-mode
  839. ;; 'diff-mode)
  840. ;; (not buffer-file-name))
  841. ;; ;; do not pass when major-mode is derived mode of diff-mode
  842. ;; (view-mode 1))
  843. (set-face-attribute 'diff-header nil
  844. :foreground nil
  845. :background nil
  846. :weight 'bold)
  847. (set-face-attribute 'diff-file-header nil
  848. :foreground nil
  849. :background nil
  850. :weight 'bold)
  851. (set-face-foreground 'diff-index "blue")
  852. (set-face-attribute 'diff-hunk-header nil
  853. :foreground "cyan"
  854. :weight 'normal)
  855. (set-face-attribute 'diff-context nil
  856. ;; :foreground "white"
  857. :foreground nil
  858. :weight 'normal)
  859. (set-face-foreground 'diff-removed "red")
  860. (set-face-foreground 'diff-added "green")
  861. (set-face-background 'diff-removed nil)
  862. (set-face-background 'diff-added nil)
  863. (set-face-attribute 'diff-changed nil
  864. :foreground "magenta"
  865. :weight 'normal)
  866. (set-face-attribute 'diff-refine-changed nil
  867. :foreground nil
  868. :background nil
  869. :weight 'bold
  870. :inverse-video t)
  871. ;; Annoying !
  872. ;;(diff-auto-refine-mode)
  873. )
  874. ;; (ffap-bindings)
  875. (set-variable 'browse-url-browser-function
  876. 'eww-browse-url)
  877. (set-variable 'sh-here-document-word "__EOC__")
  878. (when (autoload-eval-lazily 'adoc-mode
  879. nil
  880. (defvar adoc-mode-map (make-sparse-keymap))
  881. (define-key adoc-mode-map (kbd "C-m") 'newline))
  882. (setq auto-mode-alist
  883. `(("\\.adoc\\'" . adoc-mode)
  884. ("\\.asciidoc\\'" . adoc-mode)
  885. ,@auto-mode-alist)))
  886. (with-eval-after-load 'markup-faces
  887. ;; Is this too match ?
  888. (set-face-foreground 'markup-meta-face
  889. "color-245")
  890. (set-face-foreground 'markup-meta-hide-face
  891. "color-245")
  892. )
  893. ;; TODO: check if this is required
  894. (when (autoload-eval-lazily 'groovy-mode nil
  895. (defvar groovy-mode-map (make-sparse-keymap))
  896. (define-key groovy-mode-map "(" 'self-insert-command)
  897. (define-key groovy-mode-map ")" 'self-insert-command)
  898. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  899. )
  900. (add-to-list 'auto-mode-alist
  901. '("build\\.gradle\\'" . groovy-mode)))
  902. (with-eval-after-load 'yaml-mode
  903. (defvar yaml-mode-map (make-sparse-keymap))
  904. (define-key yaml-mode-map (kbd "C-m") 'newline))
  905. (with-eval-after-load 'html-mode
  906. (defvar html-mode-map (make-sparse-keymap))
  907. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  908. (with-eval-after-load 'text-mode
  909. (define-key text-mode-map (kbd "C-m") 'newline))
  910. (autoload-eval-lazily 'info nil
  911. (defvar Info-additional-directory-list)
  912. (dolist (dir (directory-files (concat user-emacs-directory
  913. "info")
  914. t
  915. "^[^.].*"))
  916. (when (file-directory-p dir)
  917. (add-to-list 'Info-additional-directory-list
  918. dir)))
  919. (let ((dir (expand-file-name "~/.brew/share/info")))
  920. (when (file-directory-p dir)
  921. (add-to-list 'Info-additional-directory-list
  922. dir))))
  923. (with-eval-after-load 'apropos
  924. (defvar apropos-mode-map (make-sparse-keymap))
  925. (define-key apropos-mode-map "n" 'next-line)
  926. (define-key apropos-mode-map "p" 'previous-line))
  927. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  928. ;; (define-key isearch-mode-map
  929. ;; (kbd "C-j") 'isearch-other-control-char)
  930. ;; (define-key isearch-mode-map
  931. ;; (kbd "C-k") 'isearch-other-control-char)
  932. ;; (define-key isearch-mode-map
  933. ;; (kbd "C-h") 'isearch-other-control-char)
  934. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  935. (define-key isearch-mode-map (kbd "M-r")
  936. 'isearch-query-replace-regexp)
  937. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  938. (setq lazy-highlight-cleanup nil)
  939. ;; face for isearch highlighing
  940. (set-face-attribute 'lazy-highlight
  941. nil
  942. :foreground `unspecified
  943. :background `unspecified
  944. :underline t
  945. ;; :weight `bold
  946. )
  947. (add-hook 'outline-mode-hook
  948. (lambda ()
  949. (when (string-match "\\.md\\'" buffer-file-name)
  950. (set (make-local-variable 'outline-regexp) "#+ "))))
  951. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  952. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  953. (when (autoload-eval-lazily 'markdown-mode
  954. '(markdown-mode gfm-mode)
  955. (defvar gfm-mode-map (make-sparse-keymap))
  956. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  957. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  958. (set-variable 'markdown-command (or (executable-find "markdown")
  959. (executable-find "markdown.pl")
  960. ""))
  961. (add-hook 'markdown-mode-hook
  962. (lambda ()
  963. (outline-minor-mode 1)
  964. (flyspell-mode)
  965. (set (make-local-variable 'comment-start) ";")))
  966. )
  967. ;; c-mode
  968. ;; http://www.emacswiki.org/emacs/IndentingC
  969. ;; http://en.wikipedia.org/wiki/Indent_style
  970. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  971. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  972. (with-eval-after-load 'cc-vars
  973. (defvar c-default-style nil)
  974. (add-to-list 'c-default-style
  975. '(c-mode . "k&r"))
  976. (add-to-list 'c-default-style
  977. '(c++-mode . "k&r")))
  978. (autoload-eval-lazily 'js2-mode nil
  979. ;; currently do not use js2-mode
  980. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  981. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  982. ;; (defvar js2-mode-map (make-sparse-keymap))
  983. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  984. ;; (interactive)
  985. ;; (js2-enter-key)
  986. ;; (indent-for-tab-command)))
  987. ;; (add-hook (kill-local-variable 'before-save-hook)
  988. ;; 'js2-before-save)
  989. ;; (add-hook 'before-save-hook
  990. ;; 'my-indent-buffer
  991. ;; nil
  992. ;; t)
  993. )
  994. (add-to-list 'interpreter-mode-alist
  995. '("node" . js-mode))
  996. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  997. (with-eval-after-load 'uniquify
  998. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  999. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1000. (setq uniquify-min-dir-content 1))
  1001. (with-eval-after-load 'view
  1002. (defvar view-mode-map (make-sparse-keymap))
  1003. (define-key view-mode-map "j" 'scroll-up-line)
  1004. (define-key view-mode-map "k" 'scroll-down-line)
  1005. (define-key view-mode-map "v" 'toggle-read-only)
  1006. (define-key view-mode-map "q" 'bury-buffer)
  1007. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1008. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1009. ;; (define-key view-mode-map
  1010. ;; "n" 'nonincremental-repeat-search-forward)
  1011. ;; (define-key view-mode-map
  1012. ;; "N" 'nonincremental-repeat-search-backward)
  1013. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1014. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1015. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1016. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1017. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1018. (global-set-key "\M-r" 'view-mode)
  1019. ;; (setq view-read-only t)
  1020. (with-eval-after-load 'term
  1021. (defvar term-raw-map (make-sparse-keymap))
  1022. (define-key term-raw-map (kbd "C-x")
  1023. (lookup-key (current-global-map)
  1024. (kbd "C-x"))))
  1025. (add-hook 'term-mode-hook
  1026. (lambda ()
  1027. ;; Stop current line highlighting
  1028. (set (make-local-variable (defvar hl-line-range-function))
  1029. (lambda () '(0 . 0)))
  1030. (set (make-local-variable 'scroll-margin)
  1031. 0)))
  1032. (add-hook 'Man-mode-hook
  1033. (lambda ()
  1034. (view-mode 1)
  1035. (setq truncate-lines nil)))
  1036. (set-variable 'Man-notify-method (if window-system
  1037. 'newframe
  1038. 'aggressive))
  1039. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1040. "woman_cache.el")))
  1041. ;; not work because man.el will be loaded when man called
  1042. (defalias 'man 'woman)
  1043. (add-to-list 'auto-mode-alist
  1044. '("tox\\.ini\\'" . conf-unix-mode))
  1045. (when (autoload-eval-lazily 'toml-mode)
  1046. (add-to-list 'auto-mode-alist
  1047. '("/tox\\.ini\\'" . toml-mode))
  1048. (add-to-list 'auto-mode-alist
  1049. '("/Pipfile\\'" . toml-mode)))
  1050. (when (autoload-eval-lazily 'json-mode)
  1051. (add-to-list 'auto-mode-alist
  1052. '("/Pipfile\\.lock\\'" . json-mode)))
  1053. (add-hook 'go-mode-hook
  1054. (lambda()
  1055. (defvar go-mode-map)
  1056. (add-hook 'before-save-hook' 'gofmt-before-save)
  1057. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1058. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1059. ;; buffers
  1060. (defvar bs-configurations)
  1061. (when (autoload-eval-lazily 'bs '(bs-show)
  1062. (add-to-list 'bs-configurations
  1063. '("specials" "^\\*" nil ".*" nil nil))
  1064. (add-to-list 'bs-configurations
  1065. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1066. (defvar bs-mode-map)
  1067. (defvar bs-current-configuration)
  1068. (define-key bs-mode-map (kbd "t")
  1069. ;; TODO: fix toggle feature
  1070. (lambda ()
  1071. (interactive)
  1072. (if (string= "specials"
  1073. bs-current-configuration)
  1074. (bs-set-configuration "files")
  1075. (bs-set-configuration "specials"))
  1076. (bs-refresh)
  1077. (bs-message-without-log "%s"
  1078. (bs--current-config-message))))
  1079. ;; (setq bs-configurations (list
  1080. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1081. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1082. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1083. )
  1084. (defalias 'list-buffers 'bs-show)
  1085. (set-variable 'bs-default-configuration "files-and-specials")
  1086. (set-variable 'bs-default-sort-name "by nothing")
  1087. (add-hook 'bs-mode-hook
  1088. (lambda ()
  1089. (set (make-local-variable 'scroll-margin) 0))))
  1090. ;;(iswitchb-mode 1)
  1091. (icomplete-mode)
  1092. (defun iswitchb-buffer-display-other-window ()
  1093. "Do iswitchb in other window."
  1094. (interactive)
  1095. (let ((iswitchb-default-method 'display))
  1096. (call-interactively 'iswitchb-buffer)))
  1097. ;; buffer killing
  1098. ;; (defun my-delete-window-killing-buffer () nil)
  1099. (defun my-query-kill-current-buffer ()
  1100. "Interactively kill current buffer."
  1101. (interactive)
  1102. (if (y-or-n-p (concat "kill current buffer? :"))
  1103. (kill-buffer (current-buffer))))
  1104. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1105. (substitute-key-definition 'kill-buffer
  1106. 'my-query-kill-current-buffer
  1107. global-map)
  1108. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1109. ;; recentf-mode
  1110. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1111. "recentf")))
  1112. (set-variable 'recentf-max-menu-items 20)
  1113. (set-variable 'recentf-max-saved-items 30)
  1114. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1115. (set-variable 'recentf-auto-cleanup 3)
  1116. ;; (safe-require-or-eval 'sync-recentf)
  1117. ;; (when (safe-require-or-eval 'recentf)
  1118. ;; (add-to-list 'recentf-exclude
  1119. ;; (regexp-quote recentf-save-file))
  1120. ;; (add-to-list 'recentf-exclude
  1121. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1122. ;; (add-to-list 'recentf-exclude
  1123. ;; "/sync-recentf-marker\\'")
  1124. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1125. ;; (remove-hook 'find-file-hook
  1126. ;; 'recentf-track-opened-file)
  1127. ;; (defun my-recentf-load-track-save-list ()
  1128. ;; "Load current recentf list from file, track current visiting file, then save
  1129. ;; the list."
  1130. ;; (recentf-load-list)
  1131. ;; (recentf-track-opened-file)
  1132. ;; (recentf-save-list))
  1133. ;; (add-hook 'find-file-hook
  1134. ;; 'my-recentf-load-track-save-list)
  1135. ;; (add-hook 'kill-emacs-hook
  1136. ;; 'recentf-load-list)
  1137. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1138. ;; ;; (add-hook 'find-file-hook
  1139. ;; ;; (lambda ()
  1140. ;; ;; (recentf-add-file default-directory)))
  1141. ;; (when (autoload-eval-lazily 'recentf-show)
  1142. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1143. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1144. ;; ;; 'recentf-load-list)
  1145. ;; )
  1146. ;; (recentf-mode 1)
  1147. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1148. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1149. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1150. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1151. ;; (add-hook 'recentf-dialog-mode-hook
  1152. ;; (lambda ()
  1153. ;; ;; (recentf-save-list)
  1154. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1155. ;; ;; 'my-recentf-cd-and-find-file)
  1156. ;; (cd "~/"))))
  1157. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1158. ;; dired
  1159. (defun my-file-head (filename &optional n)
  1160. "Return list of first N lines of file FILENAME."
  1161. ;; TODO: Fix for janapese text
  1162. ;; TODO: Fix for short text
  1163. (let ((num (or n 10))
  1164. (size 100)
  1165. (beg 0)
  1166. (end 0)
  1167. (result '())
  1168. (read -1))
  1169. (with-temp-buffer
  1170. (erase-buffer)
  1171. (while (or (<= (count-lines (point-min)
  1172. (point-max))
  1173. num)
  1174. (eq read 0))
  1175. (setq end (+ beg size))
  1176. (setq read (nth 1 (insert-file-contents-literally filename
  1177. nil
  1178. beg
  1179. end)))
  1180. (goto-char (point-max))
  1181. (setq beg (+ beg size)))
  1182. (goto-char (point-min))
  1183. (while (< (length result) num)
  1184. (let ((start (point)))
  1185. (forward-line 1)
  1186. (setq result
  1187. `(,@result ,(buffer-substring-no-properties start
  1188. (point))))))
  1189. result
  1190. ;; (buffer-substring-no-properties (point-min)
  1191. ;; (progn
  1192. ;; (forward-line num)
  1193. ;; (point)))
  1194. )))
  1195. ;; (apply 'concat (my-file-head "./shrc" 10)
  1196. (defun my-dired-echo-file-head (arg)
  1197. "Echo head of current file.
  1198. ARG is num to show, or defaults to 7."
  1199. (interactive "P")
  1200. (let ((f (dired-get-filename)))
  1201. (message "%s"
  1202. (apply 'concat
  1203. (my-file-head f
  1204. 7)))))
  1205. (defun my-dired-diff ()
  1206. "Show diff of marked file and file of current line."
  1207. (interactive)
  1208. (let ((files (dired-get-marked-files nil nil nil t)))
  1209. (if (eq (car files)
  1210. t)
  1211. (diff (cadr files) (dired-get-filename))
  1212. (message "One file must be marked!"))))
  1213. (defun dired-get-file-info ()
  1214. "Print information of current line file."
  1215. (interactive)
  1216. (let ((f (shell-quote-argument (dired-get-filename t))))
  1217. (if (file-directory-p f)
  1218. (progn
  1219. (message "Calculating disk usage...")
  1220. (shell-command (concat "du -hsD "
  1221. f)))
  1222. (shell-command (concat "file "
  1223. f)))))
  1224. (defun my-dired-scroll-up ()
  1225. "Scroll up."
  1226. (interactive)
  1227. (my-dired-previous-line (- (window-height) 1)))
  1228. (defun my-dired-scroll-down ()
  1229. "Scroll down."
  1230. (interactive)
  1231. (my-dired-next-line (- (window-height) 1)))
  1232. ;; (defun my-dired-forward-line (arg)
  1233. ;; ""
  1234. ;; (interactive "p"))
  1235. (defun my-dired-previous-line (arg)
  1236. "Move ARG lines up."
  1237. (interactive "p")
  1238. (if (> arg 0)
  1239. (progn
  1240. (if (eq (line-number-at-pos)
  1241. 1)
  1242. (goto-char (point-max))
  1243. (forward-line -1))
  1244. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1245. (dired-get-subdir))
  1246. (- arg 1)
  1247. arg)))
  1248. (dired-move-to-filename)))
  1249. (defun my-dired-next-line (arg)
  1250. "Move ARG lines down."
  1251. (interactive "p")
  1252. (if (> arg 0)
  1253. (progn
  1254. (if (eq (point)
  1255. (point-max))
  1256. (goto-char (point-min))
  1257. (forward-line 1))
  1258. (my-dired-next-line (if (or (dired-get-filename nil t)
  1259. (dired-get-subdir))
  1260. (- arg 1)
  1261. arg)))
  1262. (dired-move-to-filename)))
  1263. (defun my-tramp-remote-find-file (f)
  1264. "Open F."
  1265. (interactive (list (read-file-name "My Find File Tramp: "
  1266. "/scp:"
  1267. nil ;; "/scp:"
  1268. (confirm-nonexistent-file-or-buffer))))
  1269. (find-file f))
  1270. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1271. (if (eq window-system 'mac)
  1272. (setq dired-listing-switches "-lhF")
  1273. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1274. )
  1275. (setq dired-listing-switches "-lhF")
  1276. ;; when using dired-find-alternate-file
  1277. ;; reuse current dired buffer for the file to open
  1278. ;; (put 'dired-find-alternate-file 'disabled nil)
  1279. (set-variable 'dired-ls-F-marks-symlinks t)
  1280. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1281. (set-variable 'ls-lisp-dirs-first t)
  1282. (set-variable 'ls-lisp-use-localized-time-format t)
  1283. (set-variable 'ls-lisp-format-time-list
  1284. '("%Y-%m-%d %H:%M"
  1285. "%Y-%m-%d "))
  1286. (set-variable 'dired-dwim-target t)
  1287. (set-variable 'dired-isearch-filenames t)
  1288. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1289. (set-variable 'dired-hide-details-hide-information-lines nil)
  1290. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1291. (set-variable 'dired-recursive-deletes 'always)
  1292. ;; (add-hook 'dired-after-readin-hook
  1293. ;; 'my-replace-nasi-none)
  1294. (with-eval-after-load 'dired
  1295. (safe-require-or-eval 'ls-lisp)
  1296. (defvar dired-mode-map (make-sparse-keymap))
  1297. ;; dired-do-chgrp sometimes cause system hung
  1298. (define-key dired-mode-map "G" 'ignore)
  1299. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1300. (define-key dired-mode-map "i" 'dired-get-file-info)
  1301. (define-key dired-mode-map "f" 'find-file)
  1302. (define-key dired-mode-map "!" 'shell-command)
  1303. (define-key dired-mode-map "&" 'async-shell-command)
  1304. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1305. (define-key dired-mode-map "=" 'my-dired-diff)
  1306. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1307. (define-key dired-mode-map "b" 'gtkbm)
  1308. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1309. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1310. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1311. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1312. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1313. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1314. (substitute-key-definition 'dired-next-line
  1315. 'my-dired-next-line
  1316. dired-mode-map)
  1317. (substitute-key-definition 'dired-previous-line
  1318. 'my-dired-previous-line
  1319. dired-mode-map)
  1320. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1321. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1322. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1323. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1324. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1325. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1326. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1327. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1328. (add-hook 'dired-mode-hook
  1329. (lambda ()
  1330. (when (fboundp 'dired-hide-details-mode)
  1331. (dired-hide-details-mode t)
  1332. (local-set-key "l" 'dired-hide-details-mode))
  1333. (let ((file "._Icon\015"))
  1334. (when nil
  1335. '(file-readable-p file)
  1336. (delete-file file)))))
  1337. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1338. (with-eval-after-load 'dired
  1339. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1340. (when (autoload-eval-lazily 'dired-list-all-mode)
  1341. (setq dired-listing-switches "-lhF")
  1342. (with-eval-after-load 'dired
  1343. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1344. (when (autoload-eval-lazily 'dired-filter)
  1345. (add-hook 'dired-mode-hook
  1346. 'dired-filter-mode))
  1347. ;; Currently disabled in favor of dired-from-git-ls-files
  1348. ;; (define-key ctl-x-map "f" 'find-dired)
  1349. ;; It works!
  1350. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1351. (defun my-dired-git-ls-files (args)
  1352. "Dired from git ls-files."
  1353. (interactive "sgit ls-files args: ")
  1354. (pop-to-buffer-same-window
  1355. (dired-noselect `(,default-directory
  1356. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1357. "\0" t))
  1358. ""))
  1359. )
  1360. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1361. (with-eval-after-load 'dired
  1362. (defvar dired-mode-map (make-sparse-keymap))
  1363. (define-key dired-mode-map "f" 'my-dired-git-ls-files))
  1364. ;; (define-minor-mode my-dired-glob-filter)
  1365. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1366. ;; misc funcs
  1367. (defalias 'qcalc 'quick-calc)
  1368. (defun memo (&optional dir)
  1369. "Open memo.txt in DIR."
  1370. (interactive)
  1371. (pop-to-buffer (find-file-noselect (concat (if dir
  1372. (file-name-as-directory dir)
  1373. "")
  1374. "memo.txt"))))
  1375. (set (defvar my-rgrep-alist nil
  1376. "Alist of rgrep command.
  1377. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1378. condition to choose COMMAND when evaluated.")
  1379. `(
  1380. ;; ripgrep
  1381. ("rg"
  1382. (executable-find "rg")
  1383. "rg --hidden --no-heading --smart-case ")
  1384. ;; git grep
  1385. ("gitgrep"
  1386. (eq 0
  1387. (shell-command "git rev-parse --git-dir"))
  1388. "git --no-pager -c color.grep=always grep -nH -e ")
  1389. ;; sift
  1390. ("sift"
  1391. (executable-find "sift")
  1392. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1393. ;; the silver searcher
  1394. ("ag"
  1395. (executable-find "ag")
  1396. "ag --nogroup --nopager --filename ")
  1397. ;; ack
  1398. ("ack"
  1399. (executable-find "ack")
  1400. "ack --nogroup --nopager --with-filename ")
  1401. ;; gnu global
  1402. ("global"
  1403. (and (require 'ggtags nil t)
  1404. (executable-find "global")
  1405. (ggtags-current-project-root))
  1406. "global --result grep ")
  1407. ;; grep
  1408. ("grep"
  1409. t
  1410. ,(concat "find . "
  1411. "-path '*/.git' -prune -o "
  1412. "-path '*/.svn' -prune -o "
  1413. "-type f -print0 | "
  1414. "xargs -0 grep -nH -e "))
  1415. )
  1416. )
  1417. (defvar my-rgrep-default nil
  1418. "Default command name for my-rgrep.")
  1419. (defun my-rgrep-grep-command (&optional name alist)
  1420. "Return recursive grep command for current directory or nil.
  1421. If NAME is given, use that without testing.
  1422. Commands are searched from ALIST."
  1423. (if alist
  1424. (if name
  1425. ;; if name is given search that from alist and return the command
  1426. (nth 2 (assoc name
  1427. alist))
  1428. ;; if name is not given try test in 1th elem
  1429. (let ((car (car alist))
  1430. (cdr (cdr alist)))
  1431. (if (eval (nth 1 car))
  1432. ;; if the condition is true return the command
  1433. (nth 2 car)
  1434. ;; try next one
  1435. (and cdr
  1436. (my-rgrep-grep-command name cdr)))))
  1437. ;; if alist is not given set default value
  1438. (my-rgrep-grep-command name my-rgrep-alist)))
  1439. (defun my-rgrep (command-args)
  1440. "My recursive grep. Run COMMAND-ARGS.
  1441. If prefix argument is given, use current symbol as default search target
  1442. and search from projectile root (if projectile is available)."
  1443. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1444. nil)))
  1445. (if cmd
  1446. (list (read-shell-command "grep command: "
  1447. (concat cmd
  1448. (if current-prefix-arg
  1449. (thing-at-point 'symbol t)
  1450. ""))
  1451. 'grep-find-history))
  1452. (error "My-Rgrep: Command for rgrep not found")
  1453. )))
  1454. (if (and current-prefix-arg
  1455. (safe-require-or-eval 'projectile)
  1456. (projectile-project-p))
  1457. (projectile-with-default-dir (projectile-project-root)
  1458. (compilation-start command-args
  1459. 'grep-mode))
  1460. (compilation-start command-args
  1461. 'grep-mode)))
  1462. (defun my-rgrep-thing-at-point-projectile-root ()
  1463. "My recursive grep to find thing at point from project root."
  1464. (interactive)
  1465. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1466. nil))
  1467. (command-args
  1468. (if cmd
  1469. (concat cmd
  1470. (or (thing-at-point 'symbol t)
  1471. (error "No symbol at point")))
  1472. (error "My-Rgrep: Command for rgrep not found"))))
  1473. (if (safe-require-or-eval 'projectile)
  1474. (projectile-with-default-dir (or (projectile-project-root)
  1475. default-directory)
  1476. (compilation-start command-args
  1477. 'grep-mode))
  1478. (compilation-start command-args
  1479. 'grep-mode))))
  1480. (defmacro define-my-rgrep (name)
  1481. "Define rgrep for NAME."
  1482. `(defun ,(intern (concat "my-rgrep-"
  1483. name)) ()
  1484. ,(format "My recursive grep by %s."
  1485. name)
  1486. (interactive)
  1487. (let ((my-rgrep-default ,name))
  1488. (if (called-interactively-p 'any)
  1489. (call-interactively 'my-rgrep)
  1490. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1491. )
  1492. (define-my-rgrep "ack")
  1493. (define-my-rgrep "ag")
  1494. (define-my-rgrep "rg")
  1495. (define-my-rgrep "sift")
  1496. (define-my-rgrep "gitgrep")
  1497. (define-my-rgrep "grep")
  1498. (define-my-rgrep "global")
  1499. (define-key ctl-x-map "s" 'my-rgrep)
  1500. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1501. (defun my-occur (regexp &optional region)
  1502. "My occur command to search REGEXP."
  1503. (interactive (list (read-string "List lines matching regexp: "
  1504. (thing-at-point 'symbol t))))
  1505. (occur regexp nil region))
  1506. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1507. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1508. (defalias 'make 'compile)
  1509. (define-key ctl-x-map "c" 'compile)
  1510. ;;;;;;;;;;;;;;;;;;;;;;;
  1511. ;; adoc-simple-mode
  1512. (when (safe-require-or-eval 'adoc-mode)
  1513. (defvar adoc-simple-font-lock-keywords
  1514. nil)
  1515. (define-derived-mode adoc-simple-mode adoc-mode
  1516. "Adoc-Simple"
  1517. "Major mode for editing AsciiDoc text files.
  1518. This mode is a simplified version of `adoc-mode'."
  1519. '(set (make-local-variable 'font-lock-defaults)
  1520. '(adoc-simple-font-lock-keywords
  1521. nil nil nil nil
  1522. (font-lock-multiline . t)
  1523. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1524. (add-to-list 'auto-mode-alist
  1525. '("\\.adoc\\'" . adoc-simple-mode)))
  1526. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1527. ;; awk-preview
  1528. (defgroup awk-preview nil
  1529. "Awk previewer."
  1530. :tag "Awk Preview"
  1531. :prefix "awk-preview-"
  1532. :group 'tools)
  1533. (defcustom awk-preview-program
  1534. (or (executable-find "gawk")
  1535. (executable-find "awk")
  1536. "awk")
  1537. "Awk program to execute."
  1538. :type 'string
  1539. :group 'awk-preview)
  1540. (defcustom awk-preview-switches
  1541. ;; '("--sandbox")
  1542. nil
  1543. "String of awk options appended when running awk preview."
  1544. :type '(repeat string)
  1545. :group 'awk-preview)
  1546. (defcustom awk-preview-default-program
  1547. "# C-c C-l to update preview, C-c C-c to commit, C-c C-k to abort.
  1548. {
  1549. print NR, $0
  1550. }
  1551. "
  1552. "Default awk command."
  1553. :type 'string
  1554. :group 'awk-preview)
  1555. (defvar awk-preview--point-beg
  1556. nil
  1557. "Point of beginning.")
  1558. (make-variable-buffer-local 'awk-preview--point-beg)
  1559. (defvar awk-preview--point-end
  1560. nil
  1561. "Point of end.")
  1562. (make-variable-buffer-local 'awk-preview--point-end)
  1563. (defvar awk-preview--preview-point-beg
  1564. nil
  1565. "Point of beginning.
  1566. Used by preview buffer and always same as awk-preview--point-beg.")
  1567. (make-variable-buffer-local 'awk-preview--preview-point-beg)
  1568. (defvar awk-preview--preview-point-end
  1569. nil
  1570. "Point of beginning.
  1571. Used by preview buffer and may defferent from awk-preview--point-end.")
  1572. (make-variable-buffer-local 'awk-preview--preview-point-end)
  1573. (defvar awk-preview--program-filename
  1574. nil
  1575. "Awk preview program file name.")
  1576. (make-variable-buffer-local 'awk-preview--program-filename)
  1577. (defvar awk-preview--source-buffer
  1578. nil
  1579. "Awk source buffer.")
  1580. (make-variable-buffer-local 'awk-preview--source-buffer)
  1581. (defvar awk-preview--preview-buffer
  1582. nil
  1583. "Awk preview buffer.")
  1584. (make-variable-buffer-local 'awk-preview--preview-buffer)
  1585. (defvar awk-preview--program-buffer
  1586. nil
  1587. "Awk program buffer.")
  1588. (make-variable-buffer-local 'awk-preview--program-buffer)
  1589. (defvar awk-preview--window-configuration
  1590. nil
  1591. "Awk preview window configuration.")
  1592. (defun awk-preview--invoke-awk (buf beg end progfile output &optional delete)
  1593. "Execute PROGFILE awk process with BEG and END input and output to OUTPUT buffer.
  1594. When OUTPUT is t the result will be output to BUF.
  1595. Delete the text between BEG and END when DELETE is non-nil."
  1596. (with-current-buffer buf
  1597. (let ((status (apply 'call-process-region
  1598. beg
  1599. end
  1600. awk-preview-program
  1601. delete
  1602. output
  1603. nil
  1604. `(,@awk-preview-switches "-f" ,progfile))))
  1605. (unless (eq status
  1606. 0)
  1607. (error "awk-preview: Awk program exited abnormally."))
  1608. output)))
  1609. (defvar awk-preview-program-buffer-name
  1610. "*AWK Preview Program<%s>*"
  1611. "Buffer name of awk preview program.")
  1612. (defvar awk-preview-preview-buffer-name
  1613. "*AWK Preview<%s>*"
  1614. "Buffer name of awk preview.")
  1615. (defun awk-preview--create-program-buffer (source preview)
  1616. "Create and show awk program buffer for SOURCE and PREVIEW buffer.
  1617. Return that buffer."
  1618. (let ((source-name (buffer-name source)))
  1619. (with-current-buffer (generate-new-buffer (format awk-preview-program-buffer-name
  1620. source-name))
  1621. (erase-buffer)
  1622. (insert awk-preview-default-program)
  1623. (awk-mode)
  1624. (awk-preview-program-mode 1)
  1625. (setq awk-preview--source-buffer source)
  1626. (setq awk-preview--preview-buffer preview)
  1627. (setq awk-preview--program-buffer (current-buffer))
  1628. (setq awk-preview--program-filename (make-temp-file "awk-preview-"
  1629. nil
  1630. ".awk"))
  1631. (current-buffer))))
  1632. (defun awk-preview-program-buffer-kill-hook ()
  1633. "Cleanup for awk-preview program buffer."
  1634. (when awk-preview--program-filename
  1635. (delete-file awk-preview--program-filename)))
  1636. (add-hook 'kill-buffer-hook
  1637. 'awk-preview-program-buffer-kill-hook)
  1638. (defun awk-preview--create-preview-buffer (source)
  1639. "Create preview buffer of SOURCE buffer and return it."
  1640. (with-current-buffer source
  1641. (let ((buffer-file-name nil)
  1642. (beg awk-preview--point-beg)
  1643. (end awk-preview--point-end))
  1644. (with-current-buffer (clone-buffer (format awk-preview-preview-buffer-name
  1645. (buffer-name)))
  1646. (awk-preview-program-mode 1)
  1647. (setq awk-preview--preview-point-beg beg)
  1648. (setq awk-preview--preview-point-end end)
  1649. (setq awk-preview--source-buffer source)
  1650. (setq awk-preview--preview-buffer (current-buffer))
  1651. (goto-char end)
  1652. (setq buffer-read-only t)
  1653. (current-buffer)))))
  1654. (defun awk-preview (beg end)
  1655. "Run awk and preview result."
  1656. (interactive "r")
  1657. (setq awk-preview--point-beg beg)
  1658. (setq awk-preview--point-end end)
  1659. (setq awk-preview--source-buffer (current-buffer))
  1660. (setq awk-preview--preview-buffer
  1661. (awk-preview--create-preview-buffer (current-buffer)))
  1662. (setq awk-preview--program-buffer
  1663. (awk-preview--create-program-buffer (current-buffer)
  1664. awk-preview--preview-buffer))
  1665. ;; At creation of preview buffer, program buffer is not available so set this
  1666. ;; here
  1667. (let ((b awk-preview--program-buffer))
  1668. (with-current-buffer awk-preview--preview-buffer
  1669. (setq awk-preview--program-buffer b)))
  1670. (setq awk-preview--window-configuration
  1671. (current-window-configuration))
  1672. (set-window-buffer (get-buffer-window awk-preview--source-buffer)
  1673. awk-preview--preview-buffer)
  1674. (pop-to-buffer awk-preview--program-buffer)
  1675. (switch-to-buffer awk-preview--program-buffer)
  1676. (awk-preview-update-preview)
  1677. )
  1678. (defun awk-preview-update-preview ()
  1679. "Update awk-preview."
  1680. (interactive)
  1681. (with-current-buffer awk-preview--program-buffer
  1682. (write-region (point-min)
  1683. (point-max)
  1684. awk-preview--program-filename))
  1685. (let ((output (with-current-buffer (get-buffer-create " *awk-preview output*")
  1686. (erase-buffer)
  1687. (current-buffer)))
  1688. (progfile (with-current-buffer awk-preview--program-buffer
  1689. awk-preview--program-filename)))
  1690. (with-current-buffer awk-preview--source-buffer
  1691. (awk-preview--invoke-awk (current-buffer)
  1692. awk-preview--point-beg
  1693. awk-preview--point-end
  1694. progfile
  1695. output)
  1696. (with-current-buffer awk-preview--preview-buffer
  1697. (let ((inhibit-read-only t))
  1698. (goto-char awk-preview--preview-point-end)
  1699. (delete-region awk-preview--preview-point-beg (point))
  1700. (insert-buffer-substring output)
  1701. (setq awk-preview--preview-point-end (point))))
  1702. )))
  1703. (defun awk-preview-commit ()
  1704. "Exit awk-preview and update buffer."
  1705. (interactive)
  1706. (awk-preview--cleanup))
  1707. (defun awk-preview-abort ()
  1708. "Discard result and exit awk-preview."
  1709. (interactive)
  1710. (awk-preview--cleanup))
  1711. (defun awk-preview--cleanup()
  1712. "Cleanup awk preview buffers and variables.")
  1713. (defvar awk-preview-program-mode-map
  1714. (let ((map (make-sparse-keymap)))
  1715. (define-key map (kbd "C-c C-l") 'awk-preview-update-preview)
  1716. (define-key map (kbd "C-c C-k") 'awk-preview-abort)
  1717. (define-key map (kbd "C-c C-c") 'awk-preview-commit)
  1718. map)
  1719. "Keymap for `awk-preview-program-mode'.")
  1720. (define-minor-mode awk-preview-program-mode
  1721. "Minor mode for awk-preview program buffer."
  1722. :lighter " AWKPrev"
  1723. :keymap awk-preview-program-mode-map)
  1724. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1725. ;; editorconfig-auto-apply
  1726. (define-minor-mode editorconfig-auto-apply-mode
  1727. "When saving .editorconfig file update buffer configs."
  1728. :global t
  1729. :lighter ""
  1730. (if editorconfig-auto-apply-mode
  1731. (add-hook 'after-save-hook
  1732. 'editorconfig-auto-apply-mode--run)
  1733. (remove-hook 'after-save-hook
  1734. 'editorconfig-auto-apply-mode--run)))
  1735. (defun editorconfig-auto-apply-mode--run ()
  1736. "When saving .editorconfig file walk all buffers and update configs."
  1737. (when (eq major-mode
  1738. 'editorconfig-conf-mode)
  1739. (let ((dir (file-name-directory buffer-file-name)))
  1740. (cl-dolist (buf (buffer-list))
  1741. (when (and (buffer-file-name buf)
  1742. (file-in-directory-p (buffer-file-name buf)
  1743. dir))
  1744. (with-current-buffer buf
  1745. (editorconfig-mode-apply)))))))
  1746. (editorconfig-auto-apply-mode 1)
  1747. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1748. ;; recently
  1749. (defgroup recently nil
  1750. "Recently visited files"
  1751. :tag "Recently"
  1752. :prefix "recently-"
  1753. :group 'tools)
  1754. (defcustom recently-file
  1755. (concat user-emacs-directory
  1756. "recently.el")
  1757. "Recently file."
  1758. :type 'string
  1759. :group 'recently)
  1760. (defcustom recently-max
  1761. 100
  1762. "Recently list max length."
  1763. :type 'int
  1764. :group 'recently)
  1765. (defcustom recently-excludes
  1766. '()
  1767. "List of regexps for filenames excluded from the recent list."
  1768. :type '(repeat string)
  1769. :group 'recently)
  1770. (add-to-list 'recently-excludes
  1771. (eval-when-compile (rx "/COMMIT_EDITMSG" eot)))
  1772. (defvar recently-list
  1773. '()
  1774. "Recently list.")
  1775. (defvar recently-file-mtime
  1776. nil
  1777. "Modified time of file when last read file.")
  1778. (defun recently-write ()
  1779. "Write to file."
  1780. (with-temp-buffer
  1781. (prin1 recently-list
  1782. (current-buffer))
  1783. (write-region (point-min)
  1784. (point-max)
  1785. recently-file)))
  1786. (defun recently-read ()
  1787. "Read file."
  1788. (when (file-readable-p recently-file)
  1789. (with-temp-buffer
  1790. (insert-file-contents recently-file)
  1791. (goto-char (point-min))
  1792. (setq recently-list
  1793. (read (current-buffer))))
  1794. (setq recently-file-mtime
  1795. (nth 5
  1796. (file-attributes recently-file)))))
  1797. (defun recently-reload ()
  1798. "Reload file."
  1799. (when (and (file-readable-p recently-file)
  1800. (not (equal recently-file-mtime
  1801. (nth 5
  1802. (file-attributes recently-file)))))
  1803. (recently-read)
  1804. (cl-assert (equal recently-file-mtime
  1805. (nth 5
  1806. (file-attributes recently-file))))))
  1807. (defun recently-add (path)
  1808. "Add PATH to list."
  1809. (cl-assert (string= path
  1810. (expand-file-name path)))
  1811. (when (cl-loop for re in recently-excludes
  1812. if (string-match re path) return nil
  1813. finally return t)
  1814. (recently-reload)
  1815. (let* ((l (copy-list recently-list))
  1816. (l (delete path
  1817. l))
  1818. (l (cl-loop for e in l
  1819. unless (file-in-directory-p path e)
  1820. collect e))
  1821. (l (cons path
  1822. l))
  1823. (l (recently--truncate l
  1824. recently-max)))
  1825. (unless (equal recently-list
  1826. l)
  1827. (setq recently-list l)
  1828. (recently-write)))))
  1829. (defun recently--truncate (list len)
  1830. "Truncate LIST to LEN."
  1831. (if (> (length list)
  1832. len)
  1833. (cl-subseq list
  1834. 0
  1835. len)
  1836. list))
  1837. (defun recently-find-file-hook ()
  1838. "Add current file."
  1839. (when buffer-file-name
  1840. (recently-add buffer-file-name)))
  1841. (defun recently-dired-mode-hook ()
  1842. "Add current directory."
  1843. (recently-add (expand-file-name default-directory)))
  1844. (add-hook 'find-file-hook
  1845. 'recently-find-file-hook)
  1846. (add-hook 'dired-mode-hook
  1847. 'recently-dired-mode-hook)
  1848. ;; recently-show
  1849. (defvar recently-show-window-height 10
  1850. "Max height of window of `recently-show'")
  1851. (defvar recently-show-mode-map
  1852. (let ((map (make-sparse-keymap)))
  1853. (suppress-keymap map)
  1854. (define-key map "n" 'next-line)
  1855. (define-key map "p" 'previous-line)
  1856. (define-key map (kbd "C-m") 'recently-show-find-file)
  1857. (define-key map (kbd "SPC") 'recently-show-find-file)
  1858. (define-key map "v" 'recently-show-view-file)
  1859. (define-key map "@" 'recently-show-dired)
  1860. (define-key map "q" 'recently-show-close)
  1861. (define-key map (kbd "C-g") 'recently-show-close)
  1862. (define-key map "?" 'describe-mode)
  1863. (define-key map "/" 'isearch-forward)
  1864. map))
  1865. (defvar recently-show-before-listing-hook nil
  1866. "Hook run before creating buffer of `recently-show'.")
  1867. (defvar recently-show-window-configuration nil
  1868. "Used for internal")
  1869. (defvar recently-show-abbreviate t
  1870. "Non-nil means use `abbreviate-file-name' when listing recently opened files.")
  1871. (define-derived-mode recently-show-mode special-mode "recently-show"
  1872. "Major mode for `recently-show'."
  1873. ;; (set (make-local-variable 'scroll-margin)
  1874. ;; 0)
  1875. )
  1876. ;;;###autoload
  1877. (defun recently-show (&optional files buffer-name)
  1878. "Show simplified list of recently opened files.
  1879. If optional argument FILES is non-nil, it is a list of recently-opened
  1880. files to choose from. It defaults to the whole recent list.
  1881. If optional argument BUFFER-NAME is non-nil, it is a buffer name to
  1882. use for the buffer. It defaults to \"*recetf-show*\"."
  1883. (interactive)
  1884. (let ((bf (recently-show-create-buffer files buffer-name)))
  1885. (if bf
  1886. (progn
  1887. ;; (recently-save-list)
  1888. (setq recently-show-window-configuration (current-window-configuration))
  1889. ;;(pop-to-buffer bf t t)
  1890. (display-buffer bf)
  1891. ;; (set-window-text-height (selected-window)
  1892. ;; recently-show-window-height)
  1893. (shrink-window-if-larger-than-buffer (selected-window)))
  1894. (message "No recent file!"))))
  1895. (defun recently-show-create-buffer (&optional files buffer-name)
  1896. "Create buffer listing recently files."
  1897. (let ((bname (or buffer-name
  1898. "*recently-show*"))
  1899. (list (or files
  1900. (progn
  1901. (recently-reload)
  1902. recently-list))))
  1903. (when list
  1904. (and (get-buffer bname)
  1905. (kill-buffer bname))
  1906. (let ((bf (get-buffer-create bname)))
  1907. (with-current-buffer bf
  1908. (recently-show-mode)
  1909. (let ((inhibit-read-only t))
  1910. (mapc (lambda (f)
  1911. (insert (if recently-show-abbreviate
  1912. (abbreviate-file-name f)
  1913. f)
  1914. "\n"))
  1915. list))
  1916. (goto-char (point-min))
  1917. ;; (setq buffer-read-only t)
  1918. )
  1919. bf))))
  1920. (defun recently-show-close ()
  1921. "Close recently-show window."
  1922. (interactive)
  1923. (kill-buffer (current-buffer))
  1924. (set-window-configuration recently-show-window-configuration))
  1925. (defun recently-show-find-file ()
  1926. "Fine file of current line."
  1927. (interactive)
  1928. (let ((f (recently-show-get-filename)))
  1929. (recently-show-close)
  1930. (find-file f)))
  1931. (defun recently-show-view-file ()
  1932. "view file of current line."
  1933. (interactive)
  1934. (let ((f (recently-show-get-filename)))
  1935. (recently-show-close)
  1936. (view-file f)))
  1937. (defun recently-show-get-filename ()
  1938. "Get filename of current line."
  1939. (buffer-substring-no-properties (point-at-bol)
  1940. (point-at-eol)))
  1941. (defun recently-show-dired()
  1942. "Open dired buffer of directory containing file of current line."
  1943. (interactive)
  1944. (let ((f (recently-show-get-filename)))
  1945. (recently-show-close)
  1946. (dired (if (file-directory-p f)
  1947. f
  1948. (or (file-name-directory f)
  1949. ".")))))
  1950. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1951. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1952. ;; git walktree
  1953. (defgroup git-walktree nil
  1954. "Git Walktree."
  1955. :tag "GitWalktree"
  1956. :prefix "git-walktree-"
  1957. :group 'tools)
  1958. (defvar git-walktree-current-committish nil
  1959. "Committish name of currently browsing.")
  1960. (make-variable-buffer-local 'git-walktree-current-committish)
  1961. (defvar git-walktree-current-path nil
  1962. "Path name currently visiting without leading and trailing slash.
  1963. This path is always relative to repository root.")
  1964. (make-variable-buffer-local 'git-walktree-current-path)
  1965. (defvar git-walktree-buffer-file-name nil
  1966. "Psudo filename of current buffer.")
  1967. (make-variable-buffer-local 'git-walktree-buffer-file-name)
  1968. (defvar git-walktree-object-full-sha1 nil
  1969. "Object name in full sha1 format of current buffer.")
  1970. (make-variable-buffer-local 'git-walktree-object-full-sha1)
  1971. (defvar git-walktree-repository-root nil
  1972. "Repository root path of current buffer.")
  1973. (make-variable-buffer-local 'git-walktree-repository-root)
  1974. (put 'git-walktree-repository-root
  1975. 'permanent-local
  1976. t)
  1977. (defun git-walktree--committish-fordisplay (committish)
  1978. "Convert COMMITTISH and return is a suitable format for displaying."
  1979. (if (and committish
  1980. (string-match-p "\\`[0-9a-f]+\\'"
  1981. committish)
  1982. (>= (length committish) 32))
  1983. (git-walktree--git-plumbing "rev-parse"
  1984. "--short"
  1985. committish)
  1986. committish))
  1987. (defun git-walktree--create-buffer (committish name type)
  1988. "Create and return buffer for COMMITTISH:NAME.
  1989. TYPE is target object type."
  1990. (let* ((root (git-walktree--git-plumbing "rev-parse"
  1991. "--show-toplevel"))
  1992. (committish-display (git-walktree--committish-fordisplay committish))
  1993. (name (format "%s:%s"
  1994. (or committish-display "")
  1995. name)))
  1996. (if (and git-walktree-reuse-tree-buffer
  1997. (string= type "tree"))
  1998. (with-current-buffer (or git-walktree-tree-buffer-for-reuse
  1999. (setq git-walktree-tree-buffer-for-reuse
  2000. (generate-new-buffer "gitwalktreebuf")))
  2001. (setq git-walktree-repository-root root)
  2002. (rename-buffer name t)
  2003. (current-buffer))
  2004. (with-current-buffer (get-buffer-create name)
  2005. (if git-walktree-repository-root
  2006. (if (string= root
  2007. git-walktree-repository-root)
  2008. (current-buffer)
  2009. ;; If the buffer is for another repository, create new buffer
  2010. (with-current-buffer (generate-new-buffer name)
  2011. (setq git-walktree-repository-root root)
  2012. (current-buffer)))
  2013. ;; New buffer
  2014. (setq git-walktree-repository-root root)
  2015. (current-buffer))))))
  2016. (defun git-walktree--replace-into-buffer (target)
  2017. "Replace TARGET buffer contents with that of current buffer.
  2018. It also copy text overlays."
  2019. (let ((src (current-buffer)))
  2020. (with-current-buffer target
  2021. (replace-buffer-contents src)))
  2022. ;; Copy color overlays
  2023. (let ((overlays (overlays-in (point-min) (point-max))))
  2024. (dolist (o overlays)
  2025. (let ((beg (overlay-start o))
  2026. (end (overlay-end o)))
  2027. (move-overlay (copy-overlay o)
  2028. beg
  2029. end
  2030. target)))))
  2031. (require 'ansi-color)
  2032. (defun git-walktree--open-treeish (committish path treeish)
  2033. "Open git tree buffer of COMMITISH:PATH.
  2034. TREEISH should be a tree-ish object full-sha1 of COMMITISH:PATH."
  2035. (cl-assert path)
  2036. (cl-assert treeish)
  2037. (let* (point-tree-start
  2038. (type (git-walktree--git-plumbing "cat-file"
  2039. "-t"
  2040. treeish))
  2041. (buf (git-walktree--create-buffer committish path type))
  2042. )
  2043. (cl-assert (member type
  2044. '("commit" "tree")))
  2045. (with-current-buffer buf
  2046. (unless (and (string= treeish
  2047. git-walktree-object-full-sha1)
  2048. (or (eq committish
  2049. git-walktree-current-committish)
  2050. (string= committish
  2051. git-walktree-current-committish)))
  2052. (buffer-disable-undo)
  2053. ;; For running git command go back to repository root
  2054. (cd git-walktree-repository-root)
  2055. (save-excursion
  2056. (let ((inhibit-read-only t))
  2057. ;; Remove existing overlays generated by ansi-color-apply-on-region
  2058. (remove-overlays)
  2059. (with-temp-buffer
  2060. (if committish
  2061. (progn (git-walktree--call-process nil
  2062. "show"
  2063. ;; TODO: Make this args configurable
  2064. ;; "--no-patch"
  2065. "--color=always"
  2066. "--pretty=short"
  2067. "--decorate"
  2068. "--stat"
  2069. committish)
  2070. (ansi-color-apply-on-region (point-min)
  2071. (point))
  2072. (insert "\n")
  2073. (insert (format "Contents of '%s:%s':\n"
  2074. (git-walktree--committish-fordisplay committish)
  2075. path)))
  2076. (insert (format "Contents of treeish object '%s:\n"
  2077. treeish)))
  2078. (setq point-tree-start (point))
  2079. (git-walktree--call-process nil
  2080. "ls-tree"
  2081. ;; "-r"
  2082. "--abbrev"
  2083. treeish)
  2084. (git-walktree--replace-into-buffer buf))
  2085. ))
  2086. (git-walktree-mode)
  2087. (set-buffer-modified-p nil)
  2088. (setq git-walktree-current-committish committish)
  2089. (setq git-walktree-current-path path)
  2090. (setq git-walktree-object-full-sha1 treeish)
  2091. (let ((dir (expand-file-name path git-walktree-repository-root)))
  2092. (when (and git-walktree-try-cd
  2093. (file-directory-p dir))
  2094. (cd dir)))
  2095. (when (= (point) (point-min))
  2096. (goto-char point-tree-start)
  2097. (git-walktree-mode--move-to-file)
  2098. )
  2099. ))
  2100. buf))
  2101. (defun git-walktree--call-process (&optional infile &rest args)
  2102. "Call git command with input from INFILE and args ARGS.
  2103. Result will be inserted into current buffer."
  2104. (let ((status (apply 'call-process
  2105. git-walktree-git-executable
  2106. infile
  2107. t
  2108. nil
  2109. args)))
  2110. (unless (eq 0
  2111. status)
  2112. (error "Failed to call git process %S %S"
  2113. infile
  2114. args))))
  2115. ?w
  2116. (defun git-walktree--open-blob (committish path blob)
  2117. "Open blob object of COMMITISH:PATH.
  2118. BLOB should be a object full sha1 of COMMITISH:PATH."
  2119. (cl-assert committish)
  2120. (cl-assert path)
  2121. (cl-assert blob)
  2122. (let* ((type (git-walktree--git-plumbing "cat-file"
  2123. "-t"
  2124. blob))
  2125. (buf (git-walktree--create-buffer committish path type)))
  2126. (cl-assert (string= type "blob"))
  2127. (with-current-buffer buf
  2128. (unless (string= blob
  2129. git-walktree-object-full-sha1)
  2130. ;; For running git command go back to repository root
  2131. (cd git-walktree-repository-root)
  2132. (let ((inhibit-read-only t))
  2133. (with-temp-buffer
  2134. (git-walktree--call-process nil
  2135. "cat-file"
  2136. "-p"
  2137. blob)
  2138. (git-walktree--replace-into-buffer buf)))
  2139. (setq git-walktree-buffer-file-name
  2140. (concat git-walktree-repository-root "/git@" committish ":" path))
  2141. (setq buffer-file-name
  2142. (concat git-walktree-repository-root "/" path))
  2143. (normal-mode t)
  2144. ;; For asking filename when C-xC-s
  2145. (setq buffer-file-name nil)
  2146. (set-buffer-modified-p t)
  2147. (setq git-walktree-current-committish committish)
  2148. (setq git-walktree-current-path path)
  2149. (setq git-walktree-object-full-sha1 blob)
  2150. (let ((dir (expand-file-name (or (file-name-directory path)
  2151. ".")
  2152. git-walktree-repository-root)))
  2153. (when (and git-walktree-try-cd
  2154. (file-directory-p dir))
  2155. (cd dir)))
  2156. (view-mode 1)
  2157. ))
  2158. buf))
  2159. (defun git-walktree--open-noselect-safe-path (committish &optional path)
  2160. "Open git object of COMMITTISH:PATH.
  2161. If PATH not found in COMMITTISH tree, go up path and try again until found.
  2162. When PATH is omitted or nil, it is calculated from current file or directory."
  2163. (cl-assert committish)
  2164. (let ((type (git-walktree--git-plumbing "cat-file"
  2165. "-t"
  2166. committish)))
  2167. (cl-assert (string= type "commit")))
  2168. (setq path
  2169. (or path
  2170. (git-walktree--path-in-repository (or buffer-file-name
  2171. default-directory))))
  2172. ;; PATH must not start with and end with slashes
  2173. (cl-assert (not (string-match-p "\\`/" path)))
  2174. (cl-assert (not (string-match-p "/\\'" path)))
  2175. (let ((obj (git-walktree--resolve-object committish path)))
  2176. (while (not obj)
  2177. (setq path
  2178. (git-walktree--parent-directory path))
  2179. (setq obj
  2180. (git-walktree--resolve-object committish path)))
  2181. (git-walktree--open-noselect committish
  2182. path
  2183. obj)))
  2184. ;; TODO: Store view history
  2185. (defun git-walktree--open-noselect (committish path object)
  2186. "Open buffer to view git object of COMMITTISH:PATH.
  2187. When PATH was given and non-nil open that, otherwise open root tree.
  2188. When OBJECT was given and non-nil, assume that is the object full sha1 of
  2189. COMMITTISH:PATH without checking it."
  2190. (cl-assert committish)
  2191. (let ((type (git-walktree--git-plumbing "cat-file"
  2192. "-t"
  2193. committish)))
  2194. (cl-assert (string= type "commit")))
  2195. (setq path (or path
  2196. "."))
  2197. ;; PATH must not start with and end with slashes
  2198. (cl-assert (not (string-match-p "\\`/" path)))
  2199. (cl-assert (not (string-match-p "/\\'" path)))
  2200. (setq object (or object
  2201. (git-walktree--resolve-object committish path)))
  2202. (setq object (git-walktree--git-plumbing "rev-parse"
  2203. object))
  2204. (cl-assert object)
  2205. (let ((type (git-walktree--git-plumbing "cat-file"
  2206. "-t"
  2207. object)))
  2208. (pcase type
  2209. ((or "commit" "tree")
  2210. (git-walktree--open-treeish committish path object))
  2211. ("blob"
  2212. (git-walktree--open-blob committish path object))
  2213. (_
  2214. (error "Type cannot handle: %s" type)))))
  2215. (defun git-walktree--resolve-object (committish path)
  2216. "Return object full sha1 name of COMMITISIH:PATH.
  2217. If path is equal to \".\" return COMMITTISH's root tree object.
  2218. PATH will be always treated as relative to repository root."
  2219. (cl-assert committish)
  2220. (cl-assert path)
  2221. (cl-assert (not (string-match-p "\\`/" path)))
  2222. (cl-assert (not (string-match-p "/\\'" path)))
  2223. (if (string= path ".")
  2224. (git-walktree--git-plumbing "show"
  2225. "--no-patch"
  2226. "--pretty=format:%T"
  2227. committish)
  2228. (let ((info (git-walktree--parse-lstree-line (git-walktree--git-plumbing "ls-tree"
  2229. "--full-tree"
  2230. committish
  2231. path))))
  2232. (plist-get info :object))))
  2233. (defun git-walktree-open (committish &optional path)
  2234. "Open git tree buffer of COMMITTISH.
  2235. When PATH was given and non-nil open that, otherwise try to open current path.
  2236. If target path is not found in COMMITISH tree, go up path and try again until found."
  2237. (interactive (list (magit-read-branch-or-commit "Revision: ")))
  2238. (switch-to-buffer (git-walktree--open-noselect-safe-path committish path)))
  2239. (defalias 'git-walktree 'git-walktree-open)
  2240. (defun git-walktree--path-in-repository (path)
  2241. "Convert PATH into relative path to repository root.
  2242. Result will not have leading and trailing slashes."
  2243. (with-temp-buffer
  2244. (cd (if (file-directory-p path)
  2245. path
  2246. (file-name-directory path)))
  2247. (let ((root (git-walktree--git-plumbing "rev-parse"
  2248. "--show-toplevel")))
  2249. (file-relative-name (directory-file-name path)
  2250. root))))
  2251. (defcustom git-walktree-git-executable "git"
  2252. "Git executable."
  2253. :type 'string
  2254. :group 'git-walktree)
  2255. (defcustom git-walktree-try-cd t
  2256. "Try to cd if directory exists in current working directory if non-nil.
  2257. Otherwise use repository root for gitwalktree buffer's `default-directory'."
  2258. :type 'boolean
  2259. :group 'git-walktree)
  2260. (defcustom git-walktree-reuse-tree-buffer t
  2261. "Non-nil to reuse buffer for treeish object."
  2262. :type 'boolean
  2263. :group 'git-walktree)
  2264. (defvar git-walktree-tree-buffer-for-reuse nil
  2265. "Buffer to use when `git-walktree-reuse-tree-buffer' is non-nil.")
  2266. (defun git-walktree--git-plumbing (&rest args)
  2267. "Run git plubming command with ARGS.
  2268. Returns first line of output without newline."
  2269. (with-temp-buffer
  2270. (let ((status (apply 'call-process
  2271. git-walktree-git-executable
  2272. nil
  2273. t
  2274. nil
  2275. args)))
  2276. (unless (eq 0
  2277. status)
  2278. (error "Faild to run git %S:\n%s"
  2279. args
  2280. (buffer-substring-no-properties (point-min)
  2281. (point-max))))
  2282. (buffer-substring-no-properties (point-min)
  2283. (progn
  2284. (goto-char (point-min))
  2285. (point-at-eol))))))
  2286. (defconst git-walktree-ls-tree-line-regexp
  2287. "^\\([0-9]\\{6\\}\\) \\(\\w+\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2288. "Regexp for one line of output of git ls-tree.")
  2289. (defconst git-walktree-ls-tree-line-tree-regexp
  2290. "^\\([0-9]\\{6\\}\\) \\(tree\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2291. "Regexp for tree line of output of git ls-tree.")
  2292. (defconst git-walktree-ls-tree-line-commit-regexp
  2293. "^\\([0-9]\\{6\\}\\) \\(commit\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2294. "Regexp for commit line of output of git ls-tree.")
  2295. (defun git-walktree--parse-lstree-line (str)
  2296. "Extract object info from STR.
  2297. STR should be a string like following without newline.:
  2298. 100644 blob 6fd4d58202d0b46547c6fe43de0f8c878456f966 .editorconfig
  2299. Returns property list like (:mode MODE :type TYPE :object OBJECT :file FILE)."
  2300. (let (result mode type object file)
  2301. (save-match-data
  2302. (with-temp-buffer
  2303. (insert str)
  2304. (goto-char (point-min))
  2305. (and (re-search-forward git-walktree-ls-tree-line-regexp
  2306. nil
  2307. t)
  2308. (list :mode (match-string 1)
  2309. :type (match-string 2)
  2310. :object (match-string 3)
  2311. :file (match-string 4)))))))
  2312. (defun git-walktree-mode-open-this ()
  2313. "Open git object of current line."
  2314. (interactive)
  2315. (let ((info (git-walktree--parse-lstree-line (buffer-substring-no-properties (point-at-bol)
  2316. (point-at-eol)))))
  2317. (if info
  2318. (switch-to-buffer
  2319. (if (string= (plist-get info
  2320. :type)
  2321. "commit")
  2322. ;; For submodule cd to that directory and intialize
  2323. ;; TODO: Provide way to go back to known "parent" repository
  2324. (with-temp-buffer
  2325. (cd (plist-get info :file))
  2326. (git-walktree--open-noselect (plist-get info
  2327. :object)
  2328. nil
  2329. (plist-get info
  2330. :object)))
  2331. (git-walktree--open-noselect git-walktree-current-committish
  2332. (git-walktree--join-path (plist-get info
  2333. :file))
  2334. (plist-get info
  2335. :object))))
  2336. (message "No object on current line."))))
  2337. (defun git-walktree--join-path (name &optional base)
  2338. "Make path from NAME and BASE.
  2339. If base is omitted or nil use value of `git-walktree-current-path'."
  2340. (setq base (or base
  2341. git-walktree-current-path))
  2342. (cl-assert base)
  2343. (if (string= base ".")
  2344. name
  2345. (concat base "/" name)))
  2346. (defun git-walktree--parent-directory (path)
  2347. "Return parent directory of PATH without trailing slash.
  2348. For root directory return \".\".
  2349. If PATH is equal to \".\", return nil."
  2350. (if (string-match-p "/" path)
  2351. (directory-file-name (file-name-directory path))
  2352. (if (string= "." path)
  2353. nil
  2354. ".")))
  2355. (defun git-walktree-up (&optional committish path)
  2356. "Open parent directory of COMMITTISH and PATH.
  2357. If not given, value of current buffer will be used."
  2358. (interactive)
  2359. (setq committish
  2360. (or committish git-walktree-current-committish))
  2361. (setq path
  2362. (or path git-walktree-current-path))
  2363. (let ((parent (git-walktree--parent-directory path)))
  2364. (if parent
  2365. (switch-to-buffer (git-walktree--open-noselect committish
  2366. parent
  2367. nil))
  2368. (message "Cannot find parent directory for current tree."))))
  2369. (defun git-walktree-mode--move-to-file ()
  2370. "Move point to file field of ls-tree output in current line.
  2371. This function do nothing when current line is not ls-tree output."
  2372. (interactive)
  2373. (save-match-data
  2374. (when (save-excursion
  2375. (goto-char (point-at-bol))
  2376. (re-search-forward git-walktree-ls-tree-line-regexp
  2377. (point-at-eol) t))
  2378. (goto-char (match-beginning 4)))))
  2379. (defun git-walktree-mode-next-line (&optional arg try-vscroll)
  2380. "Move cursor vertically down ARG lines and move to file field if found."
  2381. (interactive "^p\np")
  2382. (or arg (setq arg 1))
  2383. (line-move arg nil nil try-vscroll)
  2384. (git-walktree-mode--move-to-file)
  2385. )
  2386. (defun git-walktree-mode-previous-line (&optional arg try-vscroll)
  2387. "Move cursor vertically up ARG lines and move to file field if found."
  2388. (interactive "^p\np")
  2389. (or arg (setq arg 1))
  2390. (line-move (- arg) nil nil try-vscroll)
  2391. (git-walktree-mode--move-to-file)
  2392. )
  2393. (defgroup git-walktree-faces nil
  2394. "Faces used by git-walktree."
  2395. :group 'git-walktree
  2396. :group 'faces)
  2397. (defface git-walktree-tree-face
  2398. ;; Same as dired-directory
  2399. '((t (:inherit font-lock-function-name-face)))
  2400. "Face used for tree objects."
  2401. :group 'git-walktree-faces)
  2402. (defface git-walktree-commit-face
  2403. ;; Same as dired-symlink face
  2404. '((t (:inherit font-lock-keyword-face)))
  2405. "Face used for commit objects."
  2406. :group 'git-walktree-faces)
  2407. (defvar git-walktree-known-child-revisions (make-hash-table :test 'equal)
  2408. "Hash of already known pair of commitid -> list of child commitid.
  2409. Both values should be object full sha1 names.")
  2410. (defun git-walktree--put-child (parent child)
  2411. "Register PARENT and CHILD relationship.
  2412. PARENT should be a full sha1 object name."
  2413. ;; Any way to check if PARENT is a full SHA-1 object name?
  2414. (let ((current (gethash parent git-walktree-known-child-revisions)))
  2415. (unless (member child current)
  2416. (puthash parent
  2417. (cons child
  2418. current)
  2419. git-walktree-known-child-revisions))))
  2420. ;; TODO: Add aggressive search mode
  2421. ;; https://stackoverflow.com/a/9870218
  2422. ;; git log --reverse --pretty=format:%H -n 1 --ancestry-path <PARENT>..HEAD
  2423. (defun git-walktree--get-children (parent)
  2424. "Get known children list of PARENT commit.
  2425. PARENT should be a full sha1 object name."
  2426. (gethash parent git-walktree-known-child-revisions))
  2427. (defun git-walktree--choose-committish (prompt-format collection)
  2428. "Emit PROMPT-FORMAT and ask user to which committish of COLLECTION to use.
  2429. When collection has just one element, return the first element without asking."
  2430. (cl-assert collection)
  2431. (if (< (length collection) 2)
  2432. (car collection)
  2433. (completing-read (format prompt-format
  2434. (mapconcat 'git-walktree--committish-fordisplay
  2435. collection
  2436. " "))
  2437. collection
  2438. nil
  2439. t)))
  2440. (defun git-walktree-parent-revision ()
  2441. "Open parent revision of current path.
  2442. If current path was not found in the parent revision try to go up path."
  2443. (interactive)
  2444. (cl-assert git-walktree-current-committish)
  2445. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2446. git-walktree-current-committish))
  2447. (parents (git-walktree--parent-full-sha1 commit-full-sha1)))
  2448. (dolist (parent parents)
  2449. (git-walktree--put-child parent
  2450. commit-full-sha1))
  2451. (if (< (length parents)
  2452. 1)
  2453. (message "This revision has no parent revision")
  2454. (let* ((parent (git-walktree--choose-committish "This revision has multiple parents. Which to open? (%s) "
  2455. parents))
  2456. (path git-walktree-current-path))
  2457. (cl-assert path)
  2458. (switch-to-buffer (git-walktree--open-noselect-safe-path parent
  2459. path))))))
  2460. (defun git-walktree--parent-full-sha1 (committish)
  2461. "Return list of parent commits of COMMITTISH in sha1 string."
  2462. (let ((type (git-walktree--git-plumbing "cat-file"
  2463. "-t"
  2464. committish)))
  2465. (cl-assert (string= type "commit")))
  2466. (let ((parents (git-walktree--git-plumbing "show"
  2467. "--no-patch"
  2468. "--pretty=format:%P"
  2469. committish)))
  2470. (split-string parents)))
  2471. (defun git-walktree-known-child-revision ()
  2472. "Open known revision of current path."
  2473. (interactive)
  2474. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2475. git-walktree-current-committish))
  2476. (children (git-walktree--get-children commit-full-sha1)))
  2477. (if (< (length children)
  2478. 1)
  2479. (message "There are no known child revision")
  2480. (let* ((child (git-walktree--choose-committish "There are multiple known childrens. Which to open? (%s)"
  2481. children))
  2482. (path git-walktree-current-path))
  2483. (cl-assert path)
  2484. (switch-to-buffer (git-walktree--open-noselect-safe-path child
  2485. path))))))
  2486. (defvar git-walktree-mode-map
  2487. (let ((map (make-sparse-keymap)))
  2488. (define-key map "n" 'git-walktree-mode-next-line)
  2489. (define-key map "p" 'git-walktree-mode-previous-line)
  2490. (define-key map (kbd "C-n") 'git-walktree-mode-next-line)
  2491. (define-key map (kbd "C-p") 'git-walktree-mode-previous-line)
  2492. ;; TODO: Review keybind
  2493. (define-key map "P" 'git-walktree-parent-revision)
  2494. (define-key map "N" 'git-walktree-known-child-revision)
  2495. (define-key map "^" 'git-walktree-up)
  2496. ;; TODO: implement
  2497. (define-key map (kbd "DEL") 'git-walktree-back)
  2498. (define-key map (kbd "C-m") 'git-walktree-mode-open-this)
  2499. map))
  2500. (defvar git-walktree-mode-font-lock-keywords
  2501. `(
  2502. (,git-walktree-ls-tree-line-regexp . (
  2503. (1 'shadow)
  2504. (3 'shadow)
  2505. ))
  2506. (,git-walktree-ls-tree-line-tree-regexp . (
  2507. (2 'git-walktree-tree-face)
  2508. (4 'git-walktree-tree-face)
  2509. ))
  2510. (,git-walktree-ls-tree-line-commit-regexp . (
  2511. (2 'git-walktree-commit-face)
  2512. (4 'git-walktree-commit-face)
  2513. ))
  2514. )
  2515. "Syntax highlighting for git-walktree mode.")
  2516. (define-derived-mode git-walktree-mode special-mode "GitWalktree"
  2517. "Major-mode for `git-walktree-open'."
  2518. (set (make-local-variable 'font-lock-defaults)
  2519. '(git-walktree-mode-font-lock-keywords
  2520. nil nil nil nil
  2521. ))
  2522. )
  2523. (require 'magit nil t)
  2524. ;; (git-revision--git-plumbing "cat-file" "-t" "HEAD")
  2525. ;; Local Variables:
  2526. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2527. ;; flycheck-checker: emacs-lisp
  2528. ;; End:
  2529. ;;; emancs.el ends here