rice

personal dot files and scripts for linux and macOS
Log | Files | Refs | README | LICENSE

core-defuns.el (7704B)


      1 ;;; core-defuns.el --- personal defuns
      2 
      3 ;; Author: Mark Feller <mark.feller@member.fsf.org>
      4 
      5 ;; This file is not part of GNU Emacs.
      6 
      7 ;; This file is free software; you can redistribute it and/or modify
      8 ;; it under the terms of the GNU General Public License as published by
      9 ;; the Free Software Foundation; either version 3, or (at your option)
     10 ;; any later version.
     11 
     12 ;; This file is distributed in the hope that it will be useful,
     13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 ;; GNU General Public License for more details.
     16 
     17 ;; You should have received a copy of the GNU General Public License
     18 ;; along with this file.  If not, see <http://www.gnu.org/licenses/>.
     19 
     20 ;;; Commentary:
     21 
     22 ;;; Code:
     23 
     24 (defun transpose-words (arg)
     25   "[Override for default transpose-words in simple.el]
     26               Interchange words around point, leaving point at end of
     27               them. With prefix arg ARG, effect is to take word before or
     28               around point and drag it backward past ARG other words (forward
     29               if ARG negative).  If ARG is zero, the words around or after
     30               point and around or after mark are interchanged."
     31   (interactive "*p")
     32   (if (eolp) (forward-char -1))
     33   (transpose-subr 'backward-word arg)
     34   (forward-word (+ arg 1)))
     35 
     36 (defun untabify-buffer ()
     37   (interactive)
     38   (untabify (point-min) (point-max)))
     39 
     40 (defun indent-buffer ()
     41   (interactive)
     42   (indent-region (point-min) (point-max)))
     43 
     44 (defun cleanup-buffer ()
     45   "Perform a bunch of operations on the whitespace content of a buffer.
     46 Including indent-buffer, which should not be called automatically on save."
     47   (interactive)
     48   (untabify-buffer)
     49   (delete-trailing-whitespace)
     50   (indent-buffer))
     51 
     52 (defun toggle-window-split ()
     53   "Toggle the current window split."
     54   (interactive)
     55   (if (= (count-windows) 2)
     56       (let* ((this-win-buffer (window-buffer))
     57              (next-win-buffer (window-buffer (next-window)))
     58              (this-win-edges (window-edges (selected-window)))
     59              (next-win-edges (window-edges (next-window)))
     60              (this-win-2nd (not (and (<= (car this-win-edges)
     61                                          (car next-win-edges))
     62                                      (<= (cadr this-win-edges)
     63                                          (cadr next-win-edges)))))
     64              (splitter
     65               (if (= (car this-win-edges)
     66                      (car (window-edges (next-window))))
     67                   'split-window-horizontally
     68                 'split-window-vertically)))
     69         (delete-other-windows)
     70         (let ((first-win (selected-window)))
     71           (funcall splitter)
     72           (if this-win-2nd (other-window 1))
     73           (set-window-buffer (selected-window) this-win-buffer)
     74           (set-window-buffer (next-window) next-win-buffer)
     75           (select-window first-win)
     76           (if this-win-2nd (other-window 1))))))
     77 
     78 (defun toggle-fullscreen ()
     79   "Toggle full screen"
     80   (interactive)
     81   (set-frame-parameter
     82    nil 'fullscreen
     83    (when (not (frame-parameter nil 'fullscreen)) 'fullboth)))
     84 
     85 (defun info-mode ()
     86   (interactive)
     87   (let ((file-name (buffer-file-name)))
     88     (kill-buffer (current-buffer))
     89     (info file-name)))
     90 (add-to-list 'auto-mode-alist '("\\.info\\'" . info-mode))
     91 
     92 (defadvice move-beginning-of-line (around smarter-bol activate)
     93   ;; Move to requested line if needed.
     94   (let ((arg (or (ad-get-arg 0) 1)))
     95     (when (/= arg 1)
     96       (forward-line (1- arg))))
     97   ;; Move to indentation on first call, then to actual BOL on second.
     98   (let ((pos (point)))
     99     (back-to-indentation)
    100     (when (= pos (point))
    101       ad-do-it)))
    102 
    103 ;; If the *scratch* buffer is killed, recreate it automatically
    104 ;; FROM: Morten Welind
    105 ;;http://www.geocrawler.com/archives/3/338/1994/6/0/1877802/
    106 (save-excursion
    107   (set-buffer (get-buffer-create "*scratch*"))
    108   (lisp-interaction-mode)
    109   (make-local-variable 'kill-buffer-query-functions)
    110   (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer))
    111 
    112 (defun kill-scratch-buffer ()
    113   ;; The next line is just in case someone calls this manually
    114   (set-buffer (get-buffer-create "*scratch*"))
    115   ;; Kill the current (*scratch*) buffer
    116   (remove-hook 'kill-buffer-query-functions 'kill-scratch-buffer)
    117   (kill-buffer (current-buffer))
    118   ;; Make a brand new *scratch* buffer
    119   (set-buffer (get-buffer-create "*scratch*"))
    120   (lisp-interaction-mode)
    121   (make-local-variable 'kill-buffer-query-functions)
    122   (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer)
    123   ;; Since we killed it, don't let caller do that.
    124   nil)
    125 
    126 (defun save-buffer-without-dtw ()
    127   (interactive)
    128   (let ((b (current-buffer)))   ; memorize the buffer
    129     (with-temp-buffer ; new temp buffer to bind the global value of before-save-hook
    130       (let ((before-save-hook (remove 'delete-trailing-whitespace before-save-hook)))
    131         (with-current-buffer b  ; go back to the current buffer, before-save-hook is now buffer-local
    132           (let ((before-save-hook (remove 'delete-trailing-whitespace before-save-hook)))
    133             (save-buffer)))))))
    134 
    135 (defun narrow-or-widen-dwim (p)
    136   "Widen if buffer is narrowed, narrow-dwim otherwise.
    137 Dwim means: region, org-src-block, org-subtree, or
    138 defun, whichever applies first. Narrowing to
    139 org-src-block actually calls `org-edit-src-code'.
    140 
    141 With prefix P, don't widen, just narrow even if buffer
    142 is already narrowed."
    143   (interactive "P")
    144   (declare (interactive-only))
    145   (cond ((and (buffer-narrowed-p) (not p)) (widen))
    146         ((region-active-p)
    147          (narrow-to-region (region-beginning)
    148                            (region-end)))
    149         ((derived-mode-p 'org-mode)
    150          ;; `org-edit-src-code' is not a real narrowing
    151          ;; command. Remove this first conditional if
    152          ;; you don't want it.
    153          (cond ((ignore-errors (org-edit-src-code) t)
    154                 (delete-other-windows))
    155                ((ignore-errors (org-narrow-to-block) t))
    156                (t (org-narrow-to-subtree))))
    157         ((derived-mode-p 'latex-mode)
    158          (LaTeX-narrow-to-environment))
    159         (t (narrow-to-defun))))
    160 
    161 ;; (define-key endless/toggle-map "n"
    162 ;;   #'narrow-or-widen-dwim)
    163 ;; This line actually replaces Emacs' entire narrowing
    164 ;; keymap, that's how much I like this command. Only
    165 ;; copy it if that's what you want.
    166 (define-key ctl-x-map "n" #'narrow-or-widen-dwim)
    167 (add-hook 'LaTeX-mode-hook
    168           (lambda ()
    169             (define-key LaTeX-mode-map "\C-xn"
    170               nil)))
    171 
    172 (defun toggle-modeline ()
    173   (interactive)
    174   (if (not (consp mode-line-format))
    175       (setq mode-line-format (default-value 'mode-line-format))
    176     (setq mode-line-format nil)))
    177 
    178 (defun resize-small ()
    179   (interactive)
    180   (set-frame-width (selected-frame) 85)
    181   (set-frame-height (selected-frame) 50))
    182 
    183 (defun resize-wide ()
    184   (interactive)
    185   (set-frame-width (selected-frame) 180)
    186   (set-frame-height (selected-frame) 50))
    187 
    188 (defun resize-big ()
    189   (interactive)
    190   (set-frame-width (selected-frame) 240)
    191   (set-frame-height (selected-frame) 70))
    192 
    193 (defun disable-line-numbers ()
    194   (display-line-numbers-mode -1))
    195 
    196 (defun mjf/pash-copy ()
    197   (interactive)
    198   (let ((arg (ivy-completing-read "" (mjf/pash-list))))
    199     (shell-command-to-string (format "pash copy %s" arg))))
    200 
    201 (defun mjf/pash-list ()
    202   (split-string
    203    (shell-command-to-string "pash list") "\n"))
    204 
    205 (defun mjf/focused ()
    206   (interactive)
    207   (if (bound-and-true-p olivetti-mode)
    208       (progn
    209         (olivetti-mode -1)
    210         (setq mode-line-format (default-value 'mode-line-format)))
    211     (progn
    212       (delete-other-windows)
    213       (olivetti-mode 1)
    214       (setq mode-line-format nil))))
    215 
    216 (bind-keys ("M-P" . mjf/pash-copy))
    217 
    218 (provide 'core-defuns)
    219 
    220 ;;; core-defuns.el ends here