Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

3070 linhas
104 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 (autoload-eval-lazily 'wgrep)
  695. (set-variable 'wgrep-auto-save-buffer t)
  696. (with-eval-after-load 'grep
  697. (defvar grep-mode-map)
  698. (define-key grep-mode-map
  699. "e"
  700. 'wgrep-change-to-wgrep-mode)))
  701. (with-eval-after-load 'remember
  702. (defvar remember-mode-map (make-sparse-keymap))
  703. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  704. (with-eval-after-load 'magit-files
  705. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  706. ;; existing keybindings.
  707. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  708. ;; to disable thie mode here, but do anyway.
  709. ;; See also https://github.com/magit/magit/issues/3517
  710. (global-magit-file-mode -1))
  711. (with-eval-after-load 'magit-section
  712. (set-face-background 'magit-section-highlight
  713. nil))
  714. (with-eval-after-load 'magit-diff
  715. (set-face-background 'magit-diff-added-highlight
  716. nil)
  717. (set-face-background 'magit-diff-removed-highlight
  718. nil)
  719. (set-face-background 'magit-diff-context-highlight
  720. nil)
  721. )
  722. (when (boundp 'git-rebase-filename-regexp)
  723. (add-to-list 'auto-mode-alist
  724. `(,git-rebase-filename-regexp . text-mode)))
  725. (when (safe-require-or-eval 'aggressive-indent)
  726. (defvar aggressive-indent-excluded-modes)
  727. (setq aggressive-indent-excluded-modes
  728. `(diff-mode
  729. toml-mode
  730. conf-mode
  731. dockerfile-mode
  732. groovy-mode
  733. ,@aggressive-indent-excluded-modes))
  734. (global-aggressive-indent-mode 1))
  735. (when (autoload-eval-lazily 'ggtags)
  736. (add-hook 'c-mode-common-hook
  737. (lambda ()
  738. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  739. (ggtags-mode 1))))
  740. (add-hook 'python-mode-hook
  741. (lambda ()
  742. (ggtags-mode 1))))
  743. (when (autoload-eval-lazily 'imenu-list)
  744. ;; (set-variable 'imenu-list-auto-resize t)
  745. (set-variable 'imenu-list-focus-after-activation t)
  746. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  747. (add-hook 'emacs-lisp-mode-hook
  748. (lambda ()
  749. (setq imenu-generic-expression
  750. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  751. ,@imenu-generic-expression))))
  752. (with-eval-after-load 'compile
  753. (defvar compilation-filter-start)
  754. (defvar compilation-error-regexp-alist)
  755. (require 'ansi-color)
  756. (add-hook 'compilation-filter-hook
  757. (lambda ()
  758. (let ((inhibit-read-only t))
  759. (ansi-color-apply-on-region compilation-filter-start
  760. (point)))))
  761. (add-to-list 'compilation-error-regexp-alist
  762. ;; ansible-lint
  763. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  764. ;; Workaround to avoid ensime error
  765. (defvar ensime-mode-key-prefix nil)
  766. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  767. (when (safe-require-or-eval 'company)
  768. (global-company-mode)
  769. (set-variable 'company-idle-delay 0.5)
  770. (set-variable 'company-minimum-prefix-length 2)
  771. (set-variable 'company-selection-wrap-around t))
  772. ;; https://github.com/lunaryorn/flycheck
  773. (when (safe-require-or-eval 'flycheck)
  774. (call-after-init 'global-flycheck-mode))
  775. (when (autoload-eval-lazily 'ilookup)
  776. (define-key ctl-x-map "d" 'ilookup-open-word))
  777. (set-variable 'ac-ignore-case nil)
  778. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  779. (define-key ctl-x-map "t" 'term-run-shell-command))
  780. (add-to-list 'safe-local-variable-values
  781. '(encoding utf-8))
  782. (setq enable-local-variables :safe)
  783. ;; Detect file type from shebang and set major-mode.
  784. (add-to-list 'interpreter-mode-alist
  785. '("python3" . python-mode))
  786. (add-to-list 'interpreter-mode-alist
  787. '("python2" . python-mode))
  788. (with-eval-after-load 'python
  789. (defvar python-mode-map (make-sparse-keymap))
  790. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  791. (when (autoload-eval-lazily 'pipenv)
  792. (add-hook 'python-mode-hook
  793. (lambda ()
  794. (pipenv-mode 1)
  795. (pipenv-projectile-after-switch-default)))
  796. )
  797. (set-variable 'flycheck-python-pycompile-executable "python3")
  798. (set-variable 'python-indent-guess-indent-offset nil)
  799. ;; http://fukuyama.co/foreign-regexp
  800. '(and (safe-require-or-eval 'foreign-regexp)
  801. (progn
  802. (setq foreign-regexp/regexp-type 'perl)
  803. '(setq reb-re-syntax 'foreign-regexp)
  804. ))
  805. (autoload-eval-lazily 'sql '(sql-mode)
  806. (require 'sql-indent nil t))
  807. (when (autoload-eval-lazily 'git-command)
  808. (define-key ctl-x-map "g" 'git-command))
  809. (when (safe-require-or-eval 'git-commit)
  810. (global-git-commit-mode 1))
  811. (with-eval-after-load 'git-commit
  812. (add-hook 'git-commit-setup-hook
  813. 'turn-off-auto-fill t))
  814. (autoload-eval-lazily 'sl)
  815. (with-eval-after-load 'rst
  816. (defvar rst-mode-map)
  817. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  818. (with-eval-after-load 'jdee
  819. (add-hook 'jdee-mode-hook
  820. (lambda ()
  821. (make-local-variable 'global-mode-string)
  822. (add-to-list 'global-mode-string
  823. mode-line-position))))
  824. ;; Cannot enable error thrown. Why???
  825. ;; https://github.com/m0smith/malabar-mode#Installation
  826. ;; (when (autoload-eval-lazily 'malabar-mode)
  827. ;; (add-to-list 'load-path
  828. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  829. ;; (safe-require-or-eval 'cedet-devel-load)
  830. ;; (call-after-init 'activate-malabar-mode))
  831. (with-eval-after-load 'make-mode
  832. (defvar makefile-mode-map (make-sparse-keymap))
  833. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  834. ;; this functions is set in write-file-functions, i cannot find any
  835. ;; good way to remove this.
  836. (fset 'makefile-warn-suspicious-lines 'ignore))
  837. (with-eval-after-load 'verilog-mode
  838. (defvar verilog-mode-map (make-sparse-keymap))
  839. (define-key verilog-mode-map ";" 'self-insert-command))
  840. (setq diff-switches "-u")
  841. (with-eval-after-load 'diff-mode
  842. ;; (when (and (eq major-mode
  843. ;; 'diff-mode)
  844. ;; (not buffer-file-name))
  845. ;; ;; do not pass when major-mode is derived mode of diff-mode
  846. ;; (view-mode 1))
  847. (set-face-attribute 'diff-header nil
  848. :foreground nil
  849. :background nil
  850. :weight 'bold)
  851. (set-face-attribute 'diff-file-header nil
  852. :foreground nil
  853. :background nil
  854. :weight 'bold)
  855. (set-face-foreground 'diff-index "blue")
  856. (set-face-attribute 'diff-hunk-header nil
  857. :foreground "cyan"
  858. :weight 'normal)
  859. (set-face-attribute 'diff-context nil
  860. ;; :foreground "white"
  861. :foreground nil
  862. :weight 'normal)
  863. (set-face-foreground 'diff-removed "red")
  864. (set-face-foreground 'diff-added "green")
  865. (set-face-background 'diff-removed nil)
  866. (set-face-background 'diff-added nil)
  867. (set-face-attribute 'diff-changed nil
  868. :foreground "magenta"
  869. :weight 'normal)
  870. (set-face-attribute 'diff-refine-changed nil
  871. :foreground nil
  872. :background nil
  873. :weight 'bold
  874. :inverse-video t)
  875. ;; Annoying !
  876. ;;(diff-auto-refine-mode)
  877. )
  878. ;; (ffap-bindings)
  879. (set-variable 'browse-url-browser-function
  880. 'eww-browse-url)
  881. (set-variable 'sh-here-document-word "__EOC__")
  882. (when (autoload-eval-lazily 'adoc-mode
  883. nil
  884. (defvar adoc-mode-map (make-sparse-keymap))
  885. (define-key adoc-mode-map (kbd "C-m") 'newline))
  886. (setq auto-mode-alist
  887. `(("\\.adoc\\'" . adoc-mode)
  888. ("\\.asciidoc\\'" . adoc-mode)
  889. ,@auto-mode-alist)))
  890. (with-eval-after-load 'markup-faces
  891. ;; Is this too match ?
  892. (set-face-foreground 'markup-meta-face
  893. "color-245")
  894. (set-face-foreground 'markup-meta-hide-face
  895. "color-245")
  896. )
  897. ;; TODO: check if this is required
  898. (when (autoload-eval-lazily 'groovy-mode nil
  899. (defvar groovy-mode-map (make-sparse-keymap))
  900. (define-key groovy-mode-map "(" 'self-insert-command)
  901. (define-key groovy-mode-map ")" 'self-insert-command)
  902. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  903. )
  904. (add-to-list 'auto-mode-alist
  905. '("build\\.gradle\\'" . groovy-mode)))
  906. (with-eval-after-load 'yaml-mode
  907. (defvar yaml-mode-map (make-sparse-keymap))
  908. (define-key yaml-mode-map (kbd "C-m") 'newline))
  909. (with-eval-after-load 'html-mode
  910. (defvar html-mode-map (make-sparse-keymap))
  911. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  912. (with-eval-after-load 'text-mode
  913. (define-key text-mode-map (kbd "C-m") 'newline))
  914. (autoload-eval-lazily 'info nil
  915. (defvar Info-additional-directory-list)
  916. (dolist (dir (directory-files (concat user-emacs-directory
  917. "info")
  918. t
  919. "^[^.].*"))
  920. (when (file-directory-p dir)
  921. (add-to-list 'Info-additional-directory-list
  922. dir)))
  923. (let ((dir (expand-file-name "~/.brew/share/info")))
  924. (when (file-directory-p dir)
  925. (add-to-list 'Info-additional-directory-list
  926. dir))))
  927. (with-eval-after-load 'apropos
  928. (defvar apropos-mode-map (make-sparse-keymap))
  929. (define-key apropos-mode-map "n" 'next-line)
  930. (define-key apropos-mode-map "p" 'previous-line))
  931. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  932. ;; (define-key isearch-mode-map
  933. ;; (kbd "C-j") 'isearch-other-control-char)
  934. ;; (define-key isearch-mode-map
  935. ;; (kbd "C-k") 'isearch-other-control-char)
  936. ;; (define-key isearch-mode-map
  937. ;; (kbd "C-h") 'isearch-other-control-char)
  938. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  939. (define-key isearch-mode-map (kbd "M-r")
  940. 'isearch-query-replace-regexp)
  941. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  942. (setq lazy-highlight-cleanup nil)
  943. ;; face for isearch highlighing
  944. (set-face-attribute 'lazy-highlight
  945. nil
  946. :foreground `unspecified
  947. :background `unspecified
  948. :underline t
  949. ;; :weight `bold
  950. )
  951. (add-hook 'outline-mode-hook
  952. (lambda ()
  953. (when (string-match "\\.md\\'" buffer-file-name)
  954. (set (make-local-variable 'outline-regexp) "#+ "))))
  955. (add-hook 'outline-mode-hook
  956. 'outline-show-all)
  957. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  958. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  959. (when (autoload-eval-lazily 'markdown-mode
  960. '(markdown-mode gfm-mode)
  961. (defvar gfm-mode-map (make-sparse-keymap))
  962. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  963. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  964. (set-variable 'markdown-command (or (executable-find "markdown")
  965. (executable-find "markdown.pl")
  966. ""))
  967. (add-hook 'markdown-mode-hook
  968. (lambda ()
  969. (outline-minor-mode 1)
  970. (flyspell-mode)
  971. (set (make-local-variable 'comment-start) ";")))
  972. )
  973. ;; c-mode
  974. ;; http://www.emacswiki.org/emacs/IndentingC
  975. ;; http://en.wikipedia.org/wiki/Indent_style
  976. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  977. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  978. (with-eval-after-load 'cc-vars
  979. (defvar c-default-style nil)
  980. (add-to-list 'c-default-style
  981. '(c-mode . "k&r"))
  982. (add-to-list 'c-default-style
  983. '(c++-mode . "k&r")))
  984. (autoload-eval-lazily 'js2-mode nil
  985. ;; currently do not use js2-mode
  986. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  987. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  988. ;; (defvar js2-mode-map (make-sparse-keymap))
  989. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  990. ;; (interactive)
  991. ;; (js2-enter-key)
  992. ;; (indent-for-tab-command)))
  993. ;; (add-hook (kill-local-variable 'before-save-hook)
  994. ;; 'js2-before-save)
  995. ;; (add-hook 'before-save-hook
  996. ;; 'my-indent-buffer
  997. ;; nil
  998. ;; t)
  999. )
  1000. (add-to-list 'interpreter-mode-alist
  1001. '("node" . js-mode))
  1002. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1003. (with-eval-after-load 'uniquify
  1004. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1005. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1006. (setq uniquify-min-dir-content 1))
  1007. (with-eval-after-load 'view
  1008. (defvar view-mode-map (make-sparse-keymap))
  1009. (define-key view-mode-map "j" 'scroll-up-line)
  1010. (define-key view-mode-map "k" 'scroll-down-line)
  1011. (define-key view-mode-map "v" 'toggle-read-only)
  1012. (define-key view-mode-map "q" 'bury-buffer)
  1013. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1014. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1015. ;; (define-key view-mode-map
  1016. ;; "n" 'nonincremental-repeat-search-forward)
  1017. ;; (define-key view-mode-map
  1018. ;; "N" 'nonincremental-repeat-search-backward)
  1019. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1020. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1021. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1022. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1023. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1024. (global-set-key "\M-r" 'view-mode)
  1025. ;; (setq view-read-only t)
  1026. (with-eval-after-load 'term
  1027. (defvar term-raw-map (make-sparse-keymap))
  1028. (define-key term-raw-map (kbd "C-x")
  1029. (lookup-key (current-global-map)
  1030. (kbd "C-x"))))
  1031. (add-hook 'term-mode-hook
  1032. (lambda ()
  1033. ;; Stop current line highlighting
  1034. (set (make-local-variable (defvar hl-line-range-function))
  1035. (lambda () '(0 . 0)))
  1036. (set (make-local-variable 'scroll-margin)
  1037. 0)))
  1038. (add-hook 'Man-mode-hook
  1039. (lambda ()
  1040. (view-mode 1)
  1041. (setq truncate-lines nil)))
  1042. (set-variable 'Man-notify-method (if window-system
  1043. 'newframe
  1044. 'aggressive))
  1045. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1046. "woman_cache.el")))
  1047. ;; not work because man.el will be loaded when man called
  1048. (defalias 'man 'woman)
  1049. (add-to-list 'auto-mode-alist
  1050. '("tox\\.ini\\'" . conf-unix-mode))
  1051. (when (autoload-eval-lazily 'toml-mode)
  1052. (add-to-list 'auto-mode-alist
  1053. '("/tox\\.ini\\'" . toml-mode))
  1054. (add-to-list 'auto-mode-alist
  1055. '("/Pipfile\\'" . toml-mode)))
  1056. (when (autoload-eval-lazily 'json-mode)
  1057. (add-to-list 'auto-mode-alist
  1058. '("/Pipfile\\.lock\\'" . json-mode)))
  1059. (add-hook 'go-mode-hook
  1060. (lambda()
  1061. (defvar go-mode-map)
  1062. (add-hook 'before-save-hook' 'gofmt-before-save)
  1063. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1064. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1065. ;; buffers
  1066. (defvar bs-configurations)
  1067. (when (autoload-eval-lazily 'bs '(bs-show)
  1068. (add-to-list 'bs-configurations
  1069. '("specials" "^\\*" nil ".*" nil nil))
  1070. (add-to-list 'bs-configurations
  1071. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1072. (defvar bs-mode-map)
  1073. (defvar bs-current-configuration)
  1074. (define-key bs-mode-map (kbd "t")
  1075. ;; TODO: fix toggle feature
  1076. (lambda ()
  1077. (interactive)
  1078. (if (string= "specials"
  1079. bs-current-configuration)
  1080. (bs-set-configuration "files")
  1081. (bs-set-configuration "specials"))
  1082. (bs-refresh)
  1083. (bs-message-without-log "%s"
  1084. (bs--current-config-message))))
  1085. ;; (setq bs-configurations (list
  1086. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1087. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1088. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1089. )
  1090. (defalias 'list-buffers 'bs-show)
  1091. (set-variable 'bs-default-configuration "files-and-specials")
  1092. (set-variable 'bs-default-sort-name "by nothing")
  1093. (add-hook 'bs-mode-hook
  1094. (lambda ()
  1095. (set (make-local-variable 'scroll-margin) 0))))
  1096. ;;(iswitchb-mode 1)
  1097. (icomplete-mode)
  1098. (defun iswitchb-buffer-display-other-window ()
  1099. "Do iswitchb in other window."
  1100. (interactive)
  1101. (let ((iswitchb-default-method 'display))
  1102. (call-interactively 'iswitchb-buffer)))
  1103. ;; buffer killing
  1104. ;; (defun my-delete-window-killing-buffer () nil)
  1105. (defun my-query-kill-current-buffer ()
  1106. "Interactively kill current buffer."
  1107. (interactive)
  1108. (if (y-or-n-p (concat "kill current buffer? :"))
  1109. (kill-buffer (current-buffer))))
  1110. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1111. (substitute-key-definition 'kill-buffer
  1112. 'my-query-kill-current-buffer
  1113. global-map)
  1114. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1115. ;; recentf-mode
  1116. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1117. "recentf")))
  1118. (set-variable 'recentf-max-menu-items 20)
  1119. (set-variable 'recentf-max-saved-items 30)
  1120. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1121. (set-variable 'recentf-auto-cleanup 3)
  1122. ;; (safe-require-or-eval 'sync-recentf)
  1123. ;; (when (safe-require-or-eval 'recentf)
  1124. ;; (add-to-list 'recentf-exclude
  1125. ;; (regexp-quote recentf-save-file))
  1126. ;; (add-to-list 'recentf-exclude
  1127. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1128. ;; (add-to-list 'recentf-exclude
  1129. ;; "/sync-recentf-marker\\'")
  1130. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1131. ;; (remove-hook 'find-file-hook
  1132. ;; 'recentf-track-opened-file)
  1133. ;; (defun my-recentf-load-track-save-list ()
  1134. ;; "Load current recentf list from file, track current visiting file, then save
  1135. ;; the list."
  1136. ;; (recentf-load-list)
  1137. ;; (recentf-track-opened-file)
  1138. ;; (recentf-save-list))
  1139. ;; (add-hook 'find-file-hook
  1140. ;; 'my-recentf-load-track-save-list)
  1141. ;; (add-hook 'kill-emacs-hook
  1142. ;; 'recentf-load-list)
  1143. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1144. ;; ;; (add-hook 'find-file-hook
  1145. ;; ;; (lambda ()
  1146. ;; ;; (recentf-add-file default-directory)))
  1147. ;; (when (autoload-eval-lazily 'recentf-show)
  1148. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1149. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1150. ;; ;; 'recentf-load-list)
  1151. ;; )
  1152. ;; (recentf-mode 1)
  1153. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1154. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1155. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1156. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1157. ;; (add-hook 'recentf-dialog-mode-hook
  1158. ;; (lambda ()
  1159. ;; ;; (recentf-save-list)
  1160. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1161. ;; ;; 'my-recentf-cd-and-find-file)
  1162. ;; (cd "~/"))))
  1163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1164. ;; dired
  1165. (defun my-file-head (filename &optional n)
  1166. "Return list of first N lines of file FILENAME."
  1167. ;; TODO: Fix for janapese text
  1168. ;; TODO: Fix for short text
  1169. (let ((num (or n 10))
  1170. (size 100)
  1171. (beg 0)
  1172. (end 0)
  1173. (result '())
  1174. (read -1))
  1175. (with-temp-buffer
  1176. (erase-buffer)
  1177. (while (or (<= (count-lines (point-min)
  1178. (point-max))
  1179. num)
  1180. (eq read 0))
  1181. (setq end (+ beg size))
  1182. (setq read (nth 1 (insert-file-contents-literally filename
  1183. nil
  1184. beg
  1185. end)))
  1186. (goto-char (point-max))
  1187. (setq beg (+ beg size)))
  1188. (goto-char (point-min))
  1189. (while (< (length result) num)
  1190. (let ((start (point)))
  1191. (forward-line 1)
  1192. (setq result
  1193. `(,@result ,(buffer-substring-no-properties start
  1194. (point))))))
  1195. result
  1196. ;; (buffer-substring-no-properties (point-min)
  1197. ;; (progn
  1198. ;; (forward-line num)
  1199. ;; (point)))
  1200. )))
  1201. ;; (apply 'concat (my-file-head "./shrc" 10)
  1202. (defun my-dired-echo-file-head (arg)
  1203. "Echo head of current file.
  1204. ARG is num to show, or defaults to 7."
  1205. (interactive "P")
  1206. (let ((f (dired-get-filename)))
  1207. (message "%s"
  1208. (apply 'concat
  1209. (my-file-head f
  1210. 7)))))
  1211. (defun my-dired-diff ()
  1212. "Show diff of marked file and file of current line."
  1213. (interactive)
  1214. (let ((files (dired-get-marked-files nil nil nil t)))
  1215. (if (eq (car files)
  1216. t)
  1217. (diff (cadr files) (dired-get-filename))
  1218. (message "One file must be marked!"))))
  1219. (defun dired-get-file-info ()
  1220. "Print information of current line file."
  1221. (interactive)
  1222. (let ((f (shell-quote-argument (dired-get-filename t))))
  1223. (if (file-directory-p f)
  1224. (progn
  1225. (message "Calculating disk usage...")
  1226. (shell-command (concat "du -hsD "
  1227. f)))
  1228. (shell-command (concat "file "
  1229. f)))))
  1230. (defun my-dired-scroll-up ()
  1231. "Scroll up."
  1232. (interactive)
  1233. (my-dired-previous-line (- (window-height) 1)))
  1234. (defun my-dired-scroll-down ()
  1235. "Scroll down."
  1236. (interactive)
  1237. (my-dired-next-line (- (window-height) 1)))
  1238. ;; (defun my-dired-forward-line (arg)
  1239. ;; ""
  1240. ;; (interactive "p"))
  1241. (defun my-dired-previous-line (arg)
  1242. "Move ARG lines up."
  1243. (interactive "p")
  1244. (if (> arg 0)
  1245. (progn
  1246. (if (eq (line-number-at-pos)
  1247. 1)
  1248. (goto-char (point-max))
  1249. (forward-line -1))
  1250. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1251. (dired-get-subdir))
  1252. (- arg 1)
  1253. arg)))
  1254. (dired-move-to-filename)))
  1255. (defun my-dired-next-line (arg)
  1256. "Move ARG lines down."
  1257. (interactive "p")
  1258. (if (> arg 0)
  1259. (progn
  1260. (if (eq (point)
  1261. (point-max))
  1262. (goto-char (point-min))
  1263. (forward-line 1))
  1264. (my-dired-next-line (if (or (dired-get-filename nil t)
  1265. (dired-get-subdir))
  1266. (- arg 1)
  1267. arg)))
  1268. (dired-move-to-filename)))
  1269. (defun my-tramp-remote-find-file (f)
  1270. "Open F."
  1271. (interactive (list (read-file-name "My Find File Tramp: "
  1272. "/scp:"
  1273. nil ;; "/scp:"
  1274. (confirm-nonexistent-file-or-buffer))))
  1275. (find-file f))
  1276. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1277. (if (eq window-system 'mac)
  1278. (setq dired-listing-switches "-lhF")
  1279. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1280. )
  1281. (setq dired-listing-switches "-lhF")
  1282. ;; when using dired-find-alternate-file
  1283. ;; reuse current dired buffer for the file to open
  1284. ;; (put 'dired-find-alternate-file 'disabled nil)
  1285. (set-variable 'dired-ls-F-marks-symlinks t)
  1286. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1287. (set-variable 'ls-lisp-dirs-first t)
  1288. (set-variable 'ls-lisp-use-localized-time-format t)
  1289. (set-variable 'ls-lisp-format-time-list
  1290. '("%Y-%m-%d %H:%M"
  1291. "%Y-%m-%d "))
  1292. (set-variable 'dired-dwim-target t)
  1293. (set-variable 'dired-isearch-filenames t)
  1294. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1295. (set-variable 'dired-hide-details-hide-information-lines nil)
  1296. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1297. (set-variable 'dired-recursive-deletes 'always)
  1298. ;; (add-hook 'dired-after-readin-hook
  1299. ;; 'my-replace-nasi-none)
  1300. (with-eval-after-load 'dired
  1301. (safe-require-or-eval 'ls-lisp)
  1302. (defvar dired-mode-map (make-sparse-keymap))
  1303. ;; dired-do-chgrp sometimes cause system hung
  1304. (define-key dired-mode-map "G" 'ignore)
  1305. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1306. (define-key dired-mode-map "i" 'dired-get-file-info)
  1307. (define-key dired-mode-map "f" 'find-file)
  1308. (define-key dired-mode-map "!" 'shell-command)
  1309. (define-key dired-mode-map "&" 'async-shell-command)
  1310. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1311. (define-key dired-mode-map "=" 'my-dired-diff)
  1312. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1313. (define-key dired-mode-map "b" 'gtkbm)
  1314. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1315. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1316. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1317. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1318. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1319. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1320. (substitute-key-definition 'dired-next-line
  1321. 'my-dired-next-line
  1322. dired-mode-map)
  1323. (substitute-key-definition 'dired-previous-line
  1324. 'my-dired-previous-line
  1325. dired-mode-map)
  1326. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1327. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1328. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1329. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1330. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1331. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1332. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1333. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1334. (add-hook 'dired-mode-hook
  1335. (lambda ()
  1336. (when (fboundp 'dired-hide-details-mode)
  1337. (dired-hide-details-mode t)
  1338. (local-set-key "l" 'dired-hide-details-mode))
  1339. (let ((file "._Icon\015"))
  1340. (when nil
  1341. '(file-readable-p file)
  1342. (delete-file file)))))
  1343. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1344. (with-eval-after-load 'dired
  1345. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1346. (when (autoload-eval-lazily 'dired-list-all-mode)
  1347. (setq dired-listing-switches "-lhF")
  1348. (with-eval-after-load 'dired
  1349. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1350. (when (autoload-eval-lazily 'dired-filter)
  1351. (add-hook 'dired-mode-hook
  1352. 'dired-filter-mode))
  1353. ;; Currently disabled in favor of dired-from-git-ls-files
  1354. ;; (define-key ctl-x-map "f" 'find-dired)
  1355. ;; It works!
  1356. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1357. (defun my-dired-git-ls-files (args)
  1358. "Dired from git ls-files."
  1359. (interactive "sgit ls-files args: ")
  1360. (pop-to-buffer-same-window
  1361. (dired-noselect `(,default-directory
  1362. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1363. "\0" t))
  1364. ""))
  1365. )
  1366. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1367. (with-eval-after-load 'dired
  1368. (defvar dired-mode-map (make-sparse-keymap))
  1369. (define-key dired-mode-map "f" 'my-dired-git-ls-files))
  1370. ;; (define-minor-mode my-dired-glob-filter)
  1371. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1372. ;; misc funcs
  1373. (defalias 'qcalc 'quick-calc)
  1374. (defun memo (&optional dir)
  1375. "Open memo.txt in DIR."
  1376. (interactive)
  1377. (pop-to-buffer (find-file-noselect (concat (if dir
  1378. (file-name-as-directory dir)
  1379. "")
  1380. "memo.txt"))))
  1381. (set (defvar my-rgrep-alist nil
  1382. "Alist of rgrep command.
  1383. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1384. condition to choose COMMAND when evaluated.")
  1385. `(
  1386. ;; ripgrep
  1387. ("rg"
  1388. (executable-find "rg")
  1389. "rg --hidden --no-heading --smart-case ")
  1390. ;; git grep
  1391. ("gitgrep"
  1392. (eq 0
  1393. (shell-command "git rev-parse --git-dir"))
  1394. "git --no-pager -c color.grep=always grep -nH -e ")
  1395. ;; sift
  1396. ("sift"
  1397. (executable-find "sift")
  1398. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1399. ;; the silver searcher
  1400. ("ag"
  1401. (executable-find "ag")
  1402. "ag --nogroup --nopager --filename ")
  1403. ;; ack
  1404. ("ack"
  1405. (executable-find "ack")
  1406. "ack --nogroup --nopager --with-filename ")
  1407. ;; gnu global
  1408. ("global"
  1409. (and (require 'ggtags nil t)
  1410. (executable-find "global")
  1411. (ggtags-current-project-root))
  1412. "global --result grep ")
  1413. ;; grep
  1414. ("grep"
  1415. t
  1416. ,(concat "find . "
  1417. "-path '*/.git' -prune -o "
  1418. "-path '*/.svn' -prune -o "
  1419. "-type f -print0 | "
  1420. "xargs -0 grep -nH -e "))
  1421. )
  1422. )
  1423. (defvar my-rgrep-default nil
  1424. "Default command name for my-rgrep.")
  1425. (defun my-rgrep-grep-command (&optional name alist)
  1426. "Return recursive grep command for current directory or nil.
  1427. If NAME is given, use that without testing.
  1428. Commands are searched from ALIST."
  1429. (if alist
  1430. (if name
  1431. ;; if name is given search that from alist and return the command
  1432. (nth 2 (assoc name
  1433. alist))
  1434. ;; if name is not given try test in 1th elem
  1435. (let ((car (car alist))
  1436. (cdr (cdr alist)))
  1437. (if (eval (nth 1 car))
  1438. ;; if the condition is true return the command
  1439. (nth 2 car)
  1440. ;; try next one
  1441. (and cdr
  1442. (my-rgrep-grep-command name cdr)))))
  1443. ;; if alist is not given set default value
  1444. (my-rgrep-grep-command name my-rgrep-alist)))
  1445. (defun my-rgrep (command-args)
  1446. "My recursive grep. Run COMMAND-ARGS.
  1447. If prefix argument is given, use current symbol as default search target
  1448. and search from projectile root (if projectile is available)."
  1449. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1450. nil)))
  1451. (if cmd
  1452. (list (read-shell-command "grep command: "
  1453. (concat cmd
  1454. (if current-prefix-arg
  1455. (thing-at-point 'symbol t)
  1456. ""))
  1457. 'grep-find-history))
  1458. (error "My-Rgrep: Command for rgrep not found")
  1459. )))
  1460. (if (and current-prefix-arg
  1461. (safe-require-or-eval 'projectile)
  1462. (projectile-project-p))
  1463. (projectile-with-default-dir (projectile-project-root)
  1464. (compilation-start command-args
  1465. 'grep-mode))
  1466. (compilation-start command-args
  1467. 'grep-mode)))
  1468. (defun my-rgrep-thing-at-point-projectile-root ()
  1469. "My recursive grep to find thing at point from project root."
  1470. (interactive)
  1471. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1472. nil))
  1473. (command-args
  1474. (if cmd
  1475. (concat cmd
  1476. (or (thing-at-point 'symbol t)
  1477. (error "No symbol at point")))
  1478. (error "My-Rgrep: Command for rgrep not found"))))
  1479. (if (safe-require-or-eval 'projectile)
  1480. (projectile-with-default-dir (or (projectile-project-root)
  1481. default-directory)
  1482. (compilation-start command-args
  1483. 'grep-mode))
  1484. (compilation-start command-args
  1485. 'grep-mode))))
  1486. (defmacro define-my-rgrep (name)
  1487. "Define rgrep for NAME."
  1488. `(defun ,(intern (concat "my-rgrep-"
  1489. name)) ()
  1490. ,(format "My recursive grep by %s."
  1491. name)
  1492. (interactive)
  1493. (let ((my-rgrep-default ,name))
  1494. (if (called-interactively-p 'any)
  1495. (call-interactively 'my-rgrep)
  1496. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1497. )
  1498. (define-my-rgrep "ack")
  1499. (define-my-rgrep "ag")
  1500. (define-my-rgrep "rg")
  1501. (define-my-rgrep "sift")
  1502. (define-my-rgrep "gitgrep")
  1503. (define-my-rgrep "grep")
  1504. (define-my-rgrep "global")
  1505. (define-key ctl-x-map "s" 'my-rgrep)
  1506. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1507. (defun my-occur (regexp &optional region)
  1508. "My occur command to search REGEXP."
  1509. (interactive (list (read-string "List lines matching regexp: "
  1510. (thing-at-point 'symbol t))))
  1511. (occur regexp nil region))
  1512. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1513. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1514. (defalias 'make 'compile)
  1515. (define-key ctl-x-map "c" 'compile)
  1516. ;;;;;;;;;;;;;;;;;;;;;;;
  1517. ;; adoc-simple-mode
  1518. (when (safe-require-or-eval 'adoc-mode)
  1519. (defvar adoc-simple-font-lock-keywords
  1520. nil)
  1521. (define-derived-mode adoc-simple-mode adoc-mode
  1522. "Adoc-Simple"
  1523. "Major mode for editing AsciiDoc text files.
  1524. This mode is a simplified version of `adoc-mode'."
  1525. '(set (make-local-variable 'font-lock-defaults)
  1526. '(adoc-simple-font-lock-keywords
  1527. nil nil nil nil
  1528. (font-lock-multiline . t)
  1529. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1530. (add-to-list 'auto-mode-alist
  1531. '("\\.adoc\\'" . adoc-simple-mode)))
  1532. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1533. ;; awk-preview
  1534. (defgroup awk-preview nil
  1535. "Awk previewer."
  1536. :tag "Awk Preview"
  1537. :prefix "awk-preview-"
  1538. :group 'tools)
  1539. (defcustom awk-preview-program
  1540. (or (executable-find "gawk")
  1541. (executable-find "awk")
  1542. "awk")
  1543. "Awk program to execute."
  1544. :type 'string
  1545. :group 'awk-preview)
  1546. (defcustom awk-preview-switches
  1547. ;; '("--sandbox")
  1548. nil
  1549. "String of awk options appended when running awk preview."
  1550. :type '(repeat string)
  1551. :group 'awk-preview)
  1552. (defcustom awk-preview-default-program
  1553. "# C-c C-l to update preview, C-c C-c to commit, C-c C-k to abort.
  1554. {
  1555. print NR, $0
  1556. }
  1557. "
  1558. "Default awk command."
  1559. :type 'string
  1560. :group 'awk-preview)
  1561. (cl-defstruct awk-preview--env
  1562. (running-p nil)
  1563. ;; Point of beg in source buffer
  1564. (point-beg nil)
  1565. ;; Point of end in source buffer
  1566. (point-end nil)
  1567. ;; Point of beginning
  1568. ;; Used by preview buffer and always same as awk-preview--point-beg
  1569. (preview-point-beg nil)
  1570. ;; Point of beginning
  1571. ;; Used by preview buffer and may defferent from awk-preview--point-end
  1572. (preview-point-end nil)
  1573. ;; Awk preview program file name
  1574. (program-filename nil)
  1575. ;; Source buffer
  1576. (source-buffer nil)
  1577. ;; Preview buffer
  1578. (preview-buffer nil)
  1579. ;; Program buffer
  1580. (program-buffer nil)
  1581. ;; Window configuration when entering awk-review
  1582. (window-configuration nil)
  1583. )
  1584. (defvar awk-preview--env nil
  1585. "`awk-preview--env' struct object of currently running.")
  1586. (make-variable-buffer-local 'awk-preview--env)
  1587. (defvar awk-preview--point-beg
  1588. nil
  1589. "Point of beginning.")
  1590. (make-variable-buffer-local 'awk-preview--point-beg)
  1591. (defvar awk-preview--point-end
  1592. nil
  1593. "Point of end.")
  1594. (make-variable-buffer-local 'awk-preview--point-end)
  1595. (defvar awk-preview--preview-point-beg
  1596. nil
  1597. "Point of beginning.
  1598. Used by preview buffer and always same as awk-preview--point-beg.")
  1599. (make-variable-buffer-local 'awk-preview--preview-point-beg)
  1600. (defvar awk-preview--preview-point-end
  1601. nil
  1602. "Point of beginning.
  1603. Used by preview buffer and may defferent from awk-preview--point-end.")
  1604. (make-variable-buffer-local 'awk-preview--preview-point-end)
  1605. (defvar awk-preview--program-filename
  1606. nil
  1607. "Awk preview program file name.")
  1608. (make-variable-buffer-local 'awk-preview--program-filename)
  1609. (defvar awk-preview--source-buffer
  1610. nil
  1611. "Awk source buffer.")
  1612. (make-variable-buffer-local 'awk-preview--source-buffer)
  1613. (defvar awk-preview--preview-buffer
  1614. nil
  1615. "Awk preview buffer.")
  1616. (make-variable-buffer-local 'awk-preview--preview-buffer)
  1617. (defvar awk-preview--program-buffer
  1618. nil
  1619. "Awk program buffer.")
  1620. (make-variable-buffer-local 'awk-preview--program-buffer)
  1621. (defvar awk-preview--window-configuration
  1622. nil
  1623. "Awk preview window configuration.")
  1624. (defun awk-preview--invoke-awk (buf beg end progfile output &optional delete)
  1625. "Execute PROGFILE awk process with BEG and END input and output to OUTPUT buffer.
  1626. When OUTPUT is t the result will be output to BUF.
  1627. Delete the text between BEG and END when DELETE is non-nil."
  1628. (with-current-buffer buf
  1629. (let ((status (apply 'call-process-region
  1630. beg
  1631. end
  1632. awk-preview-program
  1633. delete
  1634. output
  1635. nil
  1636. `(,@awk-preview-switches "-f" ,progfile))))
  1637. (unless (eq status
  1638. 0)
  1639. (error "awk-preview: Awk program exited abnormally."))
  1640. output)))
  1641. (defvar awk-preview-program-buffer-name
  1642. "*AWK Preview Program<%s>*"
  1643. "Buffer name of awk preview program.")
  1644. (defvar awk-preview-preview-buffer-name
  1645. "*AWK Preview<%s>*"
  1646. "Buffer name of awk preview.")
  1647. (defun awk-preview--create-program-buffer (source preview)
  1648. "Create and show awk program buffer for SOURCE and PREVIEW buffer.
  1649. Return that buffer."
  1650. (let ((source-name (buffer-name source)))
  1651. (with-current-buffer (generate-new-buffer (format awk-preview-program-buffer-name
  1652. source-name))
  1653. (erase-buffer)
  1654. (insert awk-preview-default-program)
  1655. (awk-mode)
  1656. (awk-preview-program-mode 1)
  1657. (setq awk-preview--source-buffer source)
  1658. (setq awk-preview--preview-buffer preview)
  1659. (setq awk-preview--program-buffer (current-buffer))
  1660. (setq awk-preview--program-filename (make-temp-file "awk-preview-"
  1661. nil
  1662. ".awk"))
  1663. (current-buffer))))
  1664. (defun awk-preview-program-buffer-kill-hook ()
  1665. "Cleanup for awk-preview program buffer."
  1666. (when awk-preview--program-filename
  1667. (delete-file awk-preview--program-filename)))
  1668. (add-hook 'kill-buffer-hook
  1669. 'awk-preview-program-buffer-kill-hook)
  1670. (defun awk-preview--create-preview-buffer (source)
  1671. "Create preview buffer of SOURCE buffer and return it."
  1672. (with-current-buffer source
  1673. (let ((buffer-file-name nil)
  1674. (beg awk-preview--point-beg)
  1675. (end awk-preview--point-end))
  1676. (with-current-buffer (clone-buffer (format awk-preview-preview-buffer-name
  1677. (buffer-name)))
  1678. (awk-preview-program-mode 1)
  1679. (setq awk-preview--preview-point-beg beg)
  1680. (setq awk-preview--preview-point-end end)
  1681. (setq awk-preview--source-buffer source)
  1682. (setq awk-preview--preview-buffer (current-buffer))
  1683. (goto-char end)
  1684. (setq buffer-read-only t)
  1685. (current-buffer)))))
  1686. (defun awk-preview (beg end)
  1687. "Run awk and preview result."
  1688. (interactive "r")
  1689. (setq awk-preview--point-beg beg)
  1690. (setq awk-preview--point-end end)
  1691. (setq awk-preview--source-buffer (current-buffer))
  1692. (setq awk-preview--preview-buffer
  1693. (awk-preview--create-preview-buffer (current-buffer)))
  1694. (setq awk-preview--program-buffer
  1695. (awk-preview--create-program-buffer (current-buffer)
  1696. awk-preview--preview-buffer))
  1697. ;; At creation of preview buffer, program buffer is not available so set this
  1698. ;; here
  1699. (let ((b awk-preview--program-buffer))
  1700. (with-current-buffer awk-preview--preview-buffer
  1701. (setq awk-preview--program-buffer b)))
  1702. (setq awk-preview--window-configuration
  1703. (current-window-configuration))
  1704. (set-window-buffer (get-buffer-window awk-preview--source-buffer)
  1705. awk-preview--preview-buffer)
  1706. (pop-to-buffer awk-preview--program-buffer)
  1707. (switch-to-buffer awk-preview--program-buffer)
  1708. (awk-preview-update-preview)
  1709. )
  1710. (defun awk-preview-update-preview ()
  1711. "Update awk-preview."
  1712. (interactive)
  1713. (with-current-buffer awk-preview--program-buffer
  1714. (write-region (point-min)
  1715. (point-max)
  1716. awk-preview--program-filename))
  1717. (let ((output (with-current-buffer (get-buffer-create " *awk-preview output*")
  1718. (erase-buffer)
  1719. (current-buffer)))
  1720. (progfile (with-current-buffer awk-preview--program-buffer
  1721. awk-preview--program-filename)))
  1722. (with-current-buffer awk-preview--source-buffer
  1723. (awk-preview--invoke-awk (current-buffer)
  1724. awk-preview--point-beg
  1725. awk-preview--point-end
  1726. progfile
  1727. output)
  1728. (with-current-buffer awk-preview--preview-buffer
  1729. (let ((inhibit-read-only t))
  1730. (goto-char awk-preview--preview-point-end)
  1731. (delete-region awk-preview--preview-point-beg (point))
  1732. (insert-buffer-substring output)
  1733. (setq awk-preview--preview-point-end (point))))
  1734. )))
  1735. (defun awk-preview-commit ()
  1736. "Exit awk-preview and update buffer."
  1737. (interactive)
  1738. ;; (with-current-buffer awk-preview--source-buffer
  1739. ;; (awk-preview--invoke-awk (current-buffer)
  1740. ;; awk-preview--point-beg
  1741. ;; awk-preview--point-end
  1742. ;; progfile
  1743. ;; t
  1744. ;; t))
  1745. (awk-preview--cleanup))
  1746. (defun awk-preview-abort ()
  1747. "Discard result and exit awk-preview."
  1748. (interactive)
  1749. (awk-preview--cleanup))
  1750. (defun awk-preview--cleanup()
  1751. "Cleanup awk preview buffers and variables.")
  1752. (defvar awk-preview-program-mode-map
  1753. (let ((map (make-sparse-keymap)))
  1754. (define-key map (kbd "C-c C-l") 'awk-preview-update-preview)
  1755. (define-key map (kbd "C-c C-k") 'awk-preview-abort)
  1756. (define-key map (kbd "C-c C-c") 'awk-preview-commit)
  1757. map)
  1758. "Keymap for `awk-preview-program-mode'.")
  1759. (define-minor-mode awk-preview-program-mode
  1760. "Minor mode for awk-preview program buffer."
  1761. :lighter " AWKPrev"
  1762. :keymap awk-preview-program-mode-map)
  1763. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1764. ;; editorconfig-auto-apply
  1765. (define-minor-mode editorconfig-auto-apply-mode
  1766. "When saving .editorconfig file update buffer configs."
  1767. :global t
  1768. :lighter ""
  1769. (if editorconfig-auto-apply-mode
  1770. (add-hook 'after-save-hook
  1771. 'editorconfig-auto-apply-mode--run)
  1772. (remove-hook 'after-save-hook
  1773. 'editorconfig-auto-apply-mode--run)))
  1774. (defun editorconfig-auto-apply-mode--run ()
  1775. "When saving .editorconfig file walk all buffers and update configs."
  1776. (when (eq major-mode
  1777. 'editorconfig-conf-mode)
  1778. (let ((dir (file-name-directory buffer-file-name)))
  1779. (cl-dolist (buf (buffer-list))
  1780. (when (and (buffer-file-name buf)
  1781. (file-in-directory-p (buffer-file-name buf)
  1782. dir))
  1783. (with-current-buffer buf
  1784. (editorconfig-mode-apply)))))))
  1785. (editorconfig-auto-apply-mode 1)
  1786. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1787. ;; recently
  1788. (defgroup recently nil
  1789. "Recently visited files"
  1790. :tag "Recently"
  1791. :prefix "recently-"
  1792. :group 'tools)
  1793. (defcustom recently-file
  1794. (concat user-emacs-directory
  1795. "recently.el")
  1796. "Recently file."
  1797. :type 'string
  1798. :group 'recently)
  1799. (defcustom recently-max
  1800. 100
  1801. "Recently list max length."
  1802. :type 'int
  1803. :group 'recently)
  1804. (defcustom recently-excludes
  1805. '()
  1806. "List of regexps for filenames excluded from the recent list."
  1807. :type '(repeat string)
  1808. :group 'recently)
  1809. (add-to-list 'recently-excludes
  1810. (eval-when-compile (rx "/COMMIT_EDITMSG" eot)))
  1811. (defvar recently-list
  1812. '()
  1813. "Recently list.")
  1814. (defvar recently-file-mtime
  1815. nil
  1816. "Modified time of file when last read file.")
  1817. (defun recently-write ()
  1818. "Write to file."
  1819. (with-temp-buffer
  1820. (prin1 recently-list
  1821. (current-buffer))
  1822. (write-region (point-min)
  1823. (point-max)
  1824. recently-file)))
  1825. (defun recently-read ()
  1826. "Read file."
  1827. (when (file-readable-p recently-file)
  1828. (with-temp-buffer
  1829. (insert-file-contents recently-file)
  1830. (goto-char (point-min))
  1831. (setq recently-list
  1832. (read (current-buffer))))
  1833. (setq recently-file-mtime
  1834. (nth 5
  1835. (file-attributes recently-file)))))
  1836. (defun recently-reload ()
  1837. "Reload file."
  1838. (when (and (file-readable-p recently-file)
  1839. (not (equal recently-file-mtime
  1840. (nth 5
  1841. (file-attributes recently-file)))))
  1842. (recently-read)
  1843. (cl-assert (equal recently-file-mtime
  1844. (nth 5
  1845. (file-attributes recently-file))))))
  1846. (defun recently-add (path)
  1847. "Add PATH to list."
  1848. (cl-assert (string= path
  1849. (expand-file-name path)))
  1850. (when (cl-loop for re in recently-excludes
  1851. if (string-match re path) return nil
  1852. finally return t)
  1853. (recently-reload)
  1854. (let* ((l (copy-list recently-list))
  1855. (l (delete path
  1856. l))
  1857. (l (cl-loop for e in l
  1858. unless (file-in-directory-p path e)
  1859. collect e))
  1860. (l (cons path
  1861. l))
  1862. (l (recently--truncate l
  1863. recently-max)))
  1864. (unless (equal recently-list
  1865. l)
  1866. (setq recently-list l)
  1867. (recently-write)))))
  1868. (defun recently--truncate (list len)
  1869. "Truncate LIST to LEN."
  1870. (if (> (length list)
  1871. len)
  1872. (cl-subseq list
  1873. 0
  1874. len)
  1875. list))
  1876. (defun recently-find-file-hook ()
  1877. "Add current file."
  1878. (when buffer-file-name
  1879. (recently-add buffer-file-name)))
  1880. (defun recently-dired-mode-hook ()
  1881. "Add current directory."
  1882. (recently-add (expand-file-name default-directory)))
  1883. (add-hook 'find-file-hook
  1884. 'recently-find-file-hook)
  1885. (add-hook 'dired-mode-hook
  1886. 'recently-dired-mode-hook)
  1887. ;;;;;;;;;;;;;;;;
  1888. ;; recently-show
  1889. (defvar recently-show-window-height 10
  1890. "Max height of window of `recently-show'")
  1891. ;; (defvar recently-show-mode-map
  1892. ;; (let ((map (make-sparse-keymap)))
  1893. ;; (suppress-keymap map)
  1894. ;; (define-key map "n" 'next-line)
  1895. ;; (define-key map "p" 'previous-line)
  1896. ;; (define-key map (kbd "C-m") 'recently-show-find-file)
  1897. ;; (define-key map (kbd "SPC") 'recently-show-find-file)
  1898. ;; (define-key map "v" 'recently-show-view-file)
  1899. ;; (define-key map "@" 'recently-show-dired)
  1900. ;; (define-key map "q" 'recently-show-close)
  1901. ;; (define-key map (kbd "C-g") 'recently-show-close)
  1902. ;; (define-key map "?" 'describe-mode)
  1903. ;; (define-key map "/" 'isearch-forward)
  1904. ;; map))
  1905. ;; (defvar recently-show-before-listing-hook nil
  1906. ;; "Hook run before creating buffer of `recently-show'.")
  1907. (defvar recently-show-window-configuration nil
  1908. "Used for internal")
  1909. (defvar recently-show-abbreviate t
  1910. "Non-nil means use `abbreviate-file-name' when listing recently opened files.")
  1911. ;; (define-derived-mode recently-show-mode special-mode "recently-show"
  1912. ;; "Major mode for `recently-show'."
  1913. ;; ;; (set (make-local-variable 'scroll-margin)
  1914. ;; ;; 0)
  1915. ;; )
  1916. ;;;###autoload
  1917. (defun recently-show (&optional files buffer-name)
  1918. "Show simplified list of recently opened files.
  1919. If optional argument FILES is non-nil, it is a list of recently-opened
  1920. files to choose from. It defaults to the whole recent list.
  1921. If optional argument BUFFER-NAME is non-nil, it is a buffer name to
  1922. use for the buffer. It defaults to \"*recetf-show*\"."
  1923. (interactive)
  1924. (let ((bf (recently-show--create-buffer-tabulated files buffer-name)))
  1925. (if bf
  1926. (progn
  1927. ;; (recently-save-list)
  1928. (setq recently-show-window-configuration (current-window-configuration))
  1929. ;;(pop-to-buffer bf t t)
  1930. (display-buffer bf)
  1931. ;; (set-window-text-height (selected-window)
  1932. ;; recently-show-window-height)
  1933. ;; (shrink-window-if-larger-than-buffer (selected-window))
  1934. )
  1935. (message "No recent file!"))))
  1936. (defun recently-show--create-buffer-tabulated (&optional files buffer-name)
  1937. "Create buffer listing recently files FILES."
  1938. (let ((bname (or buffer-name
  1939. "*recently-show-tabulated*"))
  1940. (list (or files
  1941. (progn
  1942. (recently-reload)
  1943. recently-list))))
  1944. (when list
  1945. (when (get-buffer bname)
  1946. (kill-buffer bname))
  1947. (with-current-buffer (get-buffer-create bname)
  1948. ;; (setq tabulated-list-sort-key (cons "Name" nil))
  1949. (setq tabulated-list-entries
  1950. (mapcar (lambda (f)
  1951. (list f
  1952. (vector (file-name-nondirectory f)
  1953. (if recently-show-abbreviate
  1954. (abbreviate-file-name f)
  1955. f))))
  1956. ;; list
  1957. recently-list
  1958. ))
  1959. (let ((max
  1960. (apply 'max
  1961. (mapcar (lambda (l)
  1962. (length (elt (cadr l) 0)))
  1963. tabulated-list-entries))))
  1964. (setq tabulated-list-format
  1965. `[("Name"
  1966. ,(min max
  1967. 30)
  1968. t)
  1969. ("Full Path" 0 t)])
  1970. )
  1971. (recently-show-tabulated-mode)
  1972. (current-buffer)))))
  1973. (defun recently-show-tabulated-find-file ()
  1974. "Find-file in `recently-show-tabulated-mode'."
  1975. (interactive)
  1976. (let ((f (tabulated-list-get-id)))
  1977. (when f
  1978. (recently-show-tabulated-close)
  1979. (find-file f))))
  1980. (defvar recently-show-tabulated-mode-map
  1981. (let ((map (make-sparse-keymap)))
  1982. (suppress-keymap map)
  1983. (define-key map (kbd "C-m") 'recently-show-tabulated-find-file)
  1984. ;; TODO: implement
  1985. (define-key map "v" 'recently-show-tabulated-view-file)
  1986. ;; TODO: implement
  1987. (define-key map "@" 'recently-show-tabulated-dired)
  1988. (define-key map (kbd "C-g") 'recently-show-tabulated-close)
  1989. (define-key map "/" 'isearch-forward)
  1990. map))
  1991. (define-derived-mode recently-show-tabulated-mode tabulated-list-mode "Recently Show"
  1992. "Major mode for browsing recently opened files and directories."
  1993. (setq tabulated-list-padding 2)
  1994. ;; TODO: Implement revert
  1995. ;; (add-hook 'tabulated-list-revert-hook 'recently-reload nil t)
  1996. (tabulated-list-init-header)
  1997. (tabulated-list-print nil nil))
  1998. ;; (defun recently-show-create-buffer (&optional files buffer-name)
  1999. ;; "Create buffer listing recently files."
  2000. ;; (let ((bname (or buffer-name
  2001. ;; "*recently-show*"))
  2002. ;; (list (or files
  2003. ;; (progn
  2004. ;; (recently-reload)
  2005. ;; recently-list))))
  2006. ;; (when list
  2007. ;; (and (get-buffer bname)
  2008. ;; (kill-buffer bname))
  2009. ;; (let ((bf (get-buffer-create bname)))
  2010. ;; (with-current-buffer bf
  2011. ;; (recently-show-mode)
  2012. ;; (let ((inhibit-read-only t))
  2013. ;; (mapc (lambda (f)
  2014. ;; (insert (if recently-show-abbreviate
  2015. ;; (abbreviate-file-name f)
  2016. ;; f)
  2017. ;; "\n"))
  2018. ;; list))
  2019. ;; (goto-char (point-min))
  2020. ;; ;; (setq buffer-read-only t)
  2021. ;; )
  2022. ;; bf))))
  2023. ;; (defun recently-show-close ()
  2024. ;; "Close recently-show window."
  2025. ;; (interactive)
  2026. ;; (kill-buffer (current-buffer))
  2027. ;; (set-window-configuration recently-show-window-configuration))
  2028. (defun recently-show-tabulated-close ()
  2029. "Close recently-show window."
  2030. (interactive)
  2031. (kill-buffer (current-buffer))
  2032. (set-window-configuration recently-show-window-configuration))
  2033. ;; (defun recently-show-find-file ()
  2034. ;; "Fine file of current line."
  2035. ;; (interactive)
  2036. ;; (let ((f (recently-show-get-filename)))
  2037. ;; (recently-show-close)
  2038. ;; (find-file f)))
  2039. ;; (defun recently-show-view-file ()
  2040. ;; "view file of current line."
  2041. ;; (interactive)
  2042. ;; (let ((f (recently-show-get-filename)))
  2043. ;; (recently-show-close)
  2044. ;; (view-file f)))
  2045. ;; (defun recently-show-get-filename ()
  2046. ;; "Get filename of current line."
  2047. ;; (buffer-substring-no-properties (point-at-bol)
  2048. ;; (point-at-eol)))
  2049. ;; (defun recently-show-dired()
  2050. ;; "Open dired buffer of directory containing file of current line."
  2051. ;; (interactive)
  2052. ;; (let ((f (recently-show-get-filename)))
  2053. ;; (recently-show-close)
  2054. ;; (dired (if (file-directory-p f)
  2055. ;; f
  2056. ;; (or (file-name-directory f)
  2057. ;; ".")))))
  2058. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  2059. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2060. ;; git walktree
  2061. (defgroup git-walktree nil
  2062. "Git Walktree."
  2063. :tag "GitWalktree"
  2064. :prefix "git-walktree-"
  2065. :group 'tools)
  2066. (defvar git-walktree-current-committish nil
  2067. "Committish name of currently browsing.")
  2068. (make-variable-buffer-local 'git-walktree-current-committish)
  2069. (defvar git-walktree-current-path nil
  2070. "Path name currently visiting without leading and trailing slash.
  2071. This path is always relative to repository root.")
  2072. (make-variable-buffer-local 'git-walktree-current-path)
  2073. (defvar git-walktree-buffer-file-name nil
  2074. "Psudo filename of current buffer.")
  2075. (make-variable-buffer-local 'git-walktree-buffer-file-name)
  2076. (defvar git-walktree-object-full-sha1 nil
  2077. "Object name in full sha1 format of current buffer.")
  2078. (make-variable-buffer-local 'git-walktree-object-full-sha1)
  2079. (defvar git-walktree-repository-root nil
  2080. "Repository root path of current buffer.")
  2081. (make-variable-buffer-local 'git-walktree-repository-root)
  2082. (put 'git-walktree-repository-root
  2083. 'permanent-local
  2084. t)
  2085. (defun git-walktree--committish-fordisplay (committish)
  2086. "Convert COMMITTISH and return is a suitable format for displaying."
  2087. (if (and committish
  2088. (string-match-p "\\`[0-9a-f]+\\'"
  2089. committish)
  2090. (>= (length committish) 32))
  2091. (git-walktree--git-plumbing "rev-parse"
  2092. "--short"
  2093. committish)
  2094. committish))
  2095. (defun git-walktree--create-buffer (committish name type)
  2096. "Create and return buffer for COMMITTISH:NAME.
  2097. TYPE is target object type."
  2098. (let* ((root (git-walktree--git-plumbing "rev-parse"
  2099. "--show-toplevel"))
  2100. (committish-display (git-walktree--committish-fordisplay committish))
  2101. (name (format "%s:%s"
  2102. (or committish-display "")
  2103. name)))
  2104. (if (and git-walktree-reuse-tree-buffer
  2105. (string= type "tree"))
  2106. (with-current-buffer (or git-walktree-tree-buffer-for-reuse
  2107. (setq git-walktree-tree-buffer-for-reuse
  2108. (generate-new-buffer "gitwalktreebuf")))
  2109. (setq git-walktree-repository-root root)
  2110. (rename-buffer name t)
  2111. (current-buffer))
  2112. (with-current-buffer (get-buffer-create name)
  2113. (if git-walktree-repository-root
  2114. (if (string= root
  2115. git-walktree-repository-root)
  2116. (current-buffer)
  2117. ;; If the buffer is for another repository, create new buffer
  2118. (with-current-buffer (generate-new-buffer name)
  2119. (setq git-walktree-repository-root root)
  2120. (current-buffer)))
  2121. ;; New buffer
  2122. (setq git-walktree-repository-root root)
  2123. (current-buffer))))))
  2124. (defun git-walktree--replace-into-buffer (target)
  2125. "Replace TARGET buffer contents with that of current buffer.
  2126. It also copy text overlays."
  2127. (let ((src (current-buffer)))
  2128. (with-current-buffer target
  2129. (replace-buffer-contents src)))
  2130. ;; Copy color overlays
  2131. (let ((overlays (overlays-in (point-min) (point-max))))
  2132. (dolist (o overlays)
  2133. (let ((beg (overlay-start o))
  2134. (end (overlay-end o)))
  2135. (move-overlay (copy-overlay o)
  2136. beg
  2137. end
  2138. target)))))
  2139. (require 'ansi-color)
  2140. (defun git-walktree--open-treeish (committish path treeish)
  2141. "Open git tree buffer of COMMITISH:PATH.
  2142. TREEISH should be a tree-ish object full-sha1 of COMMITISH:PATH."
  2143. (cl-assert path)
  2144. (cl-assert treeish)
  2145. (let* (point-tree-start
  2146. (type (git-walktree--git-plumbing "cat-file"
  2147. "-t"
  2148. treeish))
  2149. (buf (git-walktree--create-buffer committish path type))
  2150. )
  2151. (cl-assert (member type
  2152. '("commit" "tree")))
  2153. (with-current-buffer buf
  2154. (unless (and (string= treeish
  2155. git-walktree-object-full-sha1)
  2156. (or (eq committish
  2157. git-walktree-current-committish)
  2158. (string= committish
  2159. git-walktree-current-committish)))
  2160. (buffer-disable-undo)
  2161. ;; For running git command go back to repository root
  2162. (cd git-walktree-repository-root)
  2163. (save-excursion
  2164. (let ((inhibit-read-only t))
  2165. ;; Remove existing overlays generated by ansi-color-apply-on-region
  2166. (remove-overlays)
  2167. (with-temp-buffer
  2168. (if committish
  2169. (progn (git-walktree--call-process nil
  2170. "show"
  2171. ;; TODO: Make this args configurable
  2172. ;; "--no-patch"
  2173. "--color=always"
  2174. "--pretty=short"
  2175. "--decorate"
  2176. "--stat"
  2177. committish)
  2178. (ansi-color-apply-on-region (point-min)
  2179. (point))
  2180. (insert "\n")
  2181. (insert (format "Contents of '%s:%s':\n"
  2182. (git-walktree--committish-fordisplay committish)
  2183. path)))
  2184. (insert (format "Contents of treeish object '%s:\n"
  2185. treeish)))
  2186. (setq point-tree-start (point))
  2187. (git-walktree--call-process nil
  2188. "ls-tree"
  2189. ;; "-r"
  2190. "--abbrev"
  2191. treeish)
  2192. (git-walktree--replace-into-buffer buf))
  2193. ))
  2194. (git-walktree-mode)
  2195. (set-buffer-modified-p nil)
  2196. (setq git-walktree-current-committish committish)
  2197. (setq git-walktree-current-path path)
  2198. (setq git-walktree-object-full-sha1 treeish)
  2199. (let ((dir (expand-file-name path git-walktree-repository-root)))
  2200. (when (and git-walktree-try-cd
  2201. (file-directory-p dir))
  2202. (cd dir)))
  2203. (when (= (point) (point-min))
  2204. (goto-char point-tree-start)
  2205. (git-walktree-mode--move-to-file)
  2206. )
  2207. ))
  2208. buf))
  2209. (defun git-walktree--call-process (&optional infile &rest args)
  2210. "Call git command with input from INFILE and args ARGS.
  2211. Result will be inserted into current buffer."
  2212. (let ((status (apply 'call-process
  2213. git-walktree-git-executable
  2214. infile
  2215. t
  2216. nil
  2217. args)))
  2218. (unless (eq 0
  2219. status)
  2220. (error "Failed to call git process %S %S"
  2221. infile
  2222. args))))
  2223. ?w
  2224. (defun git-walktree--open-blob (committish path blob)
  2225. "Open blob object of COMMITISH:PATH.
  2226. BLOB should be a object full sha1 of COMMITISH:PATH."
  2227. (cl-assert committish)
  2228. (cl-assert path)
  2229. (cl-assert blob)
  2230. (let* ((type (git-walktree--git-plumbing "cat-file"
  2231. "-t"
  2232. blob))
  2233. (buf (git-walktree--create-buffer committish path type)))
  2234. (cl-assert (string= type "blob"))
  2235. (with-current-buffer buf
  2236. (unless (string= blob
  2237. git-walktree-object-full-sha1)
  2238. ;; For running git command go back to repository root
  2239. (cd git-walktree-repository-root)
  2240. (let ((inhibit-read-only t))
  2241. (with-temp-buffer
  2242. (git-walktree--call-process nil
  2243. "cat-file"
  2244. "-p"
  2245. blob)
  2246. (git-walktree--replace-into-buffer buf)))
  2247. (setq git-walktree-buffer-file-name
  2248. (concat git-walktree-repository-root "/git@" committish ":" path))
  2249. (setq buffer-file-name
  2250. (concat git-walktree-repository-root "/" path))
  2251. (normal-mode t)
  2252. ;; For asking filename when C-xC-s
  2253. (setq buffer-file-name nil)
  2254. (set-buffer-modified-p t)
  2255. (setq git-walktree-current-committish committish)
  2256. (setq git-walktree-current-path path)
  2257. (setq git-walktree-object-full-sha1 blob)
  2258. (let ((dir (expand-file-name (or (file-name-directory path)
  2259. ".")
  2260. git-walktree-repository-root)))
  2261. (when (and git-walktree-try-cd
  2262. (file-directory-p dir))
  2263. (cd dir)))
  2264. (view-mode 1)
  2265. ))
  2266. buf))
  2267. (defun git-walktree--open-noselect-safe-path (committish &optional path)
  2268. "Open git object of COMMITTISH:PATH.
  2269. If PATH not found in COMMITTISH tree, go up path and try again until found.
  2270. When PATH is omitted or nil, it is calculated from current file or directory."
  2271. (cl-assert committish)
  2272. (let ((type (git-walktree--git-plumbing "cat-file"
  2273. "-t"
  2274. committish)))
  2275. (cl-assert (string= type "commit")))
  2276. (setq path
  2277. (or path
  2278. (git-walktree--path-in-repository (or buffer-file-name
  2279. default-directory))))
  2280. ;; PATH must not start with and end with slashes
  2281. (cl-assert (not (string-match-p "\\`/" path)))
  2282. (cl-assert (not (string-match-p "/\\'" path)))
  2283. (let ((obj (git-walktree--resolve-object committish path)))
  2284. (while (not obj)
  2285. (setq path
  2286. (git-walktree--parent-directory path))
  2287. (setq obj
  2288. (git-walktree--resolve-object committish path)))
  2289. (git-walktree--open-noselect committish
  2290. path
  2291. obj)))
  2292. ;; TODO: Store view history
  2293. (defun git-walktree--open-noselect (committish path object)
  2294. "Open buffer to view git object of COMMITTISH:PATH.
  2295. When PATH was given and non-nil open that, otherwise open root tree.
  2296. When OBJECT was given and non-nil, assume that is the object full sha1 of
  2297. COMMITTISH:PATH without checking it."
  2298. (cl-assert committish)
  2299. (let ((type (git-walktree--git-plumbing "cat-file"
  2300. "-t"
  2301. committish)))
  2302. (cl-assert (string= type "commit")))
  2303. (setq path (or path
  2304. "."))
  2305. ;; PATH must not start with and end with slashes
  2306. (cl-assert (not (string-match-p "\\`/" path)))
  2307. (cl-assert (not (string-match-p "/\\'" path)))
  2308. (setq object (or object
  2309. (git-walktree--resolve-object committish path)))
  2310. (setq object (git-walktree--git-plumbing "rev-parse"
  2311. object))
  2312. (cl-assert object)
  2313. (let ((type (git-walktree--git-plumbing "cat-file"
  2314. "-t"
  2315. object)))
  2316. (pcase type
  2317. ((or "commit" "tree")
  2318. (git-walktree--open-treeish committish path object))
  2319. ("blob"
  2320. (git-walktree--open-blob committish path object))
  2321. (_
  2322. (error "Type cannot handle: %s" type)))))
  2323. (defun git-walktree--resolve-object (committish path)
  2324. "Return object full sha1 name of COMMITISIH:PATH.
  2325. If path is equal to \".\" return COMMITTISH's root tree object.
  2326. PATH will be always treated as relative to repository root."
  2327. (cl-assert committish)
  2328. (cl-assert path)
  2329. (cl-assert (not (string-match-p "\\`/" path)))
  2330. (cl-assert (not (string-match-p "/\\'" path)))
  2331. (if (string= path ".")
  2332. (git-walktree--git-plumbing "show"
  2333. "--no-patch"
  2334. "--pretty=format:%T"
  2335. committish)
  2336. (let ((info (git-walktree--parse-lstree-line (git-walktree--git-plumbing "ls-tree"
  2337. "--full-tree"
  2338. committish
  2339. path))))
  2340. (plist-get info :object))))
  2341. (defun git-walktree-open (committish &optional path)
  2342. "Open git tree buffer of COMMITTISH.
  2343. When PATH was given and non-nil open that, otherwise try to open current path.
  2344. If target path is not found in COMMITISH tree, go up path and try again until found."
  2345. ;; TODO: Add fallback method for cases where magit is not available
  2346. (interactive (list (magit-read-branch-or-commit "Revision: ")))
  2347. (switch-to-buffer (git-walktree--open-noselect-safe-path committish path)))
  2348. (defalias 'git-walktree 'git-walktree-open)
  2349. (defun git-walktree--path-in-repository (path)
  2350. "Convert PATH into relative path to repository root.
  2351. Result will not have leading and trailing slashes."
  2352. (with-temp-buffer
  2353. (cd (if (file-directory-p path)
  2354. path
  2355. (file-name-directory path)))
  2356. (let ((root (git-walktree--git-plumbing "rev-parse"
  2357. "--show-toplevel")))
  2358. (file-relative-name (directory-file-name path)
  2359. root))))
  2360. (defcustom git-walktree-git-executable "git"
  2361. "Git executable."
  2362. :type 'string
  2363. :group 'git-walktree)
  2364. (defcustom git-walktree-try-cd t
  2365. "Try to cd if directory exists in current working directory if non-nil.
  2366. Otherwise use repository root for gitwalktree buffer's `default-directory'."
  2367. :type 'boolean
  2368. :group 'git-walktree)
  2369. (defcustom git-walktree-reuse-tree-buffer t
  2370. "Non-nil to reuse buffer for treeish object."
  2371. :type 'boolean
  2372. :group 'git-walktree)
  2373. (defvar git-walktree-tree-buffer-for-reuse nil
  2374. "Buffer to use when `git-walktree-reuse-tree-buffer' is non-nil.")
  2375. (defun git-walktree--git-plumbing (&rest args)
  2376. "Run git plubming command with ARGS.
  2377. Returns first line of output without newline."
  2378. (with-temp-buffer
  2379. (let ((status (apply 'call-process
  2380. git-walktree-git-executable
  2381. nil
  2382. t
  2383. nil
  2384. args)))
  2385. (unless (eq 0
  2386. status)
  2387. (error "Faild to run git %S:\n%s"
  2388. args
  2389. (buffer-substring-no-properties (point-min)
  2390. (point-max))))
  2391. (buffer-substring-no-properties (point-min)
  2392. (progn
  2393. (goto-char (point-min))
  2394. (point-at-eol))))))
  2395. (defconst git-walktree-ls-tree-line-regexp
  2396. "^\\([0-9]\\{6\\}\\) \\(\\w+\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2397. "Regexp for one line of output of git ls-tree.")
  2398. (defconst git-walktree-ls-tree-line-tree-regexp
  2399. "^\\([0-9]\\{6\\}\\) \\(tree\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2400. "Regexp for tree line of output of git ls-tree.")
  2401. (defconst git-walktree-ls-tree-line-commit-regexp
  2402. "^\\([0-9]\\{6\\}\\) \\(commit\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2403. "Regexp for commit line of output of git ls-tree.")
  2404. (defun git-walktree--parse-lstree-line (str)
  2405. "Extract object info from STR.
  2406. STR should be a string like following without newline.:
  2407. 100644 blob 6fd4d58202d0b46547c6fe43de0f8c878456f966 .editorconfig
  2408. Returns property list like (:mode MODE :type TYPE :object OBJECT :file FILE)."
  2409. (let (result mode type object file)
  2410. (save-match-data
  2411. (with-temp-buffer
  2412. (insert str)
  2413. (goto-char (point-min))
  2414. (and (re-search-forward git-walktree-ls-tree-line-regexp
  2415. nil
  2416. t)
  2417. (list :mode (match-string 1)
  2418. :type (match-string 2)
  2419. :object (match-string 3)
  2420. :file (match-string 4)))))))
  2421. (defun git-walktree-mode-open-this ()
  2422. "Open git object of current line."
  2423. (interactive)
  2424. (let ((info (git-walktree--parse-lstree-line (buffer-substring-no-properties (point-at-bol)
  2425. (point-at-eol)))))
  2426. (if info
  2427. (switch-to-buffer
  2428. (if (string= (plist-get info
  2429. :type)
  2430. "commit")
  2431. ;; For submodule cd to that directory and intialize
  2432. ;; TODO: Provide way to go back to known "parent" repository
  2433. (with-temp-buffer
  2434. (cd (plist-get info :file))
  2435. (git-walktree--open-noselect (plist-get info
  2436. :object)
  2437. nil
  2438. (plist-get info
  2439. :object)))
  2440. (git-walktree--open-noselect git-walktree-current-committish
  2441. (git-walktree--join-path (plist-get info
  2442. :file))
  2443. (plist-get info
  2444. :object))))
  2445. (message "No object on current line."))))
  2446. (defun git-walktree--join-path (name &optional base)
  2447. "Make path from NAME and BASE.
  2448. If base is omitted or nil use value of `git-walktree-current-path'."
  2449. (setq base (or base
  2450. git-walktree-current-path))
  2451. (cl-assert base)
  2452. (if (string= base ".")
  2453. name
  2454. (concat base "/" name)))
  2455. (defun git-walktree--parent-directory (path)
  2456. "Return parent directory of PATH without trailing slash.
  2457. For root directory return \".\".
  2458. If PATH is equal to \".\", return nil."
  2459. (if (string-match-p "/" path)
  2460. (directory-file-name (file-name-directory path))
  2461. (if (string= "." path)
  2462. nil
  2463. ".")))
  2464. (defun git-walktree-up (&optional committish path)
  2465. "Open parent directory of COMMITTISH and PATH.
  2466. If not given, value of current buffer will be used."
  2467. (interactive)
  2468. (setq committish
  2469. (or committish git-walktree-current-committish))
  2470. (setq path
  2471. (or path git-walktree-current-path))
  2472. (let ((parent (git-walktree--parent-directory path)))
  2473. (if parent
  2474. (switch-to-buffer (git-walktree--open-noselect committish
  2475. parent
  2476. nil))
  2477. (message "Cannot find parent directory for current tree."))))
  2478. (defun git-walktree-mode--move-to-file ()
  2479. "Move point to file field of ls-tree output in current line.
  2480. This function do nothing when current line is not ls-tree output."
  2481. (interactive)
  2482. (save-match-data
  2483. (when (save-excursion
  2484. (goto-char (point-at-bol))
  2485. (re-search-forward git-walktree-ls-tree-line-regexp
  2486. (point-at-eol) t))
  2487. (goto-char (match-beginning 4)))))
  2488. (defun git-walktree-mode-next-line (&optional arg try-vscroll)
  2489. "Move cursor vertically down ARG lines and move to file field if found."
  2490. (interactive "^p\np")
  2491. (or arg (setq arg 1))
  2492. (line-move arg nil nil try-vscroll)
  2493. (git-walktree-mode--move-to-file)
  2494. )
  2495. (defun git-walktree-mode-previous-line (&optional arg try-vscroll)
  2496. "Move cursor vertically up ARG lines and move to file field if found."
  2497. (interactive "^p\np")
  2498. (or arg (setq arg 1))
  2499. (line-move (- arg) nil nil try-vscroll)
  2500. (git-walktree-mode--move-to-file)
  2501. )
  2502. (defgroup git-walktree-faces nil
  2503. "Faces used by git-walktree."
  2504. :group 'git-walktree
  2505. :group 'faces)
  2506. (defface git-walktree-tree-face
  2507. ;; Same as dired-directory
  2508. '((t (:inherit font-lock-function-name-face)))
  2509. "Face used for tree objects."
  2510. :group 'git-walktree-faces)
  2511. (defface git-walktree-commit-face
  2512. ;; Same as dired-symlink face
  2513. '((t (:inherit font-lock-keyword-face)))
  2514. "Face used for commit objects."
  2515. :group 'git-walktree-faces)
  2516. (defvar git-walktree-known-child-revisions (make-hash-table :test 'equal)
  2517. "Hash of already known pair of commitid -> list of child commitid.
  2518. Both values should be object full sha1 names.")
  2519. (defun git-walktree--put-child (parent child)
  2520. "Register PARENT and CHILD relationship.
  2521. PARENT should be a full sha1 object name."
  2522. ;; Any way to check if PARENT is a full SHA-1 object name?
  2523. (let ((current (gethash parent git-walktree-known-child-revisions)))
  2524. (unless (member child current)
  2525. (puthash parent
  2526. (cons child
  2527. current)
  2528. git-walktree-known-child-revisions))))
  2529. ;; TODO: Add aggressive search mode
  2530. ;; https://stackoverflow.com/a/9870218
  2531. ;; git log --reverse --pretty=format:%H -n 1 --ancestry-path <PARENT>..HEAD
  2532. (defun git-walktree--get-children (parent)
  2533. "Get known children list of PARENT commit.
  2534. PARENT should be a full sha1 object name."
  2535. (gethash parent git-walktree-known-child-revisions))
  2536. (defun git-walktree--choose-committish (prompt-format collection)
  2537. "Emit PROMPT-FORMAT and ask user to which committish of COLLECTION to use.
  2538. When collection has just one element, return the first element without asking."
  2539. (cl-assert collection)
  2540. (if (< (length collection) 2)
  2541. (car collection)
  2542. (completing-read (format prompt-format
  2543. (mapconcat 'git-walktree--committish-fordisplay
  2544. collection
  2545. " "))
  2546. collection
  2547. nil
  2548. t)))
  2549. (defun git-walktree-parent-revision ()
  2550. "Open parent revision of current path.
  2551. If current path was not found in the parent revision try to go up path."
  2552. (interactive)
  2553. (cl-assert git-walktree-current-committish)
  2554. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2555. git-walktree-current-committish))
  2556. (parents (git-walktree--parent-full-sha1 commit-full-sha1)))
  2557. (dolist (parent parents)
  2558. (git-walktree--put-child parent
  2559. commit-full-sha1))
  2560. (if (< (length parents)
  2561. 1)
  2562. (message "This revision has no parent revision")
  2563. (let* ((parent (git-walktree--choose-committish "This revision has multiple parents. Which to open? (%s) "
  2564. parents))
  2565. (path git-walktree-current-path))
  2566. (cl-assert path)
  2567. (switch-to-buffer (git-walktree--open-noselect-safe-path parent
  2568. path))))))
  2569. (defun git-walktree--parent-full-sha1 (committish)
  2570. "Return list of parent commits of COMMITTISH in sha1 string."
  2571. (let ((type (git-walktree--git-plumbing "cat-file"
  2572. "-t"
  2573. committish)))
  2574. (cl-assert (string= type "commit")))
  2575. (let ((parents (git-walktree--git-plumbing "show"
  2576. "--no-patch"
  2577. "--pretty=format:%P"
  2578. committish)))
  2579. (split-string parents)))
  2580. (defun git-walktree-known-child-revision ()
  2581. "Open known revision of current path."
  2582. (interactive)
  2583. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2584. git-walktree-current-committish))
  2585. (children (git-walktree--get-children commit-full-sha1)))
  2586. (if (< (length children)
  2587. 1)
  2588. (message "There are no known child revision")
  2589. (let* ((child (git-walktree--choose-committish "There are multiple known childrens. Which to open? (%s)"
  2590. children))
  2591. (path git-walktree-current-path))
  2592. (cl-assert path)
  2593. (switch-to-buffer (git-walktree--open-noselect-safe-path child
  2594. path))))))
  2595. (defvar git-walktree-mode-map
  2596. (let ((map (make-sparse-keymap)))
  2597. (define-key map "n" 'git-walktree-mode-next-line)
  2598. (define-key map "p" 'git-walktree-mode-previous-line)
  2599. (define-key map (kbd "C-n") 'git-walktree-mode-next-line)
  2600. (define-key map (kbd "C-p") 'git-walktree-mode-previous-line)
  2601. ;; TODO: Review keybind
  2602. (define-key map "P" 'git-walktree-parent-revision)
  2603. (define-key map "N" 'git-walktree-known-child-revision)
  2604. (define-key map "^" 'git-walktree-up)
  2605. ;; TODO: implement
  2606. (define-key map (kbd "DEL") 'git-walktree-back)
  2607. (define-key map (kbd "C-m") 'git-walktree-mode-open-this)
  2608. map))
  2609. (defvar git-walktree-mode-font-lock-keywords
  2610. `(
  2611. (,git-walktree-ls-tree-line-regexp . (
  2612. (1 'shadow)
  2613. (3 'shadow)
  2614. ))
  2615. (,git-walktree-ls-tree-line-tree-regexp . (
  2616. (2 'git-walktree-tree-face)
  2617. (4 'git-walktree-tree-face)
  2618. ))
  2619. (,git-walktree-ls-tree-line-commit-regexp . (
  2620. (2 'git-walktree-commit-face)
  2621. (4 'git-walktree-commit-face)
  2622. ))
  2623. )
  2624. "Syntax highlighting for git-walktree mode.")
  2625. (define-derived-mode git-walktree-mode special-mode "GitWalktree"
  2626. "Major-mode for `git-walktree-open'."
  2627. (set (make-local-variable 'font-lock-defaults)
  2628. '(git-walktree-mode-font-lock-keywords
  2629. nil nil nil nil
  2630. ))
  2631. )
  2632. (require 'magit nil t)
  2633. ;; (git-revision--git-plumbing "cat-file" "-t" "HEAD")
  2634. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2635. ;; git-worktree
  2636. (defun git-worktree-get-current-list ()
  2637. "Get current worktree list."
  2638. (with-temp-buffer
  2639. (let ((trees nil)
  2640. (status (call-process "git"
  2641. nil
  2642. t
  2643. nil
  2644. "worktree" "list" "--porcelain")))
  2645. (cl-assert (eq status 0))
  2646. (goto-char (point-min))
  2647. (save-match-data
  2648. (while (not (eq (point) (point-max)))
  2649. (let ((worktree nil)
  2650. (head nil)
  2651. (branch nil))
  2652. (while (re-search-forward "^\\([^ ]+\\) \\(.*\\)$" (point-at-eol) t)
  2653. (pcase (match-string 1)
  2654. ("worktree" (setq worktree (match-string 2)))
  2655. ("HEAD" (setq head (match-string 2)))
  2656. ("branch" (setq branch (match-string 2))))
  2657. (forward-line 1)
  2658. (goto-char (point-at-bol)))
  2659. (setq trees `(,@trees
  2660. (:worktree ,worktree :head ,head :branch ,branch)))
  2661. (forward-line 1)
  2662. (goto-char (point-at-bol)))
  2663. ))
  2664. trees)))
  2665. ;; Local Variables:
  2666. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2667. ;; flycheck-checker: emacs-lisp
  2668. ;; End:
  2669. ;;; emancs.el ends here