;; -*- emacs-lisp -*- (defun intelligent-close () "quit a frame the same way no matter what kind of frame you are on" (interactive) (if (eq (car (visible-frame-list)) (selected-frame)) ;;for parent/master frame... (if (> (length (visible-frame-list)) 1) ;;close a parent with children present (delete-frame (selected-frame)) ;;close a parent with no children present (save-buffers-kill-emacs)) ;;close a child frame (delete-frame (selected-frame)))) (defun header-flip () "flip between corresponding .cpp and .h files" (interactive) (cond ((string-equal (file-name-extension (buffer-file-name)) "cpp") (cond ((file-exists-p (concat (file-name-sans-extension (buffer-file-name)) ".h")) (find-file (concat (file-name-sans-extension (buffer-file-name)) ".h"))) ((file-exists-p (concat (file-name-sans-extension (buffer-file-name)) ".hpp")) (find-file (concat (file-name-sans-extension (buffer-file-name)) ".hpp"))))) ((string-equal (file-name-extension (buffer-file-name)) "h") (cond ((file-exists-p (concat (file-name-sans-extension (buffer-file-name)) ".inl")) (find-file (concat (file-name-sans-extension (buffer-file-name)) ".inl"))) ((file-exists-p (concat (file-name-sans-extension (buffer-file-name)) ".cpp")) (find-file (concat (file-name-sans-extension (buffer-file-name)) ".cpp"))) ((file-exists-p (concat (file-name-sans-extension (buffer-file-name)) ".c")) (find-file (concat (file-name-sans-extension (buffer-file-name)) ".c"))))) ((string-equal (file-name-extension (buffer-file-name)) "inl") (cond ((file-exists-p (concat (file-name-sans-extension (buffer-file-name)) ".cpp")) (find-file (concat (file-name-sans-extension (buffer-file-name)) ".cpp"))) ((file-exists-p (concat (file-name-sans-extension (buffer-file-name)) ".c")) (find-file (concat (file-name-sans-extension (buffer-file-name)) ".c"))) ((file-exists-p (concat (file-name-sans-extension (buffer-file-name)) ".h")) (find-file (concat (file-name-sans-extension (buffer-file-name)) ".h"))) ((file-exists-p (concat (file-name-sans-extension (buffer-file-name)) ".hpp")) (find-file (concat (file-name-sans-extension (buffer-file-name)) ".hpp"))))))) (defun nice-shell () "open an ansi terminal" (interactive) (ansi-term "/bin/bash")) (defun my-compile () "run make and move the point to the end of the output" (interactive) (save-excursion (let ((original-window (selected-window))) (command-execute 'compile) (switch-to-buffer-other-window '"*compilation*") (goto-char (point-max)) (select-window original-window)))) (defun js-gdb-layout () "open a frame and layout the gdb windows" (make-frame) (pop-to-buffer gud-comint-buffer) (delete-other-windows) (gdb-display-locals-buffer) (gdb-display-stack-buffer) (delete-other-windows) (gdb-display-threads-buffer) (delete-other-windows) (gdb-display-breakpoints-buffer) (delete-other-windows) (pop-to-buffer gud-comint-buffer) (split-window-horizontally) (split-window-vertically) (split-window-vertically) (gdb-set-window-buffer (gdb-locals-buffer-name)) (other-window 1) (gdb-set-window-buffer (gdb-stack-buffer-name)) (other-window 1) (split-window-vertically) (gdb-set-window-buffer (gdb-threads-buffer-name)) (other-window 1) (other-window 1) (switch-to-buffer (if gud-last-last-frame (gud-find-file (car gud-last-last-frame)) (gud-find-file gdb-main-file)))) (defun js-gdb-quit () "kill the gdb buffers and then close the frame" (kill-buffer gud-comint-buffer) (intelligent-close)) (defun js-gdb () "start gdb, then set the window layout" (interactive) (gdb (gud-query-cmdline 'gdb)) (sleep-for 0.5) ;; need to give gdb some time to start (js-gdb-layout)) ;;; Page-up and Page-down like it should be (defun current-screen-line () (interactive) (save-excursion (let ((original-point (point-at-bol)) (cc (current-column)) (line-count 0)) (goto-char (window-start)) (while (< (point-at-bol) original-point) (next-line 1) (setq line-count (1+ line-count))) line-count))) (defun my-pagedown () (interactive) (let ((original-line (current-screen-line)) (cc (current-column)) (line-count 0)) (scroll-up) (goto-char (window-start)) (while (< line-count original-line) (next-line 1) (setq line-count (1+ line-count))) (forward-char (min (- (point-at-eol) (point-at-bol)) cc)))) (defun my-pageup () (interactive) (let ((original-line (current-screen-line)) (cc (current-column)) (line-count 0)) (scroll-down) (goto-char (window-start)) (while (< line-count original-line) (next-line 1) (setq line-count (1+ line-count))) (forward-char (min (- (point-at-eol) (point-at-bol)) cc)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Fancy #include stuff (defvar respects-guarded-include-convention-list nil "List of predicates that will be used to check if a certain header respects the guarded include convention. If no predicates match, the package will include extra external guards.") (defun insert-guarded-include-guess-header (this-line) (let ((m (string-match ".*[ (\t]+\\([0-9a-zA-Z_:\t]+\\)[) \t]*.*\n?$" this-line))) (if (not (equal m nil)) (concat (replace-regexp-in-string "::" "/" (substring this-line (match-beginning 1) (match-end 1))) ".h") ""))) (defun or-flip-map (pred-lst v) (cond ((null pred-lst) nil) ((funcall (car pred-lst) v) t) (t (or-flip-map (cdr pred-lst) v)))) (defun library-respects-guarded-include-convention-p (full-name) (or-flip-map respects-guarded-include-convention-list full-name)) ;; (or (string-match "^loom/" full-name))) (defun insert-guarded-include (header-name) (unless (or (string-match "\".*\"" header-name) (string-match "<.*>" header-name)) (setq header-name (concat "<" header-name ">"))) (let* ((trimmed-name (substring header-name 1 (- (length header-name) 1))) (symbol-name (concat (upcase (replace-regexp-in-string "[/.]" "_" trimmed-name)) "_INCLUDED"))) (insert (format "#ifndef %s\n" symbol-name)) (insert (format "#include %s\n" header-name)) (unless (library-respects-guarded-include-convention-p trimmed-name) (insert (format "#define %s\n" symbol-name))) (insert "#endif\n"))) (defun insert-guarded-include-prompt () (insert-guarded-include (read-input "Enter header name: "))) (defun nth-line (nth) (let* ((l nth) (bol (point-at-bol l)) (eol (point-at-eol l))) (buffer-substring bol eol))) (defun insert-guarded-include-dwim () (interactive) (let* ((this-line (nth-line 1)) (prev-line (nth-line 0)) (next-line (nth-line 2)) (next-next-line (nth-line 3)) (current-line-is-include (or (string-match "^ *\\(\"[^\"]*\"\\) *$" this-line) (string-match "^ *\\(<[^\"]*>\\) *$" this-line) (string-match "#include *\\([^ ]*\\)$" this-line))) (header-name (if current-line-is-include (substring this-line (match-beginning 1) (match-end 1)) "")) (current-include-is-guarded (and (string-match "#ifndef .*_INCLUDED" prev-line) (string-match "#endif" next-line))) (current-include-is-externally-guarded (and (string-match "#ifndef .*_INCLUDED" prev-line) (string-match "#define .*_INCLUDED" next-line) (string-match "#endif" next-next-line)))) (cond ((and current-include-is-externally-guarded current-line-is-include) (undo-boundary) (goto-char (point-at-bol 0)) (kill-line 4) (insert-guarded-include header-name)) ((and current-include-is-guarded current-line-is-include) (undo-boundary) (goto-char (point-at-bol 0)) (kill-line 3) (insert-guarded-include header-name)) (current-line-is-include (undo-boundary) (beginning-of-line) (kill-line) (insert-guarded-include header-name)) (t (let ((header-name (read-input "Enter header name: " (insert-guarded-include-guess-header this-line)))) (save-excursion (goto-char (point-max)) (cond ((re-search-backward (concat "#ifndef .*_INCLUDED\n#include <" (regexp-quote header-name) ">\n") 0 t) (princ "Header already included")) ((or (re-search-backward "#ifndef .*_INCLUDED\n#include <.*>\n\\(#define .*_INCLUDED\n\\)?#endif\n" 0 t) ;; noerror (re-search-backward "#ifndef .*_INCLUDED\n#define .*_INCLUDED\n" 0 t)) ;; noerror (goto-char (match-end 0)) (undo-boundary) (newline) (insert-guarded-include header-name)) (t (goto-char (point-min)) (undo-boundary) (insert-guarded-include header-name) (newline)))))))))