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

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