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.

emacs.el 63 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  1. ;; load another file
  2. ;; (load-file "~/filepath")
  3. (unless (file-directory-p (expand-file-name user-emacs-directory))
  4. (make-directory (expand-file-name user-emacs-directory)))
  5. (let ((d (expand-file-name (concat user-emacs-directory
  6. "lisp"))))
  7. (unless (file-directory-p d)
  8. (make-directory d))
  9. (add-to-list 'load-path d))
  10. (require 'cl nil t)
  11. (progn
  12. (defvar buffer-file-changed-functions nil "Hook run when buffer file changed.
  13. Each function is called with two args, the filename before changing and after changing.")
  14. (declare-function run-buffer-file-changed-functions "emacs.el")
  15. (add-hook 'post-command-hook
  16. 'run-buffer-file-changed-functions)
  17. (lexical-let (previous-file)
  18. (defun run-buffer-file-changed-functions ()
  19. ""
  20. (unless (and previous-file
  21. (equal previous-file
  22. (expand-file-name (or buffer-file-name
  23. default-directory))))
  24. (let ((pfile previous-file)
  25. (cfile (expand-file-name (or buffer-file-name
  26. default-directory))))
  27. (setq previous-file cfile)
  28. (run-hook-with-args 'buffer-file-changed-functions pfile cfile)))))
  29. ;; (add-hook 'buffer-file-changed-function
  30. ;; (lambda (pdir cdir)
  31. ;; (message "dir changed %s to %s !" pdir cdir)))
  32. )
  33. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  34. ;; download library from web
  35. (require 'url)
  36. (defun dllib-if-unfound (url &optional byte-compile-p force-download-p)
  37. "If library does not exist, download it from URL and locate it in \"~/emacs.d/lisp/\".
  38. Return nil if library unfound and failed to download, otherwise the path where the library installed."
  39. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  40. (lib (file-name-sans-extension (file-name-nondirectory url)))
  41. (lpath (concat dir lib ".el"))
  42. (locate-p (locate-library lib)))
  43. (if (or force-download-p (not locate-p))
  44. (progn (condition-case nil
  45. (progn (message "downloading %s..." url)
  46. (url-copy-file url
  47. lpath
  48. t)
  49. (when (and byte-compile-p
  50. (require 'bytecomp nil t))
  51. (and (file-exists-p (byte-compile-dest-file lpath))
  52. (delete-file (byte-compile-dest-file lpath)))
  53. (byte-compile-file lpath))
  54. )
  55. (error (and (file-writable-p lpath)
  56. (delete-file lpath))
  57. (message "downloading %s...something wrong happened!" url)
  58. nil))
  59. (locate-library lib))
  60. locate-p)))
  61. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  62. ;; start and quit
  63. (setq inhibit-startup-message t)
  64. (setq frame-title-format (list '(:eval (format-time-string (or display-time-format "")))
  65. " | %b "
  66. '(:eval (number-to-string (length (buffer-list-not-start-with-space))))
  67. " buffers ["
  68. invocation-name
  69. " "
  70. emacs-version
  71. " "
  72. (symbol-name system-type)
  73. "] "
  74. '(:eval (symbol-name last-command))))
  75. (setq set-terminal-title-regexp "^\\(rxvt\\|xterm\\|aterm$\\|screen\\)")
  76. (defun set-terminal-title (&rest args)
  77. ""
  78. (interactive "sString to set as title: ")
  79. (let ((tty (frame-parameter nil
  80. 'tty-type)))
  81. (when (and tty
  82. (string-match set-terminal-title-regexp
  83. tty))
  84. (send-string-to-terminal (apply 'concat
  85. "\033]0;"
  86. `(,@args "\007"))))))
  87. (defun my-set-terminal-title ()
  88. ""
  89. (set-terminal-title "["
  90. invocation-name
  91. " "
  92. emacs-version
  93. " "
  94. (symbol-name system-type)
  95. "] "
  96. (abbreviate-file-name (or buffer-file-name default-directory))))
  97. (add-hook 'buffer-file-changed-functions
  98. (lambda (p c)
  99. (my-set-terminal-title)))
  100. (add-hook 'suspend-resume-hook
  101. 'my-set-terminal-title)
  102. (defun buffer-list-not-start-with-space ()
  103. (let ((bl (buffer-list))
  104. b nbl)
  105. (while bl
  106. (setq b (pop bl))
  107. (unless (string-equal " "
  108. (substring (buffer-name b)
  109. 0
  110. 1))
  111. (add-to-list 'nbl b)))
  112. nbl))
  113. (setq confirm-kill-emacs 'y-or-n-p)
  114. (setq gc-cons-threshold (* 1024 1024 4))
  115. (when window-system
  116. (add-to-list 'default-frame-alist '(cursor-type . box))
  117. (add-to-list 'default-frame-alist '(background-color . "white"))
  118. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  119. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100))) ; does not work?
  120. )
  121. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  122. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  123. (tool-bar-mode 0)
  124. (set-scroll-bar-mode nil)
  125. (add-hook 'kill-emacs-hook ; load when exitting to examine if file is written properly
  126. (lambda ()
  127. (when (file-readable-p "~/.emacs")
  128. (load-file "~/.emacs"))
  129. ))
  130. (add-hook 'after-init-hook
  131. (lambda ()
  132. (message (emacs-init-time))
  133. (switch-to-buffer "*Messages*")
  134. ))
  135. (cd ".") ; when using windows use / instead of \ in `default-directory'
  136. ;; locale
  137. (set-language-environment "Japanese")
  138. (set-default-coding-systems 'utf-8-unix)
  139. (prefer-coding-system 'utf-8-unix)
  140. (setq system-time-locale "C")
  141. ;; my prefix map
  142. (define-prefix-command 'my-prefix-map)
  143. '(add-hook 'after-init-hook
  144. (lambda ()
  145. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  146. ))
  147. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  148. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  149. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  150. ;; (comint-show-maximum-output)
  151. ;; kill scratch
  152. (add-hook 'after-init-hook
  153. (lambda ()
  154. (kill-buffer "*scratch*")))
  155. ;; modifier keys
  156. (setq mac-option-modifier 'control)
  157. (setq w32-apps-modifier 'meta)
  158. ;; display
  159. (setq redisplay-dont-pause t)
  160. (setq visible-bell t)
  161. (setq ring-bell-function 'ignore)
  162. (mouse-avoidance-mode 'banish)
  163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  164. ;; global keys
  165. (and (dllib-if-unfound "https://raw.github.com/k1LoW/emacs-drill-instructor/master/drill-instructor.el"
  166. t)
  167. (require 'drill-instructor nil t)
  168. (setq drill-instructor-global t)
  169. (let ((map drill-instructor-key-map))
  170. (define-key map (kbd "RET") nil)
  171. (define-key map (kbd "DEL") nil)
  172. (define-key map (kbd "C-h") nil)))
  173. '(mapc (lambda (key)
  174. (global-set-key (read-kbd-macro key) 'ignore))
  175. '("<up>" "<down>" "<right>" "<left>"))
  176. (define-key my-prefix-map (kbd "C-o") 'occur)
  177. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  178. (global-set-key (kbd "C-\\") help-map)
  179. (define-key ctl-x-map (kbd "DEL") help-map)
  180. (define-key ctl-x-map (kbd "C-h") help-map)
  181. (define-key help-map "a" 'apropos)
  182. ;; compose window
  183. (global-set-key [?\C--] 'other-window)
  184. (global-set-key [?\C-0] 'delete-window)
  185. (global-set-key [?\C-1] 'delete-other-windows)
  186. (global-set-key [?\C-2] 'split-window-vertically)
  187. (global-set-key [?\C-3] 'split-window-horizontally)
  188. ;; disable annoying keys
  189. (global-set-key [prior] 'ignore)
  190. (global-set-key (kbd "<next>") 'ignore)
  191. (global-set-key [menu] 'ignore)
  192. (global-set-key [down-mouse-1] 'ignore)
  193. (global-set-key [down-mouse-2] 'ignore)
  194. (global-set-key [down-mouse-3] 'ignore)
  195. (global-set-key [mouse-1] 'ignore)
  196. (global-set-key [mouse-2] 'ignore)
  197. (global-set-key [mouse-3] 'ignore)
  198. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  199. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  200. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  201. ;; mode-line
  202. (setq eol-mnemonic-dos "crlf")
  203. (setq eol-mnemonic-mac "cr")
  204. (setq eol-mnemonic-unix "lf")
  205. (which-function-mode 0)
  206. (line-number-mode 0)
  207. (column-number-mode 0)
  208. (size-indication-mode 0)
  209. (setq mode-line-position
  210. '(:eval (format "L%%l/%d,C%%c"
  211. (count-lines (point-max)
  212. (point-min)))))
  213. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  214. ;; display date
  215. (add-hook 'after-init-hook
  216. (lambda ()
  217. (when display-time-mode
  218. (display-time-update))
  219. ))
  220. (setq display-time-interval 29)
  221. (setq display-time-day-and-date t)
  222. (setq display-time-format "%a, %d %b %Y %T")
  223. (if window-system
  224. (display-time-mode 0)
  225. (display-time-mode 1))
  226. ;; ;; current directory
  227. ;; (let ((ls (member 'mode-line-buffer-identification
  228. ;; mode-line-format)))
  229. ;; (setcdr ls
  230. ;; (cons '(:eval (concat " ("
  231. ;; (abbreviate-file-name default-directory)
  232. ;; ")"))
  233. ;; (cdr ls))))
  234. ;; ;; display last modified time
  235. ;; (let ((ls (member 'mode-line-buffer-identification
  236. ;; mode-line-format)))
  237. ;; (setcdr ls
  238. ;; (cons '(:eval (concat " "
  239. ;; my-buffer-file-last-modified-time))
  240. ;; (cdr ls))))
  241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  242. ;; minibuffer
  243. (setq insert-default-directory t)
  244. (setq completion-ignore-case t
  245. read-file-name-completion-ignore-case t
  246. read-buffer-completion-ignore-case t)
  247. (setq resize-mini-window t)
  248. (temp-buffer-resize-mode 1)
  249. (savehist-mode 1)
  250. (fset 'yes-or-no-p 'y-or-n-p)
  251. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol) ; complete symbol when `eval'
  252. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  253. ;; letters, font-lock mode and fonts
  254. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  255. (when (eq system-type 'Darwin)
  256. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  257. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  258. (when (and (boundp 'input-method-activate-hook) ;ちょっと正しいかわかんない
  259. (boundp 'input-method-inactivate-hook))
  260. (add-hook 'input-method-activate-hook
  261. (lambda () (set-cursor-color "red")))
  262. (add-hook 'input-method-inactivate-hook
  263. (lambda () (set-cursor-color "black"))))
  264. (show-paren-mode 1)
  265. (setq show-paren-style 'mixed)
  266. (transient-mark-mode 1)
  267. (global-font-lock-mode 1)
  268. (setq font-lock-global-modes
  269. '(not
  270. help-mode
  271. eshell-mode
  272. term-mode
  273. Man-mode))
  274. (standard-display-ascii ?\n "$\n")
  275. (defvar my-eol-face
  276. '(("\n" . (0 font-lock-comment-face t nil)))
  277. )
  278. (defvar my-tab-face
  279. '(("\t" . '(0 highlight t nil))))
  280. (defvar my-jspace-face
  281. '(("\u3000" . '(0 highlight t nil))))
  282. (add-hook 'font-lock-mode-hook
  283. (lambda ()
  284. (font-lock-add-keywords nil my-eol-face)
  285. (font-lock-add-keywords nil my-jspace-face)
  286. ))
  287. ;; highlight current line
  288. ;; http://wiki.riywo.com/index.php?Meadow
  289. (defface hlline-face
  290. '((((type x w32)
  291. (class color)
  292. (background dark))
  293. (:background "midnightblue")) ; :foreground "white")) ;; ハイライトの文字色は変えない方がいいかも
  294. (((type x w32)
  295. (class color)
  296. (background light))
  297. (:background "gainsboro"))
  298. (t
  299. (:underline "black")))
  300. "*Face used by hl-line.")
  301. ;; (defface hlline-ul-face
  302. ;; '((t (:underline "yellow")))
  303. ;; "underline yellow")
  304. (setq hl-line-face 'hlline-face) ;; (setq hl-line-face nil)
  305. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  306. (setq hl-line-global-modes
  307. '(not
  308. term-mode))
  309. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  310. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  311. ;; fonts
  312. (defun my-set-ascii-and-jp-font-with-size (list)
  313. ""
  314. (if (> emacs-major-version 22)
  315. (progn ; 23 or later
  316. (set-face-attribute 'default nil
  317. :family (nth 0 list)
  318. :height (nth 1 list))
  319. (set-fontset-font "fontset-default"
  320. 'japanese-jisx0208
  321. (font-spec :family (nth 2 list) :size (nth 3 list)))
  322. (set-fontset-font "fontset-default"
  323. 'katakana-jisx0201
  324. (font-spec :family (nth 2 list) :size (nth 3 list))))
  325. ; font spec is available in emacs23 and later, cannot used in emacs22
  326. (progn ; 22
  327. (set-face-attribute 'default nil
  328. :family (nth 0 list)
  329. :height (nth 1 list))
  330. (set-fontset-font "fontset-default"
  331. 'japanese-jisx0208
  332. (cons (nth 2 list) "jisx0208.*"))
  333. (set-fontset-font "fontset-default"
  334. 'katakana-jisx0201
  335. (cons (nth 2 list) "jisx0201.*"))
  336. )))
  337. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 90 "takaogothic" 13))
  338. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "takaogothic" 14))
  339. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "ms gothic" 14))
  340. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 75 "takaogothic" 11))
  341. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 90 "takaogothic" 13))
  342. ;; (my-set-ascii-and-jp-font-with-size '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  343. ;; あ a
  344. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  345. t)
  346. (progn
  347. (require 'set-modeline-color nil t)))
  348. (let ((fg (face-foreground 'default))
  349. (bg (face-background 'default)))
  350. (set-face-background 'mode-line-inactive (if (face-inverse-video-p 'mode-line) fg bg))
  351. (set-face-foreground 'mode-line-inactive (if (face-inverse-video-p 'mode-line) bg fg)))
  352. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  353. ;; file handling
  354. (setq revert-without-query '(".+"))
  355. ;; save cursor position
  356. (setq save-place-file (concat user-emacs-directory
  357. "places"))
  358. (when (require 'saveplace nil t)
  359. (setq-default save-place t))
  360. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  361. (setq make-backup-files t)
  362. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  363. (setq backup-directory-alist
  364. (cons (cons "\\.*$" (expand-file-name "~/.emacs.d/backup"))
  365. backup-directory-alist))
  366. (setq version-control 'never)
  367. (setq delete-old-versions t)
  368. (setq auto-save-list-file-prefix (expand-file-name "~/.emacs.d/autosave/"))
  369. (setq delete-auto-save-files t)
  370. (add-to-list 'completion-ignored-extensions ".bak")
  371. ;; (setq delete-by-moving-to-trash t
  372. ;; trash-directory "~/.emacs.d/trash")
  373. (add-hook 'after-save-hook
  374. 'executable-make-buffer-file-executable-if-script-p)
  375. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  376. ;; editting
  377. (setq require-final-newline t)
  378. (setq kill-whole-line t)
  379. (setq scroll-conservatively 35
  380. scroll-margin 2
  381. scroll-step 0)
  382. (setq default-major-mode 'text-mode)
  383. (setq next-line-add-newlines nil)
  384. (setq kill-read-only-ok t)
  385. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  386. ;; (setq-default line-spacing 0.2)
  387. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  388. (setq-default tab-width 4)
  389. (setq-default indent-tabs-mode nil)
  390. (setq-default indent-line-function nil)
  391. ;(pc-selection-mode 1) ; this make some already defined keybind back to default
  392. (delete-selection-mode 1)
  393. (cua-mode 0)
  394. (setq line-move-visual nil)
  395. ;; key bindings
  396. ;; moving around
  397. ;; (global-set-key (kbd "M-j") 'next-line)
  398. ;; (global-set-key (kbd "M-k") 'previous-line)
  399. ;; (global-set-key (kbd "M-h") 'backward-char)
  400. ;; (global-set-key (kbd "M-l") 'forward-char)
  401. ;;(keyboard-translate ?\M-j ?\C-j)
  402. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  403. (define-key esc-map "p" 'backward-paragraph)
  404. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  405. (define-key esc-map "n" 'forward-paragraph)
  406. (global-set-key (kbd "C-<up>") (lambda () (interactive)(scroll-down 1)))
  407. (global-set-key (kbd "C-<down>") (lambda () (interactive)(scroll-up 1)))
  408. (global-set-key (kbd "C-<left>") 'scroll-down)
  409. (global-set-key (kbd "C-<right>") 'scroll-up)
  410. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  411. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  412. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  413. ;; C-h and DEL
  414. (global-set-key (kbd "C-h") (kbd "DEL"))
  415. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  416. (global-set-key (kbd "C-o")
  417. ;; (lambda ()
  418. ;; (interactive)
  419. ;; (move-end-of-line nil)
  420. ;; (newline-and-indent))
  421. (kbd "C-e C-m")
  422. )
  423. (global-set-key (kbd "C-k") 'kill-whole-line)
  424. (global-set-key (kbd "M-k") 'my-copy-whole-line)
  425. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  426. (global-set-key (kbd "M-u") 'undo)
  427. (global-set-key (kbd "C-r") 'query-replace-regexp)
  428. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  429. (global-set-key (kbd "M-i") (kbd "ESC TAB"))
  430. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  431. ;; gmail
  432. (setq mail-interactive t
  433. send-mail-function 'smtpmail-send-it
  434. ;; message-send-mail-function 'smtpmail-send-it
  435. smtpmail-smtp-server "smtp.gmail.com"
  436. smtpmail-smtp-service 587
  437. smtpmail-starttls-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  438. smtpmail-auth-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  439. user-mail-address "8.slashes@gmail.com")
  440. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  441. ;; buffer killing
  442. (defun my-delete-window-killing-buffer () nil)
  443. (defun my-query-kill-this-buffer ()
  444. ""
  445. (interactive)
  446. (if (y-or-n-p (concat "kill this buffer? :"))
  447. (kill-buffer (current-buffer))))
  448. (substitute-key-definition 'kill-buffer 'my-query-kill-this-buffer global-map)
  449. ;;(global-set-key "\C-xk" 'my-query-kill-this-buffer)
  450. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  451. ;; package
  452. (when (require 'package nil t)
  453. (add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t)
  454. (add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/"))
  455. (add-to-list 'package-archives '("ELPA" . "http://tromey.com/elpa/"))
  456. (package-initialize))
  457. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  458. ;; some modes and hooks
  459. (require 'simple nil t)
  460. (and window-system
  461. (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el" t)
  462. (require 'save-window-size nil t))
  463. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  464. ;; share clipboard with x
  465. ;; this page describes this in details, but only these sexps seem to be needed
  466. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  467. (and (not window-system)
  468. (not (eq window-system 'mac))
  469. (getenv "DISPLAY")
  470. (executable-find "xclip")
  471. ;; (< emacs-major-version 24)
  472. (dllib-if-unfound "http://www.emacswiki.org/emacs/download/xclip.el" t)
  473. (require 'xclip nil t)
  474. (turn-on-xclip))
  475. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  476. ;; mode
  477. (add-hook 'verilog-mode-hook
  478. (lambda ()
  479. (define-key verilog-mode-map ";" 'self-insert-command)))
  480. (setq diff-switches "-u")
  481. (add-hook 'diff-mode-hook
  482. (lambda ()
  483. (view-mode 1)
  484. (set-face-foreground 'diff-file-header-face nil)
  485. (set-face-foreground 'diff-header-face nil)
  486. (set-face-foreground 'diff-index-face "blue")
  487. (set-face-foreground 'diff-hunk-header-face "cyan")
  488. (set-face-foreground 'diff-context-face nil)
  489. (set-face-foreground 'diff-removed-face "red")
  490. (set-face-foreground 'diff-added-face "green")
  491. (set-face-foreground 'diff-changed-face "magenda")
  492. ))
  493. ;; (ffap-bindings)
  494. (add-hook 'sh-mode-hook
  495. (lambda ()
  496. (define-key sh-mode-map (kbd "C-x C-e") 'my-execute-shell-command-current-line)))
  497. (defun my-execute-shell-command-current-line ()
  498. ""
  499. (interactive)
  500. (shell-command (buffer-substring-no-properties (point-at-bol)
  501. (point))))
  502. ;; (add-to-list 'auto-mode-alist
  503. ;; '("\\(xinitrc\\|xprograms\\)\\'" . sh-mode))
  504. (setq auto-mode-alist
  505. `(("autostart\\'" . sh-mode)
  506. ("xinitrc\\'" . sh-mode)
  507. ("xprograms\\'" . sh-mode)
  508. ,@auto-mode-alist))
  509. (setq python-python-command (or (executable-find "python3")
  510. (executable-find "python")))
  511. (defun my-python-run-as-command ()
  512. ""
  513. (interactive)
  514. (shell-command (concat python-python-command " " buffer-file-name)))
  515. (defun my-python-display-python-buffer ()
  516. ""
  517. (interactive)
  518. (set-window-text-height (display-buffer python-buffer
  519. t)
  520. 7))
  521. (add-hook 'python-mode-hook
  522. (lambda ()
  523. (define-key python-mode-map (kbd "C-c C-e") 'my-python-run-as-command)
  524. (define-key python-mode-map (kbd "C-c C-b") 'my-python-display-python-buffer)
  525. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  526. (add-hook 'inferior-python-mode-hook
  527. (lambda ()
  528. (my-python-display-python-buffer)
  529. (define-key inferior-python-mode-map (kbd "<up>") 'comint-previous-input)
  530. (define-key inferior-python-mode-map (kbd "<down>") 'comint-next-input)))
  531. (add-hook 'text-mode-hook
  532. (lambda ()
  533. (define-key text-mode-map (kbd "C-m") 'newline)))
  534. (add-to-list 'Info-default-directory-list (expand-file-name "~/.info/emacs-ja"))
  535. (setq bookmark-default-file "~/.emacs.d/bmk")
  536. (add-hook 'apropos-mode-hook
  537. (lambda ()
  538. (define-key apropos-mode-map "n" 'next-line)
  539. (define-key apropos-mode-map "p" 'previous-line)
  540. ))
  541. (define-key minibuffer-local-map (kbd "C-u") (lambda () (interactive) (delete-region (point-at-bol) (point))))
  542. (add-hook 'isearch-mode-hook
  543. (lambda ()
  544. ;; (define-key isearch-mode-map (kbd "C-j") 'isearch-other-control-char)
  545. ;; (define-key isearch-mode-map (kbd "C-k") 'isearch-other-control-char)
  546. ;; (define-key isearch-mode-map (kbd "C-h") 'isearch-other-control-char)
  547. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  548. (define-key isearch-mode-map (kbd "M-r") 'isearch-query-replace-regexp)))
  549. (add-hook 'outline-mode-hook
  550. (lambda ()
  551. (if (string-match "\\.md\\'" buffer-file-name)
  552. (set (make-local-variable 'outline-regexp) "#+ "))))
  553. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  554. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  555. (setq markdown-command (or (executable-find "markdown")
  556. (executable-find "markdown.pl")))
  557. (when (dllib-if-unfound "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  558. t)
  559. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'markdown-mode))
  560. (autoload 'markdown-mode "markdown-mode" "Major mode for editing Markdown files." nil)
  561. (add-hook 'markdown-mode-hook
  562. (lambda ()
  563. (outline-minor-mode 1)
  564. (set (make-local-variable 'comment-start) ";"))))
  565. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  566. ;; c-mode
  567. ;; (setq c-default-style "bsd")
  568. ;; BackSpace キーを「賢く」し,インデント幅は2桁,タブはスペースに展開
  569. (add-hook 'c-mode-common-hook
  570. (lambda ()
  571. (setq c-basic-offset 2
  572. indent-tabs-mode nil)
  573. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  574. (c-toggle-hungry-state 1)
  575. ))
  576. (when (dllib-if-unfound "https://raw.github.com/mooz/js2-mode/master/js2-mode.el"
  577. t)
  578. (autoload 'js2-mode "js2-mode" nil t)
  579. (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  580. (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode)))
  581. ;; (add-hook 'js2-mode-hook
  582. ;; (lambda ()
  583. ;; (add-hook 'before-save-hook
  584. ;; 'my-indent-buffer
  585. ;; nil
  586. ;; t)))
  587. (add-hook 'js2-mode-hook
  588. (lambda ()
  589. (define-key js2-mode-map (kbd "C-m") (lambda ()
  590. (interactive)
  591. (js2-enter-key)
  592. (indent-for-tab-command)))
  593. (add-hook (kill-local-variable 'before-save-hook)
  594. 'js2-before-save)))
  595. (and nil
  596. (require 'zone nil t)
  597. (not (eq system-type 'windows-nt))
  598. ;; (zone-when-idle 180)
  599. (run-with-idle-timer 180 t (lambda ()
  600. (unless (memq major-mode
  601. '(term-mode))
  602. (zone)))))
  603. (when (require 'uniquify nil t)
  604. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  605. (setq uniquify-ignore-buffers-re "*[^*]+*")
  606. (setq uniquify-min-dir-content 1))
  607. (add-hook 'view-mode-hook
  608. (lambda()
  609. (define-key view-mode-map "j" (lambda() (interactive) (scroll-up 1)))
  610. (define-key view-mode-map "k" (lambda() (interactive) (scroll-down 1)))
  611. (define-key view-mode-map "v" 'toggle-read-only)
  612. (define-key view-mode-map "q" 'bury-buffer)
  613. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  614. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  615. ;; (define-key view-mode-map "n" 'nonincremental-repeat-search-forward)
  616. ;; (define-key view-mode-map "N" 'nonincremental-repeat-search-backward)
  617. (define-key view-mode-map "/" 'isearch-forward-regexp)
  618. (define-key view-mode-map "?" 'isearch-backward-regexp)
  619. (define-key view-mode-map "n" 'isearch-repeat-forward)
  620. (define-key view-mode-map "N" 'isearch-repeat-backward)
  621. ))
  622. (global-set-key "\M-r" 'view-mode)
  623. (setq view-read-only t)
  624. (add-hook 'Man-mode-hook
  625. (lambda ()
  626. (view-mode 1)
  627. (setq truncate-lines nil)))
  628. (setq Man-notify-method (if window-system
  629. 'newframe
  630. 'pushy))
  631. (require 'session nil t)
  632. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  633. t)
  634. (require 'gtkbm nil t)
  635. (global-set-key (kbd "C-x C-d") 'gtkbm))
  636. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/git-command.el"
  637. t)
  638. (require 'git-command nil t)
  639. (define-key ctl-x-map "g" 'git-command))
  640. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  641. ;; term mode
  642. ;; (setq multi-term-program shell-file-name)
  643. (and (dllib-if-unfound "http://www.emacswiki.org/emacs/download/multi-term.el"
  644. t)
  645. (require 'multi-term nil t)
  646. (setq multi-term-switch-after-close nil))
  647. (defun my-term-quit-or-send-raw ()
  648. ""
  649. (interactive)
  650. (if (get-buffer-process (current-buffer))
  651. (call-interactively 'term-send-raw)
  652. (kill-buffer)))
  653. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  654. ;; (setq term-ansi-default-program shell-file-name)
  655. (add-hook 'term-setup-hook
  656. (lambda ()
  657. (setq term-display-table (make-display-table))))
  658. (add-hook 'term-mode-hook
  659. (lambda ()
  660. (unless (memq (current-buffer) (and (featurep 'multi-term) ; current buffer is not multi-term buffer
  661. (multi-term-list)))
  662. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  663. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  664. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  665. ;; (define-key term-raw-map "\C-f" 'forward-char)
  666. ;; (define-key term-raw-map "\C-b" 'backward-char)
  667. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  668. (define-key term-raw-map "\C-x" (lookup-key (current-global-map) "\C-x"))
  669. (define-key term-raw-map "\C-z" (lookup-key (current-global-map) "\C-z")))
  670. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  671. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  672. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  673. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  674. (define-key term-raw-map [delete] 'term-send-raw)
  675. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  676. (define-key term-raw-map "\C-y" 'term-paste)
  677. (define-key term-raw-map "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  678. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  679. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  680. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  681. ;; (define-key term-raw-map "\C-d" 'delete-char)
  682. (set (make-local-variable 'scroll-margin) 0)
  683. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  684. ;; (cua-mode 0)
  685. ;; (and cua-mode
  686. ;; (local-unset-key (kbd "C-c")))
  687. ;; (define-key cua--prefix-override-keymap "\C-c" 'term-interrupt-subjob)
  688. (set (make-local-variable 'hl-line-range-function)
  689. (lambda ()
  690. '(0 . 0)))
  691. ))
  692. ;; (add-hook 'term-exec-hook 'forward-char)
  693. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  694. ;; buffer switching
  695. (when (require 'bs nil t)
  696. ;; (global-set-key "\C-x\C-b" 'bs-show)
  697. (defalias 'list-buffers 'bs-show))
  698. ;; (add-to-list 'bs-configurations '("processes" nil get-buffer-process ".*" nil nil))
  699. (add-to-list 'bs-configurations '("same-dir" nil buffer-same-dir-p ".*" nil nil))
  700. (add-to-list 'bs-configurations '("this-frame" nil (lambda (buf) (memq buf (my-frame-buffer-get))) ".*" nil nil))
  701. ;; (setq bs-configurations (list '("processes" nil get-buffer-process ".*" nil nil)
  702. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)))
  703. (setq bs-default-configuration "this-frame")
  704. (setq bs-default-sort-name "by name")
  705. (add-hook 'bs-mode-hook
  706. (lambda ()
  707. (setq bs-default-configuration "this-frame")
  708. ;; (and bs--show-all
  709. ;; (call-interactively 'bs-toggle-show-all))
  710. (set (make-local-variable 'scroll-margin) 0)
  711. ))
  712. (defun buffer-same-dir-p (bf)
  713. "return t if BF's dir is same as current dir, otherwise nil."
  714. (let ((cdir (expand-file-name default-directory)))
  715. (with-current-buffer bf
  716. (equal (expand-file-name default-directory) cdir))))
  717. (iswitchb-mode 1)
  718. (defun iswitchb-buffer-display-other-window ()
  719. ""
  720. (interactive)
  721. (let ((iswitchb-default-method 'display))
  722. (call-interactively 'iswitchb-buffer)))
  723. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  724. ;; sdic
  725. (defun sdic-describe-word-at-point-echo ()
  726. ""
  727. (interactive)
  728. (save-window-excursion
  729. (sdic-describe-word-at-point))
  730. (save-excursion
  731. (set-buffer sdic-buffer-name)
  732. (message (buffer-substring (point-min)
  733. (progn (goto-char (point-min))
  734. (or (and (re-search-forward "^\\w" nil t 4)
  735. (progn (previous-line) t)
  736. (point-at-eol))
  737. (point-max)))))))
  738. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  739. (setq sdic-waei-dictionary-list '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  740. (setq sdic-disable-select-window t)
  741. (setq sdic-window-height 7)
  742. (when (require 'sdic nil t)
  743. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  744. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo))
  745. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  746. ;; vc
  747. ;; (require 'vc)
  748. (setq vc-handled-backends '())
  749. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  750. ;; gauche-mode
  751. (let ((s (executable-find "gosh")))
  752. (setq scheme-program-name s
  753. gauche-program-name s))
  754. (defun run-gauche-other-window ()
  755. "Run gauche on other window"
  756. (interactive)
  757. (switch-to-buffer-other-window
  758. (get-buffer-create "*scheme*"))
  759. (run-gauche))
  760. (defun run-gauche ()
  761. "run gauche"
  762. (run-scheme gauche-program-name)
  763. )
  764. (defun scheme-send-buffer ()
  765. ""
  766. (interactive)
  767. (scheme-send-region (point-min) (point-max))
  768. (my-scheme-display-scheme-buffer)
  769. )
  770. (defun my-scheme-display-scheme-buffer ()
  771. ""
  772. (interactive)
  773. (set-window-text-height (display-buffer scheme-buffer
  774. t)
  775. 7))
  776. (add-hook 'scheme-mode-hook
  777. (lambda ()
  778. nil))
  779. (add-hook 'inferior-scheme-mode-hook
  780. (lambda ()
  781. ;; (my-scheme-display-scheme-buffer)
  782. ))
  783. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  784. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  785. (when (dllib-if-unfound "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  786. t)
  787. (setq auto-mode-alist
  788. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  789. (setq auto-mode-alist
  790. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  791. (autoload 'gauche-mode "gauche-mode" "Major mode for Scheme." t)
  792. (autoload 'run-scheme "gauche-mode" "Run an inferior Scheme process." t)
  793. (add-hook 'gauche-mode-hook
  794. (lambda ()
  795. (define-key gauche-mode-map (kbd "C-c C-z") 'run-gauche-other-window)
  796. (define-key scheme-mode-map (kbd "C-c C-c") 'scheme-send-buffer)
  797. (define-key scheme-mode-map (kbd "C-c C-b") 'my-scheme-display-scheme-buffer)
  798. )))
  799. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  800. ;; recentf-mode
  801. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  802. recentf-max-menu-items 20
  803. recentf-max-saved-items 30
  804. recentf-show-file-shortcuts-flag nil)
  805. (when (require 'recentf nil t)
  806. ;; (global-set-key "\C-x\C-r" 'recentf-open-files)
  807. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  808. ;; (add-hook 'find-file-hook
  809. ;; (lambda ()
  810. ;; (recentf-add-file default-directory)))
  811. (recentf-mode 1)
  812. ;; (add-to-list 'recentf-filename-handlers 'abbreviate-file-name)
  813. (add-to-list 'recentf-exclude (regexp-quote recentf-save-file))
  814. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  815. t)
  816. (require 'recentf-show nil t)
  817. (define-key ctl-x-map (kbd "C-r") 'recentf-show)))
  818. (add-hook 'recentf-dialog-mode-hook
  819. (lambda ()
  820. (recentf-save-list)
  821. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f") 'my-recentf-cd-and-find-file)
  822. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  823. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  824. (define-key recentf-dialog-mode-map "p" 'previous-line)
  825. (define-key recentf-dialog-mode-map "n" 'next-line)
  826. (cd "~/")))
  827. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  828. ;; dired
  829. (require 'dired)
  830. (defun my-dired-echo-file-head (arg)
  831. ""
  832. (interactive "P")
  833. (let ((f (dired-get-filename)))
  834. (message "%s"
  835. (with-temp-buffer
  836. (insert-file-contents f)
  837. (buffer-substring-no-properties (point-min)
  838. (progn (goto-line (if arg
  839. (prefix-numeric-value arg)
  840. 10))
  841. (point-at-eol)))))))
  842. (defun my-dired-diff ()
  843. ""
  844. (interactive)
  845. (let ((files (dired-get-marked-files nil nil nil t)))
  846. (if (eq (car files)
  847. t)
  848. (diff (cadr files) (dired-get-filename))
  849. (message "One files must be marked!"))))
  850. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  851. "pop up buffer using `display-buffer' and return that buffer."
  852. (let ((bf (get-buffer-create buffer-or-name)))
  853. (with-current-buffer bf
  854. (cd ".")
  855. (erase-buffer))
  856. (display-buffer bf)
  857. bf))
  858. (defun my-replace-nasi-none ()
  859. ""
  860. (save-excursion
  861. (let ((buffer-read-only nil))
  862. (goto-char (point-min))
  863. (while (search-forward "なし" nil t)
  864. (replace-match "none")))))
  865. (defun dired-get-file-info ()
  866. "dired get disk usage"
  867. (interactive)
  868. (let ((f (dired-get-filename)))
  869. (if (file-directory-p f)
  870. (progn
  871. (message "calculating du...")
  872. (shell-command (concat "du -hsD "
  873. (shell-quote-argument f))))
  874. (shell-command (concat "file "
  875. f)))))
  876. (defun my-dired-scroll-up ()
  877. ""
  878. (interactive)
  879. (my-dired-previous-line (- (window-height) 1)))
  880. (defun my-dired-scroll-down ()
  881. ""
  882. (interactive)
  883. (my-dired-next-line (- (window-height) 1)))
  884. (defun my-dired-previous-line (arg)
  885. ""
  886. (interactive "p")
  887. (when (> arg 0)
  888. ;; (ignore 'my-dired-print-current-dir-and-file)
  889. (dired-previous-line 1)
  890. (when (eq (line-number-at-pos)
  891. 2)
  892. (goto-char (point-max))
  893. (forward-line -1)
  894. (dired-move-to-filename))
  895. (my-dired-previous-line (- arg 1))
  896. ))
  897. (defun my-dired-next-line (arg)
  898. ""
  899. (interactive "p")
  900. (when (> arg 0)
  901. ;; (ignore 'my-dired-print-current-dir-and-file)
  902. (dired-next-line 1)
  903. (when (eq (point)
  904. (point-max))
  905. (goto-char (point-min))
  906. (forward-line 2)
  907. (dired-move-to-filename))
  908. (my-dired-next-line (- arg 1))
  909. ))
  910. (defun my-dired-print-current-dir-and-file ()
  911. (message "%s %s"
  912. default-directory
  913. (buffer-substring-no-properties (point-at-bol)
  914. (point-at-eol))))
  915. (defun dired-do-execute-as-command ()
  916. ""
  917. (interactive)
  918. (let ((file (dired-get-filename t)))
  919. (if (file-executable-p file)
  920. (start-process file nil file)
  921. (when (y-or-n-p "this file cant be executed. mark as executable and go? : ")
  922. (set-file-modes file (file-modes-symbolic-to-number "u+x" (file-modes file)))
  923. (start-process file nil file)))))
  924. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  925. (defun my-dired-x-open ()
  926. ""
  927. (interactive)
  928. (my-x-open (dired-get-filename t t)))
  929. (if (eq window-system 'mac)
  930. (setq dired-listing-switches "-lhFG")
  931. (setq dired-listing-switches "-lhFG --time-style=long-iso")
  932. )
  933. (setq dired-listing-switches "-lhFG")
  934. (put 'dired-find-alternate-file 'disabled nil) ; when using dired-find-alternate-file reuse current dired buffer for the file to open
  935. (setq dired-ls-F-marks-symlinks t)
  936. (require 'ls-lisp)
  937. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  938. (setq ls-lisp-dirs-first t)
  939. (setq ls-lisp-use-localized-time-format t)
  940. (setq ls-lisp-format-time-list
  941. '("%Y-%m-%d %H:%M"
  942. "%Y-%m-%d "))
  943. (setq dired-dwim-target t)
  944. ;; (add-hook 'dired-after-readin-hook
  945. ;; 'my-replace-nasi-none)
  946. ;; (add-hook 'after-init-hook
  947. ;; (lambda ()
  948. ;; (dired ".")))
  949. (add-hook 'dired-mode-hook
  950. (lambda ()
  951. (define-key dired-mode-map "o" 'my-dired-x-open)
  952. (define-key dired-mode-map "i" 'dired-get-file-info)
  953. (define-key dired-mode-map "f" 'find-file)
  954. (define-key dired-mode-map "!" 'shell-command)
  955. (define-key dired-mode-map "&" 'async-shell-command)
  956. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  957. (define-key dired-mode-map "=" 'my-dired-diff)
  958. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  959. (define-key dired-mode-map "b" 'gtkbm)
  960. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  961. (define-key dired-mode-map "@" (lambda () (interactive) (my-x-open ".")))
  962. (define-key dired-mode-map (kbd "TAB") 'other-window)
  963. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  964. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  965. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  966. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  967. (substitute-key-definition 'dired-next-line 'my-dired-next-line dired-mode-map)
  968. (substitute-key-definition 'dired-previous-line 'my-dired-previous-line dired-mode-map)
  969. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  970. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  971. (let ((file "._Icon\015"))
  972. (when (file-readable-p file)
  973. (delete-file file)))))
  974. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  975. t)
  976. (require 'pack nil t)
  977. (add-hook 'dired-mode-hook
  978. (lambda ()
  979. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  980. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  981. t)
  982. (require 'dired-list-all-mode nil t)
  983. (setq dired-listing-switches "-lhFG")
  984. (add-hook 'dired-mode-hook
  985. (lambda ()
  986. (define-key dired-mode-map "a" 'dired-list-all-mode)
  987. )))
  988. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  989. (defun my-dired-toggle-mark()
  990. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  991. (t dired-marker-char))))
  992. (delete-char 1)
  993. (insert cur)))
  994. (defun my-dired-mark (arg)
  995. "toggle mark the current (or next ARG) files.
  996. If on a subdir headerline, mark all its files except `.' and `..'.
  997. Use \\[dired-unmark-all-files] to remove all marks
  998. and \\[dired-unmark] on a subdir to remove the marks in
  999. this subdir."
  1000. (interactive "P")
  1001. (if (dired-get-subdir)
  1002. (save-excursion (dired-mark-subdir-files))
  1003. (let ((inhibit-read-only t))
  1004. (dired-repeat-over-lines
  1005. (prefix-numeric-value arg)
  1006. 'my-dired-toggle-mark))))
  1007. (defun my-dired-mark-backward (arg)
  1008. "In Dired, move up lines and toggle mark there.
  1009. Optional prefix ARG says how many lines to unflag; default is one line."
  1010. (interactive "p")
  1011. (my-dired-mark (- arg)))
  1012. (add-hook 'dired-mode-hook
  1013. (lambda ()
  1014. (local-set-key (kbd "SPC") 'my-dired-mark)
  1015. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1016. )
  1017. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1018. ;; eshell
  1019. (defun my-eshell-backward-delete-char ()
  1020. (interactive)
  1021. (when (< (save-excursion
  1022. (eshell-bol)
  1023. (point))
  1024. (point))
  1025. (backward-delete-char 1)))
  1026. (defun my-file-owner-p (file)
  1027. "t if FILE is owned by me."
  1028. (eq (user-uid) (nth 2 (file-attributes file))))
  1029. ;; ;; http://www.bookshelf.jp/pukiwiki/pukiwiki.php?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9
  1030. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1031. ;; (defun eshell/less (&rest args)
  1032. ;; "Invoke `view-file' on the file.
  1033. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1034. ;; (if args
  1035. ;; (while args
  1036. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1037. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1038. ;; (file (pop args)))
  1039. ;; (view-file file)
  1040. ;; (goto-line line))
  1041. ;; (view-file (pop args))))))
  1042. (defun eshell/o (&optional file)
  1043. (my-x-open (or file ".")))
  1044. ;; (defun eshell/vi (&rest args)
  1045. ;; "Invoke `find-file' on the file.
  1046. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1047. ;; (while args
  1048. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1049. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1050. ;; (file (pop args)))
  1051. ;; (find-file file)
  1052. ;; (goto-line line))
  1053. ;; (find-file (pop args)))))
  1054. (defun eshell/clear ()
  1055. "Clear the current buffer, leaving one prompt at the top."
  1056. (let ((inhibit-read-only t))
  1057. (erase-buffer)))
  1058. (defun eshell/d (&optional dirname switches)
  1059. "if first arg is omitted open current directory."
  1060. (dired (or dirname ".") switches))
  1061. (defun eshell/v ()
  1062. (view-mode 1))
  1063. (defun eshell/git (&rest args)
  1064. ""
  1065. (if (member (car args)
  1066. '("di" "diff" "log" "show"))
  1067. (apply 'eshell-exec-visual "git" args)
  1068. (shell-command (mapconcat 'shell-quote-argument
  1069. `("git" ,@args)
  1070. " ")
  1071. t)
  1072. ;; (eshell-external-command "git" args)
  1073. ))
  1074. (defalias 'eshell/: 'ignore)
  1075. (defalias 'eshell/type 'eshell/which)
  1076. ;; (defalias 'eshell/vim 'eshell/vi)
  1077. (defalias 'eshell/ff 'find-file)
  1078. (defalias 'eshell/q 'eshell/exit)
  1079. (defun eshell-goto-prompt ()
  1080. ""
  1081. (interactive)
  1082. (goto-char (point-max)))
  1083. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1084. "open eshell and change wd
  1085. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1086. (interactive)
  1087. (let ((dir (expand-file-name default-directory)))
  1088. (switch-to-buffer (or eshell-buffer-or-name
  1089. (eshell t)))
  1090. (unless (equal dir (expand-file-name default-directory))
  1091. ;; (cd dir)
  1092. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1093. ;; (eshell-emit-prompt)
  1094. (goto-char (point-max))
  1095. (eshell-kill-input)
  1096. (insert "cd " dir)
  1097. (eshell-send-input))))
  1098. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1099. (setq eshell-term-name "eterm-color")
  1100. (setq eshell-scroll-to-bottom-on-input t)
  1101. (setq eshell-cmpl-ignore-case t)
  1102. (setq eshell-cmpl-cycle-completions nil)
  1103. (setq eshell-highlight-prompt nil)
  1104. (setq eshell-ls-initial-args '("-hCFG"
  1105. "--color=auto"
  1106. "--time-style=long-iso")) ; "-hF")
  1107. (setq eshell-prompt-function
  1108. (lambda ()
  1109. (with-temp-buffer
  1110. (let (p1 p2 p3 p4)
  1111. (insert " [")
  1112. (setq p1 (point))
  1113. (insert (abbreviate-file-name default-directory))
  1114. (setq p2 (point))
  1115. (insert "]"
  1116. "\n")
  1117. (setq p3 (point))
  1118. (insert user-login-name
  1119. "@"
  1120. (or (getenv "HOSTNAME")
  1121. (substring (shell-command-to-string (or (executable-find "hostname")
  1122. "echo ''"))
  1123. 0
  1124. -1)))
  1125. (setq p4 (point))
  1126. (insert " "
  1127. (format-time-string "%a, %d %b %Y %T %z")
  1128. " eshell\n"
  1129. "last:"
  1130. (number-to-string eshell-last-command-status)
  1131. (if (= (user-uid)
  1132. 0)
  1133. " # "
  1134. " $ "))
  1135. (add-text-properties p1
  1136. p2
  1137. '(face ((foreground-color . "yellow"))))
  1138. (add-text-properties p3
  1139. p4
  1140. '(face ((foreground-color . "cyan"))))
  1141. (buffer-substring (point-min)
  1142. (point-max))))))
  1143. (add-hook 'eshell-mode-hook
  1144. (lambda ()
  1145. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1146. ;; (interactive)
  1147. ;; (switch-to-buffer (other-buffer))))
  1148. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1149. (interactive)
  1150. (eshell-goto-prompt)
  1151. (eshell-kill-input)))
  1152. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1153. (interactive)
  1154. (eshell-goto-prompt)
  1155. (my-keyboard-quit)))
  1156. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1157. (define-key eshell-mode-map (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1158. (define-key eshell-mode-map (kbd "C-n") 'eshell-next-matching-input-from-input)
  1159. (apply 'eshell/addpath exec-path)
  1160. (set (make-local-variable 'scroll-margin) 0)
  1161. ;; (eshell/export "GIT_PAGER=")
  1162. ;; (eshell/export "GIT_EDITOR=")
  1163. (eshell/export "LC_MESSAGES=C")
  1164. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1165. (set (make-local-variable 'hl-line-range-function)
  1166. (lambda ()
  1167. '(0 . 0)))
  1168. (add-to-list 'eshell-virtual-targets
  1169. '("/dev/less"
  1170. (lambda (str)
  1171. (if str
  1172. (with-current-buffer nil)))
  1173. nil))
  1174. ))
  1175. (add-hook 'eshell-mode-hook
  1176. (lambda ()
  1177. (add-to-list 'eshell-visual-commands "vim")
  1178. ;; (add-to-list 'eshell-visual-commands "git")
  1179. (add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer)
  1180. (mapcar (lambda (alias)
  1181. (add-to-list 'eshell-command-aliases-list
  1182. alias))
  1183. '(
  1184. ; ("ll" "ls -l $*")
  1185. ; ("la" "ls -a $*")
  1186. ; ("lla" "ls -al $*")
  1187. ("ut" "slogin 03110414@un001.ecc.u-tokyo.ac.jp $*")
  1188. ("aptin" "apt-get install $*")
  1189. ("eless" "cat >>> (with-current-buffer (get-buffer-create \"*eshell output\") (erase-buffer) (setq buffer-read-only nil) (current-buffer)); (view-buffer (get-buffer \"*eshell output*\"))")
  1190. ("g" "git $*")
  1191. ))
  1192. ))
  1193. ;; (eval-after-load "em-alias"
  1194. ;; '(progn ;; (eshell/alias "ll" "ls -l")
  1195. ;; ;; (eshell/alias "la" "ls -a")
  1196. ;; ;; (eshell/alias "lla" "ls -al")
  1197. ;; (eshell/alias "sgcc" (if (eq system-type 'windows-nt)
  1198. ;; "gcc -o win.$1.exe $1"
  1199. ;; "gcc -o ${uname}.$1.out $1"))
  1200. ;; (eshell/alias "slmgcc" (if (eq system-type 'windows-nt)
  1201. ;; "gcc -lm -o win.$1.exe $1"
  1202. ;; "gcc -lm -o ${uname}.$1.out $1"))
  1203. ;; ;; (eshell/alias "ut" "ssh g841105@un001.ecc.u-tokyo.ac.jp")
  1204. ;; (add-to-list 'recentf-exclude (concat eshell-directory-name "alias"))))
  1205. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1206. ;; get last modified date
  1207. (defvar my-buffer-file-last-modified-time nil "")
  1208. (make-variable-buffer-local 'my-buffer-file-last-modified-time)
  1209. (defun my-get-file-last-modified-time (file)
  1210. ""
  1211. (nth 5
  1212. (file-attributes file)))
  1213. (defun my-set-buffer-file-last-modified-time ()
  1214. ""
  1215. (make-local-variable 'my-buffer-file-last-modified-time)
  1216. (setq my-buffer-file-last-modified-time
  1217. (format-time-string "%Y/%m/%d %H:%M" (my-get-file-last-modified-time buffer-file-name))))
  1218. (add-hook 'find-file-hook
  1219. 'my-set-buffer-file-last-modified-time)
  1220. (add-hook 'after-save-hook
  1221. 'my-set-buffer-file-last-modified-time)
  1222. (add-hook 'after-revert-hook
  1223. 'my-set-buffer-file-last-modified-time)
  1224. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1225. ;; frame buffer
  1226. ;; todo: work well when opening the file that was already opened on another window
  1227. (add-hook 'after-make-frame-functions
  1228. (lambda (f)
  1229. (set-window-buffer (frame-selected-window f)
  1230. "*Messages*")))
  1231. (defun make-frame-command-with-name (name)
  1232. "Make frame with name specified."
  1233. (interactive "sName for new frame: ")
  1234. (set-frame-parameter (make-frame-command)
  1235. 'name
  1236. name))
  1237. (defvar my-frame-buffer-plist nil)
  1238. (defun my-frame-buffer-add (&optional buf frame)
  1239. ""
  1240. (setq my-frame-buffer-plist
  1241. (plist-put my-frame-buffer-plist
  1242. (or frame
  1243. (selected-frame))
  1244. (let ((lst (my-frame-buffer-get frame)))
  1245. (if lst
  1246. (add-to-list 'lst
  1247. (or buf
  1248. (current-buffer)))
  1249. (list (or buf
  1250. (current-buffer))))))))
  1251. (defun my-frame-buffer-remove (&optional buf frame)
  1252. ""
  1253. (setq my-frame-buffer-plist
  1254. (plist-put my-frame-buffer-plist
  1255. (or frame
  1256. (selected-frame))
  1257. (delq (or buf
  1258. (current-buffer))
  1259. (my-frame-buffer-get frame)))))
  1260. (defun my-frame-buffer-get (&optional frame)
  1261. ""
  1262. (plist-get my-frame-buffer-plist
  1263. (or frame
  1264. (selected-frame))))
  1265. (defun my-frame-buffer-kill-all-buffer (&optional frame)
  1266. ""
  1267. (mapcar 'kill-buffer
  1268. (my-frame-buffer-get frame)))
  1269. (add-hook 'find-file-hook
  1270. 'my-frame-buffer-add)
  1271. (add-hook 'term-mode-hook
  1272. 'my-frame-buffer-add)
  1273. (add-hook 'eshell-mode-hook
  1274. 'my-frame-buffer-add)
  1275. (add-hook 'Man-mode-hook
  1276. 'my-frame-buffer-add)
  1277. (add-hook 'kill-buffer-hook
  1278. 'my-frame-buffer-remove)
  1279. (add-hook 'delete-frame-functions
  1280. 'my-frame-buffer-kill-all-buffer)
  1281. (defvar my-desktop-terminal "roxterm")
  1282. (defun my-execute-terminal ()
  1283. ""
  1284. (interactive)
  1285. (if (and (or (eq system-type 'windows-nt)
  1286. window-system)
  1287. my-desktop-terminal
  1288. )
  1289. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1290. (start-process "terminal"
  1291. nil
  1292. my-desktop-terminal))
  1293. (my-term)))
  1294. (defun my-term ()
  1295. "open terminal buffer and return that buffer."
  1296. (interactive)
  1297. (if (eq system-type 'windows-nt)
  1298. (eshell t)
  1299. (if (featurep 'multi-term)
  1300. (multi-term)
  1301. (ansi-term "/bin/bash"))))
  1302. (defun my-delete-frame-or-kill-emacs ()
  1303. "delete frame when opening multiple frame, kill emacs when only one."
  1304. (interactive)
  1305. (if (eq 1
  1306. (length (frame-list)))
  1307. (save-buffers-kill-emacs)
  1308. (delete-frame)))
  1309. (define-key my-prefix-map (kbd "C-s") 'my-execute-terminal)
  1310. (define-key my-prefix-map (kbd "C-f") 'make-frame-command-with-name)
  1311. (global-set-key (kbd "C-x C-c") 'my-delete-frame-or-kill-emacs)
  1312. (define-key my-prefix-map (kbd "C-x C-c") 'save-buffers-kill-emacs)
  1313. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1314. ;; auto saving
  1315. (defun my-save-this-buffer (silent-p)
  1316. "save current buffer if can without asking"
  1317. (let ((cm (if (current-message)
  1318. (format "%s\n" (current-message))
  1319. ""))
  1320. (fun (symbol-function (if silent-p
  1321. 'ignore
  1322. 'message))))
  1323. (cond ((active-minibuffer-window) nil)
  1324. ((not buffer-file-name) (funcall fun "%ssaving... this buffer doesn't visit any file." cm) nil)
  1325. ((not (file-exists-p buffer-file-name)) (funcall fun "%ssaving... file not exist. save manually first." com) nil)
  1326. (buffer-read-only (funcall fun "%ssaving... this buffer is read-only." cm) nil)
  1327. ((not (buffer-modified-p)) (funcal fun "%ssaving... not modified yet." cm) nil)
  1328. ((not (file-writable-p buffer-file-name)) (funcall fun "%ssaving... you cannot change this file." cm) nil)
  1329. (t (funcall fun "%ssaving..." cm)
  1330. (save-buffer)
  1331. (funcall fun "%ssaving... done." cm)))))
  1332. ;; (if (and buffer-file-name
  1333. ;; (not buffer-read-only)
  1334. ;; (buffer-modified-p)
  1335. ;; (file-writable-p buffer-file-name))
  1336. ;; (save-buffer))) ; silent one
  1337. (defvar my-auto-save-this-buffer nil "auto save timer object")
  1338. (defun my-auto-save-this-buffer (sec &optional silent-p)
  1339. "auto save current buffer if idle for SEC.
  1340. when SEC is nil, stop auto save if enabled."
  1341. (if sec
  1342. (progn (when my-auto-save-this-buffer
  1343. (cancel-timer my-auto-save-this-buffer)
  1344. (setq my-auto-save-this-buffer nil))
  1345. (setq my-auto-save-this-buffer (run-with-idle-timer sec t 'my-save-this-buffer silent-p)))
  1346. (when my-auto-save-this-buffer
  1347. (cancel-timer my-auto-save-this-buffer)
  1348. (setq my-auto-save-this-buffer nil))))
  1349. (my-auto-save-this-buffer 2 t)
  1350. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1351. ;; misc funcs
  1352. (defun dir-show (&optional dir)
  1353. (interactive)
  1354. (let ((bf (get-buffer-create "*dir show*"))
  1355. (list-directory-brief-switches "-C"))
  1356. (with-current-buffer bf
  1357. (list-directory (or nil
  1358. default-directory)
  1359. nil))
  1360. ))
  1361. (defalias 'qcalc 'quick-calc)
  1362. (defun my-kill-buffers ()
  1363. ""
  1364. (interactive)
  1365. (mapcar (lambda (buf)
  1366. (when (buffer-file-name buf)
  1367. (kill-buffer buf)))
  1368. (buffer-list)))
  1369. (defvar my-filer nil)
  1370. (setq my-filer (or (executable-find "pcmanfm")
  1371. (executable-find "nautilus")))
  1372. (defun my-x-open (file)
  1373. "open file."
  1374. (interactive "FOpen File: ")
  1375. (setq file (expand-file-name file))
  1376. (message "Opening %s..." file)
  1377. (cond ((eq system-type 'windows-nt)
  1378. (call-process "cmd.exe" nil 0 nil "/c" "start" "" (convert-standard-filename file)))
  1379. ((eq system-type 'darwin)
  1380. (call-process "open" nil 0 nil file))
  1381. ((getenv "DISPLAY")
  1382. (call-process (or my-filer "xdg-open") nil 0 nil file))
  1383. (t
  1384. (find-file file))
  1385. )
  1386. ;; (recentf-add-file file)
  1387. (message "Opening %s...done" file))
  1388. (defun my-keyboard-quit ()
  1389. ""
  1390. (interactive)
  1391. (run-hooks 'before-keyboard-quit-hook)
  1392. ;; (redisplay t)
  1393. (redraw-display)
  1394. ;; (run-hooks 'window-configuration-change-hook)
  1395. (my-revert-buffer-if-needed)
  1396. ;; (revert-buffer t t)
  1397. (keyboard-quit)
  1398. (insert "insert me")
  1399. (run-hooks 'after-keyboard-quit-hook))
  1400. (substitute-key-definition 'keyboard-quit 'my-keyboard-quit global-map)
  1401. ;; (global-set-key (kbd "C-g") 'my-keyboard-quit)
  1402. (defun my-convmv-sjis2utf8-test ()
  1403. "run `convmv -r -f sjis -t utf8 *'
  1404. this is test, does not rename files"
  1405. (interactive)
  1406. (shell-command "convmv -r -f sjis -t utf8 *"))
  1407. (defun my-convmv-sjis2utf8-notest ()
  1408. "run `convmv -r -f sjis -t utf8 * --notest'"
  1409. (interactive)
  1410. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1411. (defun my-copy-whole-line ()
  1412. ""
  1413. (interactive)
  1414. (kill-new (concat (buffer-substring (point-at-bol)
  1415. (point-at-eol))
  1416. "\n")))
  1417. (defun kill-ring-save-buffer-file-name ()
  1418. "get current filename"
  1419. (interactive)
  1420. (let ((file buffer-file-name))
  1421. (if file
  1422. (progn (kill-new file)
  1423. (message file))
  1424. (message "not visiting file."))))
  1425. (defvar my-revert-buffer-if-needed-last-buffer nil)
  1426. (defun my-revert-buffer-if-needed ()
  1427. ""
  1428. (interactive)
  1429. (unless (eq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1430. (setq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1431. (when (or (and (eq major-mode 'dired-mode)
  1432. (dired-directory-changed-p default-directory))
  1433. (not (verify-visited-file-modtime (current-buffer))))
  1434. (revert-buffer t t)
  1435. (message "%s reverted." (buffer-name))
  1436. )))
  1437. (add-hook 'post-command-hook ; 'window-configuration-change-hook
  1438. 'my-revert-buffer-if-needed)
  1439. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1440. ;; forked from http://d.hatena.ne.jp/khiker/20100119/window_resize
  1441. (define-key my-prefix-map (kbd "C-w") 'my-window-organizer)
  1442. (defun my-window-organizer ()
  1443. "Control window size and position."
  1444. (interactive)
  1445. (save-selected-window
  1446. (select-window (window-at 0 0))
  1447. (let ( ;; (window-obj (selected-window))
  1448. ;; (current-width (window-width))
  1449. ;; (current-height (window-height))
  1450. action
  1451. c)
  1452. (catch 'end-flag
  1453. (while t
  1454. (setq action
  1455. (read-key-sequence-vector (format "size[%dx%d] 1: maximize; 2, 3: split; 0: delete; o: select other; j, l: enlarge; h, k: shrink; q: quit."
  1456. (window-width)
  1457. (window-height))))
  1458. (setq c (aref action 0))
  1459. (cond ((= c ?l)
  1460. (unless (eq (window-width) (frame-width))
  1461. (enlarge-window-horizontally 1)))
  1462. ((= c ?h)
  1463. (unless (eq (window-width) (frame-width))
  1464. (shrink-window-horizontally 1)))
  1465. ((= c ?j)
  1466. (enlarge-window 1))
  1467. ((= c ?k)
  1468. (shrink-window 1))
  1469. ((= c ?o)
  1470. (other-window 1))
  1471. ((memq c '(?d ?0))
  1472. (unless (eq (selected-window) (next-window (selected-window) 0 1))
  1473. (delete-window (selected-window))))
  1474. ((= c ?1)
  1475. (delete-other-windows))
  1476. ((= c ?2)
  1477. (split-window-vertically))
  1478. ((= c ?3)
  1479. (split-window-horizontally))
  1480. ((memq c '(?q ?\C-g))
  1481. (message "Quit")
  1482. (throw 'end-flag t))
  1483. (t
  1484. (beep))))))))
  1485. ;; (aref (read-key-sequence-vector "aa") 0)
  1486. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1487. ;; ;; emacsを殺伐とさせる
  1488. ;; ;; 補完させるとき失敗するからなし
  1489. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1490. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1491. ;; (setq arg
  1492. ;; (concat arg
  1493. ;; (if (eq nil (string-match "\\. *$" arg)) ".")
  1494. ;; " Stupid!")))
  1495. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1496. ;; japanese input method
  1497. (defun my-load-scim ()
  1498. "use scim-bridge.el as japanese im."
  1499. ;; Load scim-bridge.
  1500. (when (require 'scim-bridge nil t)
  1501. ;; Turn on scim-mode automatically after loading .emacs
  1502. (add-hook 'after-init-hook 'scim-mode-on)
  1503. (setq scim-cursor-color "red")
  1504. (scim-define-preedit-key ?\^h t)
  1505. (scim-define-common-key ?\* nil)
  1506. (scim-define-common-key ?\^/ nil)))
  1507. (defun my-load-anthy ()
  1508. "use anthy.el as japanese im."
  1509. ;; anthy
  1510. (when (require 'anthy nil t)
  1511. (global-set-key (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  1512. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  1513. (when (>= emacs-major-version 23)
  1514. (setq anthy-accept-timeout 1))))
  1515. ;; quail
  1516. ;; aproposs input-method for some information
  1517. ;; (setq default-input-method "japanese")
  1518. (defun my-load-mozc-el ()
  1519. ""
  1520. (setq mozc-leim-title "[MZ]")
  1521. (when (require 'mozc nil t)
  1522. (setq defauit-input-method "japanese-mozc")
  1523. ))
  1524. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1525. ;; for windows
  1526. ;; (add-to-list 'exec-path "c:/Program Files/Gauche/bin/")
  1527. (defun start-ckw-bash ()
  1528. ""
  1529. (interactive)
  1530. (start-process "ckw_bash"
  1531. nil
  1532. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe")) ; cじゃないといけないらしい
  1533. (defun my-w32-add-export-path (&rest args)
  1534. ""
  1535. (mapcar (lambda (path)
  1536. (add-to-list 'exec-path (expand-file-name path)))
  1537. (reverse args))
  1538. (setenv "PATH"
  1539. (mapconcat 'convert-standard-filename
  1540. exec-path
  1541. ";")))
  1542. (when (eq system-type 'windows-nt)
  1543. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  1544. ;; (setq python-python-command "c:/Python26/python.exe")
  1545. (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  1546. (my-w32-add-export-path "c:/Windows/system"
  1547. "c:/Windows/System32"
  1548. "c:/Program Files/Git/bin"
  1549. "c:/MinGW/bin"
  1550. "c:/MinGW/mingw32/bin"
  1551. (expand-file-name "~/bin")
  1552. (expand-file-name "~/dbx/apps/bin"))
  1553. (when window-system
  1554. (setq w32-enable-synthesized-fonts t))
  1555. (setq file-name-coding-system 'sjis))