comparison lisp/simple.el @ 10983:8ad27030d73f

(block-comment-start, block-comment-end): New vars. (indent-for-comment): Handle them.
author Richard M. Stallman <rms@gnu.org>
date Sun, 12 Mar 1995 19:23:25 +0000
parents e43125b71452
children 769706e1babb
comparison
equal deleted inserted replaced
10982:e5942b624f45 10983:8ad27030d73f
1900 Each mode establishes a different default value for this variable; you 1900 Each mode establishes a different default value for this variable; you
1901 can set the value for a particular mode using that mode's hook.") 1901 can set the value for a particular mode using that mode's hook.")
1902 (make-variable-buffer-local 'comment-column) 1902 (make-variable-buffer-local 'comment-column)
1903 1903
1904 (defconst comment-start nil 1904 (defconst comment-start nil
1905 "*String to insert to start a new comment, or nil if no comment syntax defined.") 1905 "*String to insert to start a new comment, or nil if no comment syntax.")
1906 1906
1907 (defconst comment-start-skip nil 1907 (defconst comment-start-skip nil
1908 "*Regexp to match the start of a comment plus everything up to its body. 1908 "*Regexp to match the start of a comment plus everything up to its body.
1909 If there are any \\(...\\) pairs, the comment delimiter text is held to begin 1909 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
1910 at the place matched by the close of the first pair.") 1910 at the place matched by the close of the first pair.")
1922 '(lambda () comment-column) 1922 '(lambda () comment-column)
1923 "Function to compute desired indentation for a comment. 1923 "Function to compute desired indentation for a comment.
1924 This function is called with no args with point at the beginning of 1924 This function is called with no args with point at the beginning of
1925 the comment's starting delimiter.") 1925 the comment's starting delimiter.")
1926 1926
1927 (defconst block-comment-start nil
1928 "*String to insert to start a new comment on a line by itself.
1929 If nil, use `comment-start' instead.
1930 Note that the regular expression `comment-start-skip' should skip this string
1931 as well as the `comment-start' string.")
1932
1933 (defconst block-comment-end nil
1934 "*String to insert to end a new comment on a line by itself.
1935 Should be an empty string if comments are terminated by end-of-line.
1936 If nil, use `comment-end' instead.")
1937
1927 (defun indent-for-comment () 1938 (defun indent-for-comment ()
1928 "Indent this line's comment to comment column, or insert an empty comment." 1939 "Indent this line's comment to comment column, or insert an empty comment."
1929 (interactive "*") 1940 (interactive "*")
1930 (beginning-of-line 1) 1941 (beginning-of-line 1)
1931 (if (null comment-start) 1942 (let* ((empty (save-excursion (beginning-of-line)
1932 (error "No comment syntax defined") 1943 (looking-at "[ \t]*$")))
1933 (let* ((eolpos (save-excursion (end-of-line) (point))) 1944 (starter (or (and empty block-comment-start) comment-start))
1934 cpos indent begpos) 1945 (ender (or (and empty block-comment-end) comment-end)))
1935 (if (re-search-forward comment-start-skip eolpos 'move) 1946 (if (null starter)
1936 (progn (setq cpos (point-marker)) 1947 (error "No comment syntax defined")
1937 ;; Find the start of the comment delimiter. 1948 (let* ((eolpos (save-excursion (end-of-line) (point)))
1938 ;; If there were paren-pairs in comment-start-skip, 1949 cpos indent begpos)
1939 ;; position at the end of the first pair. 1950 (if (re-search-forward comment-start-skip eolpos 'move)
1940 (if (match-end 1) 1951 (progn (setq cpos (point-marker))
1941 (goto-char (match-end 1)) 1952 ;; Find the start of the comment delimiter.
1942 ;; If comment-start-skip matched a string with 1953 ;; If there were paren-pairs in comment-start-skip,
1943 ;; internal whitespace (not final whitespace) then 1954 ;; position at the end of the first pair.
1944 ;; the delimiter start at the end of that 1955 (if (match-end 1)
1945 ;; whitespace. Otherwise, it starts at the 1956 (goto-char (match-end 1))
1946 ;; beginning of what was matched. 1957 ;; If comment-start-skip matched a string with
1947 (skip-syntax-backward " " (match-beginning 0)) 1958 ;; internal whitespace (not final whitespace) then
1948 (skip-syntax-backward "^ " (match-beginning 0))))) 1959 ;; the delimiter start at the end of that
1949 (setq begpos (point)) 1960 ;; whitespace. Otherwise, it starts at the
1950 ;; Compute desired indent. 1961 ;; beginning of what was matched.
1951 (if (= (current-column) 1962 (skip-syntax-backward " " (match-beginning 0))
1952 (setq indent (if comment-indent-hook 1963 (skip-syntax-backward "^ " (match-beginning 0)))))
1953 (funcall comment-indent-hook) 1964 (setq begpos (point))
1954 (funcall comment-indent-function)))) 1965 ;; Compute desired indent.
1955 (goto-char begpos) 1966 (if (= (current-column)
1956 ;; If that's different from current, change it. 1967 (setq indent (if comment-indent-hook
1957 (skip-chars-backward " \t") 1968 (funcall comment-indent-hook)
1958 (delete-region (point) begpos) 1969 (funcall comment-indent-function))))
1959 (indent-to indent)) 1970 (goto-char begpos)
1960 ;; An existing comment? 1971 ;; If that's different from current, change it.
1961 (if cpos 1972 (skip-chars-backward " \t")
1962 (progn (goto-char cpos) 1973 (delete-region (point) begpos)
1963 (set-marker cpos nil)) 1974 (indent-to indent))
1964 ;; No, insert one. 1975 ;; An existing comment?
1965 (insert comment-start) 1976 (if cpos
1966 (save-excursion 1977 (progn (goto-char cpos)
1967 (insert comment-end)))))) 1978 (set-marker cpos nil))
1979 ;; No, insert one.
1980 (insert starter)
1981 (save-excursion
1982 (insert ender)))))))
1968 1983
1969 (defun set-comment-column (arg) 1984 (defun set-comment-column (arg)
1970 "Set the comment column based on point. 1985 "Set the comment column based on point.
1971 With no arg, set the comment column to the current column. 1986 With no arg, set the comment column to the current column.
1972 With just minus as arg, kill any comment on this line. 1987 With just minus as arg, kill any comment on this line.