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.
 
 
 
 
 
 

2617 lines
90 KiB

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