From 25644dc06459f17ddc60d408e65318c614ea1728 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Sun, 29 Jul 2018 19:26:22 +0200 Subject: [PATCH] Move functions from prog.el to the Org config --- configuration.org | 82 +++++++++++++++++++++++++++++++++++++++++++++++ lisp/prog.el | 68 --------------------------------------- 2 files changed, 82 insertions(+), 68 deletions(-) delete mode 100644 lisp/prog.el diff --git a/configuration.org b/configuration.org index 167e045..3efc8e1 100644 --- a/configuration.org +++ b/configuration.org @@ -186,6 +186,31 @@ copied to the kill ring." (forward-char))) #+END_SRC +*** Convert camelCase to snake_case + +#+BEGIN_SRC emacs-lisp +(defun camel-to-snake-case (arg) + "Convert a camel case (camelCase or CamelCase) word to snake case (snake_case). + +If the prefix argument ARG is non-nil, convert the text to uppercase." + (interactive "p") + (progn + (let ((start (region-beginning)) + (end (region-end)) + (case-fold-search nil) + (had-initial-underscore nil)) + (goto-char start) + (when (looking-at "_") (setq had-initial-underscore t)) + (while (re-search-forward "\\([A-Z]\\)" end t) + (replace-match "_\\1") + (setq end (1+ end))) + (if arg + (upcase-region start end) + (downcase-region start end)) + (goto-char start) + (unless had-initial-underscore (delete-char 1))))) +#+END_SRC + ** Navigation *** Move to different beginnings of the current line @@ -402,6 +427,63 @@ Copied from http://emacs-doctor.com/emacs-strip-tease.html (kill-ring-save protocopy-begin protocopy-end))))) #+END_SRC +** Programming related + +*** Check if we are inside a string + +#+BEGIN_SRC emacs-lisp +(defun gpolonkai/prog-in-string-p () + "Return t if point is inside a string." + (nth 3 (syntax-ppss))) +#+END_SRC + +*** Check if we are inside a comment + +#+BEGIN_SRC emacs-lisp +(defun gpolonkai/prog-in-comment-p () + "Return t if point is inside a comment." + (nth 4 (syntax-ppss))) +#+END_SRC + +** ~python-mode~ related + +*** Add a docstring to the current thing + +…be it a function, class, or a module + +#+BEGIN_SRC emacs-lisp +(defun gpolonkai/python-add-docstring () + "Add a Python docstring to the current thing. + +If point is inside a function, add docstring to that. If point +is in a class, add docstring to that. If neither, add docstring +to the beginning of the file." + (interactive) + (save-restriction + (widen) + (beginning-of-defun) + (if (not (looking-at-p "\\(def\\|class\\) ")) + (progn + (goto-char (point-min)) + (back-to-indentation) + (forward-char) + (while (gpolonkai/prog-in-comment-p) + (forward-line) + (back-to-indentation) + (forward-char))) + (search-forward ":") + (while (or (gpolonkai/prog-in-string-p) + (gpolonkai/prog-in-comment-p)) + (search-forward ":"))) + (if (eq 1 (count-lines 1 (point))) + (open-line-above) + (open-line-below)) + (insert "\"\"\"") + (open-line-below) + (insert "\"\"\"") + (open-line-above))) +#+END_SRC + ** EShell related *** Delete a character, or close ~eshell~ if nothing to delet diff --git a/lisp/prog.el b/lisp/prog.el deleted file mode 100644 index e124d03..0000000 --- a/lisp/prog.el +++ /dev/null @@ -1,68 +0,0 @@ -;;; gp-prog --- Summary - -;;; Commentary: - -;;; Code: -(defun gpolonkai/prog-in-string-p () - "Return t if point is inside a string." - (nth 3 (syntax-ppss))) - -(defun gpolonkai/prog-in-comment-p () - "Return t if point is inside a comment." - (nth 4 (syntax-ppss))) - -(defun gpolonkai/python-add-docstring () - "Add a Python docstring to the current thing. - -If point is inside a function, add docstring to that. If point -is in a class, add docstring to that. If neither, add docstring -to the beginning of the file." - (interactive) - (save-restriction - (widen) - (beginning-of-defun) - (if (not (looking-at-p "\\(def\\|class\\) ")) - (progn - (goto-char (point-min)) - (back-to-indentation) - (forward-char) - (while (gpolonkai/prog-in-comment-p) - (forward-line) - (back-to-indentation) - (forward-char))) - (search-forward ":") - (while (or (gpolonkai/prog-in-string-p) - (gpolonkai/prog-in-comment-p)) - (search-forward ":"))) - (if (eq 1 (count-lines 1 (point))) - (open-line-above) - (open-line-below)) - (insert "\"\"\"") - (open-line-below) - (insert "\"\"\"") - (open-line-above))) - -(defun camel-to-snake-case (arg) - "Convert a camel case (camelCase or CamelCase) word to snake case (snake_case). - -If the prefix argument ARG is non-nil, convert the text to uppercase." - (interactive "p") - (progn - (let ((start (region-beginning)) - (end (region-end)) - (case-fold-search nil) - (had-initial-underscore nil)) - (goto-char start) - (when (looking-at "_") (setq had-initial-underscore t)) - (while (re-search-forward "\\([A-Z]\\)" end t) - (replace-match "_\\1") - (setq end (1+ end))) - (if arg - (upcase-region start end) - (downcase-region start end)) - (goto-char start) - (unless had-initial-underscore (delete-char 1))))) - -(provide 'gp-prog) - -;;; gp-prog.el ends here