[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/nasm-mode b2469e0 33/67: Add context-sensitive comment han
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/nasm-mode b2469e0 33/67: Add context-sensitive comment handling. |
Date: |
Sun, 29 Aug 2021 11:23:47 -0400 (EDT) |
branch: elpa/nasm-mode
commit b2469e0c6233e2421ddd7ff6dec3cc9433e84c87
Author: Christopher Wellons <wellons@nullprogram.com>
Commit: Christopher Wellons <wellons@nullprogram.com>
Add context-sensitive comment handling.
---
nasm-mode.el | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 75 insertions(+), 1 deletion(-)
diff --git a/nasm-mode.el b/nasm-mode.el
index 62557f6..63e870c 100644
--- a/nasm-mode.el
+++ b/nasm-mode.el
@@ -588,7 +588,8 @@
(defvar nasm-mode-map
(let ((map (make-sparse-keymap)))
(prog1 map
- (define-key map (kbd ":") #'nasm-colon)))
+ (define-key map (kbd ":") #'nasm-colon)
+ (define-key map (kbd ";") #'nasm-comment)))
"Key bindings for `nasm-mode'.")
(defun nasm-colon ()
@@ -612,6 +613,79 @@
(when (> (- (point-max) orig) (point))
(goto-char (- (point-max) orig)))))
+(defun nasm--current-line ()
+ "Return the current line as a string."
+ (save-excursion
+ (let ((start (progn (beginning-of-line) (point)))
+ (end (progn (end-of-line) (point))))
+ (buffer-substring-no-properties start end))))
+
+(defun nasm--empty-line-p ()
+ "Return non-nil if current line has non-whitespace."
+ (not (string-match-p "\\S-" (nasm--current-line))))
+
+(defun nasm--line-has-comment-p ()
+ "Return non-nil if current line contains a comment."
+ (save-excursion
+ (end-of-line)
+ (nth 4 (syntax-ppss))))
+
+(defun nasm--line-has-non-comment-p ()
+ "Return non-nil of the current line has code."
+ (let* ((line (nasm--current-line))
+ (match (string-match-p "\\S-" line)))
+ (when match
+ (not (eql ?\; (aref line match))))))
+
+(defun nasm--inside-indentation-p ()
+ "Return non-nil if point is within the indentation."
+ (save-excursion
+ (let ((point (point))
+ (start (progn (beginning-of-line) (point)))
+ (end (progn (back-to-indentation) (point))))
+ (<= start point end))))
+
+(defun nasm-comment (&optional arg)
+ "Begin or edit a comment with context-sensitive placement.
+
+The right-hand comment gutter is far away from the code, so this
+command uses the mark ring to help move back and forth between
+code and the comment gutter.
+
+* If no comment gutter exists yet, mark the current position and
+ jump to it.
+* If already within the gutter, pop the top mark and return to
+ the code.
+* If on a line with no code, just insert a comment character.
+* If within the indentation, just insert a comment character.
+ This is intended prevent interference when the intention is to
+ comment out the line.
+
+With a prefix arg, kill the comment on the current line with
+`comment-kill'."
+ (interactive "p")
+ (if (not (eql arg 1))
+ (comment-kill nil)
+ (cond
+ ;; Empty line? Insert.
+ ((nasm--empty-line-p)
+ (insert ";"))
+ ;; Inside the indentation? Comment out the line.
+ ((nasm--inside-indentation-p)
+ (insert ";"))
+ ;; Currently in a right-side comment? Return.
+ ((and (nasm--line-has-comment-p)
+ (nasm--line-has-non-comment-p)
+ (nth 4 (syntax-ppss)))
+ (setf (point) (mark))
+ (pop-mark))
+ ;; Line has code? Mark and jump to right-side comment.
+ ((nasm--line-has-non-comment-p)
+ (push-mark)
+ (comment-indent))
+ ;; Otherwise insert.
+ ((insert ";")))))
+
;;;###autoload
(define-derived-mode nasm-mode prog-mode "NASM"
"Major mode for editing NASM assembly programs."
- [nongnu] elpa/nasm-mode e8ca8b9 50/67: Make comment-start local., (continued)
- [nongnu] elpa/nasm-mode e8ca8b9 50/67: Make comment-start local., ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode 1ec7d0a 53/67: Add a LIMIT argument to looking-back call, ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode 9a5f989 57/67: Fix docstring indentation for nasm-font-lock-keywords, ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode 832caad 59/67: Make semicolons behave when typed into strings, ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode af3d935 61/67: Allow directive names as labels (font-locking), ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode 01f90c3 03/67: Fix byte-compiler breakage., ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode ad2ff83 05/67: Add .gitignore., ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode b595921 11/67: Add nasm-colon key binding., ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode 10f775e 28/67: Update instruction listing, since some were missing., ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode 213331e 31/67: Oops, bump version number., ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode b2469e0 33/67: Add context-sensitive comment handling.,
ELPA Syncer <=
- [nongnu] elpa/nasm-mode 4de4e56 47/67: Add clzero, monitorx, and mwaitx to instructions., ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode 5b0d30f 58/67: Allow prefix in point following mnemonic check, ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode e072340 60/67: Update TODO list, ELPA Syncer, 2021/08/29
- [nongnu] elpa/nasm-mode 1d4871e 66/67: Use sharp quotes on expressions added in last commit, ELPA Syncer, 2021/08/29