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

2524 řádky
87 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Commentary:
  3. ;;; Code:
  4. ;; (and (file-readable-p "~/.dotfiles/emacs.el")
  5. ;; (load-file "~/.dotfiles/emacs.el"))
  6. ;; make directories
  7. (unless (file-directory-p (expand-file-name user-emacs-directory))
  8. (make-directory (expand-file-name user-emacs-directory)))
  9. (let ((d (expand-file-name (concat user-emacs-directory
  10. "lisp"))))
  11. (unless (file-directory-p d)
  12. (make-directory d))
  13. (add-to-list 'load-path d))
  14. (eval-when-compile
  15. (require 'cl nil t))
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17. ;; prepare init.el
  18. (defun prepare-init-el (elfile)
  19. "Prepare ~/.emacs/init.el to load ELFILE."
  20. (interactive "fFilename to set to be loaded: ")
  21. (with-temp-buffer
  22. (insert (format "(and (file-readable-p \"%s\")
  23. (load-file \"%s\"))"
  24. elfile
  25. elfile))
  26. (write-file (expand-file-name (concat user-emacs-directory
  27. "init.el")))))
  28. ;; (add-hook 'after-change-major-mode-hook
  29. ;; (lambda ()
  30. ;; (message "cmm: %S %s"
  31. ;; major-mode
  32. ;; buffer-file-name)))
  33. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  34. ;; download library from web
  35. (defun fetch-library (url &optional byte-compile-p force-download-p)
  36. "Download a library from URL and locate it in \"~/emacs.d/lisp/\".
  37. Return nil if library unfound and failed to download,
  38. otherwise the path where the library installed.
  39. If BYTE-COMPILE-P is t byte compile the file after downloading.
  40. If FORCE-DOWNLOAD-P it t ignore exisiting library and always download."
  41. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  42. (lib (file-name-sans-extension (file-name-nondirectory url)))
  43. (lpath (concat dir lib ".el"))
  44. (locate-p (locate-library lib)))
  45. (if (or force-download-p (not locate-p))
  46. (if (progn (message "Downloading %s..."
  47. url)
  48. (download-file url
  49. lpath
  50. t))
  51. (progn (message "Downloading %s...done"
  52. url)
  53. (when (and byte-compile-p
  54. (require 'bytecomp nil t))
  55. (and (file-exists-p (byte-compile-dest-file lpath))
  56. (delete-file (byte-compile-dest-file lpath)))
  57. (message "Byte-compiling %s..."
  58. lpath)
  59. (byte-compile-file lpath)
  60. (message "Byte-compiling %s...done"
  61. lpath)))
  62. (progn (and (file-writable-p lpath)
  63. (delete-file lpath))
  64. (message "Downloading %s...failed"
  65. url))))
  66. (locate-library lib)))
  67. (defun download-file (url path &optional ok-if-already-exists)
  68. "Download file from URL and output to PATH.
  69. IF OK-IF-ALREADY-EXISTS is true force download."
  70. (let ((curl (executable-find "curl"))
  71. (wget (executable-find "wget")))
  72. (cond (wget
  73. (if (and (not ok-if-already-exists)
  74. (file-exists-p path))
  75. nil
  76. (and (eq 0
  77. (call-process wget
  78. nil
  79. nil
  80. nil
  81. "-O"
  82. path
  83. url
  84. ))
  85. path)))
  86. (curl
  87. (if (and (not ok-if-already-exists)
  88. (file-exists-p path))
  89. nil
  90. (and (eq 0
  91. (call-process curl
  92. nil
  93. nil
  94. nil
  95. "--output"
  96. path
  97. "-L"
  98. url
  99. ))
  100. path)))
  101. (t
  102. (ignore-errors
  103. (require 'url)
  104. (url-copy-file url
  105. path
  106. ok-if-already-exists)
  107. path)))))
  108. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  109. ;; package
  110. (defvar my-package-list nil
  111. "Package list just for me.")
  112. (setq my-package-list
  113. '(
  114. markdown-mode
  115. yaml-mode
  116. ;; ack
  117. color-moccur
  118. gtags
  119. flycheck
  120. ;; is flymake installs are required?
  121. ;;flymake-jshint
  122. ;;flymake-python-pyflakes
  123. xclip
  124. gnuplot-mode
  125. erlang
  126. )
  127. )
  128. (when (require 'package nil t)
  129. (add-to-list 'package-archives
  130. '("ELPA" . "http://tromey.com/elpa/"))
  131. (add-to-list 'package-archives
  132. '("melpa" . "http://melpa.milkbox.net/packages/")
  133. t)
  134. (add-to-list 'package-archives
  135. '("marmalade" . "http://marmalade-repo.org/packages/"))
  136. (package-initialize)
  137. (defun my-auto-install-package ()
  138. "Install packages semi-automatically."
  139. (interactive)
  140. (package-refresh-contents)
  141. (mapc (lambda (pkg)
  142. (or (package-installed-p pkg)
  143. (locate-library (symbol-name pkg))
  144. (package-install pkg)))
  145. my-package-list))
  146. )
  147. ;; (lazy-load-eval 'sudoku)
  148. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  149. ;; autoload
  150. (defmacro lazy-load-eval (feature &optional functions &rest body)
  151. "Define autoloading FEATURE that defines FUNCTIONS.
  152. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  153. the function same as FEATURE is defined as autoloaded function. BODY is passed
  154. to `eval-after-load'.
  155. When this macro is evaluated, this returns the path to library if FEATURE
  156. found, otherwise returns nil."
  157. (let* ((libname (symbol-name (eval feature)))
  158. (libpath (locate-library libname)))
  159. (and libpath
  160. `(progn
  161. ,@(mapcar (lambda (f)
  162. (unless (fboundp f)
  163. `(progn
  164. (message "Autoloaded function `%S' defined (%s)"
  165. (quote ,f)
  166. ,libpath)
  167. (autoload (quote ,f)
  168. ,libname
  169. ,(concat "Autoloaded function defined in \""
  170. libpath
  171. "\".")
  172. t))))
  173. (or (eval functions)
  174. `(,(eval feature))))
  175. (eval-after-load ,feature
  176. (quote (progn
  177. ,@body)))
  178. (locate-library ,libname)))))
  179. (put 'lazy-load-eval 'lisp-indent-function 2)
  180. (when (lazy-load-eval 'tetris nil
  181. (message "Tetris loaded!"))
  182. (message "Tetris found!"))
  183. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  184. ;; my-idle-hook
  185. (defvar my-idle-hook nil
  186. "Hook run when idle for several secs.")
  187. (defvar my-idle-hook-sec 5
  188. "Second to run `my-idle-hook'.")
  189. (run-with-idle-timer my-idle-hook-sec
  190. t
  191. (lambda ()
  192. (run-hooks 'my-idle-hook)))
  193. ;; (add-hook 'my-idle-hook
  194. ;; (lambda ()
  195. ;; (message "idle hook message")))
  196. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  197. ;; start and quit
  198. (setq inhibit-startup-message t)
  199. (setq confirm-kill-emacs 'y-or-n-p)
  200. (setq gc-cons-threshold (* 1024 1024 4))
  201. (when window-system
  202. (add-to-list 'default-frame-alist '(cursor-type . box))
  203. (add-to-list 'default-frame-alist '(background-color . "white"))
  204. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  205. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  206. ;; does not work?
  207. )
  208. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  209. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  210. (and (fboundp 'tool-bar-mode)
  211. (tool-bar-mode 0))
  212. (and (fboundp 'set-scroll-bar-mode)
  213. (set-scroll-bar-mode nil))
  214. (add-hook 'kill-emacs-hook
  215. ;; load init file when terminating emacs to ensure file is not broken
  216. 'reload-init-file)
  217. (add-hook 'after-init-hook
  218. (lambda ()
  219. (message "%s %s" invocation-name emacs-version)
  220. (message "%s was taken to initialize emacs." (emacs-init-time))
  221. (switch-to-buffer "*Messages*")
  222. ))
  223. (cd ".") ; when using windows use / instead of \ in `default-directory'
  224. ;; locale
  225. (set-language-environment "Japanese")
  226. (set-default-coding-systems 'utf-8-unix)
  227. (prefer-coding-system 'utf-8-unix)
  228. (setq system-time-locale "C")
  229. ;; my prefix map
  230. (defvar my-prefix-map nil
  231. "My prefix map.")
  232. (define-prefix-command 'my-prefix-map)
  233. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  234. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  235. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  236. ;; (comint-show-maximum-output)
  237. ;; kill scratch
  238. (add-hook 'after-init-hook
  239. (lambda ()
  240. (kill-buffer "*scratch*")))
  241. ;; modifier keys
  242. ;; (setq mac-option-modifier 'control)
  243. ;; display
  244. (setq redisplay-dont-pause t)
  245. (setq visible-bell t)
  246. (setq ring-bell-function 'ignore)
  247. (mouse-avoidance-mode 'banish)
  248. (and window-system
  249. (fetch-library
  250. "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el"
  251. t)
  252. (require 'save-window-size nil t))
  253. (defun reload-init-file ()
  254. "Reload Emacs init file."
  255. (interactive)
  256. (when (file-readable-p user-init-file)
  257. (load-file user-init-file)))
  258. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  259. ;; for windows
  260. (defun start-ckw-bash ()
  261. "Start ckw in windows."
  262. (interactive)
  263. (start-process
  264. "ckw_bash"
  265. nil
  266. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe"))
  267. ;; command seems to have to be in c drive
  268. (defun my-w32-add-export-path (&rest args)
  269. "Add pathes ARGS for windows."
  270. (mapc (lambda (path)
  271. (add-to-list 'exec-path (expand-file-name path)))
  272. (reverse args))
  273. (setenv "PATH"
  274. (mapconcat 'convert-standard-filename
  275. exec-path
  276. ";")))
  277. (when (eq system-type 'windows-nt)
  278. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  279. ;; (setq python-python-command "c:/Python26/python.exe")
  280. ;; (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  281. (my-w32-add-export-path "c:/Windows/system"
  282. "c:/Windows/System32"
  283. "c:/Program Files/Git/bin"
  284. "c:/MinGW/bin"
  285. "c:/MinGW/mingw32/bin"
  286. (expand-file-name "~/.local/bin")
  287. (expand-file-name "~/dbx/apps/bin"))
  288. (when window-system
  289. (setq w32-enable-synthesized-fonts t))
  290. (setq w32-apps-modifier 'meta)
  291. (setq file-name-coding-system 'sjis))
  292. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  293. ;; global keys
  294. (global-set-key (kbd "<up>") 'scroll-down-line)
  295. (global-set-key (kbd "<down>") 'scroll-up-line)
  296. (global-set-key (kbd "<left>") 'scroll-down)
  297. (global-set-key (kbd "<right>") 'scroll-up)
  298. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  299. (global-set-key (kbd "C-\\") help-map)
  300. (define-key ctl-x-map (kbd "DEL") help-map)
  301. (define-key ctl-x-map (kbd "C-h") help-map)
  302. (define-key help-map "a" 'apropos)
  303. ;; disable annoying keys
  304. (global-set-key [prior] 'ignore)
  305. (global-set-key (kbd "<next>") 'ignore)
  306. (global-set-key [menu] 'ignore)
  307. (global-set-key [down-mouse-1] 'ignore)
  308. (global-set-key [down-mouse-2] 'ignore)
  309. (global-set-key [down-mouse-3] 'ignore)
  310. (global-set-key [mouse-1] 'ignore)
  311. (global-set-key [mouse-2] 'ignore)
  312. (global-set-key [mouse-3] 'ignore)
  313. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  314. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  315. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  316. ;; title and mode-line
  317. (when (fetch-library
  318. "https://raw.github.com/10sr/emacs-lisp/master/terminal-title.el"
  319. t)
  320. ;; if TERM is not screen use default value
  321. (if (getenv "TMUX")
  322. ;; if use tmux locally just basename of current dir
  323. (setq terminal-title-format
  324. '((file-name-nondirectory (directory-file-name
  325. default-directory))))
  326. (if (and (equal (car (split-string (frame-parameter nil
  327. 'tty-type)
  328. "-"))
  329. "screen")
  330. (not (getenv "SSH_CONNECTION")))
  331. (setq terminal-title-format
  332. '((file-name-nondirectory (directory-file-name
  333. default-directory))))
  334. ;; seems that TMUX is used by locally and ssh to remote host
  335. (setq terminal-title-format
  336. `("em:"
  337. ,user-login-name
  338. "@"
  339. ,(car (split-string system-name
  340. "\\."))
  341. ":"
  342. default-directory))
  343. ))
  344. ;; this wont happen? (TMUX is not set, TERM is screen, not ssh-ed)
  345. (and (require 'terminal-title nil t)
  346. (terminal-title-mode)))
  347. (setq eol-mnemonic-dos "\\r\\n")
  348. (setq eol-mnemonic-mac "\\r")
  349. (setq eol-mnemonic-unix "\\n")
  350. (which-function-mode 0)
  351. (line-number-mode 0)
  352. (column-number-mode 0)
  353. (size-indication-mode 0)
  354. (setq mode-line-position
  355. '(:eval (format "L%%l/%d,C%%c"
  356. (count-lines (point-max)
  357. (point-min)))))
  358. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  359. ;; display date
  360. (add-hook 'after-init-hook
  361. (lambda ()
  362. (when display-time-mode
  363. (display-time-update))
  364. ))
  365. (when (require 'time nil t)
  366. (setq display-time-interval 29)
  367. (setq display-time-day-and-date t)
  368. (setq display-time-format "%a, %d %b %Y %T")
  369. (if window-system
  370. (display-time-mode 0)
  371. (display-time-mode 1)))
  372. ;; ;; current directory
  373. ;; (let ((ls (member 'mode-line-buffer-identification
  374. ;; mode-line-format)))
  375. ;; (setcdr ls
  376. ;; (cons '(:eval (concat " ("
  377. ;; (abbreviate-file-name default-directory)
  378. ;; ")"))
  379. ;; (cdr ls))))
  380. ;; ;; display last modified time
  381. ;; (let ((ls (member 'mode-line-buffer-identification
  382. ;; mode-line-format)))
  383. ;; (setcdr ls
  384. ;; (cons '(:eval (concat " "
  385. ;; my-buffer-file-last-modified-time))
  386. ;; (cdr ls))))
  387. (defun buffer-list-not-start-with-space ()
  388. "Return a list of buffers that not start with whitespaces."
  389. (let ((bl (buffer-list))
  390. b nbl)
  391. (while bl
  392. (setq b (pop bl))
  393. (unless (string-equal " "
  394. (substring (buffer-name b)
  395. 0
  396. 1))
  397. (add-to-list 'nbl b)))
  398. nbl))
  399. ;; http://www.masteringemacs.org/articles/2012/09/10/hiding-replacing-modeline-strings/
  400. ;; (add-to-list 'minor-mode-alist
  401. ;; '(global-whitespace-mode ""))
  402. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  403. ;; system info
  404. (defun my-message-current-info ()
  405. "Echo current login name, hostname and directory."
  406. (interactive)
  407. (message "%s@%s:%s"
  408. user-login-name
  409. system-name
  410. (abbreviate-file-name default-directory)))
  411. ;; (run-with-idle-timer 3
  412. ;; t
  413. ;; 'my-message-current-info)
  414. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  415. ;; minibuffer
  416. (setq insert-default-directory t)
  417. (setq completion-ignore-case t
  418. read-file-name-completion-ignore-case t
  419. read-buffer-completion-ignore-case t)
  420. (setq resize-mini-windows t)
  421. (temp-buffer-resize-mode 1)
  422. (savehist-mode 1)
  423. (fset 'yes-or-no-p 'y-or-n-p)
  424. ;; complete symbol when `eval'
  425. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol)
  426. (define-key minibuffer-local-map (kbd "C-u")
  427. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  428. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  429. ;; letters, font-lock mode and fonts
  430. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  431. ;; (set-window-margins (selected-window) 1 1)
  432. (and (or (eq system-type 'Darwin)
  433. (eq system-type 'darwin))
  434. (fboundp 'mac-set-input-method-parameter)
  435. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  436. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  437. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  438. (boundp 'input-method-inactivate-hook))
  439. (add-hook 'input-method-activate-hook
  440. (lambda () (set-cursor-color "red")))
  441. (add-hook 'input-method-inactivate-hook
  442. (lambda () (set-cursor-color "black"))))
  443. (when (require 'paren nil t)
  444. (show-paren-mode 1)
  445. (setq show-paren-delay 0.5
  446. show-paren-style 'parenthesis) ; mixed is hard to read
  447. ;; (set-face-background 'show-paren-match
  448. ;; "black")
  449. ;; ;; (face-foreground 'default))
  450. ;; (set-face-foreground 'show-paren-match
  451. ;; "white")
  452. ;; (set-face-inverse-video-p 'show-paren-match
  453. ;; t)
  454. )
  455. (transient-mark-mode 1)
  456. (global-font-lock-mode 1)
  457. (setq font-lock-global-modes
  458. '(not
  459. help-mode
  460. eshell-mode
  461. term-mode
  462. Man-mode))
  463. ;; (standard-display-ascii ?\n "$\n")
  464. (defvar my-eol-face
  465. '(("\n" . (0 font-lock-comment-face t nil)))
  466. )
  467. (defvar my-tab-face
  468. '(("\t" . '(0 highlight t nil))))
  469. (defvar my-jspace-face
  470. '(("\u3000" . '(0 highlight t nil))))
  471. (add-hook 'font-lock-mode-hook
  472. (lambda ()
  473. ;; (font-lock-add-keywords nil my-eol-face)
  474. (font-lock-add-keywords nil my-jspace-face)
  475. ))
  476. (when (require 'whitespace nil t)
  477. (add-to-list 'whitespace-display-mappings ; not work
  478. `(tab-mark ?\t ,(vconcat "^I\t")))
  479. (add-to-list 'whitespace-display-mappings
  480. `(newline-mark ?\n ,(vconcat "$\n")))
  481. (setq whitespace-style '(face
  482. trailing ; trailing blanks
  483. newline ; newlines
  484. newline-mark ; use display table for newline
  485. ;; tab-mark
  486. empty ; empty lines at beg or end of buffer
  487. lines-tail ; lines over 80
  488. ))
  489. ;; (setq whitespace-newline 'font-lock-comment-face)
  490. (global-whitespace-mode t)
  491. (if (eq (display-color-cells)
  492. 256)
  493. (set-face-foreground 'whitespace-newline "brightblack")
  494. ;; (progn
  495. ;; (set-face-bold-p 'whitespace-newline
  496. ;; t))
  497. ))
  498. (and nil
  499. (fetch-library
  500. "http://www.emacswiki.org/emacs/download/fill-column-indicator.el"
  501. t)
  502. (require 'fill-column-indicator nil t)
  503. (setq fill-column-indicator))
  504. ;; highlight current line
  505. ;; http://wiki.riywo.com/index.php?Meadow
  506. (defface my-hl-line
  507. '((((min-colors 256)
  508. (background dark))
  509. (:background "color-234"))
  510. (((min-colors 256)
  511. (background light))
  512. (:background "color-234"))
  513. (t
  514. (:underline "black")))
  515. "*Face used by hl-line.")
  516. (setq hl-line-face 'my-hl-line) ;; (setq hl-line-face nil)
  517. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  518. (setq hl-line-global-modes
  519. '(not
  520. term-mode))
  521. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  522. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  523. ;; fonts
  524. (defun my-set-ascii-and-jp-font (list)
  525. "Set font configuration List."
  526. (let ((fspec1 (if (> emacs-major-version 22)
  527. ;; font spec is available in emacs23 and later
  528. (font-spec :family (nth 2 list) :size (nth 3 list))
  529. (cons (nth 2 list) "jisx0208.*")))
  530. (fspec2 (if (> emacs-major-version 22)
  531. (font-spec :family (nth 2 list) :size (nth 3 list))
  532. (cons (nth 2 list) "jisx0201.*"))))
  533. (set-face-attribute 'default nil
  534. :family (nth 0 list)
  535. :height (nth 1 list))
  536. (set-fontset-font "fontset-default"
  537. 'japanese-jisx0208
  538. fspec1)
  539. (set-fontset-font "fontset-default"
  540. 'katakana-jisx0201
  541. fspec2)))
  542. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 90 "takaogothic" 13))
  543. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "takaogothic" 14))
  544. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "ms gothic" 14))
  545. ;; (my-set-ascii-and-jp-font '("monaco" 75 "takaogothic" 11))
  546. ;; (my-set-ascii-and-jp-font '("monaco" 90 "takaogothic" 13))
  547. ;; (my-set-ascii-and-jp-font '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  548. ;; あ a
  549. (and (fetch-library
  550. "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  551. t)
  552. (progn
  553. (require 'set-modeline-color nil t)))
  554. (let ((fg (face-foreground 'default))
  555. (bg (face-background 'default)))
  556. (set-face-background 'mode-line-inactive
  557. (if (face-inverse-video-p 'mode-line) fg bg))
  558. (set-face-foreground 'mode-line-inactive
  559. (if (face-inverse-video-p 'mode-line) bg fg)))
  560. (set-face-underline 'mode-line-inactive
  561. t)
  562. (set-face-underline 'vertical-border
  563. nil)
  564. (and (fetch-library
  565. "https://raw.github.com/tarao/elisp/master/end-mark.el"
  566. t)
  567. (require 'end-mark nil t)
  568. (global-end-mark-mode))
  569. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  570. ;; file handling
  571. (setq revert-without-query '(".+"))
  572. ;; save cursor position
  573. (setq save-place-file (concat user-emacs-directory
  574. "places"))
  575. (when (require 'saveplace nil t)
  576. (setq-default save-place t))
  577. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  578. (setq make-backup-files t)
  579. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  580. (setq backup-directory-alist
  581. (cons (cons "\\.*$" (expand-file-name "~/.emacs.d/backup"))
  582. backup-directory-alist))
  583. (setq version-control 'never)
  584. (setq delete-old-versions t)
  585. (setq auto-save-list-file-prefix (expand-file-name "~/.emacs.d/auto-save/"))
  586. (setq delete-auto-save-files t)
  587. (add-to-list 'completion-ignored-extensions ".bak")
  588. ;; (setq delete-by-moving-to-trash t
  589. ;; trash-directory "~/.emacs.d/trash")
  590. (add-hook 'after-save-hook
  591. 'executable-make-buffer-file-executable-if-script-p)
  592. (setq bookmark-default-file "~/.emacs.d/bmk")
  593. (add-hook 'recentf-load-hook
  594. (lambda ()
  595. (add-to-list 'recentf-exclude
  596. (regexp-quote bookmark-default-file))))
  597. (and (fetch-library
  598. "https://raw.github.com/10sr/emacs-lisp/master/read-only-only-mode.el"
  599. t)
  600. (lazy-load-eval 'read-only-only-mode))
  601. (and (fetch-library
  602. "https://raw.github.com/10sr/emacs-lisp/master/smart-revert.el"
  603. t)
  604. (require 'smart-revert nil t)
  605. (smart-revert-on))
  606. ;; autosave
  607. (and (fetch-library
  608. "https://raw.github.com/10sr/emacs-lisp/master/autosave.el"
  609. t)
  610. (require 'autosave nil t)
  611. (autosave-set 2))
  612. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  613. ;; editting
  614. (defun my-copy-whole-line ()
  615. "Copy whole line."
  616. (interactive)
  617. (kill-new (concat (buffer-substring (point-at-bol)
  618. (point-at-eol))
  619. "\n")))
  620. (setq require-final-newline t)
  621. (setq kill-whole-line t)
  622. (setq scroll-conservatively 35
  623. scroll-margin 2
  624. scroll-step 0)
  625. (setq-default major-mode 'text-mode)
  626. (setq next-line-add-newlines nil)
  627. (setq kill-read-only-ok t)
  628. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  629. ;; (setq-default line-spacing 0.2)
  630. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  631. (setq-default tab-width 4)
  632. (setq-default indent-tabs-mode nil)
  633. (setq-default indent-line-function nil)
  634. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  635. (delete-selection-mode 1)
  636. (cua-mode 0)
  637. (setq line-move-visual nil)
  638. ;; key bindings
  639. ;; moving around
  640. ;; (global-set-key (kbd "M-j") 'next-line)
  641. ;; (global-set-key (kbd "M-k") 'previous-line)
  642. ;; (global-set-key (kbd "M-h") 'backward-char)
  643. ;; (global-set-key (kbd "M-l") 'forward-char)
  644. ;;(keyboard-translate ?\M-j ?\C-j)
  645. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  646. (define-key esc-map "p" 'backward-paragraph)
  647. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  648. (define-key esc-map "n" 'forward-paragraph)
  649. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  650. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  651. (global-set-key (kbd "C-<left>") 'scroll-down)
  652. (global-set-key (kbd "C-<right>") 'scroll-up)
  653. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  654. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  655. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  656. ;; C-h and DEL
  657. (global-set-key (kbd "C-h") (kbd "DEL"))
  658. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  659. (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  660. (define-key esc-map "k" 'my-copy-whole-line)
  661. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  662. (define-key esc-map "u" 'undo)
  663. (define-key esc-map "i" (kbd "ESC TAB"))
  664. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  665. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  666. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  667. (define-key my-prefix-map (kbd "C-o") 'occur)
  668. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  669. ;; japanese input method
  670. (defun my-load-scim ()
  671. "Use scim-bridge.el as japanese im."
  672. ;; Load scim-bridge.
  673. (when (require 'scim-bridge nil t)
  674. ;; Turn on scim-mode automatically after loading .emacs
  675. (add-hook 'after-init-hook 'scim-mode-on)
  676. (setq scim-cursor-color "red")
  677. (scim-define-preedit-key ?\^h t)
  678. (scim-define-common-key ?\* nil)
  679. (scim-define-common-key ?\^/ nil)))
  680. (defun my-load-anthy ()
  681. "Use anthy.el as japanese im."
  682. ;; anthy
  683. (when (require 'anthy nil t)
  684. (global-set-key
  685. (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  686. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  687. (when (>= emacs-major-version 23)
  688. (setq anthy-accept-timeout 1))))
  689. ;; quail
  690. ;; aproposs input-method for some information
  691. ;; (setq default-input-method "japanese")
  692. (defun my-load-mozc-el ()
  693. "Use mozc.el as japanese im."
  694. (setq mozc-leim-title "[MZ]")
  695. (when (require 'mozc nil t)
  696. (setq defauit-input-method "japanese-mozc")
  697. ))
  698. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  699. ;; gmail
  700. (setq mail-interactive t
  701. send-mail-function 'smtpmail-send-it
  702. ;; message-send-mail-function 'smtpmail-send-it
  703. smtpmail-smtp-server "smtp.gmail.com"
  704. smtpmail-smtp-service 587
  705. smtpmail-starttls-credentials '(("smtp.gmail.com" 587
  706. "8.slashes@gmail.com" nil))
  707. smtpmail-auth-credentials '(("smtp.gmail.com" 587
  708. "8.slashes@gmail.com" nil))
  709. user-mail-address "8.slashes@gmail.com")
  710. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  711. ;; buffer killing
  712. ;; (defun my-delete-window-killing-buffer () nil)
  713. (defun my-query-kill-current-buffer ()
  714. "Interactively kill current buffer."
  715. (interactive)
  716. (if (y-or-n-p (concat "kill current buffer? :"))
  717. (kill-buffer (current-buffer))))
  718. (substitute-key-definition 'kill-buffer
  719. 'my-query-kill-current-buffer
  720. global-map)
  721. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  722. (defun my-kill-buffers ()
  723. "Kill buffers that visit files."
  724. (interactive)
  725. (mapcar (lambda (buf)
  726. (when (buffer-file-name buf)
  727. (kill-buffer buf)))
  728. (buffer-list)))
  729. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  730. ;; share clipboard with x
  731. ;; this page describes this in details, but only these sexps seem to be needed
  732. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  733. (and (not window-system)
  734. (not (eq window-system 'mac))
  735. (getenv "DISPLAY")
  736. (not (equal (getenv "DISPLAY") ""))
  737. (executable-find "xclip")
  738. ;; (< emacs-major-version 24)
  739. (fetch-library "http://www.emacswiki.org/emacs/download/xclip.el" t)
  740. (require 'xclip nil t)
  741. (turn-on-xclip))
  742. (and (eq system-type 'darwin)
  743. (fetch-library
  744. "https://raw.github.com/10sr/emacs-lisp/master/pasteboard.el"
  745. t)
  746. (require 'pasteboard nil t)
  747. (turn-on-pasteboard)
  748. (getenv "TMUX")
  749. (pasteboard-enable-rtun))
  750. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  751. ;; https://github.com/lunaryorn/flycheck
  752. (when (require 'flycheck nil t)
  753. (add-hook 'after-init-hook 'global-flycheck-mode))
  754. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  755. ;; window
  756. (and (fetch-library
  757. "https://raw.github.com/10sr/emacs-lisp/master/window-organizer.el"
  758. t)
  759. (lazy-load-eval 'window-organizer)
  760. (define-key ctl-x-map (kbd "w") 'window-organizer))
  761. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  762. ;; some modes and hooks
  763. (and (fetch-library
  764. "https://raw.github.com/10sr/emacs-lisp/master/remember-major-modes-mode.el"
  765. t)
  766. (require 'remember-major-modes-mode nil t)
  767. (remember-major-modes-mode 1)
  768. )
  769. ;; Detect file type from shebang and set major-mode.
  770. (add-to-list 'interpreter-mode-alist
  771. '("python3" . python-mode))
  772. (add-to-list 'interpreter-mode-alist
  773. '("python2" . python-mode))
  774. ;; http://fukuyama.co/foreign-regexp
  775. '(and (fetch-library
  776. "https://raw.github.com/k-talo/foreign-regexp.el/master/foreign-regexp.el"
  777. t)
  778. (require 'foreign-regexp nil t)
  779. (progn
  780. (setq foreign-regexp/regexp-type 'perl)
  781. '(setq reb-re-syntax 'foreign-regexp)
  782. ))
  783. (require 'session nil t)
  784. (lazy-load-eval 'sql '(sql-mode)
  785. (require 'sql-indent nil t))
  786. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  787. t)
  788. (lazy-load-eval 'gtkbm)
  789. (global-set-key (kbd "C-x C-d") 'gtkbm))
  790. (and (fetch-library
  791. "https://raw.github.com/10sr/emacs-lisp/master/git-command.el"
  792. t)
  793. (lazy-load-eval 'git-command
  794. nil
  795. (add-to-list 'git-command-major-mode-alist
  796. '("di" . diff-mode))
  797. (add-to-list 'git-command-major-mode-alist
  798. '("graph" . fundamental-mode))
  799. (add-to-list 'git-command-major-mode-alist
  800. '("log" . fundamental-mode))
  801. (or git-command-prompt-file
  802. (setq git-command-prompt-file
  803. (git-command-find-git-ps1
  804. "/usr/share/git-core/contrib/completion/git-prompt.sh"))))
  805. ;; (setq git-command-default-options "-c color.ui=always")
  806. (define-key ctl-x-map "g" 'git-command))
  807. (and (fetch-library
  808. "http://www.emacswiki.org/emacs/download/sl.el"
  809. t)
  810. (lazy-load-eval 'sl))
  811. (defalias 'qcalc 'quick-calc)
  812. (require 'simple nil t)
  813. (add-hook 'makefile-mode-hook
  814. (lambda ()
  815. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  816. ;; this functions is set in write-file-functions, i cannot find any
  817. ;; good way to remove this.
  818. (fset 'makefile-warn-suspicious-lines 'ignore)
  819. ))
  820. (add-hook 'verilog-mode-hook
  821. (lambda ()
  822. (define-key verilog-mode-map ";" 'self-insert-command)))
  823. (setq diff-switches "-u")
  824. (add-hook 'diff-mode-hook
  825. (lambda ()
  826. (when (and (eq major-mode
  827. 'diff-mode)
  828. (not buffer-file-name))
  829. ;; do not pass when major-mode is derived mode of diff-mode
  830. (view-mode 1))
  831. (set-face-attribute 'diff-header nil
  832. :foreground nil
  833. :background nil
  834. :weight 'bold)
  835. (set-face-attribute 'diff-file-header nil
  836. :foreground nil
  837. :background nil
  838. :weight 'bold)
  839. (set-face-foreground 'diff-index-face "blue")
  840. (set-face-attribute 'diff-hunk-header nil
  841. :foreground "cyan"
  842. :weight 'normal)
  843. (set-face-attribute 'diff-context nil
  844. ;; :foreground "white"
  845. :foreground nil
  846. :weight 'normal)
  847. (set-face-foreground 'diff-removed-face "red")
  848. (set-face-foreground 'diff-added-face "green")
  849. (set-face-background 'diff-removed-face nil)
  850. (set-face-background 'diff-added-face nil)
  851. (set-face-attribute 'diff-changed nil
  852. :foreground "magenta"
  853. :weight 'normal)
  854. ))
  855. ;; (ffap-bindings)
  856. (add-hook 'sh-mode-hook
  857. (lambda ()
  858. (define-key sh-mode-map
  859. (kbd "C-x C-e")
  860. 'my-execute-shell-command-current-line)))
  861. (setq sh-here-document-word "__EOC__")
  862. (defun my-execute-shell-command-current-line ()
  863. "Run current line as shell command."
  864. (interactive)
  865. (shell-command (buffer-substring-no-properties (point-at-bol)
  866. (point))))
  867. (setq auto-mode-alist
  868. `(("autostart\\'" . sh-mode)
  869. ("xinitrc\\'" . sh-mode)
  870. ("xprograms\\'" . sh-mode)
  871. ("PKGBUILD\\'" . sh-mode)
  872. ,@auto-mode-alist))
  873. (and (lazy-load-eval 'pkgbuild-mode)
  874. (setq auto-mode-alist (append '(("PKGBUILD\\'" . pkgbuild-mode))
  875. auto-mode-alist)))
  876. (add-hook 'yaml-mode-hook
  877. (lambda ()
  878. (define-key yaml-mode-map (kbd "C-m")
  879. 'newline)))
  880. (add-hook 'html-mode-hook
  881. (lambda ()
  882. (define-key html-mode-map (kbd "C-m")
  883. 'reindent-then-newline-and-indent)))
  884. (add-hook 'text-mode-hook
  885. (lambda ()
  886. (define-key text-mode-map (kbd "C-m") 'newline)))
  887. (add-to-list 'Info-default-directory-list
  888. (expand-file-name "~/.info/emacs-ja"))
  889. (add-hook 'apropos-mode-hook
  890. (lambda ()
  891. (define-key apropos-mode-map "n" 'next-line)
  892. (define-key apropos-mode-map "p" 'previous-line)
  893. ))
  894. (add-hook 'isearch-mode-hook
  895. (lambda ()
  896. ;; (define-key isearch-mode-map
  897. ;; (kbd "C-j") 'isearch-other-control-char)
  898. ;; (define-key isearch-mode-map
  899. ;; (kbd "C-k") 'isearch-other-control-char)
  900. ;; (define-key isearch-mode-map
  901. ;; (kbd "C-h") 'isearch-other-control-char)
  902. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  903. (define-key isearch-mode-map (kbd "M-r")
  904. 'isearch-query-replace-regexp)))
  905. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  906. (setq lazy-highlight-cleanup nil)
  907. ;; face for isearch highlighing
  908. (set-face-attribute 'lazy-highlight
  909. nil
  910. :foreground `unspecified
  911. :background `unspecified
  912. :underline t
  913. ;; :weight `bold
  914. )
  915. (add-hook 'outline-mode-hook
  916. (lambda ()
  917. (if (string-match "\\.md\\'" buffer-file-name)
  918. (set (make-local-variable 'outline-regexp) "#+ "))))
  919. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  920. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  921. (when (fetch-library
  922. "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  923. t)
  924. (lazy-load-eval 'markdown-mode)
  925. (setq markdown-command (or (executable-find "markdown")
  926. (executable-find "markdown.pl")))
  927. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'markdown-mode))
  928. (add-hook 'markdown-mode-hook
  929. (lambda ()
  930. (outline-minor-mode 1)
  931. (flyspell-mode)
  932. (set (make-local-variable 'comment-start) ";"))))
  933. ;; c-mode
  934. ;; http://www.emacswiki.org/emacs/IndentingC
  935. ;; http://en.wikipedia.org/wiki/Indent_style
  936. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  937. (when (lazy-load-eval 'cc-vars
  938. nil
  939. (add-to-list 'c-default-style
  940. '(c-mode . "k&r"))
  941. (add-to-list 'c-default-style
  942. '(c++-mode . "k&r"))
  943. (add-hook 'c-mode-common-hook
  944. (lambda ()
  945. ;; why c-basic-offset in k&r style defaults to 5 ???
  946. (setq c-basic-offset 4
  947. indent-tabs-mode nil)
  948. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  949. (c-toggle-hungry-state -1)
  950. ;; (and (require 'gtags nil t)
  951. ;; (gtags-mode 1))
  952. ))))
  953. (when (fetch-library
  954. "https://raw.github.com/mooz/js2-mode/master/js2-mode.el"
  955. t)
  956. (lazy-load-eval 'js2-mode)
  957. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  958. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  959. (add-hook 'js2-mode-hook
  960. (lambda ()
  961. (define-key js2-mode-map (kbd "C-m") (lambda ()
  962. (interactive)
  963. (js2-enter-key)
  964. (indent-for-tab-command)))
  965. ;; (add-hook (kill-local-variable 'before-save-hook)
  966. ;; 'js2-before-save)
  967. ;; (add-hook 'before-save-hook
  968. ;; 'my-indent-buffer
  969. ;; nil
  970. ;; t)
  971. )))
  972. (add-to-list 'interpreter-mode-alist
  973. '("node" . js-mode))
  974. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  975. (when (lazy-load-eval 'flymake-jslint
  976. '(flymake-jslint-load))
  977. (lazy-load-eval 'js nil
  978. (add-hook 'js-mode-hook
  979. 'flymake-jslint-load)))
  980. (require 'js-doc nil t)
  981. (when (require 'uniquify nil t)
  982. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  983. (setq uniquify-ignore-buffers-re "*[^*]+*")
  984. (setq uniquify-min-dir-content 1))
  985. (add-hook 'view-mode-hook
  986. (lambda()
  987. (define-key view-mode-map "j" 'scroll-up-line)
  988. (define-key view-mode-map "k" 'scroll-down-line)
  989. (define-key view-mode-map "v" 'toggle-read-only)
  990. (define-key view-mode-map "q" 'bury-buffer)
  991. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  992. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  993. ;; (define-key view-mode-map
  994. ;; "n" 'nonincremental-repeat-search-forward)
  995. ;; (define-key view-mode-map
  996. ;; "N" 'nonincremental-repeat-search-backward)
  997. (define-key view-mode-map "/" 'isearch-forward-regexp)
  998. (define-key view-mode-map "?" 'isearch-backward-regexp)
  999. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1000. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1001. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  1002. ))
  1003. (global-set-key "\M-r" 'view-mode)
  1004. ;; (setq view-read-only t)
  1005. ;; (defun my-view-mode-search-word (word)
  1006. ;; "Search for word current directory and subdirectories.
  1007. ;; If called intearctively, find word at point."
  1008. ;; (interactive (list (thing-at-point 'symbol)))
  1009. ;; (if word
  1010. ;; (if (and (require 'gtags nil t)
  1011. ;; (gtags-get-rootpath))
  1012. ;; (gtags-goto-tag word "s")
  1013. ;; (my-rgrep word))
  1014. ;; (message "No word at point.")
  1015. ;; nil))
  1016. (add-hook 'Man-mode-hook
  1017. (lambda ()
  1018. (view-mode 1)
  1019. (setq truncate-lines nil)))
  1020. (setq Man-notify-method (if window-system
  1021. 'newframe
  1022. 'aggressive))
  1023. (setq woman-cache-filename (expand-file-name (concat user-emacs-directory
  1024. "woman_cache.el")))
  1025. (defalias 'man 'woman)
  1026. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1027. ;; python
  1028. (when (lazy-load-eval 'python '(python-mode))
  1029. (setq python-python-command (or (executable-find "python3")
  1030. (executable-find "python")))
  1031. ;; (defun my-python-run-as-command ()
  1032. ;; ""
  1033. ;; (interactive)
  1034. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  1035. (defun my-python-display-python-buffer ()
  1036. ""
  1037. (interactive)
  1038. (set-window-text-height (display-buffer python-buffer
  1039. t)
  1040. 7))
  1041. (add-hook 'python-mode-hook
  1042. (lambda ()
  1043. (define-key python-mode-map
  1044. (kbd "C-c C-e") 'my-python-run-as-command)
  1045. (define-key python-mode-map
  1046. (kbd "C-c C-b") 'my-python-display-python-buffer)
  1047. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  1048. (add-hook 'inferior-python-mode-hook
  1049. (lambda ()
  1050. (my-python-display-python-buffer)
  1051. (define-key inferior-python-mode-map
  1052. (kbd "<up>") 'comint-previous-input)
  1053. (define-key inferior-python-mode-map
  1054. (kbd "<down>") 'comint-next-input))))
  1055. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1056. ;; GNU GLOBAL(gtags)
  1057. ;; http://uguisu.skr.jp/Windows/gtags.html
  1058. ;; http://eigyr.dip.jp/gtags.html
  1059. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  1060. (let ((d "/opt/local/share/gtags/"))
  1061. (and (file-directory-p d)
  1062. (add-to-list 'load-path
  1063. d)))
  1064. (when (lazy-load-eval 'gtags '(gtags-mode))
  1065. (add-hook 'gtags-mode-hook
  1066. (lambda ()
  1067. (view-mode gtags-mode)
  1068. (setq gtags-select-buffer-single t)
  1069. ;; (local-set-key "\M-t" 'gtags-find-tag)
  1070. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  1071. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  1072. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  1073. (define-key gtags-mode-map (kbd "C-x t h")
  1074. 'gtags-find-tag-from-here)
  1075. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  1076. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  1077. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  1078. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  1079. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  1080. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  1081. ))
  1082. (add-hook 'gtags-select-mode-hook
  1083. (lambda ()
  1084. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  1085. ))
  1086. )
  1087. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1088. ;; term mode
  1089. ;; (setq multi-term-program shell-file-name)
  1090. (and (fetch-library "http://www.emacswiki.org/emacs/download/multi-term.el"
  1091. t)
  1092. (lazy-load-eval 'multi-term)
  1093. (progn
  1094. (setq multi-term-switch-after-close nil)
  1095. (setq multi-term-dedicated-select-after-open-p t)
  1096. (setq multi-term-dedicated-window-height 20)))
  1097. (when (lazy-load-eval 'term '(term ansi-term))
  1098. (defun my-term-quit-or-send-raw ()
  1099. ""
  1100. (interactive)
  1101. (if (get-buffer-process (current-buffer))
  1102. (call-interactively 'term-send-raw)
  1103. (kill-buffer)))
  1104. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1105. ;; (setq term-ansi-default-program shell-file-name)
  1106. (add-hook 'term-setup-hook
  1107. (lambda ()
  1108. (setq term-display-table (make-display-table))))
  1109. (add-hook 'term-mode-hook
  1110. (lambda ()
  1111. (unless (memq (current-buffer)
  1112. (and (featurep 'multi-term)
  1113. ;; current buffer is not multi-term buffer
  1114. (multi-term-list)))
  1115. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1116. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1117. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1118. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1119. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1120. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1121. (define-key term-raw-map
  1122. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1123. (define-key term-raw-map
  1124. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1125. )
  1126. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1127. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1128. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1129. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1130. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1131. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1132. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1133. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1134. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1135. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1136. (define-key term-raw-map [delete] 'term-send-raw)
  1137. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1138. (define-key term-raw-map "\C-y" 'term-paste)
  1139. (define-key term-raw-map
  1140. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1141. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1142. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1143. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1144. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1145. (set (make-local-variable 'scroll-margin) 0)
  1146. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1147. ;; (cua-mode 0)
  1148. ;; (and cua-mode
  1149. ;; (local-unset-key (kbd "C-c")))
  1150. ;; (define-key cua--prefix-override-keymap
  1151. ;;"\C-c" 'term-interrupt-subjob)
  1152. (set (make-local-variable 'hl-line-range-function)
  1153. (lambda ()
  1154. '(0 . 0)))
  1155. ))
  1156. ;; (add-hook 'term-exec-hook 'forward-char)
  1157. )
  1158. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1159. ;; buffer switching
  1160. (when (lazy-load-eval 'bs '(bs-show)
  1161. ;; (add-to-list 'bs-configurations
  1162. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1163. (add-to-list 'bs-configurations
  1164. '("files-and-terminals" nil nil nil
  1165. (lambda (buf)
  1166. (and (bs-visits-non-file buf)
  1167. (save-excursion
  1168. (set-buffer buf)
  1169. (not (memq major-mode
  1170. '(term-mode
  1171. eshell-mode))))))))
  1172. ;; (setq bs-configurations (list
  1173. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1174. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1175. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1176. )
  1177. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1178. (defalias 'list-buffers 'bs-show)
  1179. (setq bs-default-configuration "files-and-terminals")
  1180. (setq bs-default-sort-name "by nothing")
  1181. (add-hook 'bs-mode-hook
  1182. (lambda ()
  1183. ;; (setq bs-default-configuration "files")
  1184. ;; (and bs--show-all
  1185. ;; (call-interactively 'bs-toggle-show-all))
  1186. (set (make-local-variable 'scroll-margin) 0))))
  1187. (iswitchb-mode 1)
  1188. (defun iswitchb-buffer-display-other-window ()
  1189. "Do iswitchb in other window."
  1190. (interactive)
  1191. (let ((iswitchb-default-method 'display))
  1192. (call-interactively 'iswitchb-buffer)))
  1193. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1194. ;; sdic
  1195. (when (lazy-load-eval 'sdic '(sdic-describe-word-at-point))
  1196. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1197. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1198. (defun sdic-describe-word-at-point-echo ()
  1199. ""
  1200. (interactive)
  1201. (save-window-excursion
  1202. (sdic-describe-word-at-point))
  1203. (save-excursion
  1204. (set-buffer sdic-buffer-name)
  1205. (message (buffer-substring (point-min)
  1206. (progn (goto-char (point-min))
  1207. (or (and (re-search-forward "^\\w"
  1208. nil
  1209. t
  1210. 4)
  1211. (progn (previous-line) t)
  1212. (point-at-eol))
  1213. (point-max)))))))
  1214. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1215. (setq sdic-waei-dictionary-list
  1216. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1217. (setq sdic-disable-select-window t)
  1218. (setq sdic-window-height 7))
  1219. ;;;;;;;;;;;;;;;;;;;;;;;;
  1220. ;; ilookup
  1221. (when (fetch-library
  1222. "https://raw.github.com/10sr/emacs-lisp/master/ilookup.el"
  1223. t)
  1224. (lazy-load-eval 'ilookup
  1225. '(ilookup-open)
  1226. (setq ilookup-dict-alist
  1227. '(
  1228. ("en" . (lambda (word)
  1229. (shell-command-to-string
  1230. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1231. word))))
  1232. ("ja" . (lambda (word)
  1233. (shell-command-to-string
  1234. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1235. word))))
  1236. ("jaj" . (lambda (word)
  1237. (shell-command-to-string
  1238. (format "sdcv -n -u jmdict-en-ja '%s'"
  1239. word))))
  1240. ("jag" .
  1241. (lambda (word)
  1242. (with-temp-buffer
  1243. (insert (shell-command-to-string
  1244. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1245. word)))
  1246. (html2text)
  1247. (buffer-substring (point-min)
  1248. (point-max)))))
  1249. ("alc" . (lambda (word)
  1250. (shell-command-to-string
  1251. (format "alc '%s' | head -n 20"
  1252. word))))
  1253. ("app" . (lambda (word)
  1254. (shell-command-to-string
  1255. (format "dict_app '%s'"
  1256. word))))
  1257. ;; letters broken
  1258. ("ms" .
  1259. (lambda (word)
  1260. (let ((url (concat
  1261. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1262. "Translate?appId=%s&text=%s&to=%s"))
  1263. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1264. (target "ja")
  1265. (eword (url-hexify-string word)))
  1266. (with-current-buffer (url-retrieve-synchronously
  1267. (format url
  1268. apikey
  1269. eword
  1270. target))
  1271. (message "")
  1272. (goto-char (point-min))
  1273. (search-forward-regexp "^$"
  1274. nil
  1275. t)
  1276. (url-unhex-string (buffer-substring-no-properties
  1277. (point)
  1278. (point-max)))))))
  1279. ))
  1280. ;; (funcall (cdr (assoc "ms"
  1281. ;; ilookup-alist))
  1282. ;; "dictionary")
  1283. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1284. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1285. (setq ilookup-default "ja")
  1286. (when (locate-library "google-translate")
  1287. (add-to-list 'ilookup-dict-alist
  1288. '("gt" .
  1289. (lambda (word)
  1290. (save-excursion
  1291. (google-translate-translate "auto"
  1292. "ja"
  1293. word))
  1294. (with-current-buffer "*Google Translate*"
  1295. (buffer-substring-no-properties (point-min)
  1296. (point-max)))))))
  1297. ))
  1298. (when (lazy-load-eval 'google-translate '(google-translate-translate
  1299. google-translate-at-point))
  1300. (setq google-translate-default-source-language "auto")
  1301. (setq google-translate-default-target-language "ja"))
  1302. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1303. ;; vc
  1304. ;; (require 'vc)
  1305. (setq vc-handled-backends '())
  1306. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1307. ;; gauche-mode
  1308. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1309. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1310. (when (and (fetch-library
  1311. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1312. t)
  1313. (lazy-load-eval 'gauche-mode '(gauche-mode run-scheme)))
  1314. (let ((s (executable-find "gosh")))
  1315. (setq scheme-program-name s
  1316. gauche-program-name s))
  1317. (defun run-gauche-other-window ()
  1318. "Run gauche on other window"
  1319. (interactive)
  1320. (switch-to-buffer-other-window
  1321. (get-buffer-create "*scheme*"))
  1322. (run-gauche))
  1323. (defun run-gauche ()
  1324. "run gauche"
  1325. (run-scheme gauche-program-name)
  1326. )
  1327. (defun scheme-send-buffer ()
  1328. ""
  1329. (interactive)
  1330. (scheme-send-region (point-min) (point-max))
  1331. (my-scheme-display-scheme-buffer)
  1332. )
  1333. (defun my-scheme-display-scheme-buffer ()
  1334. ""
  1335. (interactive)
  1336. (set-window-text-height (display-buffer scheme-buffer
  1337. t)
  1338. 7))
  1339. (add-hook 'scheme-mode-hook
  1340. (lambda ()
  1341. nil))
  1342. (add-hook 'inferior-scheme-mode-hook
  1343. (lambda ()
  1344. ;; (my-scheme-display-scheme-buffer)
  1345. ))
  1346. (setq auto-mode-alist
  1347. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1348. (setq auto-mode-alist
  1349. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1350. (add-hook 'gauche-mode-hook
  1351. (lambda ()
  1352. (define-key gauche-mode-map
  1353. (kbd "C-c C-z") 'run-gauche-other-window)
  1354. (define-key scheme-mode-map
  1355. (kbd "C-c C-c") 'scheme-send-buffer)
  1356. (define-key scheme-mode-map
  1357. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1358. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1359. ;; recentf-mode
  1360. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  1361. recentf-max-menu-items 20
  1362. recentf-max-saved-items 30
  1363. recentf-show-file-shortcuts-flag nil)
  1364. (when (require 'recentf nil t)
  1365. (add-to-list 'recentf-exclude
  1366. (regexp-quote recentf-save-file))
  1367. (add-to-list 'recentf-exclude
  1368. (regexp-quote (expand-file-name user-emacs-directory)))
  1369. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1370. (add-hook 'find-file-hook
  1371. 'recentf-save-list
  1372. t) ; save to file immediately after adding file to recentf list
  1373. (add-hook 'kill-emacs-hook
  1374. 'recentf-load-list)
  1375. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1376. ;; (add-hook 'find-file-hook
  1377. ;; (lambda ()
  1378. ;; (recentf-add-file default-directory)))
  1379. (and (fetch-library
  1380. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  1381. t)
  1382. (lazy-load-eval 'recentf-show)
  1383. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1384. (add-hook 'recentf-show-before-listing-hook
  1385. 'recentf-load-list))
  1386. (recentf-mode 1)
  1387. (add-hook 'recentf-dialog-mode-hook
  1388. (lambda ()
  1389. ;; (recentf-save-list)
  1390. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1391. ;; 'my-recentf-cd-and-find-file)
  1392. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1393. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1394. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1395. (define-key recentf-dialog-mode-map "n" 'next-line)
  1396. (cd "~/"))))
  1397. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1398. ;; dired
  1399. (when (lazy-load-eval 'dired nil)
  1400. (defun my-dired-echo-file-head (arg)
  1401. ""
  1402. (interactive "P")
  1403. (let ((f (dired-get-filename)))
  1404. (message "%s"
  1405. (with-temp-buffer
  1406. (insert-file-contents f)
  1407. (buffer-substring-no-properties
  1408. (point-min)
  1409. (progn (goto-line (if arg
  1410. (prefix-numeric-value arg)
  1411. 10))
  1412. (point-at-eol)))))))
  1413. (defun my-dired-diff ()
  1414. ""
  1415. (interactive)
  1416. (let ((files (dired-get-marked-files nil nil nil t)))
  1417. (if (eq (car files)
  1418. t)
  1419. (diff (cadr files) (dired-get-filename))
  1420. (message "One files must be marked!"))))
  1421. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1422. "pop up buffer using `display-buffer' and return that buffer."
  1423. (let ((bf (get-buffer-create buffer-or-name)))
  1424. (with-current-buffer bf
  1425. (cd ".")
  1426. (erase-buffer))
  1427. (display-buffer bf)
  1428. bf))
  1429. (defun my-replace-nasi-none ()
  1430. ""
  1431. (save-excursion
  1432. (let ((buffer-read-only nil))
  1433. (goto-char (point-min))
  1434. (while (search-forward "なし" nil t)
  1435. (replace-match "none")))))
  1436. (defun dired-get-file-info ()
  1437. "dired get file info"
  1438. (interactive)
  1439. (let ((f (shell-quote-argument (dired-get-filename t))))
  1440. (if (file-directory-p f)
  1441. (progn
  1442. (message "Calculating disk usage...")
  1443. (shell-command (concat "du -hsD "
  1444. f)))
  1445. (shell-command (concat "file "
  1446. f)))))
  1447. (defun my-dired-scroll-up ()
  1448. ""
  1449. (interactive)
  1450. (my-dired-previous-line (- (window-height) 1)))
  1451. (defun my-dired-scroll-down ()
  1452. ""
  1453. (interactive)
  1454. (my-dired-next-line (- (window-height) 1)))
  1455. ;; (defun my-dired-forward-line (arg)
  1456. ;; ""
  1457. ;; (interactive "p"))
  1458. (defun my-dired-previous-line (arg)
  1459. ""
  1460. (interactive "p")
  1461. (if (> arg 0)
  1462. (progn
  1463. (if (eq (line-number-at-pos)
  1464. 1)
  1465. (goto-char (point-max))
  1466. (forward-line -1))
  1467. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1468. (dired-get-subdir))
  1469. (- arg 1)
  1470. arg)))
  1471. (dired-move-to-filename)))
  1472. (defun my-dired-next-line (arg)
  1473. ""
  1474. (interactive "p")
  1475. (if (> arg 0)
  1476. (progn
  1477. (if (eq (point)
  1478. (point-max))
  1479. (goto-char (point-min))
  1480. (forward-line 1))
  1481. (my-dired-next-line (if (or (dired-get-filename nil t)
  1482. (dired-get-subdir))
  1483. (- arg 1)
  1484. arg)))
  1485. (dired-move-to-filename)))
  1486. (defun my-dired-print-current-dir-and-file ()
  1487. (message "%s %s"
  1488. default-directory
  1489. (buffer-substring-no-properties (point-at-bol)
  1490. (point-at-eol))))
  1491. (defun dired-do-execute-as-command ()
  1492. ""
  1493. (interactive)
  1494. (let ((file (dired-get-filename t)))
  1495. (if (file-executable-p file)
  1496. (start-process file nil file)
  1497. (when (y-or-n-p
  1498. "this file cant be executed. mark as executable and go? : ")
  1499. (set-file-modes file
  1500. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1501. (start-process file nil file)))))
  1502. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1503. (defun my-dired-x-open ()
  1504. ""
  1505. (interactive)
  1506. (my-x-open (dired-get-filename t t)))
  1507. (if (eq window-system 'mac)
  1508. (setq dired-listing-switches "-lhF")
  1509. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1510. )
  1511. (setq dired-listing-switches "-lhF")
  1512. (put 'dired-find-alternate-file 'disabled nil)
  1513. ;; when using dired-find-alternate-file
  1514. ;; reuse current dired buffer for the file to open
  1515. (setq dired-ls-F-marks-symlinks t)
  1516. (when (require 'ls-lisp nil t)
  1517. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1518. (setq ls-lisp-dirs-first t)
  1519. (setq ls-lisp-use-localized-time-format t)
  1520. (setq ls-lisp-format-time-list
  1521. '("%Y-%m-%d %H:%M"
  1522. "%Y-%m-%d ")))
  1523. (setq dired-dwim-target t)
  1524. ;; (add-hook 'dired-after-readin-hook
  1525. ;; 'my-replace-nasi-none)
  1526. ;; (add-hook 'after-init-hook
  1527. ;; (lambda ()
  1528. ;; (dired ".")))
  1529. (add-hook 'dired-mode-hook
  1530. (lambda ()
  1531. (define-key dired-mode-map "o" 'my-dired-x-open)
  1532. (define-key dired-mode-map "i" 'dired-get-file-info)
  1533. (define-key dired-mode-map "f" 'find-file)
  1534. (define-key dired-mode-map "!" 'shell-command)
  1535. (define-key dired-mode-map "&" 'async-shell-command)
  1536. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1537. (define-key dired-mode-map "=" 'my-dired-diff)
  1538. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1539. (define-key dired-mode-map "b" 'gtkbm)
  1540. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1541. (define-key dired-mode-map "@" (lambda ()
  1542. (interactive) (my-x-open ".")))
  1543. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1544. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1545. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1546. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1547. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1548. (substitute-key-definition 'dired-next-line
  1549. 'my-dired-next-line dired-mode-map)
  1550. (substitute-key-definition 'dired-previous-line
  1551. 'my-dired-previous-line dired-mode-map)
  1552. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1553. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1554. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1555. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1556. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1557. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1558. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1559. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1560. (let ((file "._Icon\015"))
  1561. (when nil (file-readable-p file)
  1562. (delete-file file)))))
  1563. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  1564. t)
  1565. (lazy-load-eval 'pack '(dired-do-pack-or-unpack pack))
  1566. (add-hook 'dired-mode-hook
  1567. (lambda ()
  1568. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  1569. (and (fetch-library
  1570. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1571. t)
  1572. (lazy-load-eval 'dired-list-all-mode)
  1573. (setq dired-listing-switches "-lhF")
  1574. (add-hook 'dired-mode-hook
  1575. (lambda ()
  1576. (define-key dired-mode-map "a" 'dired-list-all-mode)
  1577. )))
  1578. ) ; when dired locate
  1579. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1580. (defun my-dired-toggle-mark()
  1581. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1582. (t dired-marker-char))))
  1583. (delete-char 1)
  1584. (insert cur)))
  1585. (defun my-dired-mark (arg)
  1586. "Toggle mark the current (or next ARG) files.
  1587. If on a subdir headerline, mark all its files except `.' and `..'.
  1588. Use \\[dired-unmark-all-files] to remove all marks
  1589. and \\[dired-unmark] on a subdir to remove the marks in
  1590. this subdir."
  1591. (interactive "P")
  1592. (if (dired-get-subdir)
  1593. (save-excursion (dired-mark-subdir-files))
  1594. (let ((inhibit-read-only t))
  1595. (dired-repeat-over-lines
  1596. (prefix-numeric-value arg)
  1597. 'my-dired-toggle-mark))))
  1598. (defun my-dired-mark-backward (arg)
  1599. "In Dired, move up lines and toggle mark there.
  1600. Optional prefix ARG says how many lines to unflag; default is one line."
  1601. (interactive "p")
  1602. (my-dired-mark (- arg)))
  1603. (add-hook 'dired-mode-hook
  1604. (lambda ()
  1605. (local-set-key (kbd "SPC") 'my-dired-mark)
  1606. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1607. )
  1608. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1609. ;; eshell
  1610. (lazy-load-eval 'eshell nil
  1611. (defvar eshell-text-mode-map
  1612. (let ((map (make-sparse-keymap)))
  1613. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1614. map))
  1615. (define-derived-mode eshell-text-mode text-mode
  1616. "Eshell-Text"
  1617. "Text-mode for Eshell."
  1618. nil)
  1619. (defun eshell-text-mode-toggle ()
  1620. "Toggle eshell-text-mode and eshell-mode."
  1621. (interactive)
  1622. (cond ((eq major-mode
  1623. 'eshell-text-mode)
  1624. (goto-char (point-max))
  1625. (eshell-mode))
  1626. ((eq major-mode
  1627. 'eshell-mode)
  1628. (eshell-text-mode))
  1629. (t
  1630. (message "Not in eshell buffer")
  1631. nil)))
  1632. (defun my-eshell-backward-delete-char ()
  1633. (interactive)
  1634. (when (< (save-excursion
  1635. (eshell-bol)
  1636. (point))
  1637. (point))
  1638. (backward-delete-char 1)))
  1639. (defun my-file-owner-p (file)
  1640. "t if FILE is owned by me."
  1641. (eq (user-uid) (nth 2 (file-attributes file))))
  1642. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1643. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1644. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1645. ;; (defun eshell/less (&rest args)
  1646. ;; "Invoke `view-file' on the file.
  1647. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1648. ;; (if args
  1649. ;; (while args
  1650. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1651. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1652. ;; (file (pop args)))
  1653. ;; (view-file file)
  1654. ;; (goto-line line))
  1655. ;; (view-file (pop args))))))
  1656. (defun eshell/o (&optional file)
  1657. (my-x-open (or file ".")))
  1658. ;; (defun eshell/vi (&rest args)
  1659. ;; "Invoke `find-file' on the file.
  1660. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1661. ;; (while args
  1662. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1663. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1664. ;; (file (pop args)))
  1665. ;; (find-file file)
  1666. ;; (goto-line line))
  1667. ;; (find-file (pop args)))))
  1668. (defun eshell/clear ()
  1669. "Clear the current buffer, leaving one prompt at the top."
  1670. (interactive)
  1671. (let ((inhibit-read-only t))
  1672. (erase-buffer)))
  1673. (defun eshell-clear ()
  1674. (interactive)
  1675. (let ((inhibit-read-only t))
  1676. (erase-buffer)
  1677. (insert (funcall eshell-prompt-function))))
  1678. (defun eshell/d (&optional dirname switches)
  1679. "if first arg is omitted open current directory."
  1680. (dired (or dirname ".") switches))
  1681. (defun eshell/v ()
  1682. (view-mode 1))
  1683. ;; (defun eshell/aaa (&rest args)
  1684. ;; (message "%S"
  1685. ;; args))
  1686. (defvar eshell/git-cat-command
  1687. nil
  1688. "List of git commands that cat just return strings as results.")
  1689. (setq eshell/git-cat-command
  1690. '("status" "st" "b" "branch" "ls" "ls-files")
  1691. )
  1692. (defun eshell/git (&rest args)
  1693. (if (member (car args)
  1694. eshell/git-cat-command)
  1695. (shell-command-to-string (mapconcat 'shell-quote-argument
  1696. `("git"
  1697. "-c"
  1698. "color.ui=always"
  1699. ,@args)
  1700. " "))
  1701. ;; (eshell-git-shell-command-to-string args)
  1702. (if (require 'git-command nil t)
  1703. (git-command (mapconcat 'shell-quote-argument
  1704. args
  1705. " "))
  1706. (apply 'eshell-exec-visual "git" args))))
  1707. ;; (defun eshell-git-shell-command-to-string (args)
  1708. ;; "Return string of output of ARGS."
  1709. ;; (let ((sargs (mapconcat 'shell-quote-argument
  1710. ;; args
  1711. ;; " ")))
  1712. ;; (if (require 'ansi-color nil t)
  1713. ;; (identity
  1714. ;; (shell-command-to-string (concat "git "
  1715. ;; "-c color.ui=always "
  1716. ;; sargs)))
  1717. ;; (shell-command-to-string (concat "git "
  1718. ;; sargs)))))
  1719. (defalias 'eshell/g 'eshell/git)
  1720. (defalias 'eshell/: 'ignore)
  1721. (defalias 'eshell/type 'eshell/which)
  1722. ;; (defalias 'eshell/vim 'eshell/vi)
  1723. (defalias 'eshell/ff 'find-file)
  1724. (defalias 'eshell/q 'eshell/exit)
  1725. (defun eshell-goto-prompt ()
  1726. ""
  1727. (interactive)
  1728. (goto-char (point-max)))
  1729. (defun eshell-delete-char-or-logout (n)
  1730. (interactive "p")
  1731. (if (equal (eshell-get-old-input)
  1732. "")
  1733. (progn
  1734. (insert "exit")
  1735. (eshell-send-input))
  1736. (delete-char n)))
  1737. (defun eshell-kill-input ()
  1738. (interactive)
  1739. (delete-region (point)
  1740. (progn (eshell-bol)
  1741. (point))))
  1742. (defalias 'eshell/logout 'eshell/exit)
  1743. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1744. "open eshell and change wd
  1745. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1746. (interactive)
  1747. (let ((dir (expand-file-name default-directory)))
  1748. (switch-to-buffer (or eshell-buffer-or-name
  1749. (eshell t)))
  1750. (unless (equal dir (expand-file-name default-directory))
  1751. ;; (cd dir)
  1752. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1753. ;; (eshell-emit-prompt)
  1754. (goto-char (point-max))
  1755. (eshell-kill-input)
  1756. (insert "cd " dir)
  1757. (eshell-send-input))))
  1758. (defadvice eshell-next-matching-input-from-input
  1759. ;; do not cycle history
  1760. (around eshell-history-do-not-cycle activate)
  1761. (if (= 0
  1762. (or eshell-history-index
  1763. 0))
  1764. (progn
  1765. (delete-region eshell-last-output-end (point))
  1766. (insert-and-inherit eshell-matching-input-from-input-string)
  1767. (setq eshell-history-index nil))
  1768. ad-do-it))
  1769. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1770. (setq eshell-term-name "eterm-color")
  1771. (setq eshell-scroll-to-bottom-on-input t)
  1772. (setq eshell-cmpl-ignore-case t)
  1773. (setq eshell-cmpl-cycle-completions nil)
  1774. (setq eshell-highlight-prompt nil)
  1775. (setq eshell-ls-initial-args '("-hCFG"
  1776. "--color=auto"
  1777. "--time-style=long-iso")) ; "-hF")
  1778. (setq eshell-prompt-function
  1779. 'my-eshell-prompt-function)
  1780. (defun my-eshell-prompt-function ()
  1781. (with-temp-buffer
  1782. (let (p1 p2 p3 p4)
  1783. (insert ":: [")
  1784. (setq p1 (point))
  1785. (insert user-login-name
  1786. "@"
  1787. (car (split-string system-name
  1788. "\\."))
  1789. )
  1790. (setq p2 (point))
  1791. (insert ":")
  1792. (setq p3 (point))
  1793. (insert (abbreviate-file-name default-directory))
  1794. (setq p4 (point))
  1795. (insert "]")
  1796. (insert "\n:: ")
  1797. (unless (eq 0
  1798. eshell-last-command-status)
  1799. (insert (format "[STATUS:%d] "
  1800. eshell-last-command-status)))
  1801. (insert (if (= (user-uid)
  1802. 0)
  1803. "# "
  1804. "$ "))
  1805. (add-text-properties p1
  1806. p2
  1807. '(face underline))
  1808. (add-text-properties p3
  1809. p4
  1810. '(face underline))
  1811. (buffer-substring (point-min)
  1812. (point-max)))))
  1813. (add-hook 'eshell-mode-hook
  1814. (lambda ()
  1815. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1816. ;; (interactive)
  1817. ;; (switch-to-buffer (other-buffer))))
  1818. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1819. ;; (interactive)
  1820. ;; (eshell-goto-prompt)
  1821. ;; (keyboard-quit)))
  1822. (define-key eshell-mode-map (kbd "C-x t")
  1823. 'eshell-text-mode-toggle)
  1824. (define-key eshell-mode-map (kbd "C-u")
  1825. 'eshell-kill-input)
  1826. (define-key eshell-mode-map (kbd "C-d")
  1827. 'eshell-delete-char-or-logout)
  1828. ;; (define-key eshell-mode-map (kbd "C-l")
  1829. ;; 'eshell-clear)
  1830. (define-key eshell-mode-map (kbd "DEL")
  1831. 'my-eshell-backward-delete-char)
  1832. (define-key eshell-mode-map
  1833. (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1834. (define-key eshell-mode-map
  1835. (kbd "C-n") 'eshell-next-matching-input-from-input)
  1836. (apply 'eshell/addpath exec-path)
  1837. (set (make-local-variable 'scroll-margin) 0)
  1838. ;; (eshell/export "GIT_PAGER=")
  1839. ;; (eshell/export "GIT_EDITOR=")
  1840. (eshell/export "LC_MESSAGES=C")
  1841. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1842. (set (make-local-variable 'hl-line-range-function)
  1843. (lambda ()
  1844. '(0 . 0)))
  1845. (add-to-list 'eshell-virtual-targets
  1846. '("/dev/less"
  1847. (lambda (str)
  1848. (if str
  1849. (with-current-buffer nil)))
  1850. nil))
  1851. ))
  1852. (add-hook 'eshell-mode-hook
  1853. (lambda ()
  1854. (add-to-list 'eshell-visual-commands "vim")
  1855. ;; (add-to-list 'eshell-visual-commands "git")
  1856. (add-to-list 'eshell-output-filter-functions
  1857. 'eshell-truncate-buffer)
  1858. (mapcar (lambda (alias)
  1859. (add-to-list 'eshell-command-aliases-list
  1860. alias))
  1861. '(
  1862. ; ("ll" "ls -l $*")
  1863. ; ("la" "ls -a $*")
  1864. ; ("lla" "ls -al $*")
  1865. ("eless"
  1866. (concat "cat >>> (with-current-buffer "
  1867. "(get-buffer-create \"*eshell output\") "
  1868. "(erase-buffer) "
  1869. "(setq buffer-read-only nil) "
  1870. "(current-buffer)) "
  1871. "(view-buffer (get-buffer \"*eshell output*\"))")
  1872. ))
  1873. )))
  1874. ) ; eval after load eshell
  1875. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1876. ;; my-term
  1877. (defvar my-term nil
  1878. "My terminal buffer.")
  1879. (defvar my-term-function nil
  1880. "Function to create terminal buffer.
  1881. This function accept no argument and return newly created buffer of terminal.")
  1882. (defun my-term (&optional arg)
  1883. "Open terminal buffer and return that buffer.
  1884. ARG is ignored."
  1885. (interactive "P")
  1886. (if (and my-term
  1887. (buffer-name my-term))
  1888. (pop-to-buffer my-term)
  1889. (setq my-term
  1890. (save-window-excursion
  1891. (funcall my-term-function)))
  1892. (and my-term
  1893. (my-term))))
  1894. ;; (setq my-term-function
  1895. ;; (lambda ()
  1896. ;; (if (eq system-type 'windows-nt)
  1897. ;; (eshell)
  1898. ;; (if (require 'multi-term nil t)
  1899. ;; (multi-term)
  1900. ;; (ansi-term shell-file-name)))))
  1901. (setq my-term-function 'eshell)
  1902. (define-key my-prefix-map (kbd "C-s") 'my-term)
  1903. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1904. ;; x open
  1905. (defvar my-filer nil)
  1906. (setq my-filer (or (executable-find "pcmanfm")
  1907. (executable-find "nautilus")))
  1908. (defun my-x-open (file)
  1909. "open FILE."
  1910. (interactive "FOpen File: ")
  1911. (setq file (expand-file-name file))
  1912. (message "Opening %s..." file)
  1913. (cond ((eq system-type 'windows-nt)
  1914. (call-process "cmd.exe" nil 0 nil
  1915. "/c" "start" "" (convert-standard-filename file)))
  1916. ((eq system-type 'darwin)
  1917. (call-process "open" nil 0 nil file))
  1918. ((getenv "DISPLAY")
  1919. (call-process (or my-filer "xdg-open") nil 0 nil file))
  1920. (t
  1921. (find-file file))
  1922. )
  1923. ;; (recentf-add-file file)
  1924. (message "Opening %s...done" file))
  1925. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1926. ;; misc funcs
  1927. (defun memo (&optional dir)
  1928. "Open memo.txt in DIR."
  1929. (interactive)
  1930. (pop-to-buffer (find-file-noselect (concat (if dir
  1931. (file-name-as-directory dir)
  1932. "")
  1933. "memo.txt"))))
  1934. (defvar my-rgrep-alist
  1935. `(
  1936. ;; the silver searcher
  1937. ("ag"
  1938. (executable-find "ag")
  1939. "ag --nocolor --nogroup --nopager ")
  1940. ;; ack
  1941. ("ack"
  1942. (executable-find "ack")
  1943. "ack --nocolor --nogroup --nopager ")
  1944. ;; gnu global
  1945. ("global"
  1946. (and (require 'gtags nil t)
  1947. (executable-find "global")
  1948. (gtags-get-rootpath))
  1949. "global --result grep ")
  1950. ;; git grep
  1951. ("gitgrep"
  1952. (eq 0
  1953. (shell-command "git rev-parse --git-dir"))
  1954. "git --no-pager -c color.grep=false grep -nH -e ")
  1955. ;; grep
  1956. ("grep"
  1957. t
  1958. ,(concat "find . "
  1959. "-path '*/.git' -prune -o "
  1960. "-path '*/.svn' -prune -o "
  1961. "-type f -print0 | "
  1962. "xargs -0 grep -nH -e "))
  1963. )
  1964. "Alist of rgrep command.
  1965. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1966. condition to choose COMMAND when evaluated.")
  1967. (defvar my-rgrep-default nil
  1968. "Default command name for my-rgrep.")
  1969. (defun my-rgrep-grep-command (&optional name alist)
  1970. "Return recursive grep command for current directory or nil.
  1971. If NAME is given, use that without testing.
  1972. Commands are searched from ALIST."
  1973. (if alist
  1974. (if name
  1975. ;; if name is given search that from alist and return the command
  1976. (nth 2 (assoc name
  1977. alist))
  1978. ;; if name is not given try test in 1th elem
  1979. (let ((car (car alist))
  1980. (cdr (cdr alist)))
  1981. (if (eval (nth 1 car))
  1982. ;; if the condition is true return the command
  1983. (nth 2 car)
  1984. ;; try next one
  1985. (and cdr
  1986. (my-rgrep-grep-command name cdr)))))
  1987. ;; if alist is not given set default value
  1988. (my-rgrep-grep-command name my-rgrep-alist)))
  1989. (my-rgrep-grep-command "ag" nil)
  1990. (defun my-rgrep (command-args)
  1991. "My recursive grep. Run COMMAND-ARGS."
  1992. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1993. nil)))
  1994. (if cmd
  1995. (list (read-shell-command "grep command: "
  1996. cmd
  1997. 'grep-find-history))
  1998. (error "my-rgrep: Command for rgrep not found")
  1999. )))
  2000. (compilation-start command-args
  2001. 'grep-mode))
  2002. ;; (defun my-rgrep-symbol-at-point (command-args)
  2003. ;; "My recursive grep. Run COMMAND-ARGS."
  2004. ;; (interactive (list (read-shell-command "grep command: "
  2005. ;; (concat (my-rgrep-grep-command)
  2006. ;; " "
  2007. ;; (thing-at-point 'symbol))
  2008. ;; 'grep-find-history)))
  2009. ;; (compilation-start command-args
  2010. ;; 'grep-mode))
  2011. (defmacro define-my-rgrep (name)
  2012. "Define rgrep for NAME."
  2013. `(defun ,(intern (concat "my-rgrep-"
  2014. name)) ()
  2015. ,(format "My recursive grep by %s."
  2016. name)
  2017. (interactive)
  2018. (let ((my-rgrep-default ,name))
  2019. (if (called-interactively-p 'any)
  2020. (call-interactively 'my-rgrep)
  2021. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2022. )
  2023. (define-my-rgrep "ack")
  2024. (define-my-rgrep "ag")
  2025. (define-my-rgrep "gitgrep")
  2026. (define-my-rgrep "grep")
  2027. (define-my-rgrep "global")
  2028. (define-key ctl-x-map "s" 'my-rgrep)
  2029. ;; (defun make ()
  2030. ;; "Run \"make -k\" in current directory."
  2031. ;; (interactive)
  2032. ;; (compile "make -k"))
  2033. (defalias 'make 'compile)
  2034. (defvar sed-in-place-history nil
  2035. "History of `sed-in-place'.")
  2036. (defvar sed-in-place-command "sed --in-place=.bak -e")
  2037. (defun sed-in-place (command)
  2038. "Issue sed in place COMMAND."
  2039. (interactive (list (read-shell-command "sed in place: "
  2040. (concat sed-in-place-command " ")
  2041. 'sed-in-place-history)))
  2042. (shell-command command
  2043. "*sed in place*"))
  2044. (defun dired-do-sed-in-place (&optional arg)
  2045. "Issue sed in place dired. If ARG is given, use the next ARG files."
  2046. (interactive "p")
  2047. (require 'dired-aux)
  2048. (let* ((files (dired-get-marked-files t arg))
  2049. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  2050. nil
  2051. 'sed-in-place
  2052. arg
  2053. files)))
  2054. (if (equal expr
  2055. "")
  2056. (error "No expression specified")
  2057. (shell-command (concat sed-in-place-command
  2058. " '"
  2059. expr
  2060. "' "
  2061. (mapconcat 'shell-quote-argument
  2062. files
  2063. " "))
  2064. "*sed in place*"))))
  2065. (defun dir-show (&optional dir)
  2066. "Show DIR list."
  2067. (interactive)
  2068. (let ((bf (get-buffer-create "*dir show*"))
  2069. (list-directory-brief-switches "-C"))
  2070. (with-current-buffer bf
  2071. (list-directory (or nil
  2072. default-directory)
  2073. nil))
  2074. ))
  2075. (defun my-convmv-sjis2utf8-test ()
  2076. "Run `convmv -r -f sjis -t utf8 *'.
  2077. this is test, does not rename files."
  2078. (interactive)
  2079. (shell-command "convmv -r -f sjis -t utf8 *"))
  2080. (defun my-convmv-sjis2utf8-notest ()
  2081. "Run `convmv -r -f sjis -t utf8 * --notest'."
  2082. (interactive)
  2083. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  2084. (defun kill-ring-save-buffer-file-name ()
  2085. "Get current filename."
  2086. (interactive)
  2087. (let ((file buffer-file-name))
  2088. (if file
  2089. (progn (kill-new file)
  2090. (message file))
  2091. (message "not visiting file."))))
  2092. (defvar kill-ring-buffer-name "*kill-ring*"
  2093. "Buffer name for `kill-ring-buffer'.")
  2094. (defun open-kill-ring-buffer ()
  2095. "Open kill- ring buffer."
  2096. (interactive)
  2097. (pop-to-buffer
  2098. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  2099. (erase-buffer)
  2100. (yank)
  2101. (text-mode)
  2102. (current-local-map)
  2103. (goto-char (point-min))
  2104. (yank)
  2105. (current-buffer))))
  2106. (defun set-terminal-header (string)
  2107. "Set terminal header STRING."
  2108. (let ((savepos "\033[s")
  2109. (restorepos "\033[u")
  2110. (movecursor "\033[0;%dH")
  2111. (inverse "\033[7m")
  2112. (restorecolor "\033[0m")
  2113. (cols (frame-parameter nil 'width))
  2114. (length (length string)))
  2115. ;; (redraw-frame (selected-frame))
  2116. (send-string-to-terminal (concat savepos
  2117. (format movecursor
  2118. (1+ (- cols length)))
  2119. inverse
  2120. string
  2121. restorecolor
  2122. restorepos))
  2123. ))
  2124. (defun my-set-terminal-header ()
  2125. "Set terminal header."
  2126. (set-terminal-header (concat " "
  2127. user-login-name
  2128. "@"
  2129. (car (split-string system-name
  2130. "\\."))
  2131. " "
  2132. (format-time-string "%Y/%m/%d %T %z")
  2133. " ")))
  2134. ;; (run-with-timer
  2135. ;; 0.1
  2136. ;; 1
  2137. ;; 'my-set-terminal-header)
  2138. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2139. ;; ;; savage emacs
  2140. ;; ;; when enabled emacs fails to complete
  2141. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  2142. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  2143. ;; (setq arg
  2144. ;; (concat arg
  2145. ;; (if (eq nil
  2146. ;; (string-match "\\. *$"
  2147. ;; arg))
  2148. ;; ".")
  2149. ;; " Stupid!")))
  2150. (defvar info-in-prompt
  2151. nil
  2152. "System info in the form of \"[user@host] \".")
  2153. (setq info-in-prompt
  2154. (concat "["
  2155. user-login-name
  2156. "@"
  2157. (car (split-string system-name
  2158. "\\."))
  2159. "]"))
  2160. (defun my-real-function-subr-p (function)
  2161. "Return t if FUNCTION is a built-in function even if it is advised."
  2162. (let* ((advised (and (symbolp function)
  2163. (featurep 'advice)
  2164. (ad-get-advice-info function)))
  2165. (real-function
  2166. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  2167. (and (fboundp origname)
  2168. origname)))
  2169. function))
  2170. (def (if (symbolp real-function)
  2171. (symbol-function real-function)
  2172. function)))
  2173. (subrp def)))
  2174. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  2175. ;; (defadvice read-from-minibuffer (before info-in-prompt activate)
  2176. ;; "Show system info when use `read-from-minibuffer'."
  2177. ;; (ad-set-arg 0
  2178. ;; (concat my-system-info
  2179. ;; (ad-get-arg 0))))
  2180. ;; (defadvice read-string (before info-in-prompt activate)
  2181. ;; "Show system info when use `read-string'."
  2182. ;; (ad-set-arg 0
  2183. ;; (concat my-system-info
  2184. ;; (ad-get-arg 0))))
  2185. ;; (when (< emacs-major-version 24)
  2186. ;; (defadvice completing-read (before info-in-prompt activate)
  2187. ;; "Show system info when use `completing-read'."
  2188. ;; (ad-set-arg 0
  2189. ;; (concat my-system-info
  2190. ;; (ad-get-arg 0)))))
  2191. (defmacro info-in-prompt-set (&rest functions)
  2192. "Set info-in-prompt advices for FUNCTIONS."
  2193. `(progn
  2194. ,@(mapcar (lambda (f)
  2195. `(defadvice ,f (before info-in-prompt activate)
  2196. "Show info in prompt."
  2197. (let ((orig (ad-get-arg 0)))
  2198. (unless (string-match-p (regexp-quote info-in-prompt)
  2199. orig)
  2200. (ad-set-arg 0
  2201. (concat info-in-prompt
  2202. " "
  2203. orig))))))
  2204. functions)))
  2205. (info-in-prompt-set read-from-minibuffer
  2206. read-string
  2207. completing-read)
  2208. ;;; emacs.el ends here