From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 0KZMI5sZpWA/NAEAgWs5BA (envelope-from ) for ; Wed, 19 May 2021 15:58:51 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id ANkHH5sZpWCjDgAAB5/wlQ (envelope-from ) for ; Wed, 19 May 2021 13:58:51 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id C9F302CCFC for ; Wed, 19 May 2021 15:58:50 +0200 (CEST) Received: from localhost ([::1]:38072 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ljMim-00084M-DK for larch@yhetil.org; Wed, 19 May 2021 09:58:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48206) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ljMgT-00080t-16 for emacs-orgmode@gnu.org; Wed, 19 May 2021 09:56:25 -0400 Received: from confino.investici.org ([2a00:c38:11e:ffff::a020]:63133) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ljMgL-0004IV-TX for emacs-orgmode@gnu.org; Wed, 19 May 2021 09:56:23 -0400 Received: from mx1.investici.org (unknown [127.0.0.1]) by confino.investici.org (Postfix) with ESMTP id 4FlZCY6kGyz10yJ for ; Wed, 19 May 2021 13:56:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=autistici.org; s=stigmate; t=1621432569; bh=lGGST1Vj4FZrMenp+U2S99U+EU3+M7rqxqGPF4IdXrc=; h=References:From:To:Subject:Date:In-reply-to:From; b=chOnQ2DkT22btcfoC0dQbhTTAMrl3rooKVw2liuFbFCXykAvmJAP4BiOsY7Oybr5n ah7Oc5uhnNRq8QhyXGWbxf8LY/W8nHs07hMzlSEJb72DmoclQZfAay6Uv+kXZxqPw5 MKHmIaFNVSI2zuB5stc6fUyCxixaVvSZWn83zmdY= Received: from [212.103.72.250] (mx1.investici.org [212.103.72.250]) (Authenticated sender: jgregory@autistici.org) by localhost (Postfix) with ESMTPSA id 4FlZCY0wGkz10xn for ; Wed, 19 May 2021 13:56:08 +0000 (UTC) References: <41-60a4c380-5-7d67d100@4304905> User-agent: mu4e 1.5.11; emacs 27.1 From: Jonathan Gregory To: emacs-orgmode@gnu.org Subject: Re: Get list of top-level headings Date: Wed, 19 May 2021 10:48:29 -0300 In-reply-to: <41-60a4c380-5-7d67d100@4304905> Message-ID: <874keyg74d.fsf@autistici.org> MIME-Version: 1.0 Content-Type: text/plain; format=flowed Received-SPF: pass client-ip=2a00:c38:11e:ffff::a020; envelope-from=jgrg@autistici.org; helo=confino.investici.org X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1621432731; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=lGGST1Vj4FZrMenp+U2S99U+EU3+M7rqxqGPF4IdXrc=; b=nez+YdqHxStqNL6unuvPTW755/xWg7p728jzcUOvVl8nwqCuaVJd7rUE0SlyhIWpjAWkVd KqcKZUIOJx7vgchsKA2tgL5uJ4s8GrUkXBytTWcyLc9iymm7aQeWBCi7wwr2rImbu3SSp5 5PdCUjV870+bFVKExDTufJiTVD35U6WDZngVWSTAxMcITSKAtmlUYu0BziaqN9aYm6WTJw 8uKyFRifaESoUlR7/AeOQNILIXJ2kM7Y72yEtD2o47VI0jDOAPjrAZSAzR9gjPaFRHTVrd iWjb+k4ItRrqEkG5U+GBZFPN22E1SOGWRcZc9sQ+SZlO3eY9vBrOcTGTdGxWoQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1621432731; a=rsa-sha256; cv=none; b=mDGvrhLCrCqW0KBFUZWZxm18d4jpzRWM6GRf82hNIbUdQZJ5wB2Z6bR8fa96bpvWzZkOZ0 55IXvSOTwdzHnpG6sQOJkNJCEzIZNCfPf9+GI41/Lj/eQjVwS5MGG+SqxGtbdCSyK5jrJ0 hVaZeViTqtt/+x5onnr+HGBB8UOGeV1/AcuI/0BKR7Ki6DYVA1y4whfS2RwENLeKTEE5Kk adPKR2DRhKwUyDMhvXPv+GtouLgfywAgrPX1PObk1Q9+xeMYKaqrlM35gYDgMKNxr5Oynf y9WsTg2CfwDdHM8U5AGWv/im+BBjh+s32zlhk5uWsRbFUA4TqDUTTjfUAZ8JZQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=autistici.org header.s=stigmate header.b=chOnQ2Dk; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Spam-Score: -2.64 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=autistici.org header.s=stigmate header.b=chOnQ2Dk; dmarc=none; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: C9F302CCFC X-Spam-Score: -2.64 X-Migadu-Scanner: scn0.migadu.com X-TUID: uXxPA8ysWl2C Hello Florian On 19 May 2021, Florian Lindner wrote: > Hello, > > I, an Emacs Lisp newbie, want to get a list of all top-level > headings > of the current buffer. My approach so far is: > > (defun test-org-map() > (interactive) > (setq headings '()) > (org-map-entries (lambda () > (setq current-header-item > (org-element-property : > title (org-element-at-point)) > (message "Header: %s" current-header-item) > (message "Is String: %s" (stringp > (org-element-property :title (org-element-at-point)))) > (setq headings (append current-header-item > headings)) > ) > "LEVEL=1" > ) > (dolist (heading headings) > (message "Header Item: %s" heading) > ) > ) > > This gives the otput: > > Header: AAA > Is String: t > Header: BBB > Is String: t > Header Item: 66 [3 times] > Header Item: 65 [3 times] > > so basically the (org-element-property :title > (org-element-at-point) > does exactly what I want, but building the list does not what I > want. > I suppose that comes from a fundamental misunderstanding of how > strings work in elisp. > > I would appreciate a short explanation (or pointers) why this > does not > work. And of course, I am very open to completely different, > likely > better, approches to that simply problem! > > Thanks, > Florian The org-map-entries function calls FUNC at each headline, so you have to (1) find the headline/title and (2) add it to your list. One way to do this is with the push macro. --8<---------------cut here---------------start------------->8--- (defun test-org-map () (interactive) (let (headlines) (org-map-entries (lambda () (let* ((element (org-element-at-point)) (headline (org-element-property :title element))) (push headline headlines))) "LEVEL=1") (print (nreverse headlines)))) --8<---------------cut here---------------end--------------->8--- Or by searching the buffer: --8<---------------cut here---------------start------------->8--- (defun test-org-map () (interactive) (let (headlines) (save-excursion (goto-char (point-max)) (while (re-search-backward org-complex-heading-regexp nil t) (let ((headline (match-string-no-properties 4))) (when (= (org-current-level) 1) (push headline headlines)))) (print headlines)))) --8<---------------cut here---------------end--------------->8--- BTW you're missing a closing parenthesis in: (setq current-header-item (org-element-property :title (org-element-at-point))) Maybe that's why you're getting errors. -- Jonathan